LinuxContainer - henk52/knowledgesharing GitHub Wiki

Table of Contents

summary creating a linux container.

Introduction

References

Support commands:

  • repoquery -lq lxc-extra

    • List content of non installed package.

Some background info

Directories, used by lxc

  • /var/cache/lxc - Where downloaded images are stored.

  • /usr/share/lxc/templates - The storage directory for the templates.

  • /etc/lxc - Configuration files.

  • /var/lib/lxc - Where the created containers are stored.

The first time 'lxc-create' runs it will download the files to:

  • /var/cache/lxc/fedora

Please note, once the cache has been populated, then it will not be downloaded again.

LXC commands

  • lxc-copy - Copy base to a new container. E.g. lxc-copy --name f24 --newname f24_graphite

  • lxc-create -

  • sudo lxc-create -t centos -n base_centos7 — --release 7

  • sudo lxc-create --template download --name myubuntu — --dist ubuntu --release focal --arch amd64

  • lxc-start -

  • lxc-stop -

  • lxc-execute -

  • lxc-attach -

  • lxc-console -

  • lxc-monitor -

  • lxc-wait -

  • lxc-cgroup -

  • lxc-ls -

  • lxc-info -

  • lxc-freeze -

  • lxc-unfreeze -

  • lxc-clone - Seems to be replaced with: 'lxc-copy'. clone a new container from an existing one.

  • lxc-destroy -

Creating a base container

  1. sudo lxc-clone u1 u_base

  2. sudo lxc-start --name u_base

  3. apt-get install puppet git

    • sudo lxc-attach -n u_base — /usr/bin/apt-get --assume-yes install puppet git binutils unzip

  4. shutdown -h now

    • sudo lxc-attach -n u_base — /sbin/shutdown -h now

  5. sudo lxc-wait -n u_base -s 'STOPPED'

Creating a new container with puppet installation.

  1. sudo lxc-destroy -n u_bld

  2. sudo lxc-clone u_base u_bld

    • 10s

  3. sudo mkdir /var/lib/lxc/u_bld/rootfs/vagrant

  4. sudo chmod +x /var/lib/lxc/u_bld/rootfs/vagrant/install

  5. sudo lxc-start --name u_bld

  6. sudo lxc-wait -n u_bld -s 'RUNNING'

  7. sudo lxc-attach -n u_bld — /vagrant/install

#!/bin/sh
BASEPATH="/vagrant"
cd ${BASEPATH}
git clone https://github.com/henk52/vagrant-devmachine.git
cd ${BASEPATH}/vagrant-devmachine
echo "sudo puppet apply build_machine_install.pp"

Trying it out

  • lxc-create -t fedora -n p1

  • This seems to download something from the internet.

  • sudo -E lxc-start -n p1

  • ls /usr/share/lxc/templates

Create a Fedora container on Ubuntu

From a Local repo

Change: mirrorurl in /usr/share/lxc/templates/lxc-fedora or add the address to your /etc/hosts.

On the Repo server

  1. dnf install rsync-daemon

  2. systemctl daemon-reload

  3. vi /etc/rsync.conf

[ fedora ]
path            = /var/ks/mirrors/fedora20
read only       = yes
write only      = no
list            = yes
uid             = 0
gid             = 0
incoming chmod  = 0644
outgoing chmod  = 0644
max connections = 0
# This file is being maintained by Puppet.
# DO NOT EDIT

On ubuntu: 1. grep mirrorurl /usr/share/lxc/templates/lxc-fedora 1. sudo vi /etc/hosts 1. sudo lxc-create -t fedora -n p1

Cookbook

Creation

If you’re using the logical volume manager (which I recommend highly), specifying “-B lvm” will cause it to allocate a new logical volume for the guest. There are options to change the volume group, disk space allocation, and file system type if necessary. For this article, I’ll be taking the defaults.

Create a base container

sudo -E lxc-create -t fedora -n base >& /tmp/lxc-create.log

Creating a Fedora base container on Ubuntu

Creating a CentOS base container on Ubuntu

  1. sudo apt-get install yum

  2. sudo /usr/share/lxc/templates/lxc-centos --name=graphite --release=7

  3. sudo chroot /var/lib/lxc/graphite/rootfs passwd

  4. sudo vi /var/lib/lxc/graphite/config

  5. sudo lxc-start -n graphite

  6. sudo lxc-attach -n graphite

Uncomment these lines:

lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = lxcbr0
lxc.network.name = eth0

Creating an Ubuntu base container on Ubuntu

WARN: could not reopen tty: Permission denied
WARN: could not reopen tty: Permission denied
WARN: could not reopen tty: Permission denied
WARN: could not reopen tty: Permission denied
Setting up the GPG keyring
Downloading the image index
Downloading the rootfs
date
Downloading the metadata
The image cache is now ready
Unpacking the rootfs

---
You just created an Ubuntu container (release=trusty, arch=amd64, variant=default)

