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