854f643530b258de699e1706c6a61bc5d7a216b2
[sysadmin-cookbook] / recepies / lxc / ve2lxc.sh
1 #!/bin/sh -x
2
3 test -z "$1" && echo "usage: $0 /path/to/ve/private [10.60.0.253 [hostname]]" && exit
4
5 dir=$1
6 ip=$2
7 hostname=$3
8 netmask=`grep netmask /etc/network/interfaces | head -1 | sed 's/^.*netmask *//'`
9 gateway=`grep gateway /etc/network/interfaces | head -1 | sed 's/^.*gateway *//'`
10
11 test -z "$ip" && ip=10.60.0.252
12 test -z "$hostname" && hostname=ve2lxc
13
14 path=/$dir/etc/inittab
15 tmp=/tmp/inittab
16
17 cp $path $tmp || exit
18
19 function append() {
20         if ! grep "$1" $path ; then
21                 echo "$1" >> $tmp
22         fi
23 }
24
25 append "z6:6:respawn:/sbin/sulogin"
26 append "1:2345:respawn:/sbin/getty 38400 console"
27 append "c1:12345:respawn:/sbin/getty 38400 tty1 linux"
28 append "c2:12345:respawn:/sbin/getty 38400 tty2 linux"
29 append "c3:12345:respawn:/sbin/getty 38400 tty3 linux"
30 append "c4:12345:respawn:/sbin/getty 38400 tty4 linux"
31
32 if ! diff -uw $path $tmp ; then
33         cp $path $path.old && mv $tmp $path
34 fi
35
36 lxc-stop -n $hostname
37 lxc-destroy -n $hostname
38
39 test -d /cgroup || mkdir /cgroup
40 grep /cgroup /etc/fstab || echo "cgroup /cgroup cgroup rw 0 0" >> /etc/fstab
41
42 grep eth0 $dir/etc/network/interfaces || cat << __interfaces__ > $dir/etc/network/interfaces
43 auto eth0 lo
44
45 iface lo inet loopback
46
47 iface eth0 inet static
48         address $ip
49         netmask $netmask
50         gateway $gateway
51
52 __interfaces__
53
54 echo $hostname > $dir/etc/hostname
55
56 echo "$ip $hostname" >> $dir/etc/hosts
57
58 conf=/tmp/$hostname.conf
59
60 cat << __lxc__ > $conf
61 lxc.utsname = $hostname
62 lxc.tty = 4
63 lxc.pts = 1024
64 lxc.network.type = veth
65 lxc.network.flags = up
66 lxc.network.link = br0
67 lxc.network.name = eth0
68 lxc.network.mtu = 1500
69 #lxc.mount = $MNTFILE
70 lxc.rootfs = $dir
71 lxc.cgroup.devices.deny = a
72 # /dev/null and zero
73 lxc.cgroup.devices.allow = c 1:3 rwm
74 lxc.cgroup.devices.allow = c 1:5 rwm
75 # consoles
76 lxc.cgroup.devices.allow = c 5:1 rwm
77 lxc.cgroup.devices.allow = c 5:0 rwm
78 lxc.cgroup.devices.allow = c 4:0 rwm
79 lxc.cgroup.devices.allow = c 4:1 rwm
80 # /dev/{,u}random
81 lxc.cgroup.devices.allow = c 1:9 rwm
82 lxc.cgroup.devices.allow = c 1:8 rwm
83 lxc.cgroup.devices.allow = c 136:* rwm
84 lxc.cgroup.devices.allow = c 5:2 rwm
85 # rtc
86 lxc.cgroup.devices.allow = c 254:0 rwm
87 __lxc__
88
89 cp -v /etc/resolv.conf /$dir/etc/resolv.conf
90
91 mount | grep /cgroup || mount /cgroup || exit
92
93 lxc-create -n $hostname -f $conf && lxc-start -n $hostname