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