patch the f16 sliceimage for running under libvirt/lxc
[vserver-reference.git] / initscripts / lxc-sliceimage
1 #!/bin/bash
2 # chkconfig: 345 20 80
3 # description: Create BTRFS subvolumes for LXC reference images.
4 #
5
6 # not needed -- Source function library
7 #. /etc/init.d/functions
8
9 # This is where sliceimage(s) store their reference images
10 sliceimage_dir=/vservers/.vref
11 lxc_dir=/vservers/.lvref
12
13 # Check if we are in the build environment
14 function check_node () {
15     mount | grep -q 'planetlab-vservers' || exit 0
16     [ -d $sliceimage_dir ] || { echo "No sliceimage installed" ; exit 1; }
17 }
18
19 function start () {
20
21     check_node
22     
23     mkdir -p $lxc_dir
24
25     slicefamilies=$(cd $sliceimage_dir ; ls )
26
27     for slicefamily in $slicefamilies; do
28         # initialize if needed
29         [ -d $lxc_dir/$slicefamily ] || btrfs subvolume create $lxc_dir/$slicefamily
30         # xxx what is that ?
31         #btrfs subvolume create $lxc_dir/lxc-squeeze-x86_64
32         # copy the slice image into the btrfs ?
33         rsync -av --delete $sliceimage_dir/$slicefamily/ $lxc_dir/$slicefamily/
34         # tmp -- should very probably be done at build time
35         patch_lvref $lxc_dir/$slicefamily
36     done
37 }
38
39 function status () {
40     echo -n "Checking node .. "
41     check_node
42     echo OK
43     echo "From installed sliceimage variants"
44     ls $sliceimage_dir
45     echo "Exported to lxc"
46     ls $lxc_dir
47 }
48
49 # inspired from https://gist.github.com/1142202
50 ETC=/etc/systemd/system
51 LIB=/lib/systemd/system
52 function patch_lvref () {
53     # this applies to systemd only
54     [ -d $rootfs/$LIB ] || return
55     rootfs=$1; shift
56     # sliceimage comes with graphical.target as default
57     chroot $rootfs ln -sf $LIB/multi-user.target $ETC/default.target
58     # sysinit.target seems to stick on boot, so disable it. However, we need
59     # systemd-tmpfiles-setup.service that was started by the dependency of
60     # sysinit.target to boot up correctly, so start it instead.
61     chroot $rootfs cp $LIB/basic.target $ETC/basic.target
62     chroot $rootfs sed -i 's/sysinit.target/systemd-tmpfiles-setup.service/' $ETC/basic.target
63     # Stop starting sysinit.target. Symlinking one to /dev/null is a standard way
64     # to disable a target (or a service and others).
65     chroot $rootfs ln -s /dev/null $ETC/sysinit.target
66     # It also a cause of stuck on boot
67     chroot $rootfs ln -s /dev/null $ETC/udev-settle.service
68     # It prevents systemd-tmpfiles-setup.service from starting
69     chroot $rootfs ln -s /dev/null $ETC/fedora-readonly.service
70     # Libvirt lxc provides only tty1
71     chroot $rootfs rm -f $ETC/getty.target.wants/getty\@tty{2,3,4,5,6}.service
72     # It launches sulogin on console(tty1) but it interferes getty@tty1
73     chroot $rootfs ln -s /dev/null $ETC/console-shell.service
74     # Workarounds for libvirt 0.9.4. Without this, getty@tty1 doen't launch
75     # because a trigger event on tty1 doesn't happen.
76     chroot $rootfs cp $LIB/getty\@.service $ETC/getty\@.service
77     chroot $rootfs sed -i 's/^BindTo/\#&/' $ETC/getty\@.service
78     chroot $rootfs ln -sf $ETC/getty\@.service $ETC/getty.target.wants/getty\@tty1.service
79     # Allow a user who logins via ssh to sudo
80     chroot $rootfs sed -i 's/^Defaults\ *requiretty/\#&/' /etc/sudoers
81     # Allow to login at virsh console. loginuid.so doen't work in the absence of auditd
82     # which cannot run inside a container.
83     chroot $rootfs sed -i 's/^.*loginuid.so.*$/\#&/' /etc/pam.d/login
84     # Enable eth0 on bootup
85     cat <<EOF > $rootfs/etc/sysconfig/network-scripts/ifcfg-eth0
86 DEVICE=eth0
87 BOOTPROTO=dhcp
88 ONBOOT=yes
89 EOF
90     # Tweak sshd configuration
91     chroot $rootfs sed -i 's/^UsePAM\ *yes/\#&/' /etc/ssh/sshd_config
92     chroot $rootfs sed -i 's/^GSSAPIAuthentication\ *yes/\#&/' /etc/ssh/sshd_config
93     chroot $rootfs sed -i 's/^PasswordAuthentication\ *yes/\#&/' /etc/ssh/sshd_config
94     # Allow root to login at virsh console
95     echo "pts/0" >> $rootfs/etc/securetty
96     # our image does not have NetworkManager, only network, and it is off by default
97     chroot $rootfs chkconfig network on
98 }
99
100 case "$1" in
101     start|restart|reload)       start ; exit 0 ;;
102     status)                     status ; exit 0 ;;
103     stop)                       exit 0 ;;
104     *)  echo $"Usage: $0 {start|stop|status}"
105         exit 1
106         ;;
107 esac
108