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="yum initscripts 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 () {
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 yum or debootstrap
81 function package_method () {
84 f[0-9]*|centos[0-9]*|sl[0-9]*)
86 wheezy|jessie|precise|trusty|utopic|vivid|wily)
89 echo Unknown distro $fcdistro ;;
93 # return arch from debian distro and personality
94 function canonical_arch () {
97 case $(package_method $fcdistro) in
99 case $personality in *32) echo i386 ;; *64) echo x86_64 ;; *) echo Unknown-arch-1 ;; esac ;;
101 case $personality in *32) echo i386 ;; *64) echo amd64 ;; *) echo Unknown-arch-2 ;; esac ;;
103 echo Unknown-arch-3 ;;
107 # the new test framework creates /timestamp in /vservers/<name> *before* populating it
108 function almost_empty () {
110 # non existing is fine
111 [ ! -d $dir ] && return 0;
112 # need to have at most one file
113 count=$(cd $dir; ls | wc -l); [ $count -le 1 ];
116 ##############################
117 function fedora_install() {
122 lxc_root=$(lxcroot $lxc)
124 cache=/var/cache/lxc/fedora/$arch/$release
128 flock --exclusive --timeout 60 200 || { echo "Cache repository is busy." ; return 1 ; }
130 if [ ! -e "$cache/rootfs" ]; then
131 echo "Getting cache download in $cache/rootfs ... "
132 fedora_download $cache || { echo "Failed to download 'fedora base'"; return 1; }
134 echo "Updating cache $cache/rootfs ..."
135 if ! yum --installroot $cache/rootfs --releasever $release -y --nogpgcheck update ; then
136 echo "Failed to update 'fedora base', continuing with last known good cache"
138 echo "Update finished"
142 echo "Filling $lxc_root from $cache/rootfs ... "
143 rsync -a $cache/rootfs/ $lxc_root/
152 function fedora_download() {
157 # check the mini fedora was not already downloaded
158 INSTALL_ROOT=$cache/partial
161 # download a mini fedora into a cache
162 echo "Downloading fedora minimal ..."
164 mkdir -p $INSTALL_ROOT || { echo "Failed to create '$INSTALL_ROOT' directory" ; return 1; }
166 mkdir -p $INSTALL_ROOT/etc/yum.repos.d
167 mkdir -p $INSTALL_ROOT/dev
168 mknod -m 0444 $INSTALL_ROOT/dev/random c 1 8
169 mknod -m 0444 $INSTALL_ROOT/dev/urandom c 1 9
171 # copy yum config and repo files
172 cp /etc/yum.conf $INSTALL_ROOT/etc/
173 cp /etc/yum.repos.d/fedora* $INSTALL_ROOT/etc/yum.repos.d/
175 # append fedora repo files with desired $release and $basearch
176 for f in $INSTALL_ROOT/etc/yum.repos.d/* ; do
177 sed -i "s/\$basearch/$arch/g; s/\$releasever/$release/g;" $f
180 MIRROR_URL=$FEDORA_MIRROR_BASE/releases/$release/Everything/$arch/os
181 RELEASE_URL1="$MIRROR_URL/Packages/fedora-release-$release-1.noarch.rpm"
182 # with fedora18 the rpms are scattered by first name
183 # first try the second version of fedora-release first
184 RELEASE_URL2="$MIRROR_URL/Packages/f/fedora-release-$release-2.noarch.rpm"
185 RELEASE_URL3="$MIRROR_URL/Packages/f/fedora-release-$release-1.noarch.rpm"
187 RELEASE_TARGET=$INSTALL_ROOT/fedora-release-$release.noarch.rpm
189 for attempt in $RELEASE_URL1 $RELEASE_URL2 $RELEASE_URL3; do
190 if curl -f $attempt -o $RELEASE_TARGET ; then
191 echo "Retrieved $attempt"
195 echo "Failed attempt $attempt"
198 [ -n "$found" ] || { echo "Could not retrieve fedora-release rpm - exiting" ; exit 1; }
200 mkdir -p $INSTALL_ROOT/var/lib/rpm
201 rpm --root $INSTALL_ROOT --initdb
202 # when installing f12 this apparently is already present, so ignore result
203 rpm --root $INSTALL_ROOT -ivh $INSTALL_ROOT/fedora-release-$release.noarch.rpm || :
204 # however f12 root images won't get created on a f18 host
205 # (the issue here is the same as the one we ran into when dealing with a vs-box)
206 # in a nutshell, in f12 the glibc-common and filesystem rpms have an apparent conflict
207 # >>> file /usr/lib/locale from install of glibc-common-2.11.2-3.x86_64 conflicts
208 # with file from package filesystem-2.4.30-2.fc12.x86_64
209 # in fact this was - of course - allowed by f12's rpm but later on a fix was made
210 # http://rpm.org/gitweb?p=rpm.git;a=commitdiff;h=cf1095648194104a81a58abead05974a5bfa3b9a
211 # So ideally if we want to be able to build f12 images from f18 we need an rpm that has
212 # this patch undone, like we have in place on our f14 boxes (our f14 boxes need a f18-like rpm)
214 YUM="yum --installroot=$INSTALL_ROOT --releasever=$release --nogpgcheck -y"
215 echo "$YUM install $FEDORA_PREINSTALLED"
216 $YUM install $FEDORA_PREINSTALLED || { echo "Failed to download rootfs, aborting." ; return 1; }
218 mv "$INSTALL_ROOT" "$cache/rootfs"
219 echo "Download complete."
224 ##############################
225 function fedora_configure() {
231 lxc_root=$(lxcroot $lxc)
233 # disable selinux in fedora
234 mkdir -p $lxc_root/selinux
235 echo 0 > $lxc_root/selinux/enforce
240 cat <<EOF > ${lxc_root}/etc/sysconfig/network
243 cat <<EOF > ${lxc_root}/etc/hostname
248 cat <<EOF > ${lxc_root}/etc/sysconfig/network
250 HOSTNAME=$GUEST_HOSTNAME
253 cat <<EOF > $lxc_root/etc/hosts
254 127.0.0.1 localhost $GUEST_HOSTNAME
259 dev_path="${lxc_root}/dev"
262 mknod -m 666 ${dev_path}/null c 1 3
263 mknod -m 666 ${dev_path}/zero c 1 5
264 mknod -m 666 ${dev_path}/random c 1 8
265 mknod -m 666 ${dev_path}/urandom c 1 9
266 mkdir -m 755 ${dev_path}/pts
267 mkdir -m 1777 ${dev_path}/shm
268 mknod -m 666 ${dev_path}/tty c 5 0
269 mknod -m 666 ${dev_path}/tty0 c 4 0
270 mknod -m 666 ${dev_path}/tty1 c 4 1
271 mknod -m 666 ${dev_path}/tty2 c 4 2
272 mknod -m 666 ${dev_path}/tty3 c 4 3
273 mknod -m 666 ${dev_path}/tty4 c 4 4
274 mknod -m 600 ${dev_path}/console c 5 1
275 mknod -m 666 ${dev_path}/full c 1 7
276 mknod -m 600 ${dev_path}/initctl p
277 mknod -m 666 ${dev_path}/ptmx c 5 2
279 if [ "$(echo $fcdistro | cut -d"f" -f2)" -le "14" ]; then
280 fedora_configure_init $lxc
282 fedora_configure_systemd $lxc
285 guest_ifcfg=${lxc_root}/etc/sysconfig/network-scripts/ifcfg-$VIF_GUEST
286 ( [ -n "$NAT_MODE" ] && write_guest_ifcfg_natip || write_guest_ifcfg_publicip ) > $guest_ifcfg
288 [ -z "$IMAGE" ] && fedora_configure_yum $lxc $fcdistro $pldistro
293 function fedora_configure_init() {
297 lxc_root=$(lxcroot $lxc)
299 sed -i 's|.sbin.start_udev||' ${lxc_root}/etc/rc.sysinit
300 sed -i 's|.sbin.start_udev||' ${lxc_root}/etc/rc.d/rc.sysinit
301 # don't mount devpts, for pete's sake
302 sed -i 's/^.*dev.pts.*$/#\0/' ${lxc_root}/etc/rc.sysinit
303 sed -i 's/^.*dev.pts.*$/#\0/' ${lxc_root}/etc/rc.d/rc.sysinit
304 chroot ${lxc_root} $personality chkconfig udev-post off
305 chroot ${lxc_root} $personality chkconfig network on
308 # this code of course is for guests that do run on systemd
309 function fedora_configure_systemd() {
313 lxc_root=$(lxcroot $lxc)
315 # so ignore if we can't find /etc/systemd at all
316 [ -d ${lxc_root}/etc/systemd ] || return 0
317 # otherwise let's proceed
318 ln -sf /lib/systemd/system/multi-user.target ${lxc_root}/etc/systemd/system/default.target
319 touch ${lxc_root}/etc/fstab
320 ln -sf /dev/null ${lxc_root}/etc/systemd/system/udev.service
321 # Thierry - Feb 2013 relying on getty is looking for trouble
322 # so, turning getty off for now instead
323 # sed -i 's/After=dev-%i.device/After=/' ${lxc_root}/lib/systemd/system/getty\@.service
324 ln -sf /dev/null ${lxc_root}/etc/systemd/system/"getty@.service"
325 rm -f ${lxc_root}/etc/systemd/system/getty.target.wants/*service || :
326 # can't seem to handle this one with systemctl
327 chroot ${lxc_root} $personality chkconfig network on
330 # overwrite container yum config
331 function fedora_configure_yum () {
340 lxc_root=$(lxcroot $lxc)
342 chroot ${lxc_root} $personality rpm --rebuilddb
344 echo "Initializing yum.repos.d in $lxc"
345 rm -f $lxc_root/etc/yum.repos.d/*
347 cat > $lxc_root/etc/yum.repos.d/building.repo <<EOF
349 name=Fedora \$releasever - \$basearch
350 baseurl=$FEDORA_MIRROR_BASE/releases/\$releasever/Everything/\$basearch/os/
354 gpgkey=$FEDORA_MIRROR_KEYS/RPM-GPG-KEY-fedora-$release-primary
357 name=Fedora \$releasever - \$basearch - Updates
358 baseurl=$FEDORA_MIRROR_BASE/updates/\$releasever/\$basearch/
362 gpgkey=$FEDORA_MIRROR_KEYS/RPM-GPG-KEY-fedora-$release-primary
365 # for using this script as a general-purpose lxc creation wrapper
366 # just mention 'none' as the repo url
367 if [ -n "$REPO_URL" ] ; then
368 if [ ! -d $lxc_root/etc/yum.repos.d ] ; then
369 echo "WARNING : cannot create myplc repo"
371 # exclude kernel from fedora repos
372 yumexclude=$(pl_plcyumexclude $fcdistro $pldistro $DIRNAME)
373 for repo in $lxc_root/etc/yum.repos.d/* ; do
374 [ -f $repo ] && yumconf_exclude $repo "exclude=$yumexclude"
376 # the build repo is not signed at this stage
377 cat > $lxc_root/etc/yum.repos.d/myplc.repo <<EOF
388 ##############################
389 # apparently ubuntu exposes a mirrors list by country at
390 # http://mirrors.ubuntu.com/mirrors.txt
391 # need to specify the right mirror for debian variants like ubuntu and the like
392 function debian_mirror () {
396 echo http://ftp2.fr.debian.org/debian/ ;;
397 precise|trusty|utopic|vivid|wily)
398 # echo http://mir1.ovh.net/ubuntu/ubuntu/ ;;
399 echo http://www-ftp.lip6.fr/pub/linux/distributions/Ubuntu/archive/ ;;
400 *) echo unknown distro $fcdistro; exit 1;;
404 function debian_install () {
408 lxc_root=$(lxcroot $lxc)
410 arch=$(canonical_arch $personality $fcdistro)
411 mirror=$(debian_mirror $fcdistro)
412 debootstrap --no-check-gpg --arch $arch $fcdistro $lxc_root $mirror
413 # just like with fedora we ensure a few packages get installed as well
415 #virsh -c lxc:/// lxc-enter-namespace $lxc /usr/bin/$personality /bin/bash -c "apt-get update"
416 #virsh -c lxc:/// lxc-enter-namespace $lxc /usr/bin/$personality /bin/bash -c "apt-get -y install $DEBIAN_PREINSTALLED"
417 chroot ${lxc_root} $personality apt-get update
418 chroot ${lxc_root} $personality apt-get -y install $DEBIAN_PREINSTALLED
420 cat <<EOF > ${lxc_root}/etc/hostname
426 function debian_configure () {
427 guest_interfaces=${lxc_root}/etc/network/interfaces
428 ( [ -n "$NAT_MODE" ] && write_guest_interfaces_natip || write_guest_interfaces_publicip ) > $guest_interfaces
431 function write_guest_interfaces_natip () {
434 iface $VIF_GUEST inet dhcp
438 function write_guest_interfaces_publicip () {
441 iface $VIF_GUEST inet static
447 ##############################
448 function setup_lxc() {
452 #trap failure ERR INT
457 personality=$1; shift
459 lxc_root=$(lxcroot $lxc)
461 # create lxc container
463 pkg_method=$(package_method $fcdistro)
466 if [ -z "$IMAGE" ]; then
467 fedora_install $lxc || { echo "failed to install fedora root image"; exit 1 ; }
469 fedora_configure $lxc || { echo "failed to configure fedora for a container"; exit 1 ; }
472 if [ -z "$IMAGE" ]; then
473 debian_install $lxc || { echo "failed to install debian/ubuntu root image"; exit 1 ; }
475 debian_configure || { echo "failed to configure debian/ubuntu for a container"; exit 1 ; }
478 echo "$COMMAND:: unknown package_method - exiting"
483 # Enable cgroup -- xxx -- is this really useful ?
484 [ -d $lxc_root/cgroup ] || mkdir $lxc_root/cgroup
486 ### set up resolv.conf from host
487 # ubuntu precise and on, /etc/resolv.conf is a symlink to ../run/resolvconf/resolv.conf
488 [ -h $lxc_root/etc/resolv.conf ] && rm -f $lxc_root/etc/resolv.conf
489 cp /etc/resolv.conf $lxc_root/etc/resolv.conf
490 ### and /etc/hosts for at least localhost
491 [ -f $lxc_root/etc/hosts ] || echo "127.0.0.1 localhost localhost.localdomain" > $lxc_root/etc/hosts
493 # grant ssh access from host to guest
494 mkdir -p $lxc_root/root/.ssh
495 cat /root/.ssh/id_rsa.pub >> $lxc_root/root/.ssh/authorized_keys
496 chmod 700 $lxc_root/root/.ssh
497 chmod 600 $lxc_root/root/.ssh/authorized_keys
499 # don't keep the input xml, this can be retrieved at all times with virsh dumpxml
500 config_xml=/tmp/$lxc.xml
501 ( [ -n "$NAT_MODE" ] && write_lxc_xml_natip $lxc || write_lxc_xml_publicip $lxc ) > $config_xml
503 # define lxc container for libvirt
504 virsh -c lxc:/// define $config_xml
509 # this part does not belong in a domain any more
510 # but goes in a network object of its own existence
512 # <name>host-bridge</name>
513 # <forward mode="bridge"/>
514 # <bridge name="br0"/>
518 function write_lxc_xml_publicip () {
520 lxc_root=$(lxcroot $lxc)
524 <memory>$MEMORY</memory>
526 <type arch='$arch2'>exe</type>
527 <init>/sbin/init</init>
533 <clock offset='utc'/>
534 <on_poweroff>destroy</on_poweroff>
535 <on_reboot>restart</on_reboot>
536 <on_crash>destroy</on_crash>
538 <emulator>/usr/libexec/libvirt_lxc</emulator>
539 <filesystem type='mount'>
540 <source dir='$lxc_root'/>
543 <interface type="bridge">
544 <source bridge="$PUBLIC_BRIDGE"/>
545 <target dev='$VIF_HOST'/>
547 <console type='pty' />
553 # grant build guests the ability to do mknods
554 function write_lxc_xml_natip () {
556 lxc_root=$(lxcroot $lxc)
560 <memory>$MEMORY</memory>
562 <type arch='$arch2'>exe</type>
563 <init>/sbin/init</init>
567 <capabilities policy='default'>
572 <clock offset='utc'/>
573 <on_poweroff>destroy</on_poweroff>
574 <on_reboot>restart</on_reboot>
575 <on_crash>destroy</on_crash>
577 <emulator>/usr/libexec/libvirt_lxc</emulator>
578 <filesystem type='mount'>
579 <source dir='$lxc_root'/>
582 <interface type="network">
583 <source network="default"/>
585 <console type='pty' />
591 # this one is dhcp-based
592 function write_guest_ifcfg_natip () {
603 # use fixed GUEST_IP as specified by GUEST_HOSTNAME
604 function write_guest_ifcfg_publicip () {
609 HOSTNAME=$GUEST_HOSTNAME
619 function devel_or_test_tools () {
628 personality=$1; shift
630 lxc_root=$(lxcroot $lxc)
632 pkg_method=$(package_method $fcdistro)
634 pkgsfile=$(pl_locateDistroFile $DIRNAME $pldistro $PREINSTALLED)
636 ### install individual packages, then groups
637 # get target arch - use uname -i here (we want either x86_64 or i386)
639 lxc_arch=$(chroot ${lxc_root} $personality uname -i)
640 # on debian systems we get arch through the 'arch' command
641 [ "$lxc_arch" = "unknown" ] && lxc_arch=$(chroot ${lxc_root} $personality arch)
643 packages=$(pl_getPackages -a $lxc_arch $fcdistro $pldistro $pkgsfile)
644 groups=$(pl_getGroups -a $lxc_arch $fcdistro $pldistro $pkgsfile)
646 case "$pkg_method" in
648 [ -n "$packages" ] && chroot ${lxc_root} $personality yum -y install $packages
649 for group_plus in $groups; do
650 group=$(echo $group_plus | sed -e "s,+++, ,g")
651 chroot ${lxc_root} $personality yum -y groupinstall "$group"
653 # store current rpm list in /init-lxc.rpms in case we need to check the contents
654 chroot ${lxc_root} $personality rpm -aq > $lxc_root/init-lxc.rpms
658 if grep -iq ubuntu /vservers/$lxc/etc/lsb-release 2> /dev/null; then
659 # on ubuntu, at this point we end up with a single feed in /etc/apt/sources.list
660 # we need at least to add the 'universe' feed for python-rpm
661 ( cd /vservers/$lxc/etc/apt ; head -1 sources.list | sed -e s,main,universe, > sources.list.d/universe.list )
662 # also adding a link to updates sounds about right
663 ( cd /vservers/$lxc/etc/apt ; head -1 sources.list | sed -e 's, main,-updates main,' > sources.list.d/updates.list )
664 # tell apt about the changes
665 chroot /vservers/$lxc apt-get update
667 for package in $packages ; do
668 # container not started yet
669 #virsh -c lxc:/// lxc-enter-namespace $lxc /usr/bin/$personality /bin/bash -c "apt-get install -y $package" || :
670 chroot ${lxc_root} $personality apt-get install -y $package || :
672 ### xxx todo install groups with apt..
675 echo "unknown pkg_method $pkg_method"
682 function post_install () {
684 personality=$1; shift
685 lxc_root=$(lxcroot $lxc)
686 # setup localtime from the host
687 cp /etc/localtime $lxc_root/etc/localtime
688 sshd_disable_password_auth $lxc
690 [ -n "$NAT_MODE" ] && post_install_natip $lxc $personality || post_install_myplc $lxc $personality
691 # start the VM unless specified otherwise
692 if [ -n "$START_VM" ] ; then
693 echo Starting guest $lxc
694 virsh -c lxc:/// start $lxc
695 if [ -n "$NAT_MODE" ] ; then
698 wait_for_ssh $lxc $GUEST_IP
703 # just in case, let's stay on the safe side
704 function sshd_disable_password_auth () {
706 lxc_root=$(lxcroot $lxc)
707 sed --in-place=.password -e 's,^#\?PasswordAuthentication.*,PasswordAuthentication no,' \
708 $lxc_root/etc/ssh/sshd_config
711 function post_install_natip () {
718 personality=$1; shift
719 lxc_root=$(lxcroot $lxc)
721 ### From myplc-devel-native.spec
722 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
723 cat << EOF | chroot ${lxc_root} $personality bash -x
725 # customize root's prompt
726 /bin/cat << PROFILE > /root/.profile
727 export PS1="[$lxc] \\w # "
734 function post_install_myplc () {
740 personality=$1; shift
741 lxc_root=$(lxcroot $lxc)
743 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
744 cat << EOF | chroot ${lxc_root} $personality bash -x
746 # create /etc/sysconfig/network if missing
747 [ -f /etc/sysconfig/network ] || /bin/echo NETWORKING=yes > /etc/sysconfig/network
749 # turn off regular crond, as plc invokes plc_crond
750 /sbin/chkconfig crond off
752 # customize root's prompt
753 /bin/cat << PROFILE > /root/.profile
754 export PS1="[$lxc] \\w # "
760 ########################################
761 # workaround for broken lxc-enter-namespace
762 # 1st version was relying on virsh net-dhcp-leases
763 # however this was too fragile, would not work for fedora14 containers
764 # WARNING: this code is duplicated in lbuild-nightly.sh
765 function guest_ipv4() {
768 mac=$(virsh -c lxc:/// domiflist $lxc | egrep 'network|bridge' | awk '{print $5;}')
770 [ -z "$mac" ] && return 0
771 arp -en | grep "$mac" | awk '{print $1;}'
774 function wait_for_ssh () {
780 # if run in public_ip mode, we know the IP of the guest and it is specified here
781 [ -n "$1" ] && { guest_ip=$1; shift; }
783 #wait max 2 min for sshd to start
785 current_time=$(date +%s)
786 stop_time=$(($current_time + 120))
789 while [ "$current_time" -lt "$stop_time" ] ; do
790 echo "$counter-th attempt to reach sshd in container $lxc ..."
791 [ -z "$guest_ip" ] && guest_ip=$(guest_ipv4 $lxc)
792 [ -n "$guest_ip" ] && ssh -o "StrictHostKeyChecking no" $guest_ip 'uname -i' && {
793 success=true; echo "SSHD in container $lxc is UP on IP $guest_ip"; break ; } || :
794 counter=$(($counter+1))
796 current_time=$(date +%s)
799 # Thierry: this is fatal, let's just exit with a failure here
800 [ -z $success ] && { echo "SSHD in container $lxc could not be reached (guest_ip=$guest_ip)" ; exit 1 ; }
805 function failure () {
806 echo "$COMMAND : Bailing out"
812 echo "Usage: $COMMAND [options] lxc-name (aka build mode)"
813 echo "Usage: $COMMAND -n hostname [options] lxc-name (aka test mode)"
815 echo " This command creates a fresh lxc instance, for building, or running a test myplc"
816 echo "In its first form, spawned VM gets a private IP bridged with virbr0 over dhcp/nat"
817 echo "With the second form, spawned VM gets a public IP bridged on public bridge br0"
819 echo "Supported options"
820 echo " -n hostname - the hostname to use in container"
821 echo " -f fcdistro - for creating the root filesystem - defaults to $DEFAULT_FCDISTRO"
822 echo " -d pldistro - defaults to $DEFAULT_PLDISTRO - current support for fedoras debians ubuntus"
823 echo " -p personality - defaults to $DEFAULT_PERSONALITY"
824 echo " -r repo-url - used to populate yum.repos.d - required in test mode"
825 echo " -P pkgs_file - defines a set of extra packages to install in guest"
826 echo " by default we use devel.pkgs (build mode) or runtime.pkgs (test mode)"
827 echo " -i image - the location of the rootfs"
828 echo " -m memory - the amount of allocated memory in MB - defaults to $DEFAULT_MEMORY MB"
829 echo " -s do not start VM"
830 echo " -v be verbose"
838 #trap failure ERR INT
840 if [ "$(id -u)" != "0" ]; then
841 echo "This script should be run as 'root'"
846 while getopts "n:f:d:p:r:P:i:m:sv" opt ; do
848 n) GUEST_HOSTNAME=$OPTARG;;
849 f) fcdistro=$OPTARG;;
850 d) pldistro=$OPTARG;;
851 p) personality=$OPTARG;;
852 r) REPO_URL=$OPTARG;;
853 P) PREINSTALLED=$OPTARG;;
857 v) VERBOSE=true; set -x;;
862 shift $(($OPTIND - 1))
864 # parse fixed arguments
865 [[ -z "$@" ]] && usage
867 lxc_root=$(lxcroot $lxc)
870 almost_empty $lxc_root || \
871 { echo "container $lxc already exists in $lxc_root - exiting" ; exit 1 ; }
872 virsh -c lxc:/// domuuid $lxc >& /dev/null && \
873 { echo "container $lxc already exists in libvirt - exiting" ; exit 1 ; }
876 # if IMAGE, copy the provided rootfs to lxc_root
877 if [ -n "$IMAGE" ] ; then
878 [ ! -d "$IMAGE" ] && \
879 { echo "$IMAGE rootfs folder does not exist - exiting" ; exit 1 ; }
880 rsync -a $IMAGE/ $lxc_root/
883 # check we've exhausted the arguments
884 [[ -n "$@" ]] && usage
886 # NAT_MODE is true unless we specified a hostname
887 [ -n "$GUEST_HOSTNAME" ] || NAT_MODE=true
890 [ -z "$fcdistro" ] && fcdistro=$DEFAULT_FCDISTRO
891 [ -z "$pldistro" ] && pldistro=$DEFAULT_PLDISTRO
892 [ -z "$personality" ] && personality=$DEFAULT_PERSONALITY
893 [ -z "$MEMORY" ] && MEMORY=$DEFAULT_MEMORY
896 MEMORY=$(($MEMORY * 1024))
898 # the set of preinstalled packages - depends on mode
899 if [ -z "$PREINSTALLED" ] ; then
900 if [ -n "$NAT_MODE" ] ; then
901 PREINSTALLED=devel.pkgs
903 PREINSTALLED=runtime.pkgs
907 if [ -n "$NAT_MODE" ] ; then
908 # we can now set GUEST_HOSTNAME safely
909 [ -z "$GUEST_HOSTNAME" ] && GUEST_HOSTNAME=$(echo $lxc | sed -e 's,\.,-,g')
911 # as this command can be used in other contexts, not specifying
912 # a repo is considered a warning
913 # use -r none to get rid of this warning
914 if [ "$REPO_URL" == "none" ] ; then
916 elif [ -z "$REPO_URL" ] ; then
917 echo "WARNING -- setting up a yum repo is recommended"
922 release=$(echo $fcdistro | cut -df -f2)
924 if [ "$personality" == "linux32" ]; then
927 elif [ "$personality" == "linux64" ]; then
931 echo "Unknown personality: $personality"
934 # compute networking details for the test mode
935 # (build mode relies entirely on dhcp on the private subnet)
936 if [ -z "$NAT_MODE" ] ; then
938 #create_bridge_if_needed $PUBLIC_BRIDGE
939 lbuild-bridge.sh $PUBLIC_BRIDGE
941 GUEST_IP=$(gethostbyname $GUEST_HOSTNAME)
942 # use same NETMASK as bridge interface br0
943 masklen=$(ip addr show $PUBLIC_BRIDGE | grep -v inet6 | grep inet | awk '{print $2;}' | cut -d/ -f2)
944 NETMASK=$(masklen_to_netmask $masklen)
945 GATEWAY=$(ip route show | grep default | awk '{print $3}' | head -1)
946 VIF_HOST="vif$(echo $GUEST_HOSTNAME | cut -d. -f1)"
949 setup_lxc $lxc $fcdistro $pldistro $personality
951 # historically this command is for setting up a build or a test VM
952 # kind of patchy right now though
953 devel_or_test_tools $lxc $fcdistro $pldistro $personality
955 # container gets started here
956 post_install $lxc $personality