Creating debian template for lxc virtualization

by

in

To create debian template for lxc you need fresh copy of debian system.

Well, we do have one installed recently on guruplug.
All debian install on guruplug steps are here.

So, lets archive all system to tar.gz.

Firstly it’s a good practice to exclude unneeded files.
Create /tmp/exclude file with this content:

# cat /tmp/exclude 
/tmp/*
/proc/*
/dev/*
/sys/*
/mnt/*
/usr/src/*

Edit templates /etc/fstab file and comment everything out.
Create new file /etc/fstab.lxc
Contents of that file:

# cat etc/fstab.lxc 
none /path/to/your/virtual/dir/proc    proc    defaults 0 0
none /path/to/your/virtual/dir/sys    sysfs    defaults 0 0

Well, this file doesn’t have to be in virtual machine, but I think that’s more convenient, when lxc fstab configuration is kept inside virtual host. And you have more than one of them.

It’s a good idea to delete udev startup file from /etc/rcS.d/
as it makes unnecessary longer boot.

rm /etc/rcS.d/S02udev

And create archive:

# tar -czvf /root/debian.tar.gz -X /tmp/exclude /

Lets make one virtual host (commands are made on host machine).

You can use this bridge configuration for lxc containers.

I assume lxc is already installed on your host system. If not, You can do that with command.

apt-get install lxc
mkdir /cgroup

and add line to /etc/fstab

none /cgroup cgroup defaults 0 0

You can mount everything what’s on fstab by command “mount -a”.

Assume lxc containers are in /lxc on host machine.
So extract template to that directory:

tar zxvf debian.tar.gz -C /lxc/

Make configuration file in /etc/lxc/

# cat /etc/lxc/debian.conf 
lxc.utsname = debian
lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = br0
lxc.network.hwaddr = 00:1D:1F:00:01:0D
lxc.network.ipv4 = 192.168.1.13/24
#path to extracted container root
lxc.rootfs = /lxc/debian
#path to fstab.lxc in container
lxc.mount = /lxc/debian/etc/fstab.lxc
lxc.tty = 4
lxc.cgroup.devices.deny = a
# consoles
lxc.cgroup.devices.allow = c 5:1 rwm
lxc.cgroup.devices.allow = c 5:0 rwm
lxc.cgroup.devices.allow = c 4:0 rwm
lxc.cgroup.devices.allow = c 4:1 rwm
# /dev/pts/* - pts namespaces are "coming soon"
lxc.cgroup.devices.allow = c 136:* rwm
lxc.cgroup.devices.allow = c 5:2 rwm
# /dev/null and zero
lxc.cgroup.devices.allow = c 1:3 rwm
lxc.cgroup.devices.allow = c 1:5 rwm
# /dev/{,u}random
lxc.cgroup.devices.allow = c 1:9 rwm
lxc.cgroup.devices.allow = c 1:8 rwm

Yes, machine IP is static, if You want to get it by DHCP, You need to write:

lxc.network.ipv4 = 0.0.0.0/24

Make script in /bin/ directory (for fixing dev directory in container):

# cat /bin/fix_dev.sh 
#!/bin/bash
ROOT=$(pwd)
DEV=${ROOT}/dev
mv ${DEV} ${DEV}.old
mkdir -p ${DEV}
mknod -m 666 ${DEV}/null c 1 3
mknod -m 666 ${DEV}/zero c 1 5
mknod -m 666 ${DEV}/random c 1 8
mknod -m 666 ${DEV}/urandom c 1 9
mkdir -m 755 ${DEV}/pts
mkdir -m 1777 ${DEV}/shm
mknod -m 666 ${DEV}/tty c 5 0
mknod -m 600 ${DEV}/console c 5 1
mknod -m 666 ${DEV}/tty0 c 4 0
mknod -m 666 ${DEV}/full c 1 7
mknod -m 600 ${DEV}/initctl p
mknod -m 666 ${DEV}/ptmx c 5 2

Make it executable:

chmod +x /bin/fix_dev.sh 

Go to virtual machine root directory and start the script:

cd /lxc/debian
fix_dev.sh

Create virtual machine:

lxc-create -f /etc/lxc/debian.conf -n debian

Edit /etc/default/lxc to make container boot automatically.
Uncomment:

# cat /etc/default/lxc 
# Comment out to run the lxc init script
RUN=yes

# Directory containing the container configurations
CONF_DIR=/etc/lxc

# Start /etc/lxc/example.conf, /etc/lxc/autostart.conf, etc.
CONTAINERS="debian"

That’s it.
You can start container with command:

lxc-start -n debian &

Now connect to it using ssh.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.