create ref images for system slices from stubs - for planetflow - 1st draft
[sliceimage.git] / initscripts / lxc-sliceimage
1 #!/bin/bash
2 #
3 # chkconfig: 345 20 80
4 # description: Create BTRFS subvolumes for LXC reference images.
5 #
6 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
7 # Copyright (C) 2012 INRIA
8 #
9
10 # not needed -- Source function library
11 #. /etc/init.d/functions
12
13 # This is where sliceimage(s) store their reference images
14 sliceimage_dir=/vservers/.vref
15 sliceimage_stubs=/vservers/.vstub
16 lxc_dir=/vservers/.lvref
17 tmp_dir=/vservers/.ltmp
18
19 # Check if we are in the build environment
20 function check_node () {
21     mount | grep -q 'planetlab-vservers' || exit 0
22     [ -d $sliceimage_dir ] || { echo "No sliceimage installed" ; exit 1; }
23 }
24
25 function start () {
26
27     check_node
28     
29     mkdir -p $lxc_dir
30
31     slicefamilies=$(cd $sliceimage_dir ; ls )
32
33     for slicefamily in $slicefamilies; do
34         # initialize if needed
35         [ -d $lxc_dir/$slicefamily ] || btrfs subvolume create $lxc_dir/$slicefamily
36         # xxx what is that ?
37         #btrfs subvolume create $lxc_dir/lxc-squeeze-x86_64
38         # copy the slice image into the btrfs ?
39         rsync -a --delete $sliceimage_dir/$slicefamily/ $lxc_dir/$slicefamily/
40         # tmp -- should very probably be done at build time
41         patch_lvref $lxc_dir/$slicefamily
42     done
43
44     # create ref images from stubs
45     unfold_system_slices
46 }
47
48 function status () {
49     echo -n "Checking node .. "
50     check_node
51     echo OK
52     echo "From installed sliceimage variants"
53     ls $sliceimage_dir
54     echo "Exported to lxc"
55     ls $lxc_dir
56 }
57
58 # inspired from https://gist.github.com/1142202
59 ETC=/etc/systemd/system
60 LIB=/lib/systemd/system
61 function patch_lvref () {
62     # this applies to systemd only
63     [ -d $rootfs/$LIB ] || return
64     rootfs=$1; shift
65     # sliceimage comes with graphical.target as default
66     chroot $rootfs ln -sf $LIB/multi-user.target $ETC/default.target
67     # sysinit.target seems to stick on boot, so disable it. However, we need
68     # systemd-tmpfiles-setup.service that was started by the dependency of
69     # sysinit.target to boot up correctly, so start it instead.
70     chroot $rootfs cp $LIB/basic.target $ETC/basic.target
71     chroot $rootfs sed -i 's/sysinit.target/systemd-tmpfiles-setup.service/' $ETC/basic.target
72     # Stop starting sysinit.target. Symlinking one to /dev/null is a standard way
73     # to disable a target (or a service and others).
74     chroot $rootfs ln -s /dev/null $ETC/sysinit.target
75     # It also a cause of stuck on boot
76     chroot $rootfs ln -s /dev/null $ETC/udev-settle.service
77     # It prevents systemd-tmpfiles-setup.service from starting
78     chroot $rootfs ln -s /dev/null $ETC/fedora-readonly.service
79     # Libvirt lxc provides only tty1
80     chroot $rootfs rm -f $ETC/getty.target.wants/getty\@tty{2,3,4,5,6}.service
81     # It launches sulogin on console(tty1) but it interferes getty@tty1
82     chroot $rootfs ln -s /dev/null $ETC/console-shell.service
83     # Workarounds for libvirt 0.9.4. Without this, getty@tty1 doen't launch
84     # because a trigger event on tty1 doesn't happen.
85     chroot $rootfs cp $LIB/getty\@.service $ETC/getty\@.service
86     chroot $rootfs sed -i 's/^BindTo/\#&/' $ETC/getty\@.service
87     chroot $rootfs ln -sf $ETC/getty\@.service $ETC/getty.target.wants/getty\@tty1.service
88     # Allow a user who logins via ssh to sudo
89     chroot $rootfs sed -i 's/^Defaults\ *requiretty/\#&/' /etc/sudoers
90     # Allow to login at virsh console. loginuid.so doen't work in the absence of auditd
91     # which cannot run inside a container.
92     chroot $rootfs sed -i 's/^.*loginuid.so.*$/\#&/' /etc/pam.d/login
93     # Enable eth0 on bootup
94     cat <<EOF > $rootfs/etc/sysconfig/network-scripts/ifcfg-eth0
95 DEVICE=eth0
96 BOOTPROTO=dhcp
97 ONBOOT=yes
98 EOF
99     # Tweak sshd configuration
100     chroot $rootfs sed -i 's/^UsePAM\ *yes/\#&/' /etc/ssh/sshd_config
101     # commenting out the defaults in the file is not enough, need to explicitly set these to no
102     chroot $rootfs sed -i 's/^GSSAPIAuthentication.*$/GSSAPIAuthentication no/' /etc/ssh/sshd_config
103     chroot $rootfs sed -i 's/^PasswordAuthentication.*$/PasswordAuthentication no/' /etc/ssh/sshd_config
104     # Allow root to login at virsh console
105     echo "pts/0" >> $rootfs/etc/securetty
106     # our image does not have NetworkManager, only network, and it is off by default
107     chroot $rootfs chkconfig network on
108 }
109
110 function unfold_system_slices () {
111     for clonedstamp in $sliceimage_stubs/*/*.cloned; do
112         unfold_system_slice_from_cloned $clonedstamp
113     done
114 }
115
116 function unfold_system_slice_from_cloned () {
117     clonedstamp=$1; shift
118     # e.g. NAME=planetflow
119     NAME=$(basename $clonedstamp .cloned)
120     DIR=$(dirname $clonedstamp)
121     # e.g. SLICEFAMILY=planetlab-f8-i386
122     SLICEFAMILY=$(cat $clonedstamp)
123     # deduce the actual name used in .vref by replacing the first part of slice-family 
124     # (pldistro) with the slice name
125     # e.g. VREFNAME=planetflow-f8-i386
126     VREFNAME=$(echo $SLICEFAMILY | sed -e "s,^[^-]*-,$NAME-,")
127
128     VREFPATH="$lxc_dir/$VREFNAME"
129
130     # do not redo existing vref's unless force is mentioned
131     [ -z "$FORCE" -a -d "$VREFPATH" ] && continue
132     [ -z "$FORCE" ] && message=Building
133     [ -n "$FORCE" ] && message=Force-building
134
135     # Copy base reference image
136     echo -n $"$message slice image for $NAME in $VREFNAME: "
137
138     # build the systemslice from the one it was originally cloned from
139     FAMILYREF="$lxc_dir/$SLICEFAMILY"
140     if [ ! -d "$FAMILYREF" ] ; then
141         echo system slice from $clonedstamp - could not find reference $FAMILYREF - skipped
142         return
143     fi
144     
145     # safe side
146     mkdir -p "$lxc_dir"
147     # cleanup reference image if already exists
148     [ -d "$VREFPATH" ] && btrfs subvolume delete "$VREFPATH"
149     # extra safe
150     [ -d "$VREFPATH" ] && rm -rf "$VREFPATH"
151
152     # clone subvolume
153     btrfs subvolume snapshot $FAMILYREF $VREFPATH
154     
155     # merge the stub with the reference to get the system slice
156     (cd "$DIR/$NAME"/ && find . | cpio -m -d -u -p "$VREFPATH"/) || \
157         { echo "Could not apply stub $DIR/$NAME - skipping $clonedstamp" ; btrfs subvolume delete "$VREFPATH" ; return ; }
158
159     # Clean RPM state
160     rm -f "$VREFPATH/var/lib/rpm/__db"*
161
162     echo Done
163 }
164
165 case "$1" in
166     start|restart|reload)       start ; exit 0 ;;
167     status)                     status ; exit 0 ;;
168     stop)                       exit 0 ;;
169     *)  echo $"Usage: $0 {start|stop|status}"
170         exit 1
171         ;;
172 esac
173