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="http://mirror.onelab.eu/"
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 python3 -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 (1 <= masklen <= 32):
64 print("Wrong masklen", masklen)
72 result.append(masklen)
74 print(".".join([ str(256-2**(8-i)) for i in result ]))
78 ##############################
79 # return dnf or debootstrap
80 function package_method () {
81 local fcdistro=$1; shift
83 f[0-9]*|centos[0-9]*|sl[0-9]*)
85 wheezy|jessie|trusty|xenial|bionic|focal)
88 echo "Unknown package_method for distro $fcdistro" ;;
94 # interfaces for older debian/uuntu
95 # systemd for more recent debian/ubuntu
96 function network_method () {
97 local fcdistro=$1; shift
99 f[0-9]*|centos[0-9]*|sl[0-9]*)
101 wheezy|jessie|trusty|xenial|bionic)
106 echo "Unknown network_method for distro $fcdistro" ;;
110 # return arch from debian distro and personality
111 function canonical_arch () {
112 local personality=$1; shift
113 local fcdistro=$1; shift
114 case $(package_method $fcdistro) in
119 *) echo Unknown-arch-1 ;;
125 *) echo Unknown-arch-2 ;;
128 echo Unknown-arch-3 ;;
132 # the new test framework creates /timestamp in /vservers/<name> *before* populating it
133 function almost_empty () {
134 local dir="$1"; shift ;
135 # non existing is fine
136 [ ! -d $dir ] && return 0;
137 # need to have at most one file
138 local count=$(cd $dir; ls | wc -l)
142 ##############################
143 function fedora_install() {
148 local lxc_root=$(lxcroot $lxc)
150 local cache=/var/cache/lxc/fedora/$arch/${fedora_release}
154 flock --exclusive --timeout 60 200 || { echo "Cache repository is busy." ; return 1 ; }
156 if [ ! -e "$cache/rootfs" ]; then
157 echo "Getting cache download in $cache/rootfs ... "
158 fedora_download $cache || { echo "Failed to download 'fedora base'"; return 1; }
160 echo "Updating cache $cache/rootfs ..."
161 if ! dnf --installroot $cache/rootfs --releasever=${fedora_release} -y --nogpgcheck update ; then
162 echo "Failed to update 'fedora base', continuing with last known good cache"
164 echo "Update finished"
168 echo "Filling $lxc_root from $cache/rootfs ... "
169 rsync -a $cache/rootfs/ $lxc_root/
178 function fedora_download() {
181 local cache=$1; shift
183 # check the mini fedora was not already downloaded
184 INSTALL_ROOT=$cache/partial
187 # download a mini fedora into a cache
188 echo "Downloading fedora minimal ..."
190 mkdir -p $INSTALL_ROOT || { echo "Failed to create '$INSTALL_ROOT' directory" ; return 1; }
192 mkdir -p $INSTALL_ROOT/etc/yum.repos.d
193 mkdir -p $INSTALL_ROOT/dev
194 mknod -m 0444 $INSTALL_ROOT/dev/random c 1 8
195 mknod -m 0444 $INSTALL_ROOT/dev/urandom c 1 9
197 # copy yum config and repo files
198 cp /etc/yum.conf $INSTALL_ROOT/etc/
199 cp /etc/yum.repos.d/fedora{,-updates}.repo $INSTALL_ROOT/etc/yum.repos.d/
201 # append fedora repo files with hardwired releasever and basearch
202 if [ -z "$USE_UPSTREAM_REPOS" ]; then
203 for f in $INSTALL_ROOT/etc/yum.repos.d/* ; do
204 sed -i "s/\$basearch/$arch/g; s/\$releasever/${fedora_release}/g;" $f
208 # looks like all this business about fetching fedora-release is not needed
210 # MIRROR_URL=$FEDORA_MIRROR/fedora/releases/${fedora_release}/Everything/$arch/os
211 # # since fedora18 the rpms are scattered by first name
212 # # first try the second version of fedora-release first
215 # for subindex in 3 2 1; do
216 # RELEASE_URLS="$RELEASE_URLS $MIRROR_URL/Packages/f/fedora-release-${fedora_release}-${subindex}.noarch.rpm"
219 # RELEASE_TARGET=$INSTALL_ROOT/fedora-release-${fedora_release}.noarch.rpm
222 # for attempt in $RELEASE_URLS; do
223 # if curl --silent --fail $attempt -o $RELEASE_TARGET; then
224 # echo "Successfully Retrieved $attempt"
228 # echo "Failed (not to worry about) with attempt $attempt"
231 # [ -n "$found" ] || { echo "Could not retrieve fedora-release rpm - exiting" ; exit 1; }
233 mkdir -p $INSTALL_ROOT/var/lib/rpm
234 rpm --root $INSTALL_ROOT --initdb
235 # when installing f12 this apparently is already present, so ignore result
236 # rpm --root $INSTALL_ROOT -ivh $INSTALL_ROOT/fedora-release-${fedora_release}.noarch.rpm || :
237 # however f12 root images won't get created on a f18 host
238 # (the issue here is the same as the one we ran into when dealing with a vs-box)
239 # in a nutshell, in f12 the glibc-common and filesystem rpms have an apparent conflict
240 # >>> file /usr/lib/locale from install of glibc-common-2.11.2-3.x86_64 conflicts
241 # with file from package filesystem-2.4.30-2.fc12.x86_64
242 # in fact this was - of course - allowed by f12's rpm but later on a fix was made
243 # http://rpm.org/gitweb?p=rpm.git;a=commitdiff;h=cf1095648194104a81a58abead05974a5bfa3b9a
244 # So ideally if we want to be able to build f12 images from f18 we need an rpm that has
245 # this patch undone, like we have in place on our f14 boxes (our f14 boxes need a f18-like rpm)
247 DNF="dnf --installroot=$INSTALL_ROOT --nogpgcheck -y --releasever=${fedora_release}"
248 echo "$DNF install $FEDORA_PREINSTALLED"
249 $DNF install $FEDORA_PREINSTALLED || { echo "Failed to download rootfs, aborting." ; return 1; }
251 mv "$INSTALL_ROOT" "$cache/rootfs"
252 echo "Download complete."
257 ##############################
258 # xxx f35 not working yet
259 function fedora_configure() {
265 local fcdistro=$1; shift
266 local lxc_root=$(lxcroot $lxc)
268 # disable selinux in fedora
269 mkdir -p $lxc_root/selinux
270 echo 0 > $lxc_root/selinux/enforce
272 # enable networking and set hostname
273 cat <<EOF > ${lxc_root}/etc/sysconfig/network
276 cat <<EOF > ${lxc_root}/etc/hostname
280 local dev_path="${lxc_root}/dev"
283 mknod -m 666 ${dev_path}/null c 1 3
284 mknod -m 666 ${dev_path}/zero c 1 5
285 mknod -m 666 ${dev_path}/random c 1 8
286 mknod -m 666 ${dev_path}/urandom c 1 9
287 mkdir -m 755 ${dev_path}/pts
288 mkdir -m 1777 ${dev_path}/shm
289 mknod -m 666 ${dev_path}/tty c 5 0
290 mknod -m 666 ${dev_path}/tty0 c 4 0
291 mknod -m 666 ${dev_path}/tty1 c 4 1
292 mknod -m 666 ${dev_path}/tty2 c 4 2
293 mknod -m 666 ${dev_path}/tty3 c 4 3
294 mknod -m 666 ${dev_path}/tty4 c 4 4
295 mknod -m 600 ${dev_path}/console c 5 1
296 mknod -m 666 ${dev_path}/full c 1 7
297 mknod -m 600 ${dev_path}/initctl p
298 mknod -m 666 ${dev_path}/ptmx c 5 2
300 fedora_configure_systemd $lxc
302 local guest_ifcfg=${lxc_root}/etc/sysconfig/network-scripts/ifcfg-$VIF_GUEST
303 mkdir -p $(dirname ${guest_ifcfg})
304 # starting with f27, we go for NetworkManager
305 # no more NM_CONTROLLED nonsense
306 if [ -n "$NAT_MODE" ]; then
307 write_guest_ifcfg_natip
309 write_guest_ifcfg_publicip
312 [ -z "$IMAGE" ] && fedora_configure_yum $lxc $fcdistro $pldistro
317 # this code of course is for guests that do run on systemd
318 function fedora_configure_systemd() {
322 local lxc_root=$(lxcroot $lxc)
324 # so ignore if we can't find /etc/systemd at all
325 [ -d ${lxc_root}/etc/systemd ] || return 0
326 # otherwise let's proceed
327 ln -sf /lib/systemd/system/multi-user.target ${lxc_root}/etc/systemd/system/default.target
328 touch ${lxc_root}/etc/fstab
329 ln -sf /dev/null ${lxc_root}/etc/systemd/system/udev.service
330 # Thierry - Feb 2013 relying on getty is looking for trouble
331 # so, turning getty off for now instead
332 # sed -i 's/After=dev-%i.device/After=/' ${lxc_root}/lib/systemd/system/getty\@.service
333 ln -sf /dev/null ${lxc_root}/etc/systemd/system/"getty@.service"
334 rm -f ${lxc_root}/etc/systemd/system/getty.target.wants/*service || :
335 # can't seem to handle this one with systemctl
336 # second part should trigger starting with fedora31, where the network target is not manually manageable
337 chroot ${lxc_root} $personality chkconfig network on || chroot ${lxc_root} $personality systemctl enable NetworkManager
340 # overwrite container yum config
341 function fedora_configure_yum () {
347 local fcdistro=$1; shift
348 local pldistro=$1; shift
350 local lxc_root=$(lxcroot $lxc)
352 chroot ${lxc_root} $personality rpm --rebuilddb
354 if [ -z "$USE_UPSTREAM_REPOS" ]; then
355 echo "Initializing yum.repos.d in $lxc"
356 rm -f $lxc_root/etc/yum.repos.d/*
358 # use mirroring/ stuff instead of a hard-wired config
359 local repofile=$lxc_root/etc/yum.repos.d/building.repo
360 yumconf_mirrors $repofile ${DIRNAME} $fcdistro "" $FEDORA_MIRROR
361 # the keys stuff requires adjustment though
362 sed -i $repofile -e s,'gpgkey=.*',"gpgkey=${FEDORA_MIRROR_KEYS}/RPM-GPG-KEY-fedora-${fedora_release}-primary,"
364 # import fedora key so that gpgckeck does not whine or require stdin
365 # required since fedora24
366 rpm --root $lxc_root --import $FEDORA_MIRROR_KEYS/RPM-GPG-KEY-fedora-${fedora_release}-primary
368 # for using this script as a general-purpose lxc creation wrapper
369 # just mention 'none' as the repo url
370 if [ -n "$MYPLC_REPO_URL" ] ; then
371 if [ ! -d $lxc_root/etc/yum.repos.d ] ; then
372 echo "WARNING : cannot create myplc repo"
374 # exclude kernel from fedora repos
375 yumexclude=$(pl_plcyumexclude $fcdistro $pldistro $DIRNAME)
376 for repo in $lxc_root/etc/yum.repos.d/* ; do
377 [ -f $repo ] && yumconf_exclude $repo "exclude=$yumexclude"
379 # the build repo is not signed at this stage
380 cat > $lxc_root/etc/yum.repos.d/myplc.repo <<EOF
383 baseurl=$MYPLC_REPO_URL
391 ##############################
392 # apparently ubuntu exposes a mirrors list by country at
393 # http://mirrors.ubuntu.com/mirrors.txt
394 # need to specify the right mirror for debian variants like ubuntu and the like
395 function debian_mirror () {
396 local fcdistro=$1; shift
399 echo http://ftp2.fr.debian.org/debian/ ;;
400 trusty|xenial|bionic|focal)
401 echo http://www-ftp.lip6.fr/pub/linux/distributions/Ubuntu/archive/ ;;
402 *) echo unknown distro $fcdistro; exit 1;;
406 function debian_install () {
410 local lxc_root=$(lxcroot $lxc)
412 local arch=$(canonical_arch $personality $fcdistro)
413 local mirror=$(debian_mirror $fcdistro)
414 debootstrap --no-check-gpg --arch $arch $fcdistro $lxc_root $mirror
415 # just like with fedora we ensure a few packages get installed as well
417 #virsh -c lxc:/// lxc-enter-namespace $lxc /usr/bin/$personality /bin/bash -c "apt-get update"
418 #virsh -c lxc:/// lxc-enter-namespace $lxc /usr/bin/$personality /bin/bash -c "apt-get -y install $DEBIAN_PREINSTALLED"
419 chroot ${lxc_root} $personality apt-get update
420 chroot ${lxc_root} $personality apt-get -y install $DEBIAN_PREINSTALLED
422 cat <<EOF > ${lxc_root}/etc/hostname
428 function debian_configure () {
430 local fcdistro=$1; shift
431 case $(network_method $fcdistro) in
433 local guest_interfaces=${lxc_root}/etc/network/interfaces
434 ( [ -n "$NAT_MODE" ] \
435 && write_guest_interfaces_natip \
436 || write_guest_interfaces_publicip ) > $guest_interfaces
439 local systemd_config="${lxc_root}/etc/systemd/network/wired.network"
440 ( [ -n "$NAT_MODE" ] \
441 && write_guest_systemd_natip \
442 || write_guest_systemd_publicip ) > $systemd_config
443 chroot "${lxc_root}" systemctl enable systemd-networkd
448 function write_guest_interfaces_natip () {
451 iface $VIF_GUEST inet dhcp
455 function write_guest_interfaces_publicip () {
458 iface $VIF_GUEST inet static
466 # https://wiki.archlinux.org/title/systemd-networkd
467 # https://www.linuxtricks.fr/wiki/systemd-le-reseau-avec-systemd-networkd
468 function write_guest_systemd_natip () {
478 function write_guest_systemd_publicip () {
484 Address=${GUEST_IP}/${MASKLEN}
489 ##############################
490 function setup_lxc() {
494 #trap failure ERR INT
497 local fcdistro=$1; shift
498 local pldistro=$1; shift
499 local personality=$1; shift
501 local lxc_root=$(lxcroot $lxc)
503 # create lxc container
505 pkg_method=$(package_method $fcdistro)
508 if [ -z "$IMAGE" ]; then
509 fedora_install $lxc || { echo "failed to install fedora root image"; exit 1 ; }
510 # this appears to be safer; observed in Jan. 2016 on a f23 host and a f14 cached image
511 # we were getting this message when attempting the first chroot dnf install
512 # rpmdb: Program version 4.8 doesn't match environment version 5.3
513 chroot $(lxcroot $lxc) $personality rm -rf /var/lib/rpm/__db.00{0,1,2,3,4,5,6,7,8,9}
514 chroot $(lxcroot $lxc) $personality rpm --rebuilddb
516 fedora_configure $lxc $fcdistro || { echo "failed to configure fedora for a container"; exit 1 ; }
519 if [ -z "$IMAGE" ]; then
520 debian_install $lxc || { echo "failed to install debian/ubuntu root image"; exit 1 ; }
522 debian_configure $lxc $fcdistro || { echo "failed to configure debian/ubuntu for a container"; exit 1 ; }
525 echo "$COMMAND:: unknown package_method - exiting"
530 # Enable cgroup -- xxx -- is this really useful ?
531 [ -d $lxc_root/cgroup ] || mkdir $lxc_root/cgroup
533 ### set up resolv.conf from host
534 # ubuntu precise and on, /etc/resolv.conf is a symlink to ../run/resolvconf/resolv.conf
535 [ -h $lxc_root/etc/resolv.conf ] && rm -f $lxc_root/etc/resolv.conf
536 cp /etc/resolv.conf $lxc_root/etc/resolv.conf
537 ### and /etc/hosts for at least localhost
538 [ -f $lxc_root/etc/hosts ] || echo "127.0.0.1 localhost localhost.localdomain" > $lxc_root/etc/hosts
540 # grant ssh access from host to guest
541 mkdir -p $lxc_root/root/.ssh
542 cat /root/.ssh/id_rsa.pub >> $lxc_root/root/.ssh/authorized_keys
543 chmod 700 $lxc_root/root/.ssh
544 chmod 600 $lxc_root/root/.ssh/authorized_keys
546 # don't keep the input xml, this can be retrieved at all times with virsh dumpxml
547 local config_xml=/tmp/$lxc.xml
548 ( [ -n "$NAT_MODE" ] && write_lxc_xml_natip $lxc || write_lxc_xml_publicip $lxc ) > $config_xml
550 # define lxc container for libvirt
551 virsh -c lxc:/// define $config_xml
556 # this part does not belong in a domain any more
557 # but goes in a network object of its own existence
559 # <name>host-bridge</name>
560 # <forward mode="bridge"/>
561 # <bridge name="br0"/>
565 function write_lxc_xml_publicip () {
567 local lxc_root=$(lxcroot $lxc)
571 <memory>$MEMORY</memory>
573 <type arch='$arch2'>exe</type>
574 <init>/sbin/init</init>
580 <clock offset='utc'/>
581 <on_poweroff>destroy</on_poweroff>
582 <on_reboot>restart</on_reboot>
583 <on_crash>destroy</on_crash>
585 <emulator>/usr/libexec/libvirt_lxc</emulator>
586 <filesystem type='mount'>
587 <source dir='$lxc_root'/>
590 <interface type="bridge">
591 <source bridge="$PUBLIC_BRIDGE"/>
592 <target dev='$VIF_HOST'/>
594 <console type='pty' />
600 # grant build guests the ability to do mknods
601 function write_lxc_xml_natip () {
603 local lxc_root=$(lxcroot $lxc)
607 <memory>$MEMORY</memory>
609 <type arch='$arch2'>exe</type>
610 <init>/sbin/init</init>
614 <capabilities policy='default'>
619 <clock offset='utc'/>
620 <on_poweroff>destroy</on_poweroff>
621 <on_reboot>restart</on_reboot>
622 <on_crash>destroy</on_crash>
624 <emulator>/usr/libexec/libvirt_lxc</emulator>
625 <filesystem type='mount'>
626 <source dir='$lxc_root'/>
629 <interface type="network">
630 <source network="default"/>
632 <console type='pty' />
638 # this one is dhcp-based
639 function write_guest_ifcfg_natip () {
649 # use fixed GUEST_IP as specified by GUEST_HOSTNAME
650 function write_guest_ifcfg_publicip () {
655 HOSTNAME=$GUEST_HOSTNAME
664 function devel_or_test_tools () {
671 local fcdistro=$1; shift
672 local pldistro=$1; shift
673 local personality=$1; shift
675 local lxc_root=$(lxcroot $lxc)
677 local pkg_method=$(package_method $fcdistro)
679 local pkgsfile=$(pl_locateDistroFile $DIRNAME $pldistro $PREINSTALLED)
681 ### install individual packages, then groups
682 # get target arch - use uname -i here (we want either x86_64 or i386)
684 local lxc_arch=$(chroot ${lxc_root} $personality uname -i)
685 # on debian systems we get arch through the 'arch' command
686 [ "$lxc_arch" = "unknown" ] && lxc_arch=$(chroot ${lxc_root} $personality arch)
688 local packages=$(pl_getPackages -a $lxc_arch $fcdistro $pldistro $pkgsfile)
689 local groups=$(pl_getGroups -a $lxc_arch $fcdistro $pldistro $pkgsfile)
691 case "$pkg_method" in
693 # --allowerasing required starting with fedora24
696 chroot ${lxc_root} $personality dnf --version && has_dnf=true
697 if [ -n "$has_dnf" ]; then
698 echo "container has dnf - invoking with --allowerasing"
699 local pkg_installer="dnf -y install --allowerasing"
700 local grp_installer="dnf -y groupinstall --allowerasing"
702 echo "container has only dnf"
703 local pkg_installer="dnf -y install"
704 local grp_installer="dnf -y groupinstall"
706 [ -n "$packages" ] && chroot ${lxc_root} $personality $pkg_installer $packages
707 for group_plus in $groups; do
708 local group=$(echo $group_plus | sed -e "s,+++, ,g")
709 chroot ${lxc_root} $personality $grp_installer "$group"
711 # store current rpm list in /init-lxc.rpms in case we need to check the contents
712 chroot ${lxc_root} $personality rpm -aq > $lxc_root/init-lxc.rpms
716 if grep -iq ubuntu /vservers/$lxc/etc/lsb-release 2> /dev/null; then
717 # on ubuntu, at this point we end up with a single feed in /etc/apt/sources.list
718 # we need at least to add the 'universe' feed for python-rpm
719 ( cd /vservers/$lxc/etc/apt ; head -1 sources.list | sed -e s,main,universe, > sources.list.d/universe.list )
720 # also adding a link to updates sounds about right
721 ( cd /vservers/$lxc/etc/apt ; head -1 sources.list | sed -e 's, main,-updates main,' > sources.list.d/updates.list )
722 # tell apt about the changes
723 chroot /vservers/$lxc apt-get update
725 for package in $packages ; do
726 # container not started yet
727 #virsh -c lxc:/// lxc-enter-namespace $lxc /usr/bin/$personality /bin/bash -c "apt-get install -y $package" || :
728 chroot ${lxc_root} $personality apt-get install -y $package || :
730 ### xxx todo install groups with apt..
733 echo "unknown pkg_method $pkg_method"
740 function post_install () {
742 local personality=$1; shift
743 local lxc_root=$(lxcroot $lxc)
744 # setup localtime from the host
745 cp /etc/localtime $lxc_root/etc/localtime
746 sshd_disable_password_auth $lxc
748 [ -n "$NAT_MODE" ] && post_install_natip $lxc $personality || post_install_myplc $lxc $personality
749 # start the VM unless specified otherwise
750 if [ -n "$START_VM" ] ; then
751 echo Starting guest $lxc
752 virsh -c lxc:/// start $lxc
753 if [ -n "$NAT_MODE" ] ; then
756 wait_for_ssh $lxc $GUEST_IP
761 # just in case, let's stay on the safe side
762 function sshd_disable_password_auth () {
764 local lxc_root=$(lxcroot $lxc)
765 sed --in-place=.password -e 's,^#\?PasswordAuthentication.*,PasswordAuthentication no,' \
766 $lxc_root/etc/ssh/sshd_config
769 function post_install_natip () {
776 local personality=$1; shift
777 local lxc_root=$(lxcroot $lxc)
779 ### From myplc-devel-native.spec
780 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
781 cat << EOF | chroot ${lxc_root} $personality bash -x
783 # customize root's prompt
784 /bin/cat << PROFILE > /root/.profile
785 export PS1="[$lxc] \\w # "
792 function post_install_myplc () {
798 local personality=$1; shift
799 local lxc_root=$(lxcroot $lxc)
801 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
802 cat << EOF | chroot ${lxc_root} $personality bash -x
804 # create /etc/sysconfig/network if missing
805 [ -f /etc/sysconfig/network ] || /bin/echo NETWORKING=yes > /etc/sysconfig/network
807 # turn off regular crond, as plc invokes plc_crond
808 /sbin/chkconfig crond off
810 # customize root's prompt
811 /bin/cat << PROFILE > /root/.profile
812 export PS1="[$lxc] \\w # "
818 ########################################
819 # workaround for broken lxc-enter-namespace
820 # 1st version was relying on virsh net-dhcp-leases
821 # however this was too fragile, would not work for fedora14 containers
822 # WARNING: this code is duplicated in lbuild-nightly.sh
823 function guest_ipv4() {
826 local mac=$(virsh -c lxc:/// domiflist $lxc | egrep 'network|bridge' | awk '{print $5;}')
828 [ -z "$mac" ] && return 0
829 arp -en | grep "$mac" | awk '{print $1;}'
832 function wait_for_ssh () {
838 # if run in public_ip mode, we know the IP of the guest and it is specified here
839 [ -n "$1" ] && { guest_ip=$1; shift; }
841 #wait max 2 min for sshd to start
843 local current_time=$(date +%s)
844 local stop_time=$(($current_time + 120))
847 while [ "$current_time" -lt "$stop_time" ] ; do
848 echo "$counter-th attempt to reach sshd in container $lxc ..."
849 [ -z "$guest_ip" ] && guest_ip=$(guest_ipv4 $lxc)
850 [ -n "$guest_ip" ] && ssh -o "StrictHostKeyChecking no" $guest_ip 'uname -i' && {
851 success=true; echo "SSHD in container $lxc is UP on IP $guest_ip"; break ; } || :
852 # some of our boxes have gone through a long upgrade historically, and
853 # so they don't end up with the same gid mapping for the ssh_keys
854 # group as the ones in the guest that result from a fresh install
855 virsh -c lxc:/// lxc-enter-namespace $lxc /bin/bash -c "chown root:ssh_keys /etc/ssh/*_key" || :
856 counter=$(($counter+1))
858 current_time=$(date +%s)
861 # Thierry: this is fatal, let's just exit with a failure here
862 [ -z $success ] && { echo "SSHD in container $lxc could not be reached (guest_ip=$guest_ip)" ; exit 1 ; }
867 function failure () {
868 echo "$COMMAND : Bailing out"
874 echo "Usage: $COMMAND [options] lxc-name (aka build mode)"
875 echo "Usage: $COMMAND -n hostname [options] lxc-name (aka test mode)"
877 echo " This command creates a fresh lxc instance, for building, or running a test myplc"
878 echo "In its first form, spawned VM gets a private IP bridged with virbr0 over dhcp/nat"
879 echo "With the second form, spawned VM gets a public IP bridged on public bridge br0"
881 echo "Supported options"
882 echo " -n hostname - the hostname to use in container"
883 echo " -f fcdistro - for creating the root filesystem - defaults to $DEFAULT_FCDISTRO"
884 echo " -d pldistro - defaults to $DEFAULT_PLDISTRO - current support for fedoras debians ubuntus"
885 echo " -p personality - defaults to $DEFAULT_PERSONALITY"
886 echo " -r repo-url - used to populate yum.repos.d - required in test mode"
887 echo " -P pkgs_file - defines a set of extra packages to install in guest"
888 echo " by default we use devel.pkgs (build mode) or runtime.pkgs (test mode)"
889 echo " -i image - the location of the rootfs"
890 echo " -m memory - the amount of allocated memory in MB - defaults to $DEFAULT_MEMORY MB"
891 echo " -s do not start VM"
892 echo " -v be verbose"
900 #trap failure ERR INT
902 if [ "$(id -u)" != "0" ]; then
903 echo "This script should be run as 'root'"
908 while getopts "n:f:d:p:r:uP:i:m:sv" opt ; do
910 n) GUEST_HOSTNAME=$OPTARG;;
911 f) fcdistro=$OPTARG;;
912 d) pldistro=$OPTARG;;
913 p) personality=$OPTARG;;
914 r) MYPLC_REPO_URL=$OPTARG;;
915 u) USE_UPSTREAM_REPOS=true;;
916 P) PREINSTALLED=$OPTARG;;
920 v) VERBOSE=true; set -x;;
925 shift $(($OPTIND - 1))
927 # parse fixed arguments
928 [[ -z "$@" ]] && usage
930 local lxc_root=$(lxcroot $lxc)
933 # when using with the -i option, checking that $lxc_root is void
934 # is a little too much stress..
935 almost_empty $lxc_root || \
936 { echo "container $lxc already exists in $lxc_root - exiting" ; exit 1 ; }
937 virsh -c lxc:/// domuuid $lxc >& /dev/null && \
938 { echo "container $lxc already exists in libvirt - exiting" ; exit 1 ; }
941 # if IMAGE, copy the provided rootfs to lxc_root
942 if [ -n "$IMAGE" ] ; then
943 if [ ! -d "$IMAGE" ]; then
944 echo "$IMAGE rootfs folder does not exist - exiting"
947 echo "Copying $IMAGE into $lxc_root with rsync --archive --delete"
948 rsync --archive --delete $IMAGE/ $lxc_root/
952 # check we've exhausted the arguments
953 [[ -n "$@" ]] && usage
955 # NAT_MODE is true unless we specified a hostname
956 [ -n "$GUEST_HOSTNAME" ] || NAT_MODE=true
959 [ -z "$fcdistro" ] && fcdistro=$DEFAULT_FCDISTRO
960 [ -z "$pldistro" ] && pldistro=$DEFAULT_PLDISTRO
961 [ -z "$personality" ] && personality=$DEFAULT_PERSONALITY
962 [ -z "$MEMORY" ] && MEMORY=$DEFAULT_MEMORY
965 MEMORY=$(($MEMORY * 1024))
967 # the set of preinstalled packages - depends on mode
968 if [ -z "$PREINSTALLED" ] ; then
969 if [ -n "$NAT_MODE" ] ; then
970 PREINSTALLED=devel.pkgs
972 PREINSTALLED=runtime.pkgs
976 if [ -n "$NAT_MODE" ] ; then
977 # we can now set GUEST_HOSTNAME safely
978 [ -z "$GUEST_HOSTNAME" ] && GUEST_HOSTNAME=$(echo $lxc | sed -e 's,\.,-,g')
980 # as this command can be used in other contexts, not specifying
981 # a repo is considered a warning
982 # use -r none to get rid of this warning
983 if [ "$MYPLC_REPO_URL" == "none" ] ; then
985 elif [ -z "$MYPLC_REPO_URL" ] ; then
986 echo "WARNING -- setting up a yum repo is recommended"
991 fedora_release=$(echo $fcdistro | cut -df -f2)
993 if [ "$personality" == "linux32" ]; then
996 elif [ "$personality" == "linux64" ]; then
1000 echo "Unknown personality: $personality"
1003 # compute networking details for the test mode
1004 # (build mode relies entirely on dhcp on the private subnet)
1005 if [ -z "$NAT_MODE" ] ; then
1007 #create_bridge_if_needed $PUBLIC_BRIDGE
1008 lbuild-bridge.sh $PUBLIC_BRIDGE
1010 GUEST_IP=$(gethostbyname $GUEST_HOSTNAME)
1011 # use same NETMASK as bridge interface br0
1012 MASKLEN=$(ip addr show $PUBLIC_BRIDGE | grep -v inet6 | grep inet | awk '{print $2;}' | cut -d/ -f2)
1013 NETMASK=$(masklen_to_netmask $MASKLEN)
1014 GATEWAY=$(ip route show | grep default | awk '{print $3}' | head -1)
1015 VIF_HOST="vif$(echo $GUEST_HOSTNAME | cut -d. -f1)"
1018 setup_lxc $lxc $fcdistro $pldistro $personality
1020 # historically this command is for setting up a build or a test VM
1021 # kind of patchy right now though
1022 devel_or_test_tools $lxc $fcdistro $pldistro $personality
1024 # container gets started here
1025 post_install $lxc $personality