10 # pkgs parsing utilities
11 PATH=$(dirname $0):$PATH export PATH
15 DEFAULT_PLDISTRO=planetlab
16 DEFAULT_PERSONALITY=linux64
18 COMMAND_LBUILD="lbuild-initvm.sh"
19 COMMAND_LTEST="ltest-initvm.sh"
21 libvirt_version="1.0.4"
23 ## stolen from tests/system/template-qemu/qemu-bridge-init
24 #################### compute INTERFACE_LAN
25 # use /proc/net/dev instead of a hard-wired list
26 function gather_interfaces () {
28 for line in file("/proc/net/dev"):
29 if ':' not in line: continue
30 ifname=line.replace(" ","").split(":")[0]
31 if ifname.find("lo")==0: continue
32 if ifname.find("br")==0: continue
33 if ifname.find("virbr")==0: continue
34 if ifname.find("tap")==0: continue
39 function discover_interface () {
40 for ifname in $(gather_interfaces); do
41 ip link show $ifname | grep -qi 'state UP' && { echo $ifname; return; }
43 # still not found ? that's bad
46 DEFAULT_IFNAME=$(discover_interface)
48 function bridge_init () {
56 # Default Value for INTERFACE_LAN
57 INTERFACE_LAN=$(netstat -rn | grep '^0.0.0.0' | awk '{print $8;}')
60 echo "========== $COMMAND: entering start - beg"
65 echo "========== $COMMAND: entering start - end"
67 # disable netfilter calls for bridge interface (they cause panick on 2.6.35 anyway)
69 # another option would be to accept the all forward packages for
70 # bridged interface like: -A FORWARD -m physdev --physdev-is-bridged -j ACCEPT
71 sysctl net.bridge.bridge-nf-call-iptables=0
72 sysctl net.bridge.bridge-nf-call-ip6tables=0
73 sysctl net.bridge.bridge-nf-call-arptables=0
75 # take extra arg for ifname, if provided
76 [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
78 #if we have already configured the same host_box no need to do it again
79 /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null && {
80 echo "Bridge interface $INTERFACE_BRIDGE already set up - $COMMAND start exiting"
83 /sbin/ifconfig $INTERFACE_LAN &>/dev/null || {
84 echo "Cannot use interface $INTERFACE_LAN - exiting"
89 #Getting host IP/masklen
90 address=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $2;}')
91 [ -z "$address" ] && { echo "ERROR: Could not determine IP address for $INTERFACE_LAN" ; exit 1 ; }
93 broadcast=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $4;}')
94 [ -z "$broadcast" ] && echo "WARNING: Could not determine broadcast address for $INTERFACE_LAN"
96 gateway=$(netstat -rn | grep '^0.0.0.0' | awk '{print $2;}')
97 [ -z "$gateway" ] && echo "WARNING: Could not determine gateway IP"
100 echo "Creating bridge INTERFACE_BRIDGE=$INTERFACE_BRIDGE"
101 brctl addbr $INTERFACE_BRIDGE
102 #brctl stp $INTERFACE_BRIDGE yes
103 brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
104 echo "Activating promiscuous mode INTERFACE_LAN=$INTERFACE_LAN"
105 /sbin/ifconfig $INTERFACE_LAN 0.0.0.0 promisc up
107 echo "Setting bridge address=$address broadcast=$broadcast"
109 #/sbin/ifconfig $INTERFACE_BRIDGE $address broadcast $broadcast up
110 dhclient $INTERFACE_BRIDGE
113 #Reconfigure the routing table
114 echo "Configuring gateway=$gateway"
115 route add default gw $gateway
117 echo "========== $COMMAND: exiting start - beg"
120 echo "========== $COMMAND: exiting start - end"
128 function failure () {
129 echo "$COMMAND : Bailing out"
133 function cidr_notation () {
137 for i in $(seq 1 4) ; do
138 part=$(echo $netmask | cut -d. -f $i)
140 "255") cidr=$((cidr + 8));;
141 "254") cidr=$((cidr + 7));;
142 "252") cidr=$((cidr + 6));;
143 "248") cidr=$((cidr + 5));;
144 "240") cidr=$((cidr + 4));;
145 "224") cidr=$((cidr + 3));;
146 "192") cidr=$((cidr + 2));;
147 "128") cidr=$((cidr + 1));;
148 "0") cidr=$((cidr + 0));;
155 function check_yum_installed () {
157 rpm -q $package >& /dev/null || yum -y install $package
160 function check_yumgroup_installed () {
162 yum grouplist "$group" | grep -q Installed || { yum -y groupinstall "$group" ; }
165 function prepare_host() {
167 ### Thierry - jan 14 - turning off this check as our boxes now meet this req.
168 ### and I'm trying out f20's stock libvirt instead
169 # ## check if libvirt_version is installed
170 # virsh -v | grep -e $libvirt_version || { echo "$libvirt_version needs to be installed!!!" ; exit 1 ; }
172 #################### bride initialization
173 check_yum_installed bridge-utils
175 isInstalled=$(netstat -rn | grep '^0.0.0.0' | awk '{print $8;}')
176 if [ "$isInstalled" != "br0" ] ; then
186 function configure_fedora() {
188 # disable selinux in fedora
189 mkdir -p $rootfs_path/selinux
190 echo 0 > $rootfs_path/selinux/enforce
192 # configure the network
194 cat <<EOF > ${rootfs_path}/etc/sysconfig/network-scripts/ifcfg-$IFNAME
210 cat <<EOF > ${rootfs_path}/etc/hostname
215 cat <<EOF > ${rootfs_path}/etc/sysconfig/network
220 cat <<EOF > $rootfs_path/etc/hosts
221 127.0.0.1 localhost $HOSTNAME
226 dev_path="${rootfs_path}/dev"
229 mknod -m 666 ${dev_path}/null c 1 3
230 mknod -m 666 ${dev_path}/zero c 1 5
231 mknod -m 666 ${dev_path}/random c 1 8
232 mknod -m 666 ${dev_path}/urandom c 1 9
233 mkdir -m 755 ${dev_path}/pts
234 mkdir -m 1777 ${dev_path}/shm
235 mknod -m 666 ${dev_path}/tty c 5 0
236 mknod -m 666 ${dev_path}/tty0 c 4 0
237 mknod -m 666 ${dev_path}/tty1 c 4 1
238 mknod -m 666 ${dev_path}/tty2 c 4 2
239 mknod -m 666 ${dev_path}/tty3 c 4 3
240 mknod -m 666 ${dev_path}/tty4 c 4 4
241 mknod -m 600 ${dev_path}/console c 5 1
242 mknod -m 666 ${dev_path}/full c 1 7
243 mknod -m 600 ${dev_path}/initctl p
244 mknod -m 666 ${dev_path}/ptmx c 5 2
246 #echo "setting root passwd to $root_password"
247 #echo "root:$root_password" | chroot $rootfs_path chpasswd
253 function configure_fedora_init() {
255 sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.sysinit
256 sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.d/rc.sysinit
257 # don't mount devpts, for pete's sake
258 sed -i 's/^.*dev.pts.*$/#\0/' ${rootfs_path}/etc/rc.sysinit
259 sed -i 's/^.*dev.pts.*$/#\0/' ${rootfs_path}/etc/rc.d/rc.sysinit
260 chroot ${rootfs_path} /sbin/chkconfig udev-post off
261 chroot ${rootfs_path} /sbin/chkconfig network on
264 # this code of course is for guests that do run on systemd
265 function configure_fedora_systemd() {
266 # so ignore if we can't find /etc/systemd at all
267 [ -d ${rootfs_path}/etc/systemd ] || return 0
268 # otherwise let's proceed
269 ln -sf /lib/systemd/system/multi-user.target ${rootfs_path}/etc/systemd/system/default.target
270 touch ${rootfs_path}/etc/fstab
271 ln -sf /dev/null ${rootfs_path}/etc/systemd/system/udev.service
273 # this was intended for f16 initially, in order to enable getty that otherwise would not start
274 # having a getty running is helpful only if ssh won't start though, and we see a correlation between
275 # VM's that refuse to lxc-stop and VM's that run crazy getty's
276 # so, turning getty off for now instead
277 # #dependency on a device unit fails it specially that we disabled udev
278 # sed -i 's/After=dev-%i.device/After=/' ${rootfs_path}/lib/systemd/system/getty\@.service
279 ln -sf /dev/null ${rootfs_path}/etc/systemd/system/"getty@.service"
280 rm -f ${rootfs_path}/etc/systemd/system/getty.target.wants/*service || :
281 # can't seem to handle this one with systemctl
282 chroot ${rootfs_path} /sbin/chkconfig network on
285 function download_fedora() {
287 # check the mini fedora was not already downloaded
288 INSTALL_ROOT=$cache/partial
291 # download a mini fedora into a cache
292 echo "Downloading fedora minimal ..."
294 mkdir -p $INSTALL_ROOT
295 if [ $? -ne 0 ]; then
296 echo "Failed to create '$INSTALL_ROOT' directory"
300 mkdir -p $INSTALL_ROOT/etc/yum.repos.d
301 mkdir -p $INSTALL_ROOT/dev
302 mknod -m 0444 $INSTALL_ROOT/dev/random c 1 8
303 mknod -m 0444 $INSTALL_ROOT/dev/urandom c 1 9
305 # copy yum config and repo files
306 cp /etc/yum.conf $INSTALL_ROOT/etc/
307 cp /etc/yum.repos.d/fedora* $INSTALL_ROOT/etc/yum.repos.d/
309 # append fedora repo files with desired $release and $basearch
310 for f in $INSTALL_ROOT/etc/yum.repos.d/*
312 sed -i "s/\$basearch/$arch/g; s/\$releasever/$release/g;" $f
315 MIRROR_URL=http://mirror.onelab.eu/fedora/releases/$release/Everything/$arch/os
316 RELEASE_URL1="$MIRROR_URL/Packages/fedora-release-$release-1.noarch.rpm"
317 # with fedora18 the rpms are scattered by first name
318 RELEASE_URL2="$MIRROR_URL/Packages/f/fedora-release-$release-1.noarch.rpm"
319 RELEASE_TARGET=$INSTALL_ROOT/fedora-release-$release.noarch.rpm
321 for attempt in $RELEASE_URL1 $RELEASE_URL2; do
322 if curl -f $attempt -o $RELEASE_TARGET ; then
323 echo "Retrieved $attempt"
327 echo "Failed attempt $attempt"
330 [ -n "$found" ] || { echo "Could not retrieve fedora-release rpm - exiting" ; exit 1; }
332 mkdir -p $INSTALL_ROOT/var/lib/rpm
333 rpm --root $INSTALL_ROOT --initdb
334 # when installing f12 this apparently is already present, so ignore result
335 rpm --root $INSTALL_ROOT -ivh $INSTALL_ROOT/fedora-release-$release.noarch.rpm || :
336 # however f12 root images won't get created on a f18 host
337 # (the issue here is the same as the one we ran into when dealing with a vs-box)
338 # in a nutshell, in f12 the glibc-common and filesystem rpms have an apparent conflict
339 # >>> file /usr/lib/locale from install of glibc-common-2.11.2-3.x86_64 conflicts
340 # with file from package filesystem-2.4.30-2.fc12.x86_64
341 # in fact this was - of course - allowed by f12's rpm but later on a fix was made
342 # http://rpm.org/gitweb?p=rpm.git;a=commitdiff;h=cf1095648194104a81a58abead05974a5bfa3b9a
343 # So ideally if we want to be able to build f12 images from f18 we need an rpm that has
344 # this patch undone, like we have in place on our f14 boxes (our f14 boxes need a f18-like rpm)
346 YUM="yum --installroot=$INSTALL_ROOT --nogpgcheck -y"
347 PKG_LIST="yum initscripts passwd rsyslog vim-minimal dhclient chkconfig rootfiles policycoreutils openssh-server openssh-clients"
348 echo "$YUM install $PKG_LIST"
349 $YUM install $PKG_LIST
351 if [ $? -ne 0 ]; then
352 echo "Failed to download the rootfs, aborting."
356 mv "$INSTALL_ROOT" "$cache/rootfs"
357 echo "Download complete."
363 function copy_fedora() {
365 # make a local copy of the minifedora
366 echo -n "Copying rootfs to $rootfs_path ..."
367 mkdir -p $rootfs_path
368 rsync -a $cache/rootfs/ $rootfs_path/
373 function update_fedora() {
375 YUM="yum --installroot $cache/rootfs -y --nogpgcheck"
380 function install_fedora() {
383 mkdir -p /var/lock/subsys/
386 if [ $? -ne 0 ]; then
387 echo "Cache repository is busy."
391 echo "Checking cache download in $cache/rootfs ... "
392 if [ ! -e "$cache/rootfs" ]; then
394 if [ $? -ne 0 ]; then
395 echo "Failed to download 'fedora base'"
399 echo "Cache found. Updating..."
401 if [ $? -ne 0 ]; then
402 echo "Failed to update 'fedora base', continuing with last known good cache"
404 echo "Update finished"
408 echo "Copy $cache/rootfs to $rootfs_path ... "
410 if [ $? -ne 0 ]; then
411 echo "Failed to copy rootfs"
417 ) 200>/var/lock/subsys/lxc
423 function copy_configuration() {
425 mkdir -p $config_path
426 cat <<EOF >> $config_path/config
431 lxc.rootfs = $rootfs_path
432 lxc.mount = $config_path/fstab
434 lxc.network.type = $lxc_network_type
435 lxc.network.flags = up
436 lxc.network.link = $lxc_network_link
437 lxc.network.name = $IFNAME
438 lxc.network.mtu = 1500
439 lxc.network.ipv4 = $IP/$CIDR
440 lxc.network.veth.pair = $veth_pair
442 #lxc.cgroup.devices.deny = a
444 lxc.cgroup.devices.allow = c 1:3 rwm
445 lxc.cgroup.devices.allow = c 1:5 rwm
447 lxc.cgroup.devices.allow = c 5:1 rwm
448 lxc.cgroup.devices.allow = c 5:0 rwm
449 lxc.cgroup.devices.allow = c 4:0 rwm
450 lxc.cgroup.devices.allow = c 4:1 rwm
452 lxc.cgroup.devices.allow = c 1:9 rwm
453 lxc.cgroup.devices.allow = c 1:8 rwm
454 lxc.cgroup.devices.allow = c 136:* rwm
455 lxc.cgroup.devices.allow = c 5:2 rwm
457 lxc.cgroup.devices.allow = c 254:0 rwm
458 lxc.cgroup.devices.allow = b 255:0 rwm
463 cat <<EOF > $config_path/fstab
464 proc $rootfs_path/proc proc nodev,noexec,nosuid 0 0
465 devpts $rootfs_path/dev/pts devpts defaults 0 0
466 sysfs $rootfs_path/sys sysfs defaults 0 0
468 if [ $? -ne 0 ]; then
469 echo "Failed to add configuration"
479 # overwrite lxc's internal yum config
480 function configure_yum_in_lxc () {
489 echo "Initializing yum.repos.d in $lxc"
490 rm -f $rootfs_path/etc/yum.repos.d/*
492 cat > $rootfs_path/etc/yum.repos.d/building.repo <<EOF
494 name=Fedora $release - $arch
495 baseurl=http://mirror.onelab.eu/fedora/releases/$release/Everything/$arch/os/
499 gpgkey=http://mirror.onelab.eu/keys/RPM-GPG-KEY-fedora-$release-primary
502 name=Fedora $release - $arch - Updates
503 baseurl=http://mirror.onelab.eu/fedora/updates/$release/$arch/
507 gpgkey=http://mirror.onelab.eu/keys/RPM-GPG-KEY-fedora-$release-primary
510 # for using vtest-init-lxc.sh as a general-purpose lxc creation wrapper
511 # just mention 'none' as the repo url
512 if [ -n "$TEST_MODE" -a "$REPO_URL" != "none" ] ; then
513 if [ ! -d $rootfs_path/etc/yum.repos.d ] ; then
514 echo "WARNING : cannot create myplc repo"
516 # exclude kernel from fedora repos
517 yumexclude=$(pl_plcyumexclude $fcdistro $pldistro $DIRNAME)
518 for repo in $rootfs_path/etc/yum.repos.d/* ; do
519 [ -f $repo ] && yumconf_exclude $repo "exclude=$yumexclude"
521 # the build repo is not signed at this stage
522 cat > $rootfs_path/etc/yum.repos.d/myplc.repo <<EOF
533 # return yum or debootstrap
534 function package_method () {
537 f[0-9]*|centos[0-9]*|sl[0-9]*) echo yum ;;
538 lenny|etch) echo debootstrap ;;
539 *) echo Unknown distro $fcdistro ;;
543 # return arch from debian distro and personality
544 function canonical_arch () {
545 personality=$1; shift
547 case $(package_method $fcdistro) in
549 case $personality in *32) echo i386 ;; *64) echo x86_64 ;; *) echo Unknown-arch-1 ;; esac ;;
551 case $personality in *32) echo i386 ;; *64) echo amd64 ;; *) echo Unknown-arch-2 ;; esac ;;
553 echo Unknown-arch-3 ;;
557 # the new test framework creates /timestamp in /vservers/<name> *before* populating it
558 function almost_empty () {
560 # non existing is fine
561 [ ! -d $dir ] && return 0;
562 # need to have at most one file
563 count=$(cd $dir; ls | wc -l); [ $count -le 1 ];
566 function setup_lxc() {
570 #trap failure ERR INT
575 personality=$1; shift
577 # create lxc container
579 if [ $? -ne 0 ]; then
580 echo "failed write configuration file"
585 if [ $? -ne 0 ]; then
586 echo "failed to install fedora"
591 if [ $? -ne 0 ]; then
592 echo "failed to configure fedora for a container"
596 if [ "$(echo $fcdistro | cut -d"f" -f2)" -le "14" ]; then
597 configure_fedora_init
599 configure_fedora_systemd
603 mkdir $rootfs_path/cgroup
606 cp /etc/resolv.conf $rootfs_path/etc/resolv.conf
607 # and /etc/hosts for at least localhost
608 [ -f $rootfs_path/etc/hosts ] || echo "127.0.0.1 localhost localhost.localdomain" > $rootfs_path/etc/hosts
611 mkdir $rootfs_path/root/.ssh
612 cat /root/.ssh/id_rsa.pub >> $rootfs_path/root/.ssh/authorized_keys
614 # copy libvirt xml template
615 veth_pair="i$(echo $HOSTNAME | cut -d. -f1)"
617 cat > $config_path/$tmpl_name<<EOF
620 <memory>524288</memory>
622 <type arch='$arch2'>exe</type>
623 <init>/sbin/init</init>
629 <clock offset='utc'/>
630 <on_poweroff>destroy</on_poweroff>
631 <on_reboot>restart</on_reboot>
632 <on_crash>destroy</on_crash>
634 <emulator>/usr/libexec/libvirt_lxc</emulator>
635 <filesystem type='mount'>
636 <source dir='$rootfs_path'/>
639 <interface type="bridge">
640 <source bridge="br0"/>
641 <target dev='$veth_pair'/>
643 <console type='pty' />
646 <name>host-bridge</name>
647 <forward mode="bridge"/>
653 # define lxc container for libvirt
654 virsh -c lxc:// define $config_path/$tmpl_name
657 chroot $rootfs_path /bin/rpm --rebuilddb
659 configure_yum_in_lxc $lxc $fcdistro $pldistro
664 function devel_or_vtest_tools () {
673 personality=$1; shift
675 pkg_method=$(package_method $fcdistro)
677 # check for .pkgs file based on pldistro
678 if [ -n "$BUILD_MODE" ] ; then
683 pkgsfile=$(pl_locateDistroFile $DIRNAME $pldistro $pkgsname)
685 ### install individual packages, then groups
686 # get target arch - use uname -i here (we want either x86_64 or i386)
688 lxc_arch=$(chroot $rootfs_path /bin/uname -i)
689 # on debian systems we get arch through the 'arch' command
690 [ "$lxc_arch" = "unknown" ] && lxc_arch=$(chroot $rootfs_path /bin/arch)
692 packages=$(pl_getPackages -a $lxc_arch $fcdistro $pldistro $pkgsfile)
693 groups=$(pl_getGroups -a $lxc_arch $fcdistro $pldistro $pkgsfile)
695 case "$pkg_method" in
697 [ -n "$packages" ] && chroot $rootfs_path /usr/bin/yum -y install $packages
698 for group_plus in $groups; do
699 group=$(echo $group_plus | sed -e "s,+++, ,g")
700 chroot $rootfs_path /usr/bin/yum -y groupinstall "$group"
702 # store current rpm list in /init-lxc.rpms in case we need to check the contents
703 chroot $rootfs_path /bin/rpm -aq > $rootfs_path/init-lxc.rpms
706 chroot $rootfs_path /usr/bin/apt-get update
707 for package in $packages ; do
708 chroot $rootfs_path /usr/bin/apt-get install -y $package
710 ### xxx todo install groups with apt..
713 echo "unknown pkg_method $pkg_method"
720 function post_install () {
721 if [ -n "$BUILD_MODE" ] ; then
722 post_install_build "$@"
724 post_install_myplc "$@"
726 # setup localtime from the host
728 cp /etc/localtime $rootfs_path/etc/localtime
731 function post_install_build () {
738 personality=$1; shift
740 ### From myplc-devel-native.spec
741 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
742 cat << EOF | chroot $rootfs_path /bin/bash -x
743 # set up /dev/loop* in lxc
744 for i in \$(seq 0 255) ; do
745 /bin/mknod -m 640 /dev/loop\$i b 7 \$i
748 # create symlink for /dev/fd
749 [ ! -e "/dev/fd" ] && /bin/ln -s /proc/self/fd /dev/fd
751 # modify /etc/rpm/macros to not use /sbin/new-kernel-pkg
752 /bin/sed -i 's,/sbin/new-kernel-pkg:,,' /etc/rpm/macros
753 if [ -h "/sbin/new-kernel-pkg" ] ; then
754 filename=\$(/bin/readlink -f /sbin/new-kernel-pkg)
755 if [ "\$filename" == "/sbin/true" ] ; then
756 /bin/echo "WARNING: /sbin/new-kernel-pkg symlinked to /sbin/true"
757 /bin/echo "\tmost likely /etc/rpm/macros has /sbin/new-kernel-pkg declared in _netsharedpath."
758 /bin/echo "\tPlease remove /sbin/new-kernel-pkg from _netsharedpath and reintall mkinitrd."
763 # customize root's prompt
764 /bin/cat << PROFILE > /root/.profile
765 export PS1="[$lxc] \\w # "
771 # add a "build" user to the system
772 builduser=\$(grep "^build:" /etc/passwd | wc -l)
773 if [ \$builduser -eq 0 ] ; then
774 groupadd -o -g \$gid build;
775 useradd -o -c 'Automated Build' -u \$uid -g \$gid -n -M -s /bin/bash build;
778 # Allow build user to build certain RPMs as root
779 if [ -f /etc/sudoers ] ; then
780 buildsudo=\$(grep "^build.*ALL=(ALL).*NOPASSWD:.*ALL" /etc/sudoers | wc -l)
781 if [ \$buildsudo -eq 0 ] ; then
782 echo "build ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
784 sed -i 's,^Defaults.*requiretty,#Defaults requiretty,' /etc/sudoers
791 function post_install_myplc () {
797 personality=$1; shift
799 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
800 cat << EOF | chroot $rootfs_path /bin/bash -x
802 # create /etc/sysconfig/network if missing
803 [ -f /etc/sysconfig/network ] || /bin/echo NETWORKING=yes > /etc/sysconfig/network
805 # create symlink for /dev/fd
806 [ ! -e "/dev/fd" ] && /bin/ln -s /proc/self/fd /dev/fd
808 # turn off regular crond, as plc invokes plc_crond
809 /sbin/chkconfig crond off
811 # take care of loginuid in /etc/pam.d
812 /bin/sed -i "s,#*\(.*loginuid.*\),#\1," /etc/pam.d/*
814 # customize root's prompt
815 /bin/cat << PROFILE > /root/.profile
816 export PS1="[$lxc] \\w # "
822 function start_lxc() {
826 #trap failure ERR INT
830 virsh -c lxc:// start $lxc
832 echo $IP is up, waiting for ssh...
834 #wait max 5 min for sshd to start
836 stop_time=$(($(date +%s) + 300))
837 current_time=$(date +%s)
840 while [ "$current_time" -lt "$stop_time" ] ; do
841 echo "$counter-th attempt to reach sshd in container $lxc ..."
842 ssh -o "StrictHostKeyChecking no" $IP 'uname -i' && { ssh_up=true; echo "SSHD in container $lxc is UP"; break ; } || :
844 current_time=$(($current_time + 10))
845 counter=$(($counter+1))
848 # Thierry: this is fatal, let's just exit with a failure here
849 [ -z $ssh_up ] && { echo "SSHD in container $lxc is not running" ; exit 1 ; }
856 echo "Usage: $COMMAND_LBUILD [options] lxc-name"
857 echo "Usage: $COMMAND_LTEST [options] lxc-name repo-url [ -- lxc-options ]"
859 echo " This command creates a fresh lxc instance, for building, or running, myplc"
860 echo "Supported options"
861 echo " -f fcdistro - for creating the root filesystem - defaults to $DEFAULT_FCDISTRO"
862 echo " -d pldistro - defaults to $DEFAULT_PLDISTRO"
863 echo " -p personality - defaults to $DEFAULT_PERSONALITY"
864 echo " -i ifname: determines ip and netmask attached to ifname, and passes it to the lxc"
865 echo "-- lxc-options"
866 echo " --netdev : interface to be defined inside lxc"
867 echo " --interface : IP to be defined for the lxc"
868 echo " --hostname : Hostname to be defined for the lxc"
869 echo "With $COMMAND_LTEST you can give 'none' as the URL, in which case"
870 echo " myplc.repo does not get created"
878 #trap failure ERR INT
893 while getopts "f:d:p:i:" opt ; do
895 f) fcdistro=$OPTARG;;
896 d) pldistro=$OPTARG;;
897 p) personality=$OPTARG;;
903 shift $(($OPTIND - 1))
905 # parse fixed arguments
906 [[ -z "$@" ]] && usage
908 if [ -n "$TEST_MODE" ] ; then
909 [[ -z "$@" ]] && usage
913 # parse vserver options
914 if [[ -n "$@" ]] ; then
915 if [ "$1" == "--" ] ; then
923 eval set -- "$LXC_OPTIONS"
928 --netdev) IFNAME=$2; shift 2;;
929 --interface) IP=$2; shift 2;;
930 --hostname) HOSTNAME=$2; shift 2;;
936 if [ -n "$BUILD_MODE" ] ; then
937 [ -z "$IFNAME" ] && IFNAME=$DEFAULT_IFNAME
938 [ -z "$HOSTNAME" ] && HOSTNAME=$lxc
941 [ -z "$fcdistro" ] && fcdistro=$DEFAULT_FCDISTRO
942 [ -z "$pldistro" ] && pldistro=$DEFAULT_PLDISTRO
943 [ -z "$personality" ] && personality=$DEFAULT_PERSONALITY
945 release=$(echo $fcdistro | cut -df -f2)
947 if [ "$personality" == "linux32" ]; then
950 elif [ "$personality" == "linux64" ]; then
954 echo "Unknown personality: $personality"
957 # need lxc installed before we can run lxc-ls
958 # need bridge installed
961 if [ -n "$BUILD_MODE" ] ; then
963 # Bridge IP affectation
964 x=$(echo $personality | cut -dx -f2)
965 y=$(echo $fcdistro | cut -df -f2)
969 NETMASK="255.255.255.0"
970 GATEWAY="192.168.122.1"
972 lxc_network_type=veth
973 lxc_network_link=virbr0
975 echo "the IP address of container $lxc is $IP "
977 [[ -z "$REPO_URL" ]] && usage
978 [[ -z "$IP" ]] && usage
980 NETMASK=$(ifconfig br0 | grep 'inet ' | awk '{print $4}' | sed -e 's/.*://')
981 GATEWAY=$(route -n | grep 'UG' | awk '{print $2}')
982 [[ -z "$HOSTNAME" ]] && usage
983 lxc_network_type=veth
985 veth_pair="i$(echo $HOSTNAME | cut -d. -f1)"
988 CIDR=$(cidr_notation $NETMASK)
991 if [ "$(id -u)" != "0" ]; then
992 echo "This script should be run as 'root'"
997 [ ! -d $path ] && mkdir $path
998 rootfs_path=$path/$lxc/rootfs
999 config_path=$path/$lxc
1000 cache_base=/var/cache/lxc/fedora/$arch
1001 cache=$cache_base/$release
1004 # check whether the rootfs directory is created to know if the container exists
1005 # bacause /var/lib/lxc/$lxc is already created while putting $lxc.timestamp
1006 [ -d $rootfs_path ] && { echo "container $lxc already exists - exiting" ; exit 1 ; }
1008 setup_lxc $lxc $fcdistro $pldistro $personality
1010 devel_or_vtest_tools $lxc $fcdistro $pldistro $personality
1012 post_install $lxc $personality