4 # close stdin, as with ubuntu and debian VMs this script tends to hang and wait for input ..
13 # pkgs parsing utilities + lbuild-bridge.sh
14 export PATH=$(dirname $0):$PATH
16 # old guests have e.g. mount in /bin but this is no longer part of
17 # the standard PATH in recent hosts after usrmove, so let's keep it simple
18 export PATH=$PATH:/bin:/sbin
22 # xxx fixme : we pass $lxc around in functions,
23 # but in fact then we use lxc_root which is a global..
24 # it works, but this really is poor practice
25 # we should have an lxc_root function instead
31 # XXX fixme : when creating a 32bits VM we need to call linux32 as appropriate...s
35 DEFAULT_PERSONALITY=linux64
42 # the network interface name as seen from the container
46 FEDORA_MIRROR_BASE="http://mirror.onelab.eu/fedora/"
47 FEDORA_MIRROR_KEYS="http://mirror.onelab.eu/keys/"
48 FEDORA_PREINSTALLED="dnf dnf-yum passwd rsyslog vim-minimal dhclient chkconfig rootfiles policycoreutils openssh-server openssh-clients"
49 DEBIAN_PREINSTALLED="openssh-server openssh-client"
51 ########## networking utilities
52 function gethostbyname () {
54 python -c "import socket; print socket.gethostbyname('"$hostname"')" 2> /dev/null
57 # e.g. 21 -> 255.255.248.0
58 function masklen_to_netmask () {
59 local masklen=$1; shift
63 if not (masklen>=1 and masklen<=32):
64 print "Wrong masklen",masklen
72 result.append(masklen)
74 print ".".join([ str(256-2**(8-i)) for i in result ])
79 ##############################
80 # return dnf or debootstrap
81 function package_method () {
82 local fcdistro=$1; shift
84 f[0-9]*|centos[0-9]*|sl[0-9]*)
86 wheezy|jessie|precise|trusty|utopic|vivid|wily|xenial)
89 echo Unknown distro $fcdistro ;;
93 # return arch from debian distro and personality
94 function canonical_arch () {
95 local personality=$1; shift
96 local fcdistro=$1; shift
97 case $(package_method $fcdistro) in
102 *) echo Unknown-arch-1 ;;
108 *) echo Unknown-arch-2 ;;
111 echo Unknown-arch-3 ;;
115 # the new test framework creates /timestamp in /vservers/<name> *before* populating it
116 function almost_empty () {
117 local dir="$1"; shift ;
118 # non existing is fine
119 [ ! -d $dir ] && return 0;
120 # need to have at most one file
121 local count=$(cd $dir; ls | wc -l)
125 ##############################
126 function fedora_install() {
131 local lxc_root=$(lxcroot $lxc)
133 local cache=/var/cache/lxc/fedora/$arch/${fedora_release}
137 flock --exclusive --timeout 60 200 || { echo "Cache repository is busy." ; return 1 ; }
139 if [ ! -e "$cache/rootfs" ]; then
140 echo "Getting cache download in $cache/rootfs ... "
141 fedora_download $cache || { echo "Failed to download 'fedora base'"; return 1; }
143 echo "Updating cache $cache/rootfs ..."
144 if ! dnf --installroot $cache/rootfs --releasever ${fedora_release} -y --nogpgcheck update ; then
145 echo "Failed to update 'fedora base', continuing with last known good cache"
147 echo "Update finished"
151 echo "Filling $lxc_root from $cache/rootfs ... "
152 rsync -a $cache/rootfs/ $lxc_root/
161 function fedora_download() {
164 local cache=$1; shift
166 # check the mini fedora was not already downloaded
167 INSTALL_ROOT=$cache/partial
170 # download a mini fedora into a cache
171 echo "Downloading fedora minimal ..."
173 mkdir -p $INSTALL_ROOT || { echo "Failed to create '$INSTALL_ROOT' directory" ; return 1; }
175 mkdir -p $INSTALL_ROOT/etc/yum.repos.d
176 mkdir -p $INSTALL_ROOT/dev
177 mknod -m 0444 $INSTALL_ROOT/dev/random c 1 8
178 mknod -m 0444 $INSTALL_ROOT/dev/urandom c 1 9
180 # copy yum config and repo files
181 cp /etc/yum.conf $INSTALL_ROOT/etc/
182 cp /etc/yum.repos.d/fedora* $INSTALL_ROOT/etc/yum.repos.d/
184 # append fedora repo files with desired ${fedora_release} and $basearch
185 for f in $INSTALL_ROOT/etc/yum.repos.d/* ; do
186 sed -i "s/\$basearch/$arch/g; s/\$releasever/${fedora_release}/g;" $f
189 MIRROR_URL=$FEDORA_MIRROR_BASE/releases/${fedora_release}/Everything/$arch/os
190 # since fedora18 the rpms are scattered by first name
191 # first try the second version of fedora-release first
194 for subindex in 3 2 1; do
195 RELEASE_URLS="$RELEASE_URLS $MIRROR_URL/Packages/f/fedora-release-${fedora_release}-1.noarch.rpm"
198 RELEASE_TARGET=$INSTALL_ROOT/fedora-release-${fedora_release}.noarch.rpm
201 for attempt in $RELEASE_URLS; do
202 if curl --silent --fail $attempt -o $RELEASE_TARGET; then
203 echo "Successfully Retrieved $attempt"
207 echo "Failed (not to worry about) with attempt $attempt"
210 [ -n "$found" ] || { echo "Could not retrieve fedora-release rpm - exiting" ; exit 1; }
212 mkdir -p $INSTALL_ROOT/var/lib/rpm
213 rpm --root $INSTALL_ROOT --initdb
214 # when installing f12 this apparently is already present, so ignore result
215 rpm --root $INSTALL_ROOT -ivh $INSTALL_ROOT/fedora-release-${fedora_release}.noarch.rpm || :
216 # however f12 root images won't get created on a f18 host
217 # (the issue here is the same as the one we ran into when dealing with a vs-box)
218 # in a nutshell, in f12 the glibc-common and filesystem rpms have an apparent conflict
219 # >>> file /usr/lib/locale from install of glibc-common-2.11.2-3.x86_64 conflicts
220 # with file from package filesystem-2.4.30-2.fc12.x86_64
221 # in fact this was - of course - allowed by f12's rpm but later on a fix was made
222 # http://rpm.org/gitweb?p=rpm.git;a=commitdiff;h=cf1095648194104a81a58abead05974a5bfa3b9a
223 # So ideally if we want to be able to build f12 images from f18 we need an rpm that has
224 # this patch undone, like we have in place on our f14 boxes (our f14 boxes need a f18-like rpm)
226 DNF="dnf --installroot=$INSTALL_ROOT --releasever=${fedora_release} --nogpgcheck -y"
227 echo "$DNF install $FEDORA_PREINSTALLED"
228 $DNF install $FEDORA_PREINSTALLED || { echo "Failed to download rootfs, aborting." ; return 1; }
230 mv "$INSTALL_ROOT" "$cache/rootfs"
231 echo "Download complete."
236 ##############################
237 function fedora_configure() {
243 local fcdistro=$1; shift
244 local lxc_root=$(lxcroot $lxc)
246 # disable selinux in fedora
247 mkdir -p $lxc_root/selinux
248 echo 0 > $lxc_root/selinux/enforce
250 # enable networking and set hostname
251 cat <<EOF > ${lxc_root}/etc/sysconfig/network
254 cat <<EOF > ${lxc_root}/etc/hostname
258 local dev_path="${lxc_root}/dev"
261 mknod -m 666 ${dev_path}/null c 1 3
262 mknod -m 666 ${dev_path}/zero c 1 5
263 mknod -m 666 ${dev_path}/random c 1 8
264 mknod -m 666 ${dev_path}/urandom c 1 9
265 mkdir -m 755 ${dev_path}/pts
266 mkdir -m 1777 ${dev_path}/shm
267 mknod -m 666 ${dev_path}/tty c 5 0
268 mknod -m 666 ${dev_path}/tty0 c 4 0
269 mknod -m 666 ${dev_path}/tty1 c 4 1
270 mknod -m 666 ${dev_path}/tty2 c 4 2
271 mknod -m 666 ${dev_path}/tty3 c 4 3
272 mknod -m 666 ${dev_path}/tty4 c 4 4
273 mknod -m 600 ${dev_path}/console c 5 1
274 mknod -m 666 ${dev_path}/full c 1 7
275 mknod -m 600 ${dev_path}/initctl p
276 mknod -m 666 ${dev_path}/ptmx c 5 2
278 fedora_configure_systemd $lxc
280 local guest_ifcfg=${lxc_root}/etc/sysconfig/network-scripts/ifcfg-$VIF_GUEST
281 mkdir -p $(dirname ${guest_ifcfg})
282 # starting with f29, we go for NetworkManager as older network-scripts
283 # is about to be deprecated
284 local nm_controlled=false
285 [[ $fcdistro == f29 ]] && nm_controlled=true
286 [[ $fcdistro == f3[0-9] ]] && nm_controlled=true
288 if [ -n "$NAT_MODE" ]; then
289 write_guest_ifcfg_natip $nm_controlled
291 write_guest_ifcfg_publicip $nm_controlled
294 [ -z "$IMAGE" ] && fedora_configure_yum $lxc $fcdistro $pldistro
299 # this code of course is for guests that do run on systemd
300 function fedora_configure_systemd() {
304 local lxc_root=$(lxcroot $lxc)
306 # so ignore if we can't find /etc/systemd at all
307 [ -d ${lxc_root}/etc/systemd ] || return 0
308 # otherwise let's proceed
309 ln -sf /lib/systemd/system/multi-user.target ${lxc_root}/etc/systemd/system/default.target
310 touch ${lxc_root}/etc/fstab
311 ln -sf /dev/null ${lxc_root}/etc/systemd/system/udev.service
312 # Thierry - Feb 2013 relying on getty is looking for trouble
313 # so, turning getty off for now instead
314 # sed -i 's/After=dev-%i.device/After=/' ${lxc_root}/lib/systemd/system/getty\@.service
315 ln -sf /dev/null ${lxc_root}/etc/systemd/system/"getty@.service"
316 rm -f ${lxc_root}/etc/systemd/system/getty.target.wants/*service || :
317 # can't seem to handle this one with systemctl
318 chroot ${lxc_root} $personality chkconfig network on
321 # overwrite container yum config
322 function fedora_configure_yum () {
328 local fcdistro=$1; shift
329 local pldistro=$1; shift
331 local lxc_root=$(lxcroot $lxc)
333 chroot ${lxc_root} $personality rpm --rebuilddb
335 echo "Initializing yum.repos.d in $lxc"
336 rm -f $lxc_root/etc/yum.repos.d/*
338 cat > $lxc_root/etc/yum.repos.d/building.repo <<EOF
340 name=Fedora \$releasever - \$basearch
341 baseurl=$FEDORA_MIRROR_BASE/releases/\$releasever/Everything/\$basearch/os/
345 gpgkey=$FEDORA_MIRROR_KEYS/RPM-GPG-KEY-fedora-${fedora_release}-primary
348 name=Fedora \$releasever - \$basearch - Updates
349 baseurl=$FEDORA_MIRROR_BASE/updates/\$releasever/\$basearch/
353 gpgkey=$FEDORA_MIRROR_KEYS/RPM-GPG-KEY-fedora-${fedora_release}-primary
356 # import fedora key so that gpgckeck does not whine or require stdin
357 # required since fedora24
358 rpm --root $lxc_root --import $FEDORA_MIRROR_KEYS/RPM-GPG-KEY-fedora-${fedora_release}-primary
360 # for using this script as a general-purpose lxc creation wrapper
361 # just mention 'none' as the repo url
362 if [ -n "$REPO_URL" ] ; then
363 if [ ! -d $lxc_root/etc/yum.repos.d ] ; then
364 echo "WARNING : cannot create myplc repo"
366 # exclude kernel from fedora repos
367 yumexclude=$(pl_plcyumexclude $fcdistro $pldistro $DIRNAME)
368 for repo in $lxc_root/etc/yum.repos.d/* ; do
369 [ -f $repo ] && yumconf_exclude $repo "exclude=$yumexclude"
371 # the build repo is not signed at this stage
372 cat > $lxc_root/etc/yum.repos.d/myplc.repo <<EOF
383 ##############################
384 # apparently ubuntu exposes a mirrors list by country at
385 # http://mirrors.ubuntu.com/mirrors.txt
386 # need to specify the right mirror for debian variants like ubuntu and the like
387 function debian_mirror () {
388 local fcdistro=$1; shift
391 echo http://ftp2.fr.debian.org/debian/ ;;
392 precise|trusty|utopic|vivid|wily|xenial)
393 echo http://www-ftp.lip6.fr/pub/linux/distributions/Ubuntu/archive/ ;;
394 *) echo unknown distro $fcdistro; exit 1;;
398 function debian_install () {
402 local lxc_root=$(lxcroot $lxc)
404 local arch=$(canonical_arch $personality $fcdistro)
405 local mirror=$(debian_mirror $fcdistro)
406 debootstrap --no-check-gpg --arch $arch $fcdistro $lxc_root $mirror
407 # just like with fedora we ensure a few packages get installed as well
409 #virsh -c lxc:/// lxc-enter-namespace $lxc /usr/bin/$personality /bin/bash -c "apt-get update"
410 #virsh -c lxc:/// lxc-enter-namespace $lxc /usr/bin/$personality /bin/bash -c "apt-get -y install $DEBIAN_PREINSTALLED"
411 chroot ${lxc_root} $personality apt-get update
412 chroot ${lxc_root} $personality apt-get -y install $DEBIAN_PREINSTALLED
414 cat <<EOF > ${lxc_root}/etc/hostname
420 function debian_configure () {
421 local guest_interfaces=${lxc_root}/etc/network/interfaces
422 ( [ -n "$NAT_MODE" ] && write_guest_interfaces_natip || write_guest_interfaces_publicip ) > $guest_interfaces
425 function write_guest_interfaces_natip () {
428 iface $VIF_GUEST inet dhcp
432 function write_guest_interfaces_publicip () {
435 iface $VIF_GUEST inet static
441 ##############################
442 function setup_lxc() {
446 #trap failure ERR INT
449 local fcdistro=$1; shift
450 local pldistro=$1; shift
451 local personality=$1; shift
453 local lxc_root=$(lxcroot $lxc)
455 # create lxc container
457 pkg_method=$(package_method $fcdistro)
460 if [ -z "$IMAGE" ]; then
461 fedora_install $lxc || { echo "failed to install fedora root image"; exit 1 ; }
462 # this appears to be safer; observed in Jan. 2016 on a f23 host and a f14 cached image
463 # we were getting this message when attempting the first chroot dnf install
464 # rpmdb: Program version 4.8 doesn't match environment version 5.3
465 chroot $(lxcroot $lxc) $personality rm -rf /var/lib/rpm/__db.00{0,1,2,3,4,5,6,7,8,9}
466 chroot $(lxcroot $lxc) $personality rpm --rebuilddb
468 fedora_configure $lxc $fcdistro || { echo "failed to configure fedora for a container"; exit 1 ; }
471 if [ -z "$IMAGE" ]; then
472 debian_install $lxc || { echo "failed to install debian/ubuntu root image"; exit 1 ; }
474 debian_configure || { echo "failed to configure debian/ubuntu for a container"; exit 1 ; }
477 echo "$COMMAND:: unknown package_method - exiting"
482 # Enable cgroup -- xxx -- is this really useful ?
483 [ -d $lxc_root/cgroup ] || mkdir $lxc_root/cgroup
485 ### set up resolv.conf from host
486 # ubuntu precise and on, /etc/resolv.conf is a symlink to ../run/resolvconf/resolv.conf
487 [ -h $lxc_root/etc/resolv.conf ] && rm -f $lxc_root/etc/resolv.conf
488 cp /etc/resolv.conf $lxc_root/etc/resolv.conf
489 ### and /etc/hosts for at least localhost
490 [ -f $lxc_root/etc/hosts ] || echo "127.0.0.1 localhost localhost.localdomain" > $lxc_root/etc/hosts
492 # grant ssh access from host to guest
493 mkdir -p $lxc_root/root/.ssh
494 cat /root/.ssh/id_rsa.pub >> $lxc_root/root/.ssh/authorized_keys
495 chmod 700 $lxc_root/root/.ssh
496 chmod 600 $lxc_root/root/.ssh/authorized_keys
498 # don't keep the input xml, this can be retrieved at all times with virsh dumpxml
499 local config_xml=/tmp/$lxc.xml
500 ( [ -n "$NAT_MODE" ] && write_lxc_xml_natip $lxc || write_lxc_xml_publicip $lxc ) > $config_xml
502 # define lxc container for libvirt
503 virsh -c lxc:/// define $config_xml
508 # this part does not belong in a domain any more
509 # but goes in a network object of its own existence
511 # <name>host-bridge</name>
512 # <forward mode="bridge"/>
513 # <bridge name="br0"/>
517 function write_lxc_xml_publicip () {
519 local lxc_root=$(lxcroot $lxc)
523 <memory>$MEMORY</memory>
525 <type arch='$arch2'>exe</type>
526 <init>/sbin/init</init>
532 <clock offset='utc'/>
533 <on_poweroff>destroy</on_poweroff>
534 <on_reboot>restart</on_reboot>
535 <on_crash>destroy</on_crash>
537 <emulator>/usr/libexec/libvirt_lxc</emulator>
538 <filesystem type='mount'>
539 <source dir='$lxc_root'/>
542 <interface type="bridge">
543 <source bridge="$PUBLIC_BRIDGE"/>
544 <target dev='$VIF_HOST'/>
546 <console type='pty' />
552 # grant build guests the ability to do mknods
553 function write_lxc_xml_natip () {
555 local lxc_root=$(lxcroot $lxc)
559 <memory>$MEMORY</memory>
561 <type arch='$arch2'>exe</type>
562 <init>/sbin/init</init>
566 <capabilities policy='default'>
571 <clock offset='utc'/>
572 <on_poweroff>destroy</on_poweroff>
573 <on_reboot>restart</on_reboot>
574 <on_crash>destroy</on_crash>
576 <emulator>/usr/libexec/libvirt_lxc</emulator>
577 <filesystem type='mount'>
578 <source dir='$lxc_root'/>
581 <interface type="network">
582 <source network="default"/>
584 <console type='pty' />
590 # this one is dhcp-based
591 function write_guest_ifcfg_natip () {
592 local nm_controlled=$1; shift
600 [ "$nm_controlled" == true ] || echo NM_CONTROLLED=no
603 # use fixed GUEST_IP as specified by GUEST_HOSTNAME
604 function write_guest_ifcfg_publicip () {
605 local nm_controlled=$1; shift
610 HOSTNAME=$GUEST_HOSTNAME
617 [ "$nm_controlled" == true ] || echo NM_CONTROLLED=no
620 function devel_or_test_tools () {
627 local fcdistro=$1; shift
628 local pldistro=$1; shift
629 local personality=$1; shift
631 local lxc_root=$(lxcroot $lxc)
633 local pkg_method=$(package_method $fcdistro)
635 local pkgsfile=$(pl_locateDistroFile $DIRNAME $pldistro $PREINSTALLED)
637 ### install individual packages, then groups
638 # get target arch - use uname -i here (we want either x86_64 or i386)
640 local lxc_arch=$(chroot ${lxc_root} $personality uname -i)
641 # on debian systems we get arch through the 'arch' command
642 [ "$lxc_arch" = "unknown" ] && lxc_arch=$(chroot ${lxc_root} $personality arch)
644 local packages=$(pl_getPackages -a $lxc_arch $fcdistro $pldistro $pkgsfile)
645 local groups=$(pl_getGroups -a $lxc_arch $fcdistro $pldistro $pkgsfile)
647 case "$pkg_method" in
649 # --allowerasing required starting with fedora24
652 chroot ${lxc_root} $personality dnf --version && has_dnf=true
653 if [ -n "$has_dnf" ]; then
654 echo "container has dnf - invoking with --allowerasing"
655 local pkg_installer="dnf -y install --allowerasing"
656 local grp_installer="dnf -y groupinstall --allowerasing"
658 echo "container has only dnf"
659 local pkg_installer="dnf -y install"
660 local grp_installer="dnf -y groupinstall"
662 [ -n "$packages" ] && chroot ${lxc_root} $personality $pkg_installer $packages
663 for group_plus in $groups; do
664 local group=$(echo $group_plus | sed -e "s,+++, ,g")
665 chroot ${lxc_root} $personality $grp_installer "$group"
667 # store current rpm list in /init-lxc.rpms in case we need to check the contents
668 chroot ${lxc_root} $personality rpm -aq > $lxc_root/init-lxc.rpms
672 if grep -iq ubuntu /vservers/$lxc/etc/lsb-release 2> /dev/null; then
673 # on ubuntu, at this point we end up with a single feed in /etc/apt/sources.list
674 # we need at least to add the 'universe' feed for python-rpm
675 ( cd /vservers/$lxc/etc/apt ; head -1 sources.list | sed -e s,main,universe, > sources.list.d/universe.list )
676 # also adding a link to updates sounds about right
677 ( cd /vservers/$lxc/etc/apt ; head -1 sources.list | sed -e 's, main,-updates main,' > sources.list.d/updates.list )
678 # tell apt about the changes
679 chroot /vservers/$lxc apt-get update
681 for package in $packages ; do
682 # container not started yet
683 #virsh -c lxc:/// lxc-enter-namespace $lxc /usr/bin/$personality /bin/bash -c "apt-get install -y $package" || :
684 chroot ${lxc_root} $personality apt-get install -y $package || :
686 ### xxx todo install groups with apt..
689 echo "unknown pkg_method $pkg_method"
696 function post_install () {
698 local personality=$1; shift
699 local lxc_root=$(lxcroot $lxc)
700 # setup localtime from the host
701 cp /etc/localtime $lxc_root/etc/localtime
702 sshd_disable_password_auth $lxc
704 [ -n "$NAT_MODE" ] && post_install_natip $lxc $personality || post_install_myplc $lxc $personality
705 # start the VM unless specified otherwise
706 if [ -n "$START_VM" ] ; then
707 echo Starting guest $lxc
708 virsh -c lxc:/// start $lxc
709 if [ -n "$NAT_MODE" ] ; then
712 wait_for_ssh $lxc $GUEST_IP
717 # just in case, let's stay on the safe side
718 function sshd_disable_password_auth () {
720 local lxc_root=$(lxcroot $lxc)
721 sed --in-place=.password -e 's,^#\?PasswordAuthentication.*,PasswordAuthentication no,' \
722 $lxc_root/etc/ssh/sshd_config
725 function post_install_natip () {
732 local personality=$1; shift
733 local lxc_root=$(lxcroot $lxc)
735 ### From myplc-devel-native.spec
736 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
737 cat << EOF | chroot ${lxc_root} $personality bash -x
739 # customize root's prompt
740 /bin/cat << PROFILE > /root/.profile
741 export PS1="[$lxc] \\w # "
748 function post_install_myplc () {
754 local personality=$1; shift
755 local lxc_root=$(lxcroot $lxc)
757 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
758 cat << EOF | chroot ${lxc_root} $personality bash -x
760 # create /etc/sysconfig/network if missing
761 [ -f /etc/sysconfig/network ] || /bin/echo NETWORKING=yes > /etc/sysconfig/network
763 # turn off regular crond, as plc invokes plc_crond
764 /sbin/chkconfig crond off
766 # customize root's prompt
767 /bin/cat << PROFILE > /root/.profile
768 export PS1="[$lxc] \\w # "
774 ########################################
775 # workaround for broken lxc-enter-namespace
776 # 1st version was relying on virsh net-dhcp-leases
777 # however this was too fragile, would not work for fedora14 containers
778 # WARNING: this code is duplicated in lbuild-nightly.sh
779 function guest_ipv4() {
782 local mac=$(virsh -c lxc:/// domiflist $lxc | egrep 'network|bridge' | awk '{print $5;}')
784 [ -z "$mac" ] && return 0
785 arp -en | grep "$mac" | awk '{print $1;}'
788 function wait_for_ssh () {
794 # if run in public_ip mode, we know the IP of the guest and it is specified here
795 [ -n "$1" ] && { guest_ip=$1; shift; }
797 #wait max 2 min for sshd to start
799 local current_time=$(date +%s)
800 local stop_time=$(($current_time + 120))
803 while [ "$current_time" -lt "$stop_time" ] ; do
804 echo "$counter-th attempt to reach sshd in container $lxc ..."
805 [ -z "$guest_ip" ] && guest_ip=$(guest_ipv4 $lxc)
806 [ -n "$guest_ip" ] && ssh -o "StrictHostKeyChecking no" $guest_ip 'uname -i' && {
807 success=true; echo "SSHD in container $lxc is UP on IP $guest_ip"; break ; } || :
808 counter=$(($counter+1))
810 current_time=$(date +%s)
813 # Thierry: this is fatal, let's just exit with a failure here
814 [ -z $success ] && { echo "SSHD in container $lxc could not be reached (guest_ip=$guest_ip)" ; exit 1 ; }
819 function failure () {
820 echo "$COMMAND : Bailing out"
826 echo "Usage: $COMMAND [options] lxc-name (aka build mode)"
827 echo "Usage: $COMMAND -n hostname [options] lxc-name (aka test mode)"
829 echo " This command creates a fresh lxc instance, for building, or running a test myplc"
830 echo "In its first form, spawned VM gets a private IP bridged with virbr0 over dhcp/nat"
831 echo "With the second form, spawned VM gets a public IP bridged on public bridge br0"
833 echo "Supported options"
834 echo " -n hostname - the hostname to use in container"
835 echo " -f fcdistro - for creating the root filesystem - defaults to $DEFAULT_FCDISTRO"
836 echo " -d pldistro - defaults to $DEFAULT_PLDISTRO - current support for fedoras debians ubuntus"
837 echo " -p personality - defaults to $DEFAULT_PERSONALITY"
838 echo " -r repo-url - used to populate yum.repos.d - required in test mode"
839 echo " -P pkgs_file - defines a set of extra packages to install in guest"
840 echo " by default we use devel.pkgs (build mode) or runtime.pkgs (test mode)"
841 echo " -i image - the location of the rootfs"
842 echo " -m memory - the amount of allocated memory in MB - defaults to $DEFAULT_MEMORY MB"
843 echo " -s do not start VM"
844 echo " -v be verbose"
852 #trap failure ERR INT
854 if [ "$(id -u)" != "0" ]; then
855 echo "This script should be run as 'root'"
860 while getopts "n:f:d:p:r:P:i:m:sv" opt ; do
862 n) GUEST_HOSTNAME=$OPTARG;;
863 f) fcdistro=$OPTARG;;
864 d) pldistro=$OPTARG;;
865 p) personality=$OPTARG;;
866 r) REPO_URL=$OPTARG;;
867 P) PREINSTALLED=$OPTARG;;
871 v) VERBOSE=true; set -x;;
876 shift $(($OPTIND - 1))
878 # parse fixed arguments
879 [[ -z "$@" ]] && usage
881 local lxc_root=$(lxcroot $lxc)
884 almost_empty $lxc_root || \
885 { echo "container $lxc already exists in $lxc_root - exiting" ; exit 1 ; }
886 virsh -c lxc:/// domuuid $lxc >& /dev/null && \
887 { echo "container $lxc already exists in libvirt - exiting" ; exit 1 ; }
890 # if IMAGE, copy the provided rootfs to lxc_root
891 if [ -n "$IMAGE" ] ; then
892 [ ! -d "$IMAGE" ] && \
893 { echo "$IMAGE rootfs folder does not exist - exiting" ; exit 1 ; }
894 rsync -a $IMAGE/ $lxc_root/
897 # check we've exhausted the arguments
898 [[ -n "$@" ]] && usage
900 # NAT_MODE is true unless we specified a hostname
901 [ -n "$GUEST_HOSTNAME" ] || NAT_MODE=true
904 [ -z "$fcdistro" ] && fcdistro=$DEFAULT_FCDISTRO
905 [ -z "$pldistro" ] && pldistro=$DEFAULT_PLDISTRO
906 [ -z "$personality" ] && personality=$DEFAULT_PERSONALITY
907 [ -z "$MEMORY" ] && MEMORY=$DEFAULT_MEMORY
910 MEMORY=$(($MEMORY * 1024))
912 # the set of preinstalled packages - depends on mode
913 if [ -z "$PREINSTALLED" ] ; then
914 if [ -n "$NAT_MODE" ] ; then
915 PREINSTALLED=devel.pkgs
917 PREINSTALLED=runtime.pkgs
921 if [ -n "$NAT_MODE" ] ; then
922 # we can now set GUEST_HOSTNAME safely
923 [ -z "$GUEST_HOSTNAME" ] && GUEST_HOSTNAME=$(echo $lxc | sed -e 's,\.,-,g')
925 # as this command can be used in other contexts, not specifying
926 # a repo is considered a warning
927 # use -r none to get rid of this warning
928 if [ "$REPO_URL" == "none" ] ; then
930 elif [ -z "$REPO_URL" ] ; then
931 echo "WARNING -- setting up a yum repo is recommended"
936 fedora_release=$(echo $fcdistro | cut -df -f2)
938 if [ "$personality" == "linux32" ]; then
941 elif [ "$personality" == "linux64" ]; then
945 echo "Unknown personality: $personality"
948 # compute networking details for the test mode
949 # (build mode relies entirely on dhcp on the private subnet)
950 if [ -z "$NAT_MODE" ] ; then
952 #create_bridge_if_needed $PUBLIC_BRIDGE
953 lbuild-bridge.sh $PUBLIC_BRIDGE
955 GUEST_IP=$(gethostbyname $GUEST_HOSTNAME)
956 # use same NETMASK as bridge interface br0
957 masklen=$(ip addr show $PUBLIC_BRIDGE | grep -v inet6 | grep inet | awk '{print $2;}' | cut -d/ -f2)
958 NETMASK=$(masklen_to_netmask $masklen)
959 GATEWAY=$(ip route show | grep default | awk '{print $3}' | head -1)
960 VIF_HOST="vif$(echo $GUEST_HOSTNAME | cut -d. -f1)"
963 setup_lxc $lxc $fcdistro $pldistro $personality
965 # historically this command is for setting up a build or a test VM
966 # kind of patchy right now though
967 devel_or_test_tools $lxc $fcdistro $pldistro $personality
969 # container gets started here
970 post_install $lxc $personality