update list of debian-based distros
[build.git] / lbuild-initvm.sh
1 #!/bin/bash
2 # -*-shell-*-
3
4 #shopt -s huponexit
5
6 COMMAND=$(basename $0)
7 DIRNAME=$(dirname $0)
8 BUILD_DIR=$(pwd)
9
10 # pkgs parsing utilities
11 PATH=$(dirname $0):$PATH export PATH
12 . build.common
13
14 DEFAULT_FCDISTRO=f16
15 DEFAULT_PLDISTRO=planetlab
16 DEFAULT_PERSONALITY=linux64
17
18 COMMAND_LBUILD="lbuild-initvm.sh"
19 COMMAND_LTEST="ltest-initvm.sh"
20
21 libvirt_version="1.0.4"
22
23 ## stolen from tests/system/template-qemu/qemu-bridge-init
24 #################### compute INTERFACE_LAN
25 # use /proc/net/dev instead of a hard-wired list
26 function gather_interfaces () {
27     python <<EOF
28 for line in file("/proc/net/dev"):
29     if ':' not in line: continue
30     ifname=line.replace(" ","").split(":")[0]
31     if ifname.find("lo")==0: continue
32     if ifname.find("br")==0: continue
33     if ifname.find("virbr")==0: continue
34     if ifname.find("tap")==0: continue
35     print ifname
36 EOF
37 }
38     
39 function discover_interface () {
40     for ifname in $(gather_interfaces); do
41         ip link show $ifname | grep -qi 'state UP' && { echo $ifname; return; }
42     done
43     # still not found ? that's bad
44     echo unknown
45 }
46 DEFAULT_IFNAME=$(discover_interface)
47
48 function bridge_init () {
49
50     # turn on verbosity
51     set -x
52
53     # constant
54     INTERFACE_BRIDGE=br0
55
56     # Default Value for INTERFACE_LAN
57     INTERFACE_LAN=$(netstat -rn | grep '^0.0.0.0' | awk '{print $8;}')
58
59
60     echo "========== $COMMAND: entering start - beg"
61     hostname
62     uname -a
63     ifconfig
64     netstat -rn
65     echo "========== $COMMAND: entering start - end"
66
67     # disable netfilter calls for bridge interface (they cause panick on 2.6.35 anyway)
68     #
69     # another option would be to accept the all forward packages for
70     # bridged interface like: -A FORWARD -m physdev --physdev-is-bridged -j ACCEPT
71     sysctl net.bridge.bridge-nf-call-iptables=0
72     sysctl net.bridge.bridge-nf-call-ip6tables=0
73     sysctl net.bridge.bridge-nf-call-arptables=0
74
75     # take extra arg for ifname, if provided
76     [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
77
78     #if we have already configured the same host_box no need to do it again
79     /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null && {
80         echo "Bridge interface $INTERFACE_BRIDGE already set up - $COMMAND start exiting"
81         return 0
82     }
83     /sbin/ifconfig $INTERFACE_LAN &>/dev/null || {
84         echo "Cannot use interface $INTERFACE_LAN - exiting"
85         exit 1
86     }
87
88     
89     #Getting host IP/masklen
90     address=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $2;}')
91     [ -z "$address" ] && { echo "ERROR: Could not determine IP address for $INTERFACE_LAN" ; exit 1 ; }
92
93     broadcast=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $4;}')
94     [ -z "$broadcast" ] && echo "WARNING: Could not determine broadcast address for $INTERFACE_LAN"
95
96     gateway=$(netstat -rn | grep '^0.0.0.0' | awk '{print $2;}')
97     [ -z "$gateway" ] && echo "WARNING: Could not determine gateway IP"
98
99     # creating the bridge
100     echo "Creating bridge INTERFACE_BRIDGE=$INTERFACE_BRIDGE"
101     brctl addbr $INTERFACE_BRIDGE
102     #brctl stp $INTERFACE_BRIDGE yes
103     brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
104     echo "Activating promiscuous mode INTERFACE_LAN=$INTERFACE_LAN"
105     /sbin/ifconfig $INTERFACE_LAN 0.0.0.0 promisc up
106     sleep 2
107     echo "Setting bridge address=$address broadcast=$broadcast"
108     # static
109     #/sbin/ifconfig $INTERFACE_BRIDGE $address broadcast $broadcast up
110     dhclient $INTERFACE_BRIDGE
111     sleep 1
112
113     #Reconfigure the routing table
114     echo "Configuring gateway=$gateway"
115     route add default gw $gateway
116
117     echo "========== $COMMAND: exiting start - beg"
118     ifconfig
119     netstat -rn
120     echo "========== $COMMAND: exiting start - end"
121
122
123 return 0
124
125 }
126
127
128 function failure () {
129     echo "$COMMAND : Bailing out"
130     exit 1
131 }
132
133 function cidr_notation () {
134
135 netmask=$1; shift
136 cidr=0
137 for i in $(seq 1 4) ; do
138     part=$(echo $netmask | cut -d. -f $i)
139     case $part in
140         "255") cidr=$((cidr + 8));;
141         "254") cidr=$((cidr + 7));;
142         "252") cidr=$((cidr + 6));;
143         "248") cidr=$((cidr + 5));;
144         "240") cidr=$((cidr + 4));;
145         "224") cidr=$((cidr + 3));;
146         "192") cidr=$((cidr + 2));;
147         "128") cidr=$((cidr + 1));;
148         "0") cidr=$((cidr + 0));;
149      esac
150 done
151 echo $cidr
152
153 }
154
155 function check_yum_installed () {
156     package=$1; shift
157     rpm -q $package >& /dev/null || yum -y install $package
158 }
159
160 function check_yumgroup_installed () {
161     group="$1"; shift
162     yum grouplist "$group" | grep -q Installed || { yum -y groupinstall "$group" ; }
163 }
164
165 function prepare_host() {
166    
167 ### Thierry - jan 14 - turning off this check as our boxes now meet this req.
168 ### and I'm trying out f20's stock libvirt instead    
169 #    ## check if libvirt_version is installed
170 #    virsh -v | grep -e $libvirt_version || { echo "$libvirt_version needs to be installed!!!" ; exit 1 ; }
171
172     #################### bride initialization
173     check_yum_installed bridge-utils
174     #Bridge init
175     isInstalled=$(netstat -rn | grep '^0.0.0.0' | awk '{print $8;}')
176     if [ "$isInstalled" != "br0" ] ; then
177         bridge_init
178         sleep 5
179     fi
180
181     return 0
182 }
183
184
185
186 function configure_fedora() {
187
188     # disable selinux in fedora
189     mkdir -p $rootfs_path/selinux
190     echo 0 > $rootfs_path/selinux/enforce
191
192    # configure the network 
193
194     cat <<EOF > ${rootfs_path}/etc/sysconfig/network-scripts/ifcfg-$IFNAME
195 DEVICE=$IFNAME
196 BOOTPROTO=static
197 ONBOOT=yes
198 HOSTNAME=$HOSTNAME
199 IPADDR=$IP
200 NETMASK=$NETMASK
201 GATEWAY=$GATEWAY
202 NM_CONTROLLED=no
203 TYPE=Ethernet
204 MTU=1500
205 EOF
206
207     # set the hostname
208     case "$fcdistro" in 
209         f18|f2?)
210             cat <<EOF > ${rootfs_path}/etc/hostname
211 $HOSTNAME
212 EOF
213             echo ;;
214         *)
215             cat <<EOF > ${rootfs_path}/etc/sysconfig/network
216 NETWORKING=yes
217 HOSTNAME=$HOSTNAME
218 EOF
219             # set minimal hosts
220             cat <<EOF > $rootfs_path/etc/hosts
221 127.0.0.1 localhost $HOSTNAME
222 EOF
223             echo ;;
224     esac
225
226     dev_path="${rootfs_path}/dev"
227     rm -rf $dev_path
228     mkdir -p $dev_path
229     mknod -m 666 ${dev_path}/null c 1 3
230     mknod -m 666 ${dev_path}/zero c 1 5
231     mknod -m 666 ${dev_path}/random c 1 8
232     mknod -m 666 ${dev_path}/urandom c 1 9
233     mkdir -m 755 ${dev_path}/pts
234     mkdir -m 1777 ${dev_path}/shm
235     mknod -m 666 ${dev_path}/tty c 5 0
236     mknod -m 666 ${dev_path}/tty0 c 4 0
237     mknod -m 666 ${dev_path}/tty1 c 4 1
238     mknod -m 666 ${dev_path}/tty2 c 4 2
239     mknod -m 666 ${dev_path}/tty3 c 4 3
240     mknod -m 666 ${dev_path}/tty4 c 4 4
241     mknod -m 600 ${dev_path}/console c 5 1
242     mknod -m 666 ${dev_path}/full c 1 7
243     mknod -m 600 ${dev_path}/initctl p
244     mknod -m 666 ${dev_path}/ptmx c 5 2
245
246     #echo "setting root passwd to $root_password"
247     #echo "root:$root_password" | chroot $rootfs_path chpasswd
248
249     return 0
250 }
251
252
253 function configure_fedora_init() {
254
255     sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.sysinit
256     sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.d/rc.sysinit
257     # don't mount devpts, for pete's sake
258     sed -i 's/^.*dev.pts.*$/#\0/' ${rootfs_path}/etc/rc.sysinit
259     sed -i 's/^.*dev.pts.*$/#\0/' ${rootfs_path}/etc/rc.d/rc.sysinit
260     chroot ${rootfs_path} /sbin/chkconfig udev-post off
261     chroot ${rootfs_path} /sbin/chkconfig network on
262 }
263
264 # this code of course is for guests that do run on systemd
265 function configure_fedora_systemd() {
266     # so ignore if we can't find /etc/systemd at all 
267     [ -d ${rootfs_path}/etc/systemd ] || return 0
268     # otherwise let's proceed
269     ln -sf /lib/systemd/system/multi-user.target ${rootfs_path}/etc/systemd/system/default.target
270     touch ${rootfs_path}/etc/fstab
271     ln -sf /dev/null ${rootfs_path}/etc/systemd/system/udev.service
272 # Thierry - Feb 2013
273 # this was intended for f16 initially, in order to enable getty that otherwise would not start
274 # having a getty running is helpful only if ssh won't start though, and we see a correlation between
275 # VM's that refuse to lxc-stop and VM's that run crazy getty's
276 # so, turning getty off for now instead
277 #   #dependency on a device unit fails it specially that we disabled udev
278 #    sed -i 's/After=dev-%i.device/After=/' ${rootfs_path}/lib/systemd/system/getty\@.service
279     ln -sf /dev/null ${rootfs_path}/etc/systemd/system/"getty@.service"
280     rm -f ${rootfs_path}/etc/systemd/system/getty.target.wants/*service || :
281 # can't seem to handle this one with systemctl
282     chroot ${rootfs_path} /sbin/chkconfig network on
283 }
284
285 function download_fedora() {
286 set -x
287     # check the mini fedora was not already downloaded
288     INSTALL_ROOT=$cache/partial
289     echo $INSTALL_ROOT
290
291     # download a mini fedora into a cache
292     echo "Downloading fedora minimal ..."
293
294     mkdir -p $INSTALL_ROOT
295     if [ $? -ne 0 ]; then
296         echo "Failed to create '$INSTALL_ROOT' directory"
297         return 1
298     fi
299
300     mkdir -p $INSTALL_ROOT/etc/yum.repos.d   
301     mkdir -p $INSTALL_ROOT/dev
302     mknod -m 0444 $INSTALL_ROOT/dev/random c 1 8
303     mknod -m 0444 $INSTALL_ROOT/dev/urandom c 1 9
304
305     # copy yum config and repo files
306     cp /etc/yum.conf $INSTALL_ROOT/etc/
307     cp /etc/yum.repos.d/fedora* $INSTALL_ROOT/etc/yum.repos.d/
308
309     # append fedora repo files with desired $release and $basearch
310     for f in $INSTALL_ROOT/etc/yum.repos.d/*
311     do
312       sed -i "s/\$basearch/$arch/g; s/\$releasever/$release/g;" $f
313     done 
314
315     MIRROR_URL=http://mirror.onelab.eu/fedora/releases/$release/Everything/$arch/os
316     RELEASE_URL1="$MIRROR_URL/Packages/fedora-release-$release-1.noarch.rpm"
317     # with fedora18 the rpms are scattered by first name
318     RELEASE_URL2="$MIRROR_URL/Packages/f/fedora-release-$release-1.noarch.rpm"
319     RELEASE_TARGET=$INSTALL_ROOT/fedora-release-$release.noarch.rpm
320     found=""
321     for attempt in $RELEASE_URL1 $RELEASE_URL2; do
322         if curl -f $attempt -o $RELEASE_TARGET ; then
323             echo "Retrieved $attempt"
324             found=true
325             break
326         else
327             echo "Failed attempt $attempt"
328         fi
329     done
330     [ -n "$found" ] || { echo "Could not retrieve fedora-release rpm - exiting" ; exit 1; }
331     
332     mkdir -p $INSTALL_ROOT/var/lib/rpm
333     rpm --root $INSTALL_ROOT  --initdb
334     # when installing f12 this apparently is already present, so ignore result
335     rpm --root $INSTALL_ROOT -ivh $INSTALL_ROOT/fedora-release-$release.noarch.rpm || :
336     # however f12 root images won't get created on a f18 host
337     # (the issue here is the same as the one we ran into when dealing with a vs-box)
338     # in a nutshell, in f12 the glibc-common and filesystem rpms have an apparent conflict
339     # >>> file /usr/lib/locale from install of glibc-common-2.11.2-3.x86_64 conflicts 
340     #          with file from package filesystem-2.4.30-2.fc12.x86_64
341     # in fact this was - of course - allowed by f12's rpm but later on a fix was made 
342     #   http://rpm.org/gitweb?p=rpm.git;a=commitdiff;h=cf1095648194104a81a58abead05974a5bfa3b9a
343     # So ideally if we want to be able to build f12 images from f18 we need an rpm that has
344     # this patch undone, like we have in place on our f14 boxes (our f14 boxes need a f18-like rpm)
345
346     YUM="yum --installroot=$INSTALL_ROOT --nogpgcheck -y"
347     PKG_LIST="yum initscripts passwd rsyslog vim-minimal dhclient chkconfig rootfiles policycoreutils openssh-server openssh-clients"
348     echo "$YUM install $PKG_LIST"
349     $YUM install $PKG_LIST
350
351     if [ $? -ne 0 ]; then
352         echo "Failed to download the rootfs, aborting."
353         return 1
354     fi
355
356     mv "$INSTALL_ROOT" "$cache/rootfs"
357     echo "Download complete."
358
359     return 0
360 }
361
362
363 function copy_fedora() {
364 set -x
365     # make a local copy of the minifedora
366     echo -n "Copying rootfs to $rootfs_path ..."
367     mkdir -p $rootfs_path
368     rsync -a $cache/rootfs/ $rootfs_path/
369     return 0
370 }
371
372
373 function update_fedora() {
374 set -x
375     YUM="yum --installroot $cache/rootfs -y --nogpgcheck"
376     $YUM update
377 }
378
379
380 function install_fedora() {
381     set -x
382
383     mkdir -p /var/lock/subsys/
384     (
385         flock -n -x 200
386         if [ $? -ne 0 ]; then
387             echo "Cache repository is busy."
388             return 1
389         fi
390
391         echo "Checking cache download in $cache/rootfs ... "
392         if [ ! -e "$cache/rootfs" ]; then
393             download_fedora
394             if [ $? -ne 0 ]; then
395                 echo "Failed to download 'fedora base'"
396                 return 1
397             fi
398         else
399             echo "Cache found. Updating..."
400             update_fedora
401             if [ $? -ne 0 ]; then
402                 echo "Failed to update 'fedora base', continuing with last known good cache"
403             else
404                 echo "Update finished"
405             fi
406         fi
407
408         echo "Copy $cache/rootfs to $rootfs_path ... "
409         copy_fedora
410         if [ $? -ne 0 ]; then
411             echo "Failed to copy rootfs"
412             return 1
413         fi
414
415         return 0
416
417         ) 200>/var/lock/subsys/lxc
418
419     return $?
420 }
421
422
423 function copy_configuration() {
424
425     mkdir -p $config_path
426     cat <<EOF >> $config_path/config
427 lxc.utsname = $lxc
428 lxc.arch = $arch2
429 lxc.tty = 4
430 lxc.pts = 1024
431 lxc.rootfs = $rootfs_path
432 lxc.mount  = $config_path/fstab
433 #networking
434 lxc.network.type = $lxc_network_type
435 lxc.network.flags = up
436 lxc.network.link = $lxc_network_link
437 lxc.network.name = $IFNAME
438 lxc.network.mtu = 1500
439 lxc.network.ipv4 = $IP/$CIDR
440 lxc.network.veth.pair = $veth_pair
441 #cgroups
442 #lxc.cgroup.devices.deny = a
443 # /dev/null and zero
444 lxc.cgroup.devices.allow = c 1:3 rwm
445 lxc.cgroup.devices.allow = c 1:5 rwm
446 # consoles
447 lxc.cgroup.devices.allow = c 5:1 rwm
448 lxc.cgroup.devices.allow = c 5:0 rwm
449 lxc.cgroup.devices.allow = c 4:0 rwm
450 lxc.cgroup.devices.allow = c 4:1 rwm
451 # /dev/{,u}random
452 lxc.cgroup.devices.allow = c 1:9 rwm
453 lxc.cgroup.devices.allow = c 1:8 rwm
454 lxc.cgroup.devices.allow = c 136:* rwm
455 lxc.cgroup.devices.allow = c 5:2 rwm
456 # rtc
457 lxc.cgroup.devices.allow = c 254:0 rwm
458 lxc.cgroup.devices.allow = b 255:0 rwm
459 EOF
460
461
462
463     cat <<EOF > $config_path/fstab
464 proc            $rootfs_path/proc         proc    nodev,noexec,nosuid 0 0
465 devpts          $rootfs_path/dev/pts      devpts defaults 0 0
466 sysfs           $rootfs_path/sys          sysfs defaults  0 0
467 EOF
468     if [ $? -ne 0 ]; then
469         echo "Failed to add configuration"
470         return 1
471     fi
472
473     return 0
474 }
475
476
477
478
479 # overwrite lxc's internal yum config
480 function configure_yum_in_lxc () {
481     set -x 
482     set -e 
483     trap failure ERR INT
484
485     lxc=$1; shift
486     fcdistro=$1; shift
487     pldistro=$1; shift
488
489     echo "Initializing yum.repos.d in $lxc"
490     rm -f $rootfs_path/etc/yum.repos.d/*
491
492     cat > $rootfs_path/etc/yum.repos.d/building.repo <<EOF
493 [fedora]
494 name=Fedora $release - $arch
495 baseurl=http://mirror.onelab.eu/fedora/releases/$release/Everything/$arch/os/
496 enabled=1
497 metadata_expire=7d
498 gpgcheck=1
499 gpgkey=http://mirror.onelab.eu/keys/RPM-GPG-KEY-fedora-$release-primary
500
501 [updates]
502 name=Fedora $release - $arch - Updates
503 baseurl=http://mirror.onelab.eu/fedora/updates/$release/$arch/
504 enabled=1
505 metadata_expire=7d
506 gpgcheck=1
507 gpgkey=http://mirror.onelab.eu/keys/RPM-GPG-KEY-fedora-$release-primary
508 EOF
509     
510     # for using vtest-init-lxc.sh as a general-purpose lxc creation wrapper
511     # just mention 'none' as the repo url
512     if [ -n "$TEST_MODE" -a "$REPO_URL" != "none" ] ; then
513         if [ ! -d $rootfs_path/etc/yum.repos.d ] ; then
514             echo "WARNING : cannot create myplc repo"
515         else
516             # exclude kernel from fedora repos 
517             yumexclude=$(pl_plcyumexclude $fcdistro $pldistro $DIRNAME)
518             for repo in $rootfs_path/etc/yum.repos.d/* ; do
519                 [ -f $repo ] && yumconf_exclude $repo "exclude=$yumexclude" 
520             done
521             # the build repo is not signed at this stage
522             cat > $rootfs_path/etc/yum.repos.d/myplc.repo <<EOF
523 [myplc]
524 name= MyPLC
525 baseurl=$REPO_URL
526 enabled=1
527 gpgcheck=0
528 EOF
529         fi
530     fi
531 }    
532
533 # return yum or debootstrap
534 function package_method () {
535     fcdistro=$1; shift
536     case $fcdistro in
537         f[0-9]*|centos[0-9]*|sl[0-9]*) echo yum ;;
538         squeeze|wheezy|oneiric|precise|quantal|raring|saucy) echo debootstrap ;;
539         *) echo Unknown distro $fcdistro ;;
540     esac 
541 }
542
543 # return arch from debian distro and personality
544 function canonical_arch () {
545     personality=$1; shift
546     fcdistro=$1; shift
547     case $(package_method $fcdistro) in
548         yum)
549             case $personality in *32) echo i386 ;; *64) echo x86_64 ;; *) echo Unknown-arch-1 ;; esac ;;
550         debootstrap)
551             case $personality in *32) echo i386 ;; *64) echo amd64 ;; *) echo Unknown-arch-2 ;; esac ;;
552         *)
553             echo Unknown-arch-3 ;;
554     esac
555 }
556
557 # the new test framework creates /timestamp in /vservers/<name> *before* populating it
558 function almost_empty () { 
559     dir="$1"; shift ; 
560     # non existing is fine
561     [ ! -d $dir ] && return 0; 
562     # need to have at most one file
563     count=$(cd $dir; ls | wc -l); [ $count -le 1 ]; 
564 }
565
566 function setup_lxc() {
567
568     set -x
569     set -e
570     #trap failure ERR INT
571
572     lxc=$1; shift
573     fcdistro=$1; shift
574     pldistro=$1; shift
575     personality=$1; shift
576
577     # create lxc container 
578     copy_configuration
579     if [ $? -ne 0 ]; then
580         echo "failed write configuration file"
581         exit 1
582     fi
583
584     pkg_method=$(package_method $fcdistro)
585     case $pkg_method in
586         yum)
587             install_fedora || { echo "failed to install fedora"; exit 1 ; }
588             configure_fedora || { echo "failed to configure fedora for a container"; exit 1 ; }
589             if [ "$(echo $fcdistro | cut -d"f" -f2)" -le "14" ]; then
590                 configure_fedora_init
591             else
592                 configure_fedora_systemd
593             fi
594             ;;
595         debootstrap)
596             echo "$COMMAND: no support for debootstrap-based systems - yet"
597             exit 1
598             ;;
599         *)
600             echo "$COMMAND:: unknown package_method - exiting"
601             exit 1
602             ;;
603     esac
604
605     # Enable cgroup
606     mkdir $rootfs_path/cgroup
607     
608     # set up resolv.conf
609     cp /etc/resolv.conf $rootfs_path/etc/resolv.conf
610     # and /etc/hosts for at least localhost
611     [ -f $rootfs_path/etc/hosts ] || echo "127.0.0.1 localhost localhost.localdomain" > $rootfs_path/etc/hosts
612     
613     # grant ssh access from host to guest
614     mkdir $rootfs_path/root/.ssh
615     cat /root/.ssh/id_rsa.pub >> $rootfs_path/root/.ssh/authorized_keys
616     
617     # copy libvirt xml template
618     veth_pair="i$(echo $HOSTNAME | cut -d. -f1)" 
619     tmpl_name="$lxc.xml"
620     cat > $config_path/$tmpl_name<<EOF
621 <domain type='lxc'>
622   <name>$lxc</name>
623   <memory>524288</memory>
624   <os>
625     <type arch='$arch2'>exe</type>
626     <init>/sbin/init</init>
627   </os>
628   <features>
629     <acpi/>
630   </features>
631   <vcpu>1</vcpu>
632   <clock offset='utc'/>
633   <on_poweroff>destroy</on_poweroff>
634   <on_reboot>restart</on_reboot>
635   <on_crash>destroy</on_crash>
636   <devices>
637     <emulator>/usr/libexec/libvirt_lxc</emulator>
638     <filesystem type='mount'>
639       <source dir='$rootfs_path'/>
640       <target dir='/'/>
641     </filesystem>
642     <interface type="bridge">
643       <source bridge="br0"/>
644       <target dev='$veth_pair'/>
645     </interface>
646     <console type='pty' />
647   </devices>
648   <network>
649     <name>host-bridge</name>
650     <forward mode="bridge"/>
651     <bridge name="br0"/>
652   </network>
653 </domain>
654 EOF
655    
656     # define lxc container for libvirt
657     virsh -c lxc:// define $config_path/$tmpl_name
658
659     # rpm --rebuilddb
660     chroot $rootfs_path /bin/rpm --rebuilddb
661
662     configure_yum_in_lxc $lxc $fcdistro $pldistro
663
664     return 0
665 }
666
667 function devel_or_vtest_tools () {
668
669     set -x 
670     set -e 
671     trap failure ERR INT
672
673     lxc=$1; shift
674     fcdistro=$1; shift
675     pldistro=$1; shift
676     personality=$1; shift
677
678     pkg_method=$(package_method $fcdistro)
679
680     pkgsfile=$(pl_locateDistroFile $DIRNAME $pldistro $PREINSTALLED)
681
682     ### install individual packages, then groups
683     # get target arch - use uname -i here (we want either x86_64 or i386)
684    
685     lxc_arch=$(chroot $rootfs_path /bin/uname -i)
686     # on debian systems we get arch through the 'arch' command
687     [ "$lxc_arch" = "unknown" ] && lxc_arch=$(chroot $rootfs_path /bin/arch)
688
689     packages=$(pl_getPackages -a $lxc_arch $fcdistro $pldistro $pkgsfile)
690     groups=$(pl_getGroups -a $lxc_arch $fcdistro $pldistro $pkgsfile)
691
692     case "$pkg_method" in
693         yum)
694             [ -n "$packages" ] && chroot $rootfs_path /usr/bin/yum -y install $packages
695             for group_plus in $groups; do
696                 group=$(echo $group_plus | sed -e "s,+++, ,g")
697                 chroot $rootfs_path /usr/bin/yum -y groupinstall "$group"
698             done
699             # store current rpm list in /init-lxc.rpms in case we need to check the contents
700             chroot $rootfs_path /bin/rpm -aq > $rootfs_path/init-lxc.rpms
701             ;;
702         debootstrap)
703             chroot $rootfs_path /usr/bin/apt-get update
704             for package in $packages ; do 
705                 chroot $rootfs_path  /usr/bin/apt-get install -y $package 
706             done
707             ### xxx todo install groups with apt..
708             ;;
709         *)
710             echo "unknown pkg_method $pkg_method"
711             ;;
712     esac
713
714     return 0
715 }
716
717 function post_install () {
718     lxc=$1; shift 
719     personality=$1; shift
720     if [ -n "$BUILD_MODE" ] ; then
721         post_install_build $lxc $personality
722         start_lxc $lxc
723     else
724         post_install_myplc $lxc $personality
725         start_lxc $lxc
726         wait_for_ssh $lxc
727     fi
728     # setup localtime from the host
729     cp /etc/localtime $rootfs_path/etc/localtime
730 }
731
732 function post_install_build () {
733
734     set -x 
735     set -e 
736     trap failure ERR INT
737
738     lxc=$1; shift
739     personality=$1; shift
740
741 ### From myplc-devel-native.spec
742 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
743     cat << EOF | chroot $rootfs_path /bin/bash -x
744     # set up /dev/loop* in lxc
745     for i in \$(seq 0 255) ; do
746         /bin/mknod -m 640 /dev/loop\$i b 7 \$i
747     done
748     
749     # create symlink for /dev/fd
750     [ ! -e "/dev/fd" ] && /bin/ln -s /proc/self/fd /dev/fd
751
752     # modify /etc/rpm/macros to not use /sbin/new-kernel-pkg
753     /bin/sed -i 's,/sbin/new-kernel-pkg:,,' /etc/rpm/macros
754     if [ -h "/sbin/new-kernel-pkg" ] ; then
755         filename=\$(/bin/readlink -f /sbin/new-kernel-pkg)
756         if [ "\$filename" == "/sbin/true" ] ; then
757                 /bin/echo "WARNING: /sbin/new-kernel-pkg symlinked to /sbin/true"
758                 /bin/echo "\tmost likely /etc/rpm/macros has /sbin/new-kernel-pkg declared in _netsharedpath."
759                 /bin/echo "\tPlease remove /sbin/new-kernel-pkg from _netsharedpath and reintall mkinitrd."
760                 exit 1
761         fi
762     fi
763     
764     # customize root's prompt
765     /bin/cat << PROFILE > /root/.profile
766 export PS1="[$lxc] \\w # "
767 PROFILE
768
769     uid=2000
770     gid=2000
771     
772     # add a "build" user to the system
773     builduser=\$(grep "^build:" /etc/passwd | wc -l)
774     if [ \$builduser -eq 0 ] ; then
775         groupadd -o -g \$gid build;
776         useradd -o -c 'Automated Build' -u \$uid -g \$gid -n -M -s /bin/bash build;
777     fi
778
779 # Allow build user to build certain RPMs as root
780     if [ -f /etc/sudoers ] ; then
781         buildsudo=\$(grep "^build.*ALL=(ALL).*NOPASSWD:.*ALL"  /etc/sudoers | wc -l)
782         if [ \$buildsudo -eq 0 ] ; then
783             echo "build   ALL=(ALL)       NOPASSWD: ALL" >> /etc/sudoers
784         fi
785         sed -i 's,^Defaults.*requiretty,#Defaults requiretty,' /etc/sudoers
786     fi
787 #
788 EOF
789         
790 }
791
792 function post_install_myplc  () {
793     set -x 
794     set -e 
795     trap failure ERR INT
796
797     lxc=$1; shift
798     personality=$1; shift
799
800 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
801     cat << EOF | chroot $rootfs_path /bin/bash -x
802
803     # create /etc/sysconfig/network if missing
804     [ -f /etc/sysconfig/network ] || /bin/echo NETWORKING=yes > /etc/sysconfig/network
805
806     # create symlink for /dev/fd
807     [ ! -e "/dev/fd" ] && /bin/ln -s /proc/self/fd /dev/fd
808
809     # turn off regular crond, as plc invokes plc_crond
810     /sbin/chkconfig crond off
811
812     # take care of loginuid in /etc/pam.d 
813     /bin/sed -i "s,#*\(.*loginuid.*\),#\1," /etc/pam.d/*
814
815     # customize root's prompt
816     /bin/cat << PROFILE > /root/.profile
817 export PS1="[$lxc] \\w # "
818 PROFILE
819
820 EOF
821 }
822
823 function start_lxc() {
824
825     set -x
826     set -e
827     #trap failure ERR INT
828
829     lxc=$1; shift
830   
831     virsh -c lxc:// start $lxc
832   
833     return 0
834 }
835
836 function wait_for_ssh () {
837     set -x
838     set -e
839     #trap failure ERR INT
840
841     lxc=$1; shift
842   
843     echo $IP is up, waiting for ssh...
844
845     #wait max 5 min for sshd to start 
846     ssh_up=""
847     stop_time=$(($(date +%s) + 300))
848     current_time=$(date +%s)
849     
850     counter=1
851     while [ "$current_time" -lt "$stop_time" ] ; do
852          echo "$counter-th attempt to reach sshd in container $lxc ..."
853          ssh -o "StrictHostKeyChecking no" $IP 'uname -i' && { ssh_up=true; echo "SSHD in container $lxc is UP"; break ; } || :
854          sleep 10
855          current_time=$(($current_time + 10))
856          counter=$(($counter+1))
857     done
858
859     # Thierry: this is fatal, let's just exit with a failure here
860     [ -z $ssh_up ] && { echo "SSHD in container $lxc is not running" ; exit 1 ; } 
861     return 0
862 }
863
864 function usage () {
865     set +x 
866     echo "Usage: $COMMAND_LBUILD [options] lxc-name"
867     echo "Usage: $COMMAND_LTEST [options] lxc-name repo-url [ -- lxc-options ]"
868     echo "Description:"
869     echo "   This command creates a fresh lxc instance, for building, or running, myplc"
870     echo "Supported options"
871     echo " -f fcdistro - for creating the root filesystem - defaults to $DEFAULT_FCDISTRO"
872     echo " -d pldistro - defaults to $DEFAULT_PLDISTRO"
873     echo " -p personality - defaults to $DEFAULT_PERSONALITY"
874     echo " -i ifname: determines ip and netmask attached to ifname, and passes it to the lxc"
875     echo "-- lxc-options"
876     echo "  --netdev : interface to be defined inside lxc"
877     echo "  --interface : IP to be defined for the lxc"
878     echo "  --hostname : Hostname to be defined for the lxc"
879     echo "With $COMMAND_LTEST you can give 'none' as the URL, in which case"
880     echo "   myplc.repo does not get created"
881     exit 1
882 }
883
884 ### parse args and 
885 function main () {
886
887     #set -e
888     #trap failure ERR INT
889
890     case "$COMMAND" in
891         $COMMAND_LBUILD)
892             BUILD_MODE=true ;;
893         $COMMAND_LTEST)
894             TEST_MODE=true;;
895         *)
896             usage ;;
897     esac
898
899     VERBOSE=
900     RESISTANT=""
901     IFNAME=""
902     LXC_OPTIONS=""
903
904     # the set of preinstalled packages - depends on vbuild or vtest
905     if [ -n "$VBUILD_MODE" ] ; then
906         PREINSTALLED=devel.pkgs
907     else
908         PREINSTALLED=vtest.pkgs
909     fi
910     while getopts "f:d:p:P:i:" opt ; do
911         case $opt in
912             f) fcdistro=$OPTARG;;
913             d) pldistro=$OPTARG;;
914             p) personality=$OPTARG;;
915             P) PREINSTALLED=$OPTARG;;
916             i) IFNAME=$OPTARG;;
917             *) usage ;;
918         esac
919     done
920         
921     shift $(($OPTIND - 1))
922
923     # parse fixed arguments
924     [[ -z "$@" ]] && usage
925     lxc=$1 ; shift
926     if [ -n "$TEST_MODE" ] ; then
927         [[ -z "$@" ]] && usage
928         REPO_URL=$1 ; shift
929     fi
930
931     # parse vserver options
932     if [[ -n "$@" ]] ; then
933         if [ "$1" == "--" ] ; then
934             shift
935             LXC_OPTIONS="$@"
936         else
937             usage
938         fi
939     fi
940
941     eval set -- "$LXC_OPTIONS"
942
943     while true
944      do
945         case "$1" in
946              --netdev)      IFNAME=$2; shift 2;;
947              --interface)   IP=$2; shift 2;;
948              --hostname)    HOSTNAME=$2; shift 2;;
949              *)             break ;;
950         esac
951       done
952
953    
954     if [ -n "$BUILD_MODE" ] ; then
955         [ -z "$IFNAME" ] && IFNAME=$DEFAULT_IFNAME
956         [ -z "$HOSTNAME" ] && HOSTNAME=$lxc
957     fi
958
959     [ -z "$fcdistro" ] && fcdistro=$DEFAULT_FCDISTRO
960     [ -z "$pldistro" ] && pldistro=$DEFAULT_PLDISTRO
961     [ -z "$personality" ] && personality=$DEFAULT_PERSONALITY
962     
963     release=$(echo $fcdistro | cut -df -f2)
964
965     if [ "$personality" == "linux32" ]; then
966         arch=i386
967         arch2=i686
968     elif [ "$personality" == "linux64" ]; then
969         arch=x86_64
970         arch2=x86_64
971     else
972         echo "Unknown personality: $personality"
973     fi
974
975     # need lxc installed before we can run lxc-ls
976     # need bridge installed
977     prepare_host    
978
979     if [ -n "$BUILD_MODE" ] ; then
980
981         # Bridge IP affectation
982         x=$(echo $personality | cut -dx -f2)
983         y=$(echo $fcdistro | cut -df -f2)
984         z=$(($x + $y))
985
986         IP="192.168.122.$z"
987         NETMASK="255.255.255.0"
988         GATEWAY="192.168.122.1"
989         
990         lxc_network_type=veth
991         lxc_network_link=virbr0
992         veth_pair="veth$z"
993         echo "the IP address of container $lxc is $IP "
994     else
995         [[ -z "$REPO_URL" ]] && usage
996         [[ -z "$IP" ]] && usage
997        
998         NETMASK=$(ifconfig br0 | grep 'inet ' | awk '{print $4}' | sed -e 's/.*://')
999         GATEWAY=$(route -n | grep 'UG' | awk '{print $2}')
1000         [[ -z "$HOSTNAME" ]] && usage
1001         lxc_network_type=veth
1002         lxc_network_link=br0
1003         veth_pair="i$(echo $HOSTNAME | cut -d. -f1)"
1004     fi
1005
1006     CIDR=$(cidr_notation $NETMASK)
1007     
1008
1009     if [ "$(id -u)" != "0" ]; then
1010           echo "This script should be run as 'root'"
1011           exit 1
1012     fi
1013
1014     path=/vservers
1015     [ ! -d $path ] && mkdir $path
1016     rootfs_path=$path/$lxc/rootfs
1017     config_path=$path/$lxc
1018     cache_base=/var/cache/lxc/fedora/$arch
1019     cache=$cache_base/$release
1020     root_password=root
1021     
1022     # check whether the rootfs directory is created to know if the container exists
1023     # bacause /var/lib/lxc/$lxc is already created while putting $lxc.timestamp
1024     [ -d $rootfs_path ] && { echo "container $lxc already exists - exiting" ; exit 1 ; }
1025
1026     setup_lxc $lxc $fcdistro $pldistro $personality 
1027
1028     devel_or_vtest_tools $lxc $fcdistro $pldistro $personality
1029
1030     post_install $lxc $personality
1031     
1032     echo $COMMAND Done
1033 }
1034
1035 main "$@"