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