To enable sshd, run: apt-get install openssh-server

For security reason, container images ship without user accounts
and without a root password.

Use lxc-attach or chroot directly into the rootfs to set a root password
or create user accounts.
lxctst@XXX:~$ date
Wed Jul 29 02:48:17 CEST 2015

Cleaning out the cache to start over.

  • sudo rm --preserve-root --one-file-system -rf /var/cache/lxc/fedora/x86_64/20

Configuration file

man lxc.container.conf

sudo -E lxc-create -t fedora -n comp1 -f my_lxc.conf

lxc.network.type=veth
lxc.network.type=internalbr0
lxc.network.name=eth0
lxc.network.flags=up

lxc.network.type=veth
lxc.network.type=internalbr1
lxc.network.name=eth1
lxc.network.flags=up

Setting up a secondary private network between two containers

lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = testbr0
lxc.network.hwaddr = 00:16:3e:ce:7b:fd
lxc.network.ipv4 = 172.16.0.1/24
  • Please remember that the hwaddr has to be unique

  • you could also leave out the network address and then set it inside the container as you do with a normal machine.

LXC in a vagrant virtualbox

What is needed to set-up an lxc environment on a vagrant machine, during boot.

  • optional proxy server.

  • lxc package

  • lxc patches to make it run on Fedora 20.

  • Base environment.

  • Since this takes about 10 min, it might be a good idea to have this in a tgz.

  • Here I need a list of additional packages that has to go into the lxc to form a base image.

  • Can I use augeas to hack this?

Adding packages to the default installation

  1. vi /usr/share/lxc/templates/lxc-fedora

  2. search for PKG_LIST and add your own packages to the end of the list.

CGROUPS

From: https://www.kernel.org/doc/Documentation/cgroups/cgroups.txt When mounting a cgroup hierarchy, you may specify a comma-separated list of subsystems to mount as the filesystem mount options. By default, mounting the cgroup filesystem attempts to mount a hierarchy containing all registered subsystems.

Troubleshooting

'yum' command is missing

Fix: apt-get install yum

lxc-create -t centos -n graphite
Host CPE ID from /etc/os-release: 
'yum' command is missing
lxc-create: lxccontainer.c: create_run_template: 1295 container creation template for graphite failed
lxc-create: tools/lxc_create.c: main: 318 Error creating container graphite

[email protected] holdoff time over, scheduling restart.

Maybe I need to put the fedora.common.conf into /usr/share/lxc/config ?

[email protected] holdoff time over, scheduling restart.
[email protected] holdoff time over, scheduling restart.
[email protected] holdoff time over, scheduling restart.
[email protected] holdoff time over, scheduling restart.

Cannot make/remove an entry for the specified session

You don’t have to set 'audit=0'

You can download from https://github.com/lxc/lxc

Add 'audit=0' to the 'linux' command line in /boot/grub2/grub.cfg of the hosts OS. * https://bugzilla.redhat.com/show_bug.cgi?id=966807 * https://bugzilla.redhat.com/show_bug.cgi?id=1002914

p1 login: root
Password:

Cannot make/remove an entry for the specified session

Ubuntu

Unpriviledged lxc

WARN: could not reopen tty: Permission denied

lxc-create -t download -n u1 -- -d ubuntu -r trusty -a amd64
WARN: could not reopen tty: Permission denied
newuidmap: uid range [0-1) -> [100000-100001) not allowed
error mapping child
setgid: Invalid argument
lxc_container: lxccontainer.c: do_create_container_dir: 760 Failed to chown container dir
lxc_container: lxc_create.c: main: 271 Error creating container u1

lxc_container: cgmanager.c: lxc_cgmanager_create: 299 call to cgmanager_create_sync failed: invalid request

Fix, for ubuntu 14.04.1: # sudo apt-get update # sudo apt-get dist-upgrade

It seems like a newer systemd is needed.

 lxc-start -n u1
WARN: could not reopen tty: Permission denied
WARN: could not reopen tty: Permission denied
WARN: could not reopen tty: Permission denied
WARN: could not reopen tty: Permission denied
WARN: could not reopen tty: Permission denied
lxc_container: cgmanager.c: lxc_cgmanager_create: 299 call to cgmanager_create_sync failed: invalid request
lxc_container: cgmanager.c: lxc_cgmanager_create: 301 Failed to create hugetlb:u1
lxc_container: cgmanager.c: cgm_create: 646 Error creating cgroup hugetlb:u1
lxc_container: start.c: lxc_spawn: 861 failed creating cgroups
lxc_container: start.c: __lxc_start: 1080 failed to spawn 'u1'
lxc_container: lxc_start.c: main: 342 The container failed to start.
lxc_container: lxc_start.c: main: 346 Additional information can be obtained by setting the --logfile and --logpriority options.
⚠️ **GitHub.com Fallback** ⚠️