Revision 151 (by dpavlin, 2009/10/21 12:06:10) configurable hostname as optional third param
#!/bin/sh -x

test -z "$1" && echo "usage: $0 /path/to/ve/private [10.60.0.253 [hostname]]" && exit

dir=$1
ip=$2
hostname=$3
netmask=`grep netmask /etc/network/interfaces | head -1 | sed 's/^.*netmask *//'`
gateway=`grep gateway /etc/network/interfaces | head -1 | sed 's/^.*gateway *//'`

test -z "$ip" && ip=10.60.0.252
test -z "$hostname" && hostname=ve2lxc

path=/$dir/etc/inittab
tmp=/tmp/inittab

cp $path $tmp || exit

function append() {
	if ! grep "$1" $path ; then
		echo "$1" >> $tmp
	fi
}

append "z6:6:respawn:/sbin/sulogin"
append "1:2345:respawn:/sbin/getty 38400 console"
append "c1:12345:respawn:/sbin/getty 38400 tty1 linux"
append "c2:12345:respawn:/sbin/getty 38400 tty2 linux"
append "c3:12345:respawn:/sbin/getty 38400 tty3 linux"
append "c4:12345:respawn:/sbin/getty 38400 tty4 linux"

if ! diff -uw $path $tmp ; then
	cp $path $path.old && mv $tmp $path
fi

lxc-stop -n $hostname
lxc-destroy -n $hostname

test -d /cgroup || mkdir /cgroup
grep /cgroup /etc/fstab || echo "cgroup /cgroup cgroup rw 0 0" >> /etc/fstab

grep eth0 $dir/etc/network/interfaces || cat << __interfaces__ > $dir/etc/network/interfaces
auto eth0 lo

iface lo inet loopback

iface eth0 inet static
	address $ip
	netmask $netmask
	gateway $gateway

__interfaces__

echo $hostname > $dir/etc/hostname

echo "$ip $hostname" >> $dir/etc/hosts

conf=/tmp/$hostname.conf

cat << __lxc__ > $conf
lxc.utsname = $hostname
lxc.tty = 4
lxc.pts = 1024
lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = br0
lxc.network.name = eth0
lxc.network.mtu = 1500
#lxc.mount = $MNTFILE
lxc.rootfs = $dir
lxc.cgroup.devices.deny = a
# /dev/null and zero
lxc.cgroup.devices.allow = c 1:3 rwm
lxc.cgroup.devices.allow = c 1:5 rwm
# 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/{,u}random
lxc.cgroup.devices.allow = c 1:9 rwm
lxc.cgroup.devices.allow = c 1:8 rwm
lxc.cgroup.devices.allow = c 136:* rwm
lxc.cgroup.devices.allow = c 5:2 rwm
# rtc
lxc.cgroup.devices.allow = c 254:0 rwm
__lxc__

cp -v /etc/resolv.conf /$dir/etc/resolv.conf

mount | grep /cgroup || mount /cgroup || exit

lxc-create -n $hostname -f $conf && lxc-start -n $hostname