10 # pkgs parsing utilities
11 PATH=$(dirname $0):$PATH export PATH
16 DEFAULT_PERSONALITY=linux64
18 COMMAND_LBUILD="lbuild-initvm.sh"
19 COMMAND_LTEST="ltest-initvm.sh"
22 # when creating build boxes we use private NAT'ed addresses for the VMs
23 # as per virbr0 that is taken care of by libvirt at startup
24 PRIVATE_BRIDGE="virbr0"
25 PRIVATE_PREFIX="192.168.122."
26 PRIVATE_GATEWAY="192.168.122.1"
27 # beware that changing this would break the logic of random_private_byte...
30 # we just try randomly in that range until a free IP address shows up
36 # the network interface name as seen from the container
39 ##############################
40 ## stolen from tests/system/template-qemu/qemu-bridge-init
41 # use /proc/net/dev instead of a hard-wired list
42 function gather_interfaces () {
44 for line in file("/proc/net/dev"):
45 if ':' not in line: continue
46 ifname=line.replace(" ","").split(":")[0]
47 if ifname.find("lo")==0: continue
48 if ifname.find("br")==0: continue
49 if ifname.find("virbr")==0: continue
50 if ifname.find("tap")==0: continue
55 function discover_interface () {
56 for ifname in $(gather_interfaces); do
57 ip link show $ifname | grep -qi 'state UP' && { echo $ifname; return; }
59 # still not found ? that's bad
62 ########## check for a free IP
63 function ip_is_busy () {
65 ping -c 1 -W 1 $target >& /dev/null
68 function random_private_byte () {
69 for attempt in $(seq $PRIVATE_ATTEMPTS); do
70 byte=$(($RANDOM % 256))
71 if [ "$byte" == 0 -o "$byte" == 1 ] ; then continue; fi
72 ip=${PRIVATE_PREFIX}${byte}
73 ip_is_busy $ip || { echo $byte; return; }
75 echo "Cannot seem to find a free IP address in range ${PRIVATE_PREFIX}.xx/24 after $PRIVATE_ATTEMPTS attempts - exiting"
79 ########## networking -- ctd
80 function gethostbyname () {
82 python -c "import socket; print socket.gethostbyname('"$hostname"')" 2> /dev/null
85 # e.g. 21 -> 255.255.248.0
86 function masklen_to_netmask () {
91 if not (masklen>=1 and masklen<=32):
92 print "Wrong masklen",masklen
100 result.append(masklen)
102 print ".".join([ str(256-2**(8-i)) for i in result ])
107 #################### bridge initialization
108 function create_bridge_if_needed() {
113 # already created ? - we're done
114 ip addr show $PUBLIC_BRIDGE >& /dev/null && {
115 echo "Bridge already set up - skipping create_bridge_if_needed"
119 # find out the physical interface to bridge onto
120 if_lan=$(discover_interface)
122 ip addr show $if_lan &>/dev/null || {
123 echo "Cannot use interface $if_lan - exiting"
127 #################### bride initialization
128 check_yum_installed bridge-utils
130 echo "========== $COMMAND: entering create_bridge - beg"
135 echo "========== $COMMAND: entering create_bridge - end"
137 # disable netfilter calls for bridge interface (they cause panick on 2.6.35 anyway)
139 # another option would be to accept the all forward packages for
140 # bridged interface like: -A FORWARD -m physdev --physdev-is-bridged -j ACCEPT
141 sysctl net.bridge.bridge-nf-call-iptables=0
142 sysctl net.bridge.bridge-nf-call-ip6tables=0
143 sysctl net.bridge.bridge-nf-call-arptables=0
146 #Getting host IP/masklen
147 address=$(ip addr show $if_lan | grep -v inet6 | grep inet | head --lines=1 | awk '{print $2;}')
148 [ -z "$address" ] && { echo "ERROR: Could not determine IP address for $if_lan" ; exit 1 ; }
150 broadcast=$(ip addr show $if_lan | grep -v inet6 | grep inet | head --lines=1 | awk '{print $4;}')
151 [ -z "$broadcast" ] && echo "WARNING: Could not determine broadcast address for $if_lan"
153 gateway=$(ip route show | grep default | awk '{print $3;}')
154 [ -z "$gateway" ] && echo "WARNING: Could not determine gateway IP"
157 # creating the bridge
158 echo "Creating bridge PUBLIC_BRIDGE=$PUBLIC_BRIDGE"
159 brctl addbr $PUBLIC_BRIDGE
160 brctl addif $PUBLIC_BRIDGE $if_lan
161 echo "Activating promiscuous mode if_lan=$if_lan"
162 ip link set $if_lan up promisc on
164 # rely on dhcp to re assign IP..
165 echo "Starting dhclient on $PUBLIC_BRIDGE"
166 dhclient $PUBLIC_BRIDGE
169 #Reconfigure the routing table
170 echo "Configuring gateway=$gateway"
171 ip route add default via $gateway dev $PUBLIC_BRIDGE
172 ip route del default via $gateway dev $if_lan
173 # at this point we have an extra route like e.g.
175 #default via 138.96.112.250 dev br0
176 #138.96.112.0/21 dev em1 proto kernel scope link src 138.96.112.57
177 #138.96.112.0/21 dev br0 proto kernel scope link src 138.96.112.57
178 #192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1
179 route_dest=$(ip route show | grep -v default | grep "dev $PUBLIC_BRIDGE" | awk '{print $1;}')
180 ip route del $route_dest dev $if_lan
182 echo "========== $COMMAND: exiting create_bridge - beg"
185 echo "========== $COMMAND: exiting create_bridge - end"
193 ##############################
194 # return yum or debootstrap
195 function package_method () {
198 f[0-9]*|centos[0-9]*|sl[0-9]*) echo yum ;;
199 squeeze|wheezy|oneiric|precise|quantal|raring|saucy) echo debootstrap ;;
200 *) echo Unknown distro $fcdistro ;;
204 # return arch from debian distro and personality
205 function canonical_arch () {
206 personality=$1; shift
208 case $(package_method $fcdistro) in
210 case $personality in *32) echo i386 ;; *64) echo x86_64 ;; *) echo Unknown-arch-1 ;; esac ;;
212 case $personality in *32) echo i386 ;; *64) echo amd64 ;; *) echo Unknown-arch-2 ;; esac ;;
214 echo Unknown-arch-3 ;;
218 # the new test framework creates /timestamp in /vservers/<name> *before* populating it
219 function almost_empty () {
221 # non existing is fine
222 [ ! -d $dir ] && return 0;
223 # need to have at most one file
224 count=$(cd $dir; ls | wc -l); [ $count -le 1 ];
227 ##############################
228 function check_yum_installed () {
230 rpm -q $package >& /dev/null || yum -y install $package
233 function check_yumgroup_installed () {
235 yum grouplist "$group" | grep -q Installed || { yum -y groupinstall "$group" ; }
238 ##############################
239 function fedora_install() {
243 mkdir -p /var/lock/subsys/
245 flock -n -x 200 || { echo "Cache repository is busy." ; return 1 ; }
247 if [ ! -e "$cache/rootfs" ]; then
248 echo "Getting cache download in $cache/rootfs ... "
249 fedora_download || { echo "Failed to download 'fedora base'"; return 1; }
251 echo "Updating cache $cache/rootfs ..."
252 if ! yum --installroot $cache/rootfs -y --nogpgcheck update ; then
253 echo "Failed to update 'fedora base', continuing with last known good cache"
255 echo "Update finished"
259 echo "Copy $cache/rootfs to $rootfs_path ... "
260 mkdir -p $rootfs_path
261 rsync -a $cache/rootfs/ $rootfs_path/
265 ) 200>/var/lock/subsys/lxc
270 function fedora_download() {
272 # check the mini fedora was not already downloaded
273 INSTALL_ROOT=$cache/partial
276 # download a mini fedora into a cache
277 echo "Downloading fedora minimal ..."
279 mkdir -p $INSTALL_ROOT || { echo "Failed to create '$INSTALL_ROOT' directory" ; return 1; }
281 mkdir -p $INSTALL_ROOT/etc/yum.repos.d
282 mkdir -p $INSTALL_ROOT/dev
283 mknod -m 0444 $INSTALL_ROOT/dev/random c 1 8
284 mknod -m 0444 $INSTALL_ROOT/dev/urandom c 1 9
286 # copy yum config and repo files
287 cp /etc/yum.conf $INSTALL_ROOT/etc/
288 cp /etc/yum.repos.d/fedora* $INSTALL_ROOT/etc/yum.repos.d/
290 # append fedora repo files with desired $release and $basearch
291 for f in $INSTALL_ROOT/etc/yum.repos.d/* ; do
292 sed -i "s/\$basearch/$arch/g; s/\$releasever/$release/g;" $f
295 MIRROR_URL=http://mirror.onelab.eu/fedora/releases/$release/Everything/$arch/os
296 RELEASE_URL1="$MIRROR_URL/Packages/fedora-release-$release-1.noarch.rpm"
297 # with fedora18 the rpms are scattered by first name
298 RELEASE_URL2="$MIRROR_URL/Packages/f/fedora-release-$release-1.noarch.rpm"
299 RELEASE_TARGET=$INSTALL_ROOT/fedora-release-$release.noarch.rpm
301 for attempt in $RELEASE_URL1 $RELEASE_URL2; do
302 if curl -f $attempt -o $RELEASE_TARGET ; then
303 echo "Retrieved $attempt"
307 echo "Failed attempt $attempt"
310 [ -n "$found" ] || { echo "Could not retrieve fedora-release rpm - exiting" ; exit 1; }
312 mkdir -p $INSTALL_ROOT/var/lib/rpm
313 rpm --root $INSTALL_ROOT --initdb
314 # when installing f12 this apparently is already present, so ignore result
315 rpm --root $INSTALL_ROOT -ivh $INSTALL_ROOT/fedora-release-$release.noarch.rpm || :
316 # however f12 root images won't get created on a f18 host
317 # (the issue here is the same as the one we ran into when dealing with a vs-box)
318 # in a nutshell, in f12 the glibc-common and filesystem rpms have an apparent conflict
319 # >>> file /usr/lib/locale from install of glibc-common-2.11.2-3.x86_64 conflicts
320 # with file from package filesystem-2.4.30-2.fc12.x86_64
321 # in fact this was - of course - allowed by f12's rpm but later on a fix was made
322 # http://rpm.org/gitweb?p=rpm.git;a=commitdiff;h=cf1095648194104a81a58abead05974a5bfa3b9a
323 # So ideally if we want to be able to build f12 images from f18 we need an rpm that has
324 # this patch undone, like we have in place on our f14 boxes (our f14 boxes need a f18-like rpm)
326 YUM="yum --installroot=$INSTALL_ROOT --nogpgcheck -y"
327 PKG_LIST="yum initscripts passwd rsyslog vim-minimal dhclient chkconfig rootfiles policycoreutils openssh-server openssh-clients"
328 echo "$YUM install $PKG_LIST"
329 $YUM install $PKG_LIST || { echo "Failed to download rootfs, aborting." ; return 1; }
331 mv "$INSTALL_ROOT" "$cache/rootfs"
332 echo "Download complete."
337 ##############################
338 function fedora_configure() {
343 # disable selinux in fedora
344 mkdir -p $rootfs_path/selinux
345 echo 0 > $rootfs_path/selinux/enforce
350 cat <<EOF > ${rootfs_path}/etc/hostname
355 cat <<EOF > ${rootfs_path}/etc/sysconfig/network
357 HOSTNAME=$GUEST_HOSTNAME
360 cat <<EOF > $rootfs_path/etc/hosts
361 127.0.0.1 localhost $GUEST_HOSTNAME
366 dev_path="${rootfs_path}/dev"
369 mknod -m 666 ${dev_path}/null c 1 3
370 mknod -m 666 ${dev_path}/zero c 1 5
371 mknod -m 666 ${dev_path}/random c 1 8
372 mknod -m 666 ${dev_path}/urandom c 1 9
373 mkdir -m 755 ${dev_path}/pts
374 mkdir -m 1777 ${dev_path}/shm
375 mknod -m 666 ${dev_path}/tty c 5 0
376 mknod -m 666 ${dev_path}/tty0 c 4 0
377 mknod -m 666 ${dev_path}/tty1 c 4 1
378 mknod -m 666 ${dev_path}/tty2 c 4 2
379 mknod -m 666 ${dev_path}/tty3 c 4 3
380 mknod -m 666 ${dev_path}/tty4 c 4 4
381 mknod -m 600 ${dev_path}/console c 5 1
382 mknod -m 666 ${dev_path}/full c 1 7
383 mknod -m 600 ${dev_path}/initctl p
384 mknod -m 666 ${dev_path}/ptmx c 5 2
386 #echo "setting root passwd to $root_password"
387 #echo "root:$root_password" | chroot $rootfs_path chpasswd
389 if [ "$(echo $fcdistro | cut -d"f" -f2)" -le "14" ]; then
390 fedora_configure_init
392 fedora_configure_systemd
395 fedora_configure_yum $lxc $fcdistro $pldistro
400 function fedora_configure_init() {
403 sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.sysinit
404 sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.d/rc.sysinit
405 # don't mount devpts, for pete's sake
406 sed -i 's/^.*dev.pts.*$/#\0/' ${rootfs_path}/etc/rc.sysinit
407 sed -i 's/^.*dev.pts.*$/#\0/' ${rootfs_path}/etc/rc.d/rc.sysinit
408 chroot ${rootfs_path} /sbin/chkconfig udev-post off
409 chroot ${rootfs_path} /sbin/chkconfig network on
412 # this code of course is for guests that do run on systemd
413 function fedora_configure_systemd() {
416 # so ignore if we can't find /etc/systemd at all
417 [ -d ${rootfs_path}/etc/systemd ] || return 0
418 # otherwise let's proceed
419 ln -sf /lib/systemd/system/multi-user.target ${rootfs_path}/etc/systemd/system/default.target
420 touch ${rootfs_path}/etc/fstab
421 ln -sf /dev/null ${rootfs_path}/etc/systemd/system/udev.service
423 # this was intended for f16 initially, in order to enable getty that otherwise would not start
424 # having a getty running is helpful only if ssh won't start though, and we see a correlation between
425 # VM's that refuse to lxc-stop and VM's that run crazy getty's
426 # so, turning getty off for now instead
427 # #dependency on a device unit fails it specially that we disabled udev
428 # sed -i 's/After=dev-%i.device/After=/' ${rootfs_path}/lib/systemd/system/getty\@.service
429 ln -sf /dev/null ${rootfs_path}/etc/systemd/system/"getty@.service"
430 rm -f ${rootfs_path}/etc/systemd/system/getty.target.wants/*service || :
431 # can't seem to handle this one with systemctl
432 chroot ${rootfs_path} /sbin/chkconfig network on
435 # overwrite container yum config
436 function fedora_configure_yum () {
446 chroot $rootfs_path /bin/rpm --rebuilddb
448 echo "Initializing yum.repos.d in $lxc"
449 rm -f $rootfs_path/etc/yum.repos.d/*
451 cat > $rootfs_path/etc/yum.repos.d/building.repo <<EOF
453 name=Fedora $release - $arch
454 baseurl=http://mirror.onelab.eu/fedora/releases/$release/Everything/$arch/os/
458 gpgkey=http://mirror.onelab.eu/keys/RPM-GPG-KEY-fedora-$release-primary
461 name=Fedora $release - $arch - Updates
462 baseurl=http://mirror.onelab.eu/fedora/updates/$release/$arch/
466 gpgkey=http://mirror.onelab.eu/keys/RPM-GPG-KEY-fedora-$release-primary
469 # for using vtest-init-lxc.sh as a general-purpose lxc creation wrapper
470 # just mention 'none' as the repo url
471 if [ -n "$REPO_URL" ] ; then
472 if [ ! -d $rootfs_path/etc/yum.repos.d ] ; then
473 echo "WARNING : cannot create myplc repo"
475 # exclude kernel from fedora repos
476 yumexclude=$(pl_plcyumexclude $fcdistro $pldistro $DIRNAME)
477 for repo in $rootfs_path/etc/yum.repos.d/* ; do
478 [ -f $repo ] && yumconf_exclude $repo "exclude=$yumexclude"
480 # the build repo is not signed at this stage
481 cat > $rootfs_path/etc/yum.repos.d/myplc.repo <<EOF
492 ##############################
493 # need to specify the right mirror for debian variants like ubuntu and the like
494 function debian_mirror () {
498 echo http://ftp2.fr.debian.org/debian/ ;;
499 oneiric|precise|quantal|raring|saucy)
500 echo http://mir1.ovh.net/ubuntu/ubuntu/ ;;
501 *) echo unknown distro $fcdistro; exit 1;;
505 function debian_install () {
508 mkdir -p $rootfs_path
509 arch=$(canonical_arch $personality $fcdistro)
510 mirror=$(debian_mirror $fcdistro)
511 # old guests have mount in /bin but this is no longer part of
512 # the standard PATH in recent hosts
513 PATH=$PATH:/bin:/sbin debootstrap --arch $arch $fcdistro $rootfs_path $mirror
516 function debian_configure () {
517 echo "WARNING No debian config available yet"
519 ##############################
520 function setup_lxc() {
524 #trap failure ERR INT
529 personality=$1; shift
531 # create lxc container
533 pkg_method=$(package_method $fcdistro)
536 fedora_install || { echo "failed to install fedora root image"; exit 1 ; }
537 fedora_configure || { echo "failed to configure fedora for a container"; exit 1 ; }
540 debian_install || { echo "failed to install debian/ubuntu root image"; exit 1 ; }
541 debian_configure || { echo "failed to configure debian/ubuntu for a container"; exit 1 ; }
542 echo "$COMMAND: no support for debootstrap-based systems - yet"
546 echo "$COMMAND:: unknown package_method - exiting"
551 # Enable cgroup -- xxx -- is this really useful ?
552 mkdir $rootfs_path/cgroup
555 cp /etc/resolv.conf $rootfs_path/etc/resolv.conf
556 # and /etc/hosts for at least localhost
557 [ -f $rootfs_path/etc/hosts ] || echo "127.0.0.1 localhost localhost.localdomain" > $rootfs_path/etc/hosts
559 # grant ssh access from host to guest
560 mkdir $rootfs_path/root/.ssh
561 cat /root/.ssh/id_rsa.pub >> $rootfs_path/root/.ssh/authorized_keys
563 config_xml=$config_path/"lxc.xml"
564 guest_ifcfg=${rootfs_path}/etc/sysconfig/network-scripts/ifcfg-$VIF_GUEST
565 if [ -n "$BUILD_MODE" ] ; then
566 write_lxc_xml_build $lxc > $config_xml
567 write_guest_ifcfg_build > $guest_ifcfg
569 write_lxc_xml_test $lxc > $config_xml
570 write_guest_ifcfg_test > $guest_ifcfg
573 # define lxc container for libvirt
574 virsh -c lxc:/// define $config_xml
579 function write_lxc_xml_test () {
584 <memory>524288</memory>
586 <type arch='$arch2'>exe</type>
587 <init>/sbin/init</init>
593 <clock offset='utc'/>
594 <on_poweroff>destroy</on_poweroff>
595 <on_reboot>restart</on_reboot>
596 <on_crash>destroy</on_crash>
598 <emulator>/usr/libexec/libvirt_lxc</emulator>
599 <filesystem type='mount'>
600 <source dir='$rootfs_path'/>
603 <interface type="bridge">
604 <source bridge="$PUBLIC_BRIDGE"/>
605 <target dev='$VIF_HOST'/>
607 <console type='pty' />
610 <name>host-bridge</name>
611 <forward mode="bridge"/>
612 <bridge name="$PUBLIC_BRIDGE"/>
618 function write_lxc_xml_build () {
623 <memory>524288</memory>
625 <type arch='$arch2'>exe</type>
626 <init>/sbin/init</init>
632 <clock offset='utc'/>
633 <on_poweroff>destroy</on_poweroff>
634 <on_reboot>restart</on_reboot>
635 <on_crash>destroy</on_crash>
637 <emulator>/usr/libexec/libvirt_lxc</emulator>
638 <filesystem type='mount'>
639 <source dir='$rootfs_path'/>
642 <interface type="network">
643 <source network="default"/>
645 <console type='pty' />
651 # this one is dhcp-based
652 function write_guest_ifcfg_build () {
663 # use fixed IP as specified by GUEST_HOSTNAME
664 function write_guest_ifcfg_test () {
669 HOSTNAME=$GUEST_HOSTNAME
679 function devel_or_vtest_tools () {
688 personality=$1; shift
690 pkg_method=$(package_method $fcdistro)
692 pkgsfile=$(pl_locateDistroFile $DIRNAME $pldistro $PREINSTALLED)
694 ### install individual packages, then groups
695 # get target arch - use uname -i here (we want either x86_64 or i386)
697 lxc_arch=$(chroot $rootfs_path /bin/uname -i)
698 # on debian systems we get arch through the 'arch' command
699 [ "$lxc_arch" = "unknown" ] && lxc_arch=$(chroot $rootfs_path /bin/arch)
701 packages=$(pl_getPackages -a $lxc_arch $fcdistro $pldistro $pkgsfile)
702 groups=$(pl_getGroups -a $lxc_arch $fcdistro $pldistro $pkgsfile)
704 case "$pkg_method" in
706 [ -n "$packages" ] && chroot $rootfs_path /usr/bin/yum -y install $packages
707 for group_plus in $groups; do
708 group=$(echo $group_plus | sed -e "s,+++, ,g")
709 chroot $rootfs_path /usr/bin/yum -y groupinstall "$group"
711 # store current rpm list in /init-lxc.rpms in case we need to check the contents
712 chroot $rootfs_path /bin/rpm -aq > $rootfs_path/init-lxc.rpms
715 chroot $rootfs_path /usr/bin/apt-get update
716 for package in $packages ; do
717 chroot $rootfs_path /usr/bin/apt-get install -y $package
719 ### xxx todo install groups with apt..
722 echo "unknown pkg_method $pkg_method"
729 function post_install () {
731 personality=$1; shift
732 if [ -n "$BUILD_MODE" ] ; then
733 post_install_build $lxc $personality
735 # manually run dhclient in guest - somehow this network won't start on its own
736 virsh -c lxc:/// lxc-enter-namespace $lxc /usr/sbin/dhclient $VIF_GUEST
738 post_install_myplc $lxc $personality
742 # setup localtime from the host
743 cp /etc/localtime $rootfs_path/etc/localtime
746 function post_install_build () {
753 personality=$1; shift
755 ### From myplc-devel-native.spec
756 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
757 cat << EOF | chroot $rootfs_path /bin/bash -x
758 # set up /dev/loop* in lxc
759 for i in \$(seq 0 255) ; do
760 /bin/mknod -m 640 /dev/loop\$i b 7 \$i
763 # create symlink for /dev/fd
764 [ ! -e "/dev/fd" ] && /bin/ln -s /proc/self/fd /dev/fd
766 # modify /etc/rpm/macros to not use /sbin/new-kernel-pkg
767 /bin/sed -i 's,/sbin/new-kernel-pkg:,,' /etc/rpm/macros
768 if [ -h "/sbin/new-kernel-pkg" ] ; then
769 filename=\$(/bin/readlink -f /sbin/new-kernel-pkg)
770 if [ "\$filename" == "/sbin/true" ] ; then
771 /bin/echo "WARNING: /sbin/new-kernel-pkg symlinked to /sbin/true"
772 /bin/echo "\tmost likely /etc/rpm/macros has /sbin/new-kernel-pkg declared in _netsharedpath."
773 /bin/echo "\tPlease remove /sbin/new-kernel-pkg from _netsharedpath and reintall mkinitrd."
778 # customize root's prompt
779 /bin/cat << PROFILE > /root/.profile
780 export PS1="[$lxc] \\w # "
786 # add a "build" user to the system
787 builduser=\$(grep "^build:" /etc/passwd | wc -l)
788 if [ \$builduser -eq 0 ] ; then
789 groupadd -o -g \$gid build;
790 useradd -o -c 'Automated Build' -u \$uid -g \$gid -n -M -s /bin/bash build;
793 # Allow build user to build certain RPMs as root
794 if [ -f /etc/sudoers ] ; then
795 buildsudo=\$(grep "^build.*ALL=(ALL).*NOPASSWD:.*ALL" /etc/sudoers | wc -l)
796 if [ \$buildsudo -eq 0 ] ; then
797 echo "build ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
799 sed -i 's,^Defaults.*requiretty,#Defaults requiretty,' /etc/sudoers
806 function post_install_myplc () {
812 personality=$1; shift
814 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
815 cat << EOF | chroot $rootfs_path /bin/bash -x
817 # create /etc/sysconfig/network if missing
818 [ -f /etc/sysconfig/network ] || /bin/echo NETWORKING=yes > /etc/sysconfig/network
820 # create symlink for /dev/fd
821 [ ! -e "/dev/fd" ] && /bin/ln -s /proc/self/fd /dev/fd
823 # turn off regular crond, as plc invokes plc_crond
824 /sbin/chkconfig crond off
826 # take care of loginuid in /etc/pam.d
827 /bin/sed -i "s,#*\(.*loginuid.*\),#\1," /etc/pam.d/*
829 # customize root's prompt
830 /bin/cat << PROFILE > /root/.profile
831 export PS1="[$lxc] \\w # "
837 function lxc_start() {
841 #trap failure ERR INT
845 virsh -c lxc:/// start $lxc
850 function wait_for_ssh () {
853 #trap failure ERR INT
857 echo $IP is up, waiting for ssh...
859 #wait max 5 min for sshd to start
861 stop_time=$(($(date +%s) + 300))
862 current_time=$(date +%s)
865 while [ "$current_time" -lt "$stop_time" ] ; do
866 echo "$counter-th attempt to reach sshd in container $lxc ..."
867 ssh -o "StrictHostKeyChecking no" $IP 'uname -i' && { ssh_up=true; echo "SSHD in container $lxc is UP"; break ; } || :
869 current_time=$(($current_time + 10))
870 counter=$(($counter+1))
873 # Thierry: this is fatal, let's just exit with a failure here
874 [ -z $ssh_up ] && { echo "SSHD in container $lxc is not running" ; exit 1 ; }
879 function failure () {
880 echo "$COMMAND : Bailing out"
886 echo "Usage: $COMMAND_LBUILD [options] lxc-name"
887 echo "Usage: $COMMAND_LTEST [options] lxc-name"
889 echo " This command creates a fresh lxc instance, for building, or running a test myplc"
890 echo "Supported options"
891 echo " -f fcdistro - for creating the root filesystem - defaults to $DEFAULT_FCDISTRO"
892 echo " -d pldistro - defaults to $DEFAULT_PLDISTRO"
893 echo " -p personality - defaults to $DEFAULT_PERSONALITY"
894 echo " -n hostname - the hostname to use in container - required with $COMMAND_LTEST"
895 echo " -r repo-url - used to populate yum.repos.d - required with $COMMAND_LTEST"
896 echo " -P pkgs_file - defines the set of extra pacakges"
897 echo " by default we use vtest.pkgs or devel.pkgs according to $COMMAND"
898 echo " -v be verbose"
906 #trap failure ERR INT
908 if [ "$(id -u)" != "0" ]; then
909 echo "This script should be run as 'root'"
922 echo 'build mode=' $BUILD_MODE 'test mode=' $TEST_MODE
924 # the set of preinstalled packages - depends on vbuild or vtest
925 if [ -n "$BUILD_MODE" ] ; then
926 PREINSTALLED=devel.pkgs
928 PREINSTALLED=vtest.pkgs
930 while getopts "f:d:p:n:r:P:v" opt ; do
932 f) fcdistro=$OPTARG;;
933 d) pldistro=$OPTARG;;
934 p) personality=$OPTARG;;
935 n) GUEST_HOSTNAME=$OPTARG;;
936 r) REPO_URL=$OPTARG;;
937 P) PREINSTALLED=$OPTARG;;
938 v) VERBOSE=true; set -x;;
943 shift $(($OPTIND - 1))
945 # parse fixed arguments
946 [[ -z "$@" ]] && usage
949 # check we've exhausted the arguments
950 [[ -n "$@" ]] && usage
952 [ -z "$fcdistro" ] && fcdistro=$DEFAULT_FCDISTRO
953 [ -z "$pldistro" ] && pldistro=$DEFAULT_PLDISTRO
954 [ -z "$personality" ] && personality=$DEFAULT_PERSONALITY
956 if [ -n "$BUILD_MODE" ] ; then
957 [ -z "$GUEST_HOSTNAME" ] && GUEST_HOSTNAME=$lxc
959 [[ -z "$GUEST_HOSTNAME" ]] && usage
960 # use -r none to get rid of this warning
961 if [ "$REPO_URL" == "none" ] ; then
963 elif [ -z "$REPO_URL" ] ; then
964 echo "WARNING -- setting up a yum repo is recommended"
969 release=$(echo $fcdistro | cut -df -f2)
971 if [ "$personality" == "linux32" ]; then
974 elif [ "$personality" == "linux64" ]; then
978 echo "Unknown personality: $personality"
981 if [ -n "$BUILD_MODE" ] ; then
983 # Bridge IP affectation
984 byte=$(random_private_byte)
985 IP=${PRIVATE_PREFIX}$byte
986 NETMASK=$(masklen_to_netmask $PRIVATE_MASKLEN)
987 GATEWAY=$PRIVATE_GATEWAY
990 [[ -z "GUEST_HOSTNAME" ]] && usage
992 create_bridge_if_needed
994 IP=$(gethostbyname $GUEST_HOSTNAME)
995 # use same NETMASK as bridge interface br0
996 MASKLEN=$(ip addr show $PUBLIC_BRIDGE | grep -v inet6 | grep inet | awk '{print $2;}' | cut -d/ -f2)
997 NETMASK=$(masklen_to_netmask $MASKLEN)
998 GATEWAY=$(ip route show | grep default | awk '{print $3}')
999 VIF_HOST="i$(echo $GUEST_HOSTNAME | cut -d. -f1)"
1002 echo "the IP address of container $lxc is $IP, host virtual interface is $VIF_HOST"
1005 [ ! -d $path ] && mkdir $path
1006 rootfs_path=$path/$lxc/rootfs
1007 config_path=$path/$lxc
1008 cache_base=/var/cache/lxc/fedora/$arch
1009 cache=$cache_base/$release
1012 # check whether the rootfs directory is created to know if the container exists
1013 # bacause /var/lib/lxc/$lxc is already created while putting $lxc.timestamp
1014 [ -d $rootfs_path ] && \
1015 { echo "container $lxc already exists in filesystem - exiting" ; exit 1 ; }
1016 virsh -c lxc:/// domuuid $lxc >& /dev/null && \
1017 { echo "container $lxc already exists in libvirt - exiting" ; exit 1 ; }
1019 setup_lxc $lxc $fcdistro $pldistro $personality
1021 devel_or_vtest_tools $lxc $fcdistro $pldistro $personality
1023 post_install $lxc $personality