improve lxc upgrade
[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
9 # pkgs parsing utilities
10 PATH=$(dirname $0):$PATH export PATH
11 . build.common
12
13 DEFAULT_FCDISTRO=f16
14 DEFAULT_PLDISTRO=planetlab
15 DEFAULT_PERSONALITY=linux64
16 DEFAULT_IFNAME=eth0
17
18 COMMAND_VBUILD="vbuild-init-lxc.sh"
19 COMMAND_MYPLC="vtest-init-lxc.sh"
20
21 lxc_version="0.8.0-rc2"
22
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     sleep 1
86
87     #Reconfigure the routing table
88     echo "Configuring gateway=$gateway"
89     route add default gw $gateway
90
91     echo "========== $COMMAND: exiting start - beg"
92     ifconfig
93     netstat -rn
94     echo "========== $COMMAND: exiting start - end"
95
96
97 return 0
98
99 }
100
101
102 function failure () {
103     echo "$COMMAND : Bailing out"
104     exit 1
105 }
106
107 function cidr_notation () {
108
109 netmask=$1; shift
110 cidr=0
111 for i in $(seq 1 4) ; do
112     part=$(echo $netmask | cut -d. -f $i)
113     case $part in
114         "255") cidr=$((cidr + 8));;
115         "254") cidr=$((cidr + 7));;
116         "252") cidr=$((cidr + 6));;
117         "248") cidr=$((cidr + 5));;
118         "240") cidr=$((cidr + 4));;
119         "224") cidr=$((cidr + 3));;
120         "192") cidr=$((cidr + 2));;
121         "128") cidr=$((cidr + 1));;
122         "0") cidr=$((cidr + 0));;
123      esac
124 done
125 echo $cidr
126
127 }
128
129 function check_yum_installed () {
130     package=$1; shift
131     rpm -q $package >& /dev/null || yum -y install $package
132 }
133
134 function check_yumgroup_installed () {
135     group="$1"; shift
136     yum grouplist "$group" | grep -q Installed || { yum -y groupinstall "$group" ; }
137 }
138
139 function prepare_host() {
140         
141     #################### lxc-tools : rebuild as current fedora release has flaws
142     #install development tools
143     check_yumgroup_installed "Development Tools"
144     #install libcap-devel, libvirt
145     check_yum_installed libcap-devel
146     check_yum_installed libvirt
147
148     #retrieve and install lxc from sources 
149     raw_version=$(lxc-version ||: )
150     lxc_installed_version=$(echo $raw_version | sed -e 's,.*: ,,')
151     if [ "$lxc_installed_version" != "$lxc_version" ] ; then
152         echo "Expecting version" '['$lxc_version']'
153         echo "Found version" '['$lxc_installed_version']'
154         echo "Installing lxc ..."
155         cd /root
156         [ -d lxc ] || git clone git://lxc.git.sourceforge.net/gitroot/lxc/lxc 
157         cd lxc
158         git pull
159         git checkout $lxc_version
160         ./autogen.sh
161         ./configure --prefix=/usr --exec-prefix=/usr
162         make
163         make install
164     fi
165  
166 #    #create a placeholder (just a hack to make lxc works)
167 #    [ -d "/usr/local/var/lib" ] || mkdir -p /usr/local/var/lib
168
169     #################### bride initialization
170     check_yum_installed bridge-utils
171     #Bridge init
172     isInstalled=$(netstat -rn | grep '^0.0.0.0' | awk '{print $8;}')
173     if [ "$isInstalled" != "br0" ] ; then
174         bridge_init
175         sleep5
176     fi
177
178     return 0
179 }
180
181
182
183 function configure_fedora() {
184
185     # disable selinux in fedora
186     mkdir -p $rootfs_path/selinux
187     echo 0 > $rootfs_path/selinux/enforce
188
189    # configure the network 
190
191     cat <<EOF > ${rootfs_path}/etc/sysconfig/network-scripts/ifcfg-$IFNAME
192 DEVICE=$IFNAME
193 BOOTPROTO=static
194 ONBOOT=yes
195 HOSTNAME=$HOSTNAME
196 IPADDR=$IP
197 NETMASK=$NETMASK
198 GATEWAY=$GATEWAY
199 NM_CONTROLLED=no
200 TYPE=Ethernet
201 MTU=1500
202 EOF
203
204 # set the hostname
205     cat <<EOF > ${rootfs_path}/etc/sysconfig/network
206 NETWORKING=yes
207 HOSTNAME=$HOSTNAME
208 EOF
209
210     # set minimal hosts
211 #    cat <<EOF > $rootfs_path/etc/hosts
212 #127.0.0.1 localhost $HOSTNAME
213 #EOF
214
215     dev_path="${rootfs_path}/dev"
216     rm -rf $dev_path
217     mkdir -p $dev_path
218     mknod -m 666 ${dev_path}/null c 1 3
219     mknod -m 666 ${dev_path}/zero c 1 5
220     mknod -m 666 ${dev_path}/random c 1 8
221     mknod -m 666 ${dev_path}/urandom c 1 9
222     mkdir -m 755 ${dev_path}/pts
223     mkdir -m 1777 ${dev_path}/shm
224     mknod -m 666 ${dev_path}/tty c 5 0
225     mknod -m 666 ${dev_path}/tty0 c 4 0
226     mknod -m 666 ${dev_path}/tty1 c 4 1
227     mknod -m 666 ${dev_path}/tty2 c 4 2
228     mknod -m 666 ${dev_path}/tty3 c 4 3
229     mknod -m 666 ${dev_path}/tty4 c 4 4
230     mknod -m 600 ${dev_path}/console c 5 1
231     mknod -m 666 ${dev_path}/full c 1 7
232     mknod -m 600 ${dev_path}/initctl p
233     mknod -m 666 ${dev_path}/ptmx c 5 2
234
235     #echo "setting root passwd to $root_password"
236     #echo "root:$root_password" | chroot $rootfs_path chpasswd
237
238     return 0
239 }
240
241
242 function configure_fedora_init() {
243
244     sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.sysinit
245     sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.d/rc.sysinit
246     chroot ${rootfs_path} chkconfig udev-post off
247     chroot ${rootfs_path} chkconfig network on
248 }
249
250
251 function configure_fedora_systemd() {
252
253     unlink ${rootfs_path}/etc/systemd/system/default.target
254     touch ${rootfs_path}/etc/fstab
255     chroot ${rootfs_path} ln -s /dev/null //etc/systemd/system/udev.service
256     chroot ${rootfs_path} ln -s /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
257     #dependency on a device unit fails it specially that we disabled udev
258     sed -i 's/After=dev-%i.device/After=/' ${rootfs_path}/lib/systemd/system/getty\@.service
259     chroot ${rootfs_path} chkconfig network on
260 }
261
262 function download_fedora() {
263 set -x
264     # check the mini fedora was not already downloaded
265     INSTALL_ROOT=$cache/partial
266     echo $INSTALL_ROOT
267     mkdir -p $INSTALL_ROOT
268     if [ $? -ne 0 ]; then
269         echo "Failed to create '$INSTALL_ROOT' directory"
270         return 1
271     fi
272
273     # download a mini fedora into a cache
274     echo "Downloading fedora minimal ..."
275     YUM="yum --installroot $INSTALL_ROOT -y --nogpgcheck --releasever=$release"
276     PKG_LIST="yum initscripts passwd rsyslog vim-minimal dhclient chkconfig rootfiles policycoreutils openssh-server openssh-clients"
277   
278     
279     MIRROR_URL=http://mirror.onelab.eu/fedora/releases/$release/Everything/$arch/os
280     RELEASE_URL="$MIRROR_URL/Packages/fedora-release-$release-1.noarch.rpm"
281     echo "Fetching from $RELEASE_URL"
282     curl -f "$RELEASE_URL" > $INSTALL_ROOT/fedora-release-$release.noarch.rpm
283     
284     mkdir -p $INSTALL_ROOT/var/lib/rpm
285     rpm --root $INSTALL_ROOT  --initdb
286     rpm --root $INSTALL_ROOT -ivh $INSTALL_ROOT/fedora-release-$release.noarch.rpm
287     echo "$YUM install $PKG_LIST"
288     $YUM install $PKG_LIST
289
290     if [ $? -ne 0 ]; then
291         echo "Failed to download the rootfs, aborting."
292         return 1
293     fi
294
295     mv "$INSTALL_ROOT" "$cache/rootfs"
296     echo "Download complete."
297
298     return 0
299 }
300
301
302 function copy_fedora() {
303 set -x
304     # make a local copy of the minifedora
305     echo -n "Copying rootfs to $rootfs_path ..."
306     mkdir -p $rootfs_path
307     rsync -a $cache/rootfs/ $rootfs_path/
308     return 0
309 }
310
311
312 function update_fedora() {
313 set -x
314     YUM="yum --installroot $cache/rootfs -y --nogpgcheck"
315     $YUM update
316 }
317
318
319 function install_fedora() {
320     set -x
321
322     mkdir -p /var/lock/subsys/
323     (
324         flock -n -x 200
325         if [ $? -ne 0 ]; then
326             echo "Cache repository is busy."
327             return 1
328         fi
329
330         echo "Checking cache download in $cache/rootfs ... "
331         if [ ! -e "$cache/rootfs" ]; then
332             download_fedora
333             if [ $? -ne 0 ]; then
334                 echo "Failed to download 'fedora base'"
335                 return 1
336             fi
337         else
338             echo "Cache found. Updating..."
339             update_fedora
340             if [ $? -ne 0 ]; then
341                 echo "Failed to update 'fedora base', continuing with last known good cache"
342             else
343                 echo "Update finished"
344             fi
345         fi
346
347         echo "Copy $cache/rootfs to $rootfs_path ... "
348         copy_fedora
349         if [ $? -ne 0 ]; then
350             echo "Failed to copy rootfs"
351             return 1
352         fi
353
354         return 0
355
356         ) 200>/var/lock/subsys/lxc
357
358     return $?
359 }
360
361
362 function copy_configuration() {
363
364     mkdir -p $config_path
365     cat <<EOF >> $config_path/config
366 lxc.utsname = $lxc
367 lxc.arch = $arch2
368 lxc.tty = 4
369 lxc.pts = 1024
370 lxc.rootfs = $rootfs_path
371 lxc.mount  = $config_path/fstab
372 #networking
373 lxc.network.type = $lxc_network_type
374 lxc.network.flags = up
375 lxc.network.link = $lxc_network_link
376 lxc.network.name = $IFNAME
377 lxc.network.mtu = 1500
378 lxc.network.ipv4 = $IP/$CIDR
379 #cgroups
380 #lxc.cgroup.devices.deny = a
381 # /dev/null and zero
382 lxc.cgroup.devices.allow = c 1:3 rwm
383 lxc.cgroup.devices.allow = c 1:5 rwm
384 # consoles
385 lxc.cgroup.devices.allow = c 5:1 rwm
386 lxc.cgroup.devices.allow = c 5:0 rwm
387 lxc.cgroup.devices.allow = c 4:0 rwm
388 lxc.cgroup.devices.allow = c 4:1 rwm
389 # /dev/{,u}random
390 lxc.cgroup.devices.allow = c 1:9 rwm
391 lxc.cgroup.devices.allow = c 1:8 rwm
392 lxc.cgroup.devices.allow = c 136:* rwm
393 lxc.cgroup.devices.allow = c 5:2 rwm
394 # rtc
395 lxc.cgroup.devices.allow = c 254:0 rwm
396 lxc.cgroup.devices.allow = b 255:0 rwm
397 EOF
398
399
400
401     cat <<EOF > $config_path/fstab
402 proc            $rootfs_path/proc         proc    nodev,noexec,nosuid 0 0
403 devpts          $rootfs_path/dev/pts      devpts defaults 0 0
404 sysfs           $rootfs_path/sys          sysfs defaults  0 0
405 EOF
406     if [ $? -ne 0 ]; then
407         echo "Failed to add configuration"
408         return 1
409     fi
410
411     return 0
412 }
413
414
415
416
417 # overwrite lxc's internal yum config
418 function configure_yum_in_lxc () {
419     set -x 
420     set -e 
421     trap failure ERR INT
422
423     lxc=$1; shift
424     fcdistro=$1; shift
425     pldistro=$1; shift
426
427     echo "Initializing yum.repos.d in $lxc"
428     rm -f $rootfs_path/etc/yum.repos.d/*
429
430     cat > $rootfs_path/etc/yum.repos.d/building.repo <<EOF
431 [fedora]
432 name=Fedora $release - \$basearch
433 baseurl=http://mirror.onelab.eu/fedora/releases/$release/Everything/\$basearch/os/
434 enabled=1
435 metadata_expire=7d
436 gpgcheck=1
437 gpgkey=http://mirror.onelab.eu/keys/RPM-GPG-KEY-fedora-$release-primary
438
439 [updates]
440 name=Fedora $release - \$basearch - Updates
441 baseurl=http://mirror.onelab.eu/fedora/updates/$release/\$basearch/
442 enabled=1
443 metadata_expire=7d
444 gpgcheck=1
445 gpgkey=http://mirror.onelab.eu/keys/RPM-GPG-KEY-fedora-$release-primary
446 EOF
447     
448     # for using vtest-init-lxc.sh as a general-purpose lxc creation wrapper
449     # just mention 'none' as the repo url
450     if [ -n "$MYPLC_MODE" -a "$REPO_URL" != "none" ] ; then
451         if [ ! -d $rootfs_path/etc/yum.repos.d ] ; then
452             echo "WARNING : cannot create myplc repo"
453         else
454             # exclude kernel from fedora repos 
455             yumexclude=$(pl_plcyumexclude $fcdistro $pldistro $DIRNAME)
456             for repo in $rootfs_path/etc/yum.repos.d/* ; do
457                 [ -f $repo ] && yumconf_exclude $repo "exclude=$yumexclude" 
458             done
459             # the build repo is not signed at this stage
460             cat > $rootfs_path/etc/yum.repos.d/myplc.repo <<EOF
461 [myplc]
462 name= MyPLC
463 baseurl=$REPO_URL
464 enabled=1
465 gpgcheck=0
466 EOF
467         fi
468     fi
469 }    
470
471 # return yum or debootstrap
472 function package_method () {
473     fcdistro=$1; shift
474     case $fcdistro in
475         f[0-9]*|centos[0-9]*|sl[0-9]*) echo yum ;;
476         lenny|etch) echo debootstrap ;;
477         *) echo Unknown distro $fcdistro ;;
478     esac 
479 }
480
481 # return arch from debian distro and personality
482 function canonical_arch () {
483     personality=$1; shift
484     fcdistro=$1; shift
485     case $(package_method $fcdistro) in
486         yum)
487             case $personality in *32) echo i386 ;; *64) echo x86_64 ;; *) echo Unknown-arch-1 ;; esac ;;
488         debootstrap)
489             case $personality in *32) echo i386 ;; *64) echo amd64 ;; *) echo Unknown-arch-2 ;; esac ;;
490         *)
491             echo Unknown-arch-3 ;;
492     esac
493 }
494
495 # the new test framework creates /timestamp in /vservers/<name> *before* populating it
496 function almost_empty () { 
497     dir="$1"; shift ; 
498     # non existing is fine
499     [ ! -d $dir ] && return 0; 
500     # need to have at most one file
501     count=$(cd $dir; ls | wc -l); [ $count -le 1 ]; 
502 }
503
504 function setup_lxc() {
505
506     set -x
507     set -e
508     #trap failure ERR INT
509
510     lxc=$1; shift
511     fcdistro=$1; shift
512     pldistro=$1; shift
513     personality=$1; shift
514
515     # create lxc container 
516     copy_configuration
517     if [ $? -ne 0 ]; then
518         echo "failed write configuration file"
519         exit 1
520     fi
521
522     install_fedora
523     if [ $? -ne 0 ]; then
524         echo "failed to install fedora"
525         exit 1
526     fi
527
528     configure_fedora
529     if [ $? -ne 0 ]; then
530         echo "failed to configure fedora for a container"
531         exit 1
532     fi
533
534     type /bin/systemd >/dev/null 2>&1
535     if [ $? -ne 0 ]; then
536         configure_fedora_init
537     else
538         configure_fedora_systemd
539     fi
540
541     # Enable cgroup
542     mkdir $rootfs_path/cgroup
543     
544     # set up resolv.conf
545     cp /etc/resolv.conf $rootfs_path/etc/resolv.conf
546     # and /etc/hosts for at least localhost
547     [ -f $rootfs_path/etc/hosts ] || echo "127.0.0.1 localhost localhost.localdomain" > $rootfs_path/etc/hosts
548     
549     # ssh access to lxc
550     mkdir $rootfs_path/root/.ssh
551     cat /root/.ssh/id_rsa.pub >> $rootfs_path/root/.ssh/authorized_keys
552     
553     lxc-start -d -n $lxc
554
555     sleep 20
556
557     # rpm --rebuilddb
558     chroot $rootfs_path rpm --rebuilddb
559     #ssh -o "StrictHostKeyChecking no" $IP "rpm --rebuilddb"
560
561     configure_yum_in_lxc $lxc $fcdistro $pldistro
562
563     return 0
564 }
565
566 function devel_or_vtest_tools () {
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     pkg_method=$(package_method $fcdistro)
578
579     # check for .pkgs file based on pldistro
580     if [ -n "$VBUILD_MODE" ] ; then
581         pkgsname=devel.pkgs
582     else
583         pkgsname=vtest.pkgs
584     fi
585     pkgsfile=$(pl_locateDistroFile $DIRNAME $pldistro $pkgsname)
586
587     ### install individual packages, then groups
588     # get target arch - use uname -i here (we want either x86_64 or i386)
589    
590     lxc_arch=$(chroot $rootfs_path uname -i)
591     # on debian systems we get arch through the 'arch' command
592     [ "$lxc_arch" = "unknown" ] && lxc_arch=$(chroot $rootfs_path arch)
593
594     packages=$(pl_getPackages -a $lxc_arch $fcdistro $pldistro $pkgsfile)
595     groups=$(pl_getGroups -a $lxc_arch $fcdistro $pldistro $pkgsfile)
596
597     case "$pkg_method" in
598         yum)
599             [ -n "$packages" ] && chroot $rootfs_path yum -y install $packages
600             for group_plus in $groups; do
601                 group=$(echo $group_plus | sed -e "s,+++, ,g")
602                 chroot $rootfs_path yum -y groupinstall "$group"
603             done
604             # store current rpm list in /init-lxc.rpms in case we need to check the contents
605             chroot $rootfs_path rpm -aq > $rootfs_path/init-lxc.rpms
606             ;;
607         debootstrap)
608             chroot $rootfs_path apt-get update
609             for package in $packages ; do 
610                 chroot $rootfs_path  apt-get install -y $package 
611             done
612             ### xxx todo install groups with apt..
613             ;;
614         *)
615             echo "unknown pkg_method $pkg_method"
616             ;;
617     esac
618
619     return 0
620 }
621
622 function post_install () {
623     if [ -n "$VBUILD_MODE" ] ; then
624         post_install_vbuild "$@" 
625     else
626         post_install_myplc "$@"
627     fi
628     # setup localtime from the host
629     lxc=$1; shift 
630     cp /etc/localtime $rootfs_path/etc/localtime
631 }
632
633 function post_install_vbuild () {
634
635     set -x 
636     set -e 
637     trap failure ERR INT
638
639     lxc=$1; shift
640     personality=$1; shift
641
642 ### From myplc-devel-native.spec
643 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
644     cat << EOF | chroot $rootfs_path bash -x
645     # set up /dev/loop* in lxc
646     for i in \$(seq 0 255) ; do
647         mknod -m 640 /dev/loop\$i b 7 \$i
648     done
649     
650     # create symlink for /dev/fd
651     [ ! -e "/dev/fd" ] && ln -s /proc/self/fd /dev/fd
652
653     # modify /etc/rpm/macros to not use /sbin/new-kernel-pkg
654     sed -i 's,/sbin/new-kernel-pkg:,,' /etc/rpm/macros
655     if [ -h "/sbin/new-kernel-pkg" ] ; then
656         filename=\$(readlink -f /sbin/new-kernel-pkg)
657         if [ "\$filename" == "/sbin/true" ] ; then
658                 echo "WARNING: /sbin/new-kernel-pkg symlinked to /sbin/true"
659                 echo "\tmost likely /etc/rpm/macros has /sbin/new-kernel-pkg declared in _netsharedpath."
660                 echo "\tPlease remove /sbin/new-kernel-pkg from _netsharedpath and reintall mkinitrd."
661                 exit 1
662         fi
663     fi
664     
665     # customize root's prompt
666     cat << PROFILE > /root/.profile
667 export PS1="[$lxc] \\w # "
668 PROFILE
669
670     uid=2000
671     gid=2000
672     
673     # add a "build" user to the system
674     builduser=\$(grep "^build:" /etc/passwd | wc -l)
675     if [ \$builduser -eq 0 ] ; then
676         groupadd -o -g \$gid build;
677         useradd -o -c 'Automated Build' -u \$uid -g \$gid -n -M -s /bin/bash build;
678     fi
679
680 # Allow build user to build certain RPMs as root
681     if [ -f /etc/sudoers ] ; then
682         buildsudo=\$(grep "^build.*ALL=(ALL).*NOPASSWD:.*ALL"  /etc/sudoers | wc -l)
683         if [ \$buildsudo -eq 0 ] ; then
684             echo "build   ALL=(ALL)       NOPASSWD: ALL" >> /etc/sudoers
685         fi
686         sed -i 's,^Defaults.*requiretty,#Defaults requiretty,' /etc/sudoers
687     fi
688 #
689 EOF
690
691 }
692
693 function post_install_myplc  () {
694     set -x 
695     set -e 
696     trap failure ERR INT
697
698     lxc=$1; shift
699     personality=$1; shift
700
701 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
702     cat << EOF | chroot $rootfs_path bash -x
703
704     # create /etc/sysconfig/network if missing
705     [ -f /etc/sysconfig/network ] || echo NETWORKING=yes > /etc/sysconfig/network
706
707     # create symlink for /dev/fd
708     [ ! -e "/dev/fd" ] && ln -s /proc/self/fd /dev/fd
709
710     # turn off regular crond, as plc invokes plc_crond
711     chkconfig crond off
712
713     # take care of loginuid in /etc/pam.d 
714     sed -i "s,#*\(.*loginuid.*\),#\1," /etc/pam.d/*
715
716     # customize root's prompt
717     cat << PROFILE > /root/.profile
718 export PS1="[$lxc] \\w # "
719 PROFILE
720
721 EOF
722 }
723
724 function usage () {
725     set +x 
726     echo "Usage: $COMMAND_VBUILD [options] lxc-name"
727     echo "Usage: $COMMAND_MYPLC [options] lxc-name repo-url [ -- lxc-options ]"
728     echo "Description:"
729     echo "   This command creates a fresh lxc instance, for building, or running, myplc"
730     echo "Supported options"
731     echo " -f fcdistro - for creating the root filesystem - defaults to $DEFAULT_FCDISTRO"
732     echo " -d pldistro - defaults to $DEFAULT_PLDISTRO"
733     echo " -p personality - defaults to $DEFAULT_PERSONALITY"
734     echo " -i ifname: determines ip and netmask attached to ifname, and passes it to the lxc"
735     echo "-- lxc-options"
736     echo "  --netdev : interface to be defined inside lxc"
737     echo "  --interface : IP to be defined for the lxc"
738     echo "  --hostname : Hostname to be defined for the lxc"
739     echo "With $COMMAND_MYPLC you can give 'none' as the URL, in which case"
740     echo "   myplc.repo does not get created"
741     exit 1
742 }
743
744 ### parse args and 
745 function main () {
746
747     #set -e
748     #trap failure ERR INT
749
750     case "$COMMAND" in
751         $COMMAND_VBUILD)
752             VBUILD_MODE=true ;;
753         $COMMAND_MYPLC)
754             MYPLC_MODE=true;;
755         *)
756             usage ;;
757     esac
758
759     VERBOSE=
760     RESISTANT=""
761     IFNAME=""
762     LXC_OPTIONS=""
763     while getopts "f:d:p:i:" opt ; do
764         case $opt in
765             f) fcdistro=$OPTARG;;
766             d) pldistro=$OPTARG;;
767             p) personality=$OPTARG;;
768             i) IFNAME=$OPTARG;;
769             *) usage ;;
770         esac
771     done
772         
773     shift $(($OPTIND - 1))
774
775     # parse fixed arguments
776     [[ -z "$@" ]] && usage
777     lxc=$1 ; shift
778     if [ -n "$MYPLC_MODE" ] ; then
779         [[ -z "$@" ]] && usage
780         REPO_URL=$1 ; shift
781     fi
782
783     # parse vserver options
784     if [[ -n "$@" ]] ; then
785         if [ "$1" == "--" ] ; then
786             shift
787             LXC_OPTIONS="$@"
788         else
789             usage
790         fi
791     fi
792
793     eval set -- "$LXC_OPTIONS"
794
795     while true
796      do
797         case "$1" in
798              --netdev)      IFNAME=$2; shift 2;;
799              --interface)   IP=$2; shift 2;;
800              --hostname)    HOSTNAME=$2; shift 2;;
801              *)             break ;;
802         esac
803       done
804
805    
806     if [ -n "$VBUILD_MODE" ] ; then
807         [ -z "$IFNAME" ] && IFNAME=$DEFAULT_IFNAME
808         [ -z "$HOSTNAME" ] && HOSTNAME=$lxc
809     fi
810
811     [ -z "$fcdistro" ] && fcdistro=$DEFAULT_FCDISTRO
812     [ -z "$pldistro" ] && pldistro=$DEFAULT_PLDISTRO
813     [ -z "$personality" ] && personality=$DEFAULT_PERSONALITY
814     
815     release=$(echo $fcdistro | cut -df -f2)
816
817     if [ "$personality" == "linux32" ]; then
818         arch=i386
819         arch2=x86
820     elif [ "$personality" == "linux64" ]; then
821         arch=x86_64
822         arch2=x86_64
823     else
824         echo "Unknown personality: $personality"
825     fi
826
827     
828     if [ -n "$VBUILD_MODE" ] ; then
829
830         # Bridge IP affectation
831         x=$(echo $personality | cut -dx -f2)
832         y=$(echo $fcdistro | cut -df -f2)
833         z=$(($x + $y))
834
835         IP="192.168.122.$z"
836         NETMASK="255.255.255.0"
837         GATEWAY="192.168.122.1"
838         
839         lxc_network_type=veth
840         lxc_network_link=virbr0
841         echo "the IP address of container $lxc is $IP "
842     else
843         [[ -z "$REPO_URL" ]] && usage
844         [[ -z "$IP" ]] && usage
845         NETMASK=$(ifconfig br0 | grep 'inet addr' | awk '{print $4}' | sed -e 's/.*://')
846         GATEWAY=$(route -n | grep 'UG' | awk '{print $2}')
847         [[ -z "$HOSTNAME" ]] && usage
848         lxc_network_type=veth
849         lxc_network_link=br0
850     fi
851
852     CIDR=$(cidr_notation $NETMASK)
853     
854
855     if [ "$(id -u)" != "0" ]; then
856           echo "This script should be run as 'root'"
857           exit 1
858     fi
859
860     # need lxc installed before we can run lxc-ls
861     prepare_host
862     
863     if [ ! -z "$(lxc-ls | grep $lxc)" ];then
864         echo "container $lxc exists"
865         exit 1
866     fi
867
868     
869     path=/var/lib/lxc
870     rootfs_path=$path/$lxc/rootfs
871     config_path=$path/$lxc
872     cache_base=/var/cache/lxc/fedora/$arch
873     cache=$cache_base/$release
874     root_password=root
875
876
877     setup_lxc $lxc $fcdistro $pldistro $personality 
878
879     devel_or_vtest_tools $lxc $fcdistro $pldistro $personality
880
881     post_install $lxc $personality
882     
883
884     echo $COMMAND Done
885 }
886
887 main "$@"