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