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_PREFIX="192.168.122."
25 PRIVATE_GATEWAY="192.168.122.1"
26 # beware that changing this would break the logic of random_private_byte...
29 # we just try randomly in that range until a free IP address shows up
35 # the network interface name as seen from the container
38 ##############################
39 ## stolen from tests/system/template-qemu/qemu-bridge-init
40 # use /proc/net/dev instead of a hard-wired list
41 function gather_interfaces () {
43 for line in file("/proc/net/dev"):
44 if ':' not in line: continue
45 ifname=line.replace(" ","").split(":")[0]
46 if ifname.find("lo")==0: continue
47 if ifname.find("br")==0: continue
48 if ifname.find("virbr")==0: continue
49 if ifname.find("tap")==0: continue
54 function discover_interface () {
55 for ifname in $(gather_interfaces); do
56 ip link show $ifname | grep -qi 'state UP' && { echo $ifname; return; }
58 # still not found ? that's bad
61 ########## check for a free IP
62 function ip_is_busy () {
64 ping -c 1 -W 1 $target >& /dev/null
67 function random_private_byte () {
68 for attempt in $(seq $PRIVATE_ATTEMPTS); do
69 byte=$(($RANDOM % 256))
70 if [ "$byte" == 0 -o "$byte" == 1 ] ; then continue; fi
71 ip=${PRIVATE_PREFIX}${byte}
72 ip_is_busy $ip || { echo $byte; return; }
74 echo "Cannot seem to find a free IP address in range ${PRIVATE_PREFIX}.xx/24 after $PRIVATE_ATTEMPTS attempts - exiting"
78 ########## networking -- ctd
79 function gethostbyname () {
81 python -c "import socket; print socket.gethostbyname('"$hostname"')" 2> /dev/null
84 # e.g. 21 -> 255.255.248.0
85 function masklen_to_netmask () {
90 if not (masklen>=1 and masklen<=32):
91 print "Wrong masklen",masklen
99 result.append(masklen)
101 print ".".join([ str(256-2**(8-i)) for i in result ])
106 #################### bridge initialization
107 function create_bridge_if_needed() {
112 # already created ? - we're done
113 ip addr show $INTERFACE_BRIDGE >& /dev/null && {
114 echo "Bridge already set up - skipping create_bridge_if_needed"
118 # find out the physical interface to bridge onto
119 if_lan=$(discover_interface)
121 ip addr show $if_lan &>/dev/null || {
122 echo "Cannot use interface $if_lan - exiting"
126 #################### bride initialization
127 check_yum_installed bridge-utils
129 echo "========== $COMMAND: entering create_bridge - beg"
134 echo "========== $COMMAND: entering create_bridge - end"
136 # disable netfilter calls for bridge interface (they cause panick on 2.6.35 anyway)
138 # another option would be to accept the all forward packages for
139 # bridged interface like: -A FORWARD -m physdev --physdev-is-bridged -j ACCEPT
140 sysctl net.bridge.bridge-nf-call-iptables=0
141 sysctl net.bridge.bridge-nf-call-ip6tables=0
142 sysctl net.bridge.bridge-nf-call-arptables=0
145 #Getting host IP/masklen
146 address=$(ip addr show $if_lan | grep -v inet6 | grep inet | head --lines=1 | awk '{print $2;}')
147 [ -z "$address" ] && { echo "ERROR: Could not determine IP address for $if_lan" ; exit 1 ; }
149 broadcast=$(ip addr show $if_lan | grep -v inet6 | grep inet | head --lines=1 | awk '{print $4;}')
150 [ -z "$broadcast" ] && echo "WARNING: Could not determine broadcast address for $if_lan"
152 gateway=$(ip route show | grep default | awk '{print $3;}')
153 [ -z "$gateway" ] && echo "WARNING: Could not determine gateway IP"
156 # creating the bridge
157 echo "Creating bridge INTERFACE_BRIDGE=$INTERFACE_BRIDGE"
158 brctl addbr $INTERFACE_BRIDGE
159 brctl addif $INTERFACE_BRIDGE $if_lan
160 echo "Activating promiscuous mode if_lan=$if_lan"
161 ip link set $if_lan up promisc on
163 # rely on dhcp to re assign IP..
164 echo "Starting dhclient on $INTERFACE_BRIDGE"
165 dhclient $INTERFACE_BRIDGE
168 #Reconfigure the routing table
169 echo "Configuring gateway=$gateway"
170 ip route add default via $gateway dev $INTERFACE_BRIDGE
171 ip route del default via $gateway dev $if_lan
172 # at this point we have an extra route like e.g.
174 #default via 138.96.112.250 dev br0
175 #138.96.112.0/21 dev em1 proto kernel scope link src 138.96.112.57
176 #138.96.112.0/21 dev br0 proto kernel scope link src 138.96.112.57
177 #192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1
178 route_dest=$(ip route show | grep -v default | grep "dev $INTERFACE_BRIDGE" | awk '{print $1;}')
179 ip route del $route_dest dev $if_lan
181 echo "========== $COMMAND: exiting create_bridge - beg"
184 echo "========== $COMMAND: exiting create_bridge - end"
193 ##############################
194 function check_yum_installed () {
196 rpm -q $package >& /dev/null || yum -y install $package
199 function check_yumgroup_installed () {
201 yum grouplist "$group" | grep -q Installed || { yum -y groupinstall "$group" ; }
204 ##############################
206 function configure_fedora() {
208 # disable selinux in fedora
209 mkdir -p $rootfs_path/selinux
210 echo 0 > $rootfs_path/selinux/enforce
212 # configure the network
214 cat <<EOF > ${rootfs_path}/etc/sysconfig/network-scripts/ifcfg-$VIF_GUEST
230 cat <<EOF > ${rootfs_path}/etc/hostname
235 cat <<EOF > ${rootfs_path}/etc/sysconfig/network
240 cat <<EOF > $rootfs_path/etc/hosts
241 127.0.0.1 localhost $HOSTNAME
246 dev_path="${rootfs_path}/dev"
249 mknod -m 666 ${dev_path}/null c 1 3
250 mknod -m 666 ${dev_path}/zero c 1 5
251 mknod -m 666 ${dev_path}/random c 1 8
252 mknod -m 666 ${dev_path}/urandom c 1 9
253 mkdir -m 755 ${dev_path}/pts
254 mkdir -m 1777 ${dev_path}/shm
255 mknod -m 666 ${dev_path}/tty c 5 0
256 mknod -m 666 ${dev_path}/tty0 c 4 0
257 mknod -m 666 ${dev_path}/tty1 c 4 1
258 mknod -m 666 ${dev_path}/tty2 c 4 2
259 mknod -m 666 ${dev_path}/tty3 c 4 3
260 mknod -m 666 ${dev_path}/tty4 c 4 4
261 mknod -m 600 ${dev_path}/console c 5 1
262 mknod -m 666 ${dev_path}/full c 1 7
263 mknod -m 600 ${dev_path}/initctl p
264 mknod -m 666 ${dev_path}/ptmx c 5 2
266 #echo "setting root passwd to $root_password"
267 #echo "root:$root_password" | chroot $rootfs_path chpasswd
272 function configure_fedora_init() {
274 sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.sysinit
275 sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.d/rc.sysinit
276 # don't mount devpts, for pete's sake
277 sed -i 's/^.*dev.pts.*$/#\0/' ${rootfs_path}/etc/rc.sysinit
278 sed -i 's/^.*dev.pts.*$/#\0/' ${rootfs_path}/etc/rc.d/rc.sysinit
279 chroot ${rootfs_path} /sbin/chkconfig udev-post off
280 chroot ${rootfs_path} /sbin/chkconfig network on
283 # this code of course is for guests that do run on systemd
284 function configure_fedora_systemd() {
285 # so ignore if we can't find /etc/systemd at all
286 [ -d ${rootfs_path}/etc/systemd ] || return 0
287 # otherwise let's proceed
288 ln -sf /lib/systemd/system/multi-user.target ${rootfs_path}/etc/systemd/system/default.target
289 touch ${rootfs_path}/etc/fstab
290 ln -sf /dev/null ${rootfs_path}/etc/systemd/system/udev.service
292 # this was intended for f16 initially, in order to enable getty that otherwise would not start
293 # having a getty running is helpful only if ssh won't start though, and we see a correlation between
294 # VM's that refuse to lxc-stop and VM's that run crazy getty's
295 # so, turning getty off for now instead
296 # #dependency on a device unit fails it specially that we disabled udev
297 # sed -i 's/After=dev-%i.device/After=/' ${rootfs_path}/lib/systemd/system/getty\@.service
298 ln -sf /dev/null ${rootfs_path}/etc/systemd/system/"getty@.service"
299 rm -f ${rootfs_path}/etc/systemd/system/getty.target.wants/*service || :
300 # can't seem to handle this one with systemctl
301 chroot ${rootfs_path} /sbin/chkconfig network on
304 function download_fedora() {
306 # check the mini fedora was not already downloaded
307 INSTALL_ROOT=$cache/partial
310 # download a mini fedora into a cache
311 echo "Downloading fedora minimal ..."
313 mkdir -p $INSTALL_ROOT
314 if [ $? -ne 0 ]; then
315 echo "Failed to create '$INSTALL_ROOT' directory"
319 mkdir -p $INSTALL_ROOT/etc/yum.repos.d
320 mkdir -p $INSTALL_ROOT/dev
321 mknod -m 0444 $INSTALL_ROOT/dev/random c 1 8
322 mknod -m 0444 $INSTALL_ROOT/dev/urandom c 1 9
324 # copy yum config and repo files
325 cp /etc/yum.conf $INSTALL_ROOT/etc/
326 cp /etc/yum.repos.d/fedora* $INSTALL_ROOT/etc/yum.repos.d/
328 # append fedora repo files with desired $release and $basearch
329 for f in $INSTALL_ROOT/etc/yum.repos.d/*
331 sed -i "s/\$basearch/$arch/g; s/\$releasever/$release/g;" $f
334 MIRROR_URL=http://mirror.onelab.eu/fedora/releases/$release/Everything/$arch/os
335 RELEASE_URL1="$MIRROR_URL/Packages/fedora-release-$release-1.noarch.rpm"
336 # with fedora18 the rpms are scattered by first name
337 RELEASE_URL2="$MIRROR_URL/Packages/f/fedora-release-$release-1.noarch.rpm"
338 RELEASE_TARGET=$INSTALL_ROOT/fedora-release-$release.noarch.rpm
340 for attempt in $RELEASE_URL1 $RELEASE_URL2; do
341 if curl -f $attempt -o $RELEASE_TARGET ; then
342 echo "Retrieved $attempt"
346 echo "Failed attempt $attempt"
349 [ -n "$found" ] || { echo "Could not retrieve fedora-release rpm - exiting" ; exit 1; }
351 mkdir -p $INSTALL_ROOT/var/lib/rpm
352 rpm --root $INSTALL_ROOT --initdb
353 # when installing f12 this apparently is already present, so ignore result
354 rpm --root $INSTALL_ROOT -ivh $INSTALL_ROOT/fedora-release-$release.noarch.rpm || :
355 # however f12 root images won't get created on a f18 host
356 # (the issue here is the same as the one we ran into when dealing with a vs-box)
357 # in a nutshell, in f12 the glibc-common and filesystem rpms have an apparent conflict
358 # >>> file /usr/lib/locale from install of glibc-common-2.11.2-3.x86_64 conflicts
359 # with file from package filesystem-2.4.30-2.fc12.x86_64
360 # in fact this was - of course - allowed by f12's rpm but later on a fix was made
361 # http://rpm.org/gitweb?p=rpm.git;a=commitdiff;h=cf1095648194104a81a58abead05974a5bfa3b9a
362 # So ideally if we want to be able to build f12 images from f18 we need an rpm that has
363 # this patch undone, like we have in place on our f14 boxes (our f14 boxes need a f18-like rpm)
365 YUM="yum --installroot=$INSTALL_ROOT --nogpgcheck -y"
366 PKG_LIST="yum initscripts passwd rsyslog vim-minimal dhclient chkconfig rootfiles policycoreutils openssh-server openssh-clients"
367 echo "$YUM install $PKG_LIST"
368 $YUM install $PKG_LIST
370 if [ $? -ne 0 ]; then
371 echo "Failed to download the rootfs, aborting."
375 mv "$INSTALL_ROOT" "$cache/rootfs"
376 echo "Download complete."
382 function copy_fedora() {
384 # make a local copy of the minifedora
385 echo -n "Copying rootfs to $rootfs_path ..."
386 mkdir -p $rootfs_path
387 rsync -a $cache/rootfs/ $rootfs_path/
392 function update_fedora() {
394 YUM="yum --installroot $cache/rootfs -y --nogpgcheck"
399 function install_fedora() {
402 mkdir -p /var/lock/subsys/
405 if [ $? -ne 0 ]; then
406 echo "Cache repository is busy."
410 echo "Checking cache download in $cache/rootfs ... "
411 if [ ! -e "$cache/rootfs" ]; then
413 if [ $? -ne 0 ]; then
414 echo "Failed to download 'fedora base'"
418 echo "Cache found. Updating..."
420 if [ $? -ne 0 ]; then
421 echo "Failed to update 'fedora base', continuing with last known good cache"
423 echo "Update finished"
427 echo "Copy $cache/rootfs to $rootfs_path ... "
429 if [ $? -ne 0 ]; then
430 echo "Failed to copy rootfs"
436 ) 200>/var/lock/subsys/lxc
442 # overwrite lxc's internal yum config
443 function configure_yum_in_lxc () {
452 echo "Initializing yum.repos.d in $lxc"
453 rm -f $rootfs_path/etc/yum.repos.d/*
455 cat > $rootfs_path/etc/yum.repos.d/building.repo <<EOF
457 name=Fedora $release - $arch
458 baseurl=http://mirror.onelab.eu/fedora/releases/$release/Everything/$arch/os/
462 gpgkey=http://mirror.onelab.eu/keys/RPM-GPG-KEY-fedora-$release-primary
465 name=Fedora $release - $arch - Updates
466 baseurl=http://mirror.onelab.eu/fedora/updates/$release/$arch/
470 gpgkey=http://mirror.onelab.eu/keys/RPM-GPG-KEY-fedora-$release-primary
473 # for using vtest-init-lxc.sh as a general-purpose lxc creation wrapper
474 # just mention 'none' as the repo url
475 if [ -n "$REPO_URL" ] ; then
476 if [ ! -d $rootfs_path/etc/yum.repos.d ] ; then
477 echo "WARNING : cannot create myplc repo"
479 # exclude kernel from fedora repos
480 yumexclude=$(pl_plcyumexclude $fcdistro $pldistro $DIRNAME)
481 for repo in $rootfs_path/etc/yum.repos.d/* ; do
482 [ -f $repo ] && yumconf_exclude $repo "exclude=$yumexclude"
484 # the build repo is not signed at this stage
485 cat > $rootfs_path/etc/yum.repos.d/myplc.repo <<EOF
496 # return yum or debootstrap
497 function package_method () {
500 f[0-9]*|centos[0-9]*|sl[0-9]*) echo yum ;;
501 squeeze|wheezy|oneiric|precise|quantal|raring|saucy) echo debootstrap ;;
502 *) echo Unknown distro $fcdistro ;;
506 # return arch from debian distro and personality
507 function canonical_arch () {
508 personality=$1; shift
510 case $(package_method $fcdistro) in
512 case $personality in *32) echo i386 ;; *64) echo x86_64 ;; *) echo Unknown-arch-1 ;; esac ;;
514 case $personality in *32) echo i386 ;; *64) echo amd64 ;; *) echo Unknown-arch-2 ;; esac ;;
516 echo Unknown-arch-3 ;;
520 # the new test framework creates /timestamp in /vservers/<name> *before* populating it
521 function almost_empty () {
523 # non existing is fine
524 [ ! -d $dir ] && return 0;
525 # need to have at most one file
526 count=$(cd $dir; ls | wc -l); [ $count -le 1 ];
529 function setup_lxc() {
533 #trap failure ERR INT
538 personality=$1; shift
540 # create lxc container
542 pkg_method=$(package_method $fcdistro)
545 install_fedora || { echo "failed to install fedora"; exit 1 ; }
546 configure_fedora || { echo "failed to configure fedora for a container"; exit 1 ; }
547 if [ "$(echo $fcdistro | cut -d"f" -f2)" -le "14" ]; then
548 configure_fedora_init
550 configure_fedora_systemd
554 echo "$COMMAND: no support for debootstrap-based systems - yet"
558 echo "$COMMAND:: unknown package_method - exiting"
563 # Enable cgroup -- xxx -- is this really useful ?
564 mkdir $rootfs_path/cgroup
567 cp /etc/resolv.conf $rootfs_path/etc/resolv.conf
568 # and /etc/hosts for at least localhost
569 [ -f $rootfs_path/etc/hosts ] || echo "127.0.0.1 localhost localhost.localdomain" > $rootfs_path/etc/hosts
571 # grant ssh access from host to guest
572 mkdir $rootfs_path/root/.ssh
573 cat /root/.ssh/id_rsa.pub >> $rootfs_path/root/.ssh/authorized_keys
575 # copy libvirt xml template
577 cat > $config_path/$tmpl_name<<EOF
580 <memory>524288</memory>
582 <type arch='$arch2'>exe</type>
583 <init>/sbin/init</init>
589 <clock offset='utc'/>
590 <on_poweroff>destroy</on_poweroff>
591 <on_reboot>restart</on_reboot>
592 <on_crash>destroy</on_crash>
594 <emulator>/usr/libexec/libvirt_lxc</emulator>
595 <filesystem type='mount'>
596 <source dir='$rootfs_path'/>
599 <interface type="bridge">
600 <source bridge="$INTERFACE_BRIDGE"/>
601 <target dev='$VIF_HOST'/>
603 <console type='pty' />
606 <name>host-bridge</name>
607 <forward mode="bridge"/>
608 <bridge name="$INTERFACE_BRIDGE"/>
613 # define lxc container for libvirt
614 virsh -c lxc:// define $config_path/$tmpl_name
617 chroot $rootfs_path /bin/rpm --rebuilddb
619 configure_yum_in_lxc $lxc $fcdistro $pldistro
624 function devel_or_vtest_tools () {
633 personality=$1; shift
635 pkg_method=$(package_method $fcdistro)
637 pkgsfile=$(pl_locateDistroFile $DIRNAME $pldistro $PREINSTALLED)
639 ### install individual packages, then groups
640 # get target arch - use uname -i here (we want either x86_64 or i386)
642 lxc_arch=$(chroot $rootfs_path /bin/uname -i)
643 # on debian systems we get arch through the 'arch' command
644 [ "$lxc_arch" = "unknown" ] && lxc_arch=$(chroot $rootfs_path /bin/arch)
646 packages=$(pl_getPackages -a $lxc_arch $fcdistro $pldistro $pkgsfile)
647 groups=$(pl_getGroups -a $lxc_arch $fcdistro $pldistro $pkgsfile)
649 case "$pkg_method" in
651 [ -n "$packages" ] && chroot $rootfs_path /usr/bin/yum -y install $packages
652 for group_plus in $groups; do
653 group=$(echo $group_plus | sed -e "s,+++, ,g")
654 chroot $rootfs_path /usr/bin/yum -y groupinstall "$group"
656 # store current rpm list in /init-lxc.rpms in case we need to check the contents
657 chroot $rootfs_path /bin/rpm -aq > $rootfs_path/init-lxc.rpms
660 chroot $rootfs_path /usr/bin/apt-get update
661 for package in $packages ; do
662 chroot $rootfs_path /usr/bin/apt-get install -y $package
664 ### xxx todo install groups with apt..
667 echo "unknown pkg_method $pkg_method"
674 function post_install () {
676 personality=$1; shift
677 if [ -n "$BUILD_MODE" ] ; then
678 post_install_build $lxc $personality
681 post_install_myplc $lxc $personality
685 # setup localtime from the host
686 cp /etc/localtime $rootfs_path/etc/localtime
689 function post_install_build () {
696 personality=$1; shift
698 ### From myplc-devel-native.spec
699 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
700 cat << EOF | chroot $rootfs_path /bin/bash -x
701 # set up /dev/loop* in lxc
702 for i in \$(seq 0 255) ; do
703 /bin/mknod -m 640 /dev/loop\$i b 7 \$i
706 # create symlink for /dev/fd
707 [ ! -e "/dev/fd" ] && /bin/ln -s /proc/self/fd /dev/fd
709 # modify /etc/rpm/macros to not use /sbin/new-kernel-pkg
710 /bin/sed -i 's,/sbin/new-kernel-pkg:,,' /etc/rpm/macros
711 if [ -h "/sbin/new-kernel-pkg" ] ; then
712 filename=\$(/bin/readlink -f /sbin/new-kernel-pkg)
713 if [ "\$filename" == "/sbin/true" ] ; then
714 /bin/echo "WARNING: /sbin/new-kernel-pkg symlinked to /sbin/true"
715 /bin/echo "\tmost likely /etc/rpm/macros has /sbin/new-kernel-pkg declared in _netsharedpath."
716 /bin/echo "\tPlease remove /sbin/new-kernel-pkg from _netsharedpath and reintall mkinitrd."
721 # customize root's prompt
722 /bin/cat << PROFILE > /root/.profile
723 export PS1="[$lxc] \\w # "
729 # add a "build" user to the system
730 builduser=\$(grep "^build:" /etc/passwd | wc -l)
731 if [ \$builduser -eq 0 ] ; then
732 groupadd -o -g \$gid build;
733 useradd -o -c 'Automated Build' -u \$uid -g \$gid -n -M -s /bin/bash build;
736 # Allow build user to build certain RPMs as root
737 if [ -f /etc/sudoers ] ; then
738 buildsudo=\$(grep "^build.*ALL=(ALL).*NOPASSWD:.*ALL" /etc/sudoers | wc -l)
739 if [ \$buildsudo -eq 0 ] ; then
740 echo "build ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
742 sed -i 's,^Defaults.*requiretty,#Defaults requiretty,' /etc/sudoers
749 function post_install_myplc () {
755 personality=$1; shift
757 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
758 cat << EOF | chroot $rootfs_path /bin/bash -x
760 # create /etc/sysconfig/network if missing
761 [ -f /etc/sysconfig/network ] || /bin/echo NETWORKING=yes > /etc/sysconfig/network
763 # create symlink for /dev/fd
764 [ ! -e "/dev/fd" ] && /bin/ln -s /proc/self/fd /dev/fd
766 # turn off regular crond, as plc invokes plc_crond
767 /sbin/chkconfig crond off
769 # take care of loginuid in /etc/pam.d
770 /bin/sed -i "s,#*\(.*loginuid.*\),#\1," /etc/pam.d/*
772 # customize root's prompt
773 /bin/cat << PROFILE > /root/.profile
774 export PS1="[$lxc] \\w # "
780 function start_lxc() {
784 #trap failure ERR INT
788 virsh -c lxc:// start $lxc
793 function wait_for_ssh () {
796 #trap failure ERR INT
800 echo $IP is up, waiting for ssh...
802 #wait max 5 min for sshd to start
804 stop_time=$(($(date +%s) + 300))
805 current_time=$(date +%s)
808 while [ "$current_time" -lt "$stop_time" ] ; do
809 echo "$counter-th attempt to reach sshd in container $lxc ..."
810 ssh -o "StrictHostKeyChecking no" $IP 'uname -i' && { ssh_up=true; echo "SSHD in container $lxc is UP"; break ; } || :
812 current_time=$(($current_time + 10))
813 counter=$(($counter+1))
816 # Thierry: this is fatal, let's just exit with a failure here
817 [ -z $ssh_up ] && { echo "SSHD in container $lxc is not running" ; exit 1 ; }
822 function failure () {
823 echo "$COMMAND : Bailing out"
829 echo "Usage: $COMMAND_LBUILD [options] lxc-name"
830 echo "Usage: $COMMAND_LTEST [options] lxc-name"
832 echo " This command creates a fresh lxc instance, for building, or running a test myplc"
833 echo "Supported options"
834 echo " -f fcdistro - for creating the root filesystem - defaults to $DEFAULT_FCDISTRO"
835 echo " -d pldistro - defaults to $DEFAULT_PLDISTRO"
836 echo " -p personality - defaults to $DEFAULT_PERSONALITY"
837 echo " -n hostname - the hostname to use in container - required with $COMMAND_LTEST"
838 echo " -r repo-url - used to populate yum.repos.d - required with $COMMAND_LTEST"
839 echo " -P pkgs_file - defines the set of extra pacakges"
840 echo " by default we use vtest.pkgs or devel.pkgs according to $COMMAND"
841 echo " -v be verbose"
849 #trap failure ERR INT
851 if [ "$(id -u)" != "0" ]; then
852 echo "This script should be run as 'root'"
865 echo 'build mode=' $BUILD_MODE 'test mode=' $TEST_MODE
867 # the set of preinstalled packages - depends on vbuild or vtest
868 if [ -n "$VBUILD_MODE" ] ; then
869 PREINSTALLED=devel.pkgs
871 PREINSTALLED=vtest.pkgs
873 while getopts "f:d:p:n:r:P:v" opt ; do
875 f) fcdistro=$OPTARG;;
876 d) pldistro=$OPTARG;;
877 p) personality=$OPTARG;;
878 n) HOSTNAME=$OPTARG;;
879 r) REPO_URL=$OPTARG;;
880 P) PREINSTALLED=$OPTARG;;
881 v) VERBOSE=true; set -x;;
886 shift $(($OPTIND - 1))
888 # parse fixed arguments
889 [[ -z "$@" ]] && usage
892 # check we've exhausted the arguments
893 [[ -n "$@" ]] && usage
895 [ -z "$fcdistro" ] && fcdistro=$DEFAULT_FCDISTRO
896 [ -z "$pldistro" ] && pldistro=$DEFAULT_PLDISTRO
897 [ -z "$personality" ] && personality=$DEFAULT_PERSONALITY
899 if [ -n "$BUILD_MODE" ] ; then
900 [ -z "$HOSTNAME" ] && HOSTNAME=$lxc
902 [[ -z "$HOSTNAME" ]] && usage
903 # use -r none to get rid of this warning
904 if [ "$REPO_URL" == "none" ] ; then
906 elif [ -z "$REPO_URL" ] ; then
907 echo "WARNING -- setting up a yum repo is recommended"
912 release=$(echo $fcdistro | cut -df -f2)
914 if [ "$personality" == "linux32" ]; then
917 elif [ "$personality" == "linux64" ]; then
921 echo "Unknown personality: $personality"
924 # need lxc installed before we can run lxc-ls
925 # need bridge installed
926 create_bridge_if_needed
928 if [ -n "$BUILD_MODE" ] ; then
930 # Bridge IP affectation
931 byte=$(random_private_byte)
932 IP=${PRIVATE_PREFIX}$byte
933 NETMASK=$(masklen_to_netmask $PRIVATE_MASKLEN)
934 GATEWAY=$PRIVATE_GATEWAY
937 [[ -z "HOSTNAME" ]] && usage
939 IP=$(gethostbyname $HOSTNAME)
940 # use same NETMASK as bridge interface br0
941 MASKLEN=$(ip addr show $INTERFACE_BRIDGE | grep -v inet6 | grep inet | awk '{print $2;}' | cut -d/ -f2)
942 NETMASK=$(masklen_to_netmask $MASKLEN)
943 GATEWAY=$(ip route show | grep default | awk '{print $3}')
944 VIF_HOST="i$(echo $HOSTNAME | cut -d. -f1)"
947 echo "the IP address of container $lxc is $IP, host virtual interface is $VIF_HOST"
950 [ ! -d $path ] && mkdir $path
951 rootfs_path=$path/$lxc/rootfs
952 config_path=$path/$lxc
953 cache_base=/var/cache/lxc/fedora/$arch
954 cache=$cache_base/$release
957 # check whether the rootfs directory is created to know if the container exists
958 # bacause /var/lib/lxc/$lxc is already created while putting $lxc.timestamp
959 [ -d $rootfs_path ] && \
960 { echo "container $lxc already exists in filesystem - exiting" ; exit 1 ; }
961 virsh --connect lxc:// domuuid $lxc >& /dev/null && \
962 { echo "container $lxc already exists in libvirt - exiting" ; exit 1 ; }
964 setup_lxc $lxc $fcdistro $pldistro $personality
966 devel_or_vtest_tools $lxc $fcdistro $pldistro $personality
968 post_install $lxc $personality