10 # pkgs parsing utilities
11 PATH=$(dirname $0):$PATH export PATH
15 DEFAULT_PLDISTRO=planetlab
16 DEFAULT_PERSONALITY=linux64
19 COMMAND_VBUILD="vbuild-init-lxc.sh"
20 COMMAND_MYPLC="vtest-init-lxc.sh"
22 libvirt_version="1.0.4"
23 function bridge_init () {
31 # Default Value for INTERFACE_LAN
32 INTERFACE_LAN=$(netstat -rn | grep '^0.0.0.0' | awk '{print $8;}')
35 echo "========== $COMMAND: entering start - beg"
40 echo "========== $COMMAND: entering start - end"
42 # disable netfilter calls for bridge interface (they cause panick on 2.6.35 anyway)
44 # another option would be to accept the all forward packages for
45 # bridged interface like: -A FORWARD -m physdev --physdev-is-bridged -j ACCEPT
46 sysctl net.bridge.bridge-nf-call-iptables=0
47 sysctl net.bridge.bridge-nf-call-ip6tables=0
48 sysctl net.bridge.bridge-nf-call-arptables=0
50 # take extra arg for ifname, if provided
51 [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
53 #if we have already configured the same host_box no need to do it again
54 /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null && {
55 echo "Bridge interface $INTERFACE_BRIDGE already set up - $COMMAND start exiting"
58 /sbin/ifconfig $INTERFACE_LAN &>/dev/null || {
59 echo "Cannot use interface $INTERFACE_LAN - exiting"
64 #Getting host IP/masklen
65 address=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $2;}')
66 [ -z "$address" ] && { echo "ERROR: Could not determine IP address for $INTERFACE_LAN" ; exit 1 ; }
68 broadcast=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $4;}')
69 [ -z "$broadcast" ] && echo "WARNING: Could not determine broadcast address for $INTERFACE_LAN"
71 gateway=$(netstat -rn | grep '^0.0.0.0' | awk '{print $2;}')
72 [ -z "$gateway" ] && echo "WARNING: Could not determine gateway IP"
75 echo "Creating bridge INTERFACE_BRIDGE=$INTERFACE_BRIDGE"
76 brctl addbr $INTERFACE_BRIDGE
77 #brctl stp $INTERFACE_BRIDGE yes
78 brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
79 echo "Activating promiscuous mode INTERFACE_LAN=$INTERFACE_LAN"
80 /sbin/ifconfig $INTERFACE_LAN 0.0.0.0 promisc up
82 echo "Setting bridge address=$address broadcast=$broadcast"
84 #/sbin/ifconfig $INTERFACE_BRIDGE $address broadcast $broadcast up
85 dhclient $INTERFACE_BRIDGE
88 #Reconfigure the routing table
89 echo "Configuring gateway=$gateway"
90 route add default gw $gateway
92 echo "========== $COMMAND: exiting start - beg"
95 echo "========== $COMMAND: exiting start - end"
103 function failure () {
104 echo "$COMMAND : Bailing out"
108 function cidr_notation () {
112 for i in $(seq 1 4) ; do
113 part=$(echo $netmask | cut -d. -f $i)
115 "255") cidr=$((cidr + 8));;
116 "254") cidr=$((cidr + 7));;
117 "252") cidr=$((cidr + 6));;
118 "248") cidr=$((cidr + 5));;
119 "240") cidr=$((cidr + 4));;
120 "224") cidr=$((cidr + 3));;
121 "192") cidr=$((cidr + 2));;
122 "128") cidr=$((cidr + 1));;
123 "0") cidr=$((cidr + 0));;
130 function check_yum_installed () {
132 rpm -q $package >& /dev/null || yum -y install $package
135 function check_yumgroup_installed () {
137 yum grouplist "$group" | grep -q Installed || { yum -y groupinstall "$group" ; }
140 function prepare_host() {
142 host_fcdistro="$(cat /etc/fedora-release | cut -d' ' -f3)"
143 ## check if libvirt_version is installed
144 virsh -v | grep -e $libvirt_version || { echo "$libvirt_version needs to be installed!!!" ; exit 1 ; }
145 # host_fcdistro="$(cat /etc/fedora-release | cut -d' ' -f3)"
146 # if [ ! -f /etc/yum.repos.d/libvirt.repo ] ; then
147 # touch /etc/yum.repos.d/libvirt.repo
148 # cat <<EOF > /etc/yum.repos.d/libvirt.repo
150 #name=libvirt-1.0.2-1
151 #baseurl=http://build.onelab.eu/lxc/2013.02.25--lxc$host_fcdistro/RPMS/
156 # yum --assumeno update
157 # check_yumgroup_installed "Development Tools"
158 # check_yum_installed libcap-devel
159 # check_yum_installed libvirt
160 # systemctl start libvirtd
163 #################### bride initialization
164 check_yum_installed bridge-utils
166 isInstalled=$(netstat -rn | grep '^0.0.0.0' | awk '{print $8;}')
167 if [ "$isInstalled" != "br0" ] ; then
177 function configure_fedora() {
179 # disable selinux in fedora
180 mkdir -p $rootfs_path/selinux
181 echo 0 > $rootfs_path/selinux/enforce
183 # configure the network
185 cat <<EOF > ${rootfs_path}/etc/sysconfig/network-scripts/ifcfg-$IFNAME
199 if [[ "$fcdistro" == "f18" ]] ; then
200 cat <<EOF > ${rootfs_path}/etc/hostname
204 cat <<EOF > ${rootfs_path}/etc/sysconfig/network
209 cat <<EOF > $rootfs_path/etc/hosts
210 127.0.0.1 localhost $HOSTNAME
214 dev_path="${rootfs_path}/dev"
217 mknod -m 666 ${dev_path}/null c 1 3
218 mknod -m 666 ${dev_path}/zero c 1 5
219 mknod -m 666 ${dev_path}/random c 1 8
220 mknod -m 666 ${dev_path}/urandom c 1 9
221 mkdir -m 755 ${dev_path}/pts
222 mkdir -m 1777 ${dev_path}/shm
223 mknod -m 666 ${dev_path}/tty c 5 0
224 mknod -m 666 ${dev_path}/tty0 c 4 0
225 mknod -m 666 ${dev_path}/tty1 c 4 1
226 mknod -m 666 ${dev_path}/tty2 c 4 2
227 mknod -m 666 ${dev_path}/tty3 c 4 3
228 mknod -m 666 ${dev_path}/tty4 c 4 4
229 mknod -m 600 ${dev_path}/console c 5 1
230 mknod -m 666 ${dev_path}/full c 1 7
231 mknod -m 600 ${dev_path}/initctl p
232 mknod -m 666 ${dev_path}/ptmx c 5 2
234 #echo "setting root passwd to $root_password"
235 #echo "root:$root_password" | chroot $rootfs_path chpasswd
241 function configure_fedora_init() {
243 sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.sysinit
244 sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.d/rc.sysinit
245 chroot ${rootfs_path} /sbin/chkconfig udev-post off
246 chroot ${rootfs_path} /sbin/chkconfig network on
249 # this code of course is for guests that do run on systemd
250 function configure_fedora_systemd() {
251 # so ignore if we can't find /etc/systemd at all
252 [ -d ${rootfs_path}/etc/systemd ] || return 0
253 # otherwise let's proceed
254 unlink ${rootfs_path}/etc/systemd/system/default.target
255 ln -s /lib/systemd/system/multi-user.target ${rootfs_path}/etc/systemd/system/default.target
256 touch ${rootfs_path}/etc/fstab
257 ln -s /dev/null ${rootfs_path}/etc/systemd/system/udev.service
259 # this was intended for f16 initially, in order to enable getty that otherwise would not start
260 # having a getty running is helpful only if ssh won't start though, and we see a correlation between
261 # VM's that refuse to lxc-stop and VM's that run crazy getty's
262 # so, turning getty off for now instead
263 # #dependency on a device unit fails it specially that we disabled udev
264 # sed -i 's/After=dev-%i.device/After=/' ${rootfs_path}/lib/systemd/system/getty\@.service
265 ln -s /dev/null ${rootfs_path}/etc/systemd/system/"getty@.service"
266 rm -f ${rootfs_path}/etc/systemd/system/getty.target.wants/*service || :
267 # can't seem to handle this one with systemctl
268 chroot ${rootfs_path} /sbin/chkconfig network on
271 function download_fedora() {
273 # check the mini fedora was not already downloaded
274 INSTALL_ROOT=$cache/partial
276 mkdir -p $INSTALL_ROOT
277 if [ $? -ne 0 ]; then
278 echo "Failed to create '$INSTALL_ROOT' directory"
282 # download a mini fedora into a cache
283 echo "Downloading fedora minimal ..."
284 YUM="yum --installroot $INSTALL_ROOT -y --nogpgcheck --releasever=$release"
285 PKG_LIST="yum initscripts passwd rsyslog vim-minimal dhclient chkconfig rootfiles policycoreutils openssh-server openssh-clients"
288 MIRROR_URL=http://mirror.onelab.eu/fedora/releases/$release/Everything/$arch/os
289 RELEASE_URL1="$MIRROR_URL/Packages/fedora-release-$release-1.noarch.rpm"
290 # with fedora18 the rpms are scattered by first name
291 RELEASE_URL2="$MIRROR_URL/Packages/f/fedora-release-$release-1.noarch.rpm"
292 RELEASE_TARGET=$INSTALL_ROOT/fedora-release-$release.noarch.rpm
294 for attempt in $RELEASE_URL1 $RELEASE_URL2; do
295 if curl -f $attempt -o $RELEASE_TARGET ; then
296 echo "Retrieved $attempt"
300 echo "Failed attempt $attempt"
303 [ -n "$found" ] || { echo "Could not retrieve fedora-release rpm - exiting" ; exit 1; }
305 mkdir -p $INSTALL_ROOT/var/lib/rpm
306 rpm --root $INSTALL_ROOT --initdb
307 # when installing f12 this apparently is already present, so ignore result
308 rpm --root $INSTALL_ROOT -ivh $INSTALL_ROOT/fedora-release-$release.noarch.rpm || :
309 # however f12 root images won't get created on a f18 host
310 # (the issue here is the same as the one we ran into when dealing with a vs-box)
311 # in a nutshell, in f12 the glibc-common and filesystem rpms have an apparent conflict
312 # >>> file /usr/lib/locale from install of glibc-common-2.11.2-3.x86_64 conflicts
313 # with file from package filesystem-2.4.30-2.fc12.x86_64
314 # in fact this was - of course - allowed by f12's rpm but later on a fix was made
315 # http://rpm.org/gitweb?p=rpm.git;a=commitdiff;h=cf1095648194104a81a58abead05974a5bfa3b9a
316 # So ideally if we want to be able to build f12 images from f18 we need an rpm that has
317 # this patch undone, like we have in place on our f14 boxes (our f14 boxes need a f18-like rpm)
318 echo "$YUM install $PKG_LIST"
319 $YUM install $PKG_LIST
321 if [ $? -ne 0 ]; then
322 echo "Failed to download the rootfs, aborting."
326 mv "$INSTALL_ROOT" "$cache/rootfs"
327 echo "Download complete."
333 function copy_fedora() {
335 # make a local copy of the minifedora
336 echo -n "Copying rootfs to $rootfs_path ..."
337 mkdir -p $rootfs_path
338 rsync -a $cache/rootfs/ $rootfs_path/
343 function update_fedora() {
345 YUM="yum --installroot $cache/rootfs -y --nogpgcheck"
350 function install_fedora() {
353 mkdir -p /var/lock/subsys/
356 if [ $? -ne 0 ]; then
357 echo "Cache repository is busy."
361 echo "Checking cache download in $cache/rootfs ... "
362 if [ ! -e "$cache/rootfs" ]; then
364 if [ $? -ne 0 ]; then
365 echo "Failed to download 'fedora base'"
369 echo "Cache found. Updating..."
371 if [ $? -ne 0 ]; then
372 echo "Failed to update 'fedora base', continuing with last known good cache"
374 echo "Update finished"
378 echo "Copy $cache/rootfs to $rootfs_path ... "
380 if [ $? -ne 0 ]; then
381 echo "Failed to copy rootfs"
387 ) 200>/var/lock/subsys/lxc
393 function copy_configuration() {
395 mkdir -p $config_path
396 cat <<EOF >> $config_path/config
401 lxc.rootfs = $rootfs_path
402 lxc.mount = $config_path/fstab
404 lxc.network.type = $lxc_network_type
405 lxc.network.flags = up
406 lxc.network.link = $lxc_network_link
407 lxc.network.name = $IFNAME
408 lxc.network.mtu = 1500
409 lxc.network.ipv4 = $IP/$CIDR
410 lxc.network.veth.pair = $veth_pair
412 #lxc.cgroup.devices.deny = a
414 lxc.cgroup.devices.allow = c 1:3 rwm
415 lxc.cgroup.devices.allow = c 1:5 rwm
417 lxc.cgroup.devices.allow = c 5:1 rwm
418 lxc.cgroup.devices.allow = c 5:0 rwm
419 lxc.cgroup.devices.allow = c 4:0 rwm
420 lxc.cgroup.devices.allow = c 4:1 rwm
422 lxc.cgroup.devices.allow = c 1:9 rwm
423 lxc.cgroup.devices.allow = c 1:8 rwm
424 lxc.cgroup.devices.allow = c 136:* rwm
425 lxc.cgroup.devices.allow = c 5:2 rwm
427 lxc.cgroup.devices.allow = c 254:0 rwm
428 lxc.cgroup.devices.allow = b 255:0 rwm
433 cat <<EOF > $config_path/fstab
434 proc $rootfs_path/proc proc nodev,noexec,nosuid 0 0
435 devpts $rootfs_path/dev/pts devpts defaults 0 0
436 sysfs $rootfs_path/sys sysfs defaults 0 0
438 if [ $? -ne 0 ]; then
439 echo "Failed to add configuration"
449 # overwrite lxc's internal yum config
450 function configure_yum_in_lxc () {
459 echo "Initializing yum.repos.d in $lxc"
460 rm -f $rootfs_path/etc/yum.repos.d/*
462 cat > $rootfs_path/etc/yum.repos.d/building.repo <<EOF
464 name=Fedora $release - \$basearch
465 baseurl=http://mirror.onelab.eu/fedora/releases/$release/Everything/\$basearch/os/
469 gpgkey=http://mirror.onelab.eu/keys/RPM-GPG-KEY-fedora-$release-primary
472 name=Fedora $release - \$basearch - Updates
473 baseurl=http://mirror.onelab.eu/fedora/updates/$release/\$basearch/
477 gpgkey=http://mirror.onelab.eu/keys/RPM-GPG-KEY-fedora-$release-primary
480 # for using vtest-init-lxc.sh as a general-purpose lxc creation wrapper
481 # just mention 'none' as the repo url
482 if [ -n "$MYPLC_MODE" -a "$REPO_URL" != "none" ] ; then
483 if [ ! -d $rootfs_path/etc/yum.repos.d ] ; then
484 echo "WARNING : cannot create myplc repo"
486 # exclude kernel from fedora repos
487 yumexclude=$(pl_plcyumexclude $fcdistro $pldistro $DIRNAME)
488 for repo in $rootfs_path/etc/yum.repos.d/* ; do
489 [ -f $repo ] && yumconf_exclude $repo "exclude=$yumexclude"
491 # the build repo is not signed at this stage
492 cat > $rootfs_path/etc/yum.repos.d/myplc.repo <<EOF
503 # return yum or debootstrap
504 function package_method () {
507 f[0-9]*|centos[0-9]*|sl[0-9]*) echo yum ;;
508 lenny|etch) echo debootstrap ;;
509 *) echo Unknown distro $fcdistro ;;
513 # return arch from debian distro and personality
514 function canonical_arch () {
515 personality=$1; shift
517 case $(package_method $fcdistro) in
519 case $personality in *32) echo i386 ;; *64) echo x86_64 ;; *) echo Unknown-arch-1 ;; esac ;;
521 case $personality in *32) echo i386 ;; *64) echo amd64 ;; *) echo Unknown-arch-2 ;; esac ;;
523 echo Unknown-arch-3 ;;
527 # the new test framework creates /timestamp in /vservers/<name> *before* populating it
528 function almost_empty () {
530 # non existing is fine
531 [ ! -d $dir ] && return 0;
532 # need to have at most one file
533 count=$(cd $dir; ls | wc -l); [ $count -le 1 ];
536 function setup_lxc() {
540 #trap failure ERR INT
545 personality=$1; shift
547 # create lxc container
549 if [ $? -ne 0 ]; then
550 echo "failed write configuration file"
555 if [ $? -ne 0 ]; then
556 echo "failed to install fedora"
561 if [ $? -ne 0 ]; then
562 echo "failed to configure fedora for a container"
566 type /bin/systemd >/dev/null 2>&1
567 if [ $? -ne 0 ]; then
568 configure_fedora_init
570 configure_fedora_systemd
574 mkdir $rootfs_path/cgroup
577 cp /etc/resolv.conf $rootfs_path/etc/resolv.conf
578 # and /etc/hosts for at least localhost
579 [ -f $rootfs_path/etc/hosts ] || echo "127.0.0.1 localhost localhost.localdomain" > $rootfs_path/etc/hosts
582 mkdir $rootfs_path/root/.ssh
583 cat /root/.ssh/id_rsa.pub >> $rootfs_path/root/.ssh/authorized_keys
585 # copy libvirt xml template
586 veth_pair="i$(echo $HOSTNAME | cut -d. -f1)"
588 cat > $config_path/$tmpl_name<<EOF
591 <memory>524288</memory>
594 <init>/sbin/init</init>
600 <clock offset='utc'/>
601 <on_poweroff>destroy</on_poweroff>
602 <on_reboot>restart</on_reboot>
603 <on_crash>destroy</on_crash>
605 <emulator>/usr/libexec/libvirt_lxc</emulator>
606 <filesystem type='mount'>
607 <source dir='$rootfs_path'/>
610 <interface type="bridge">
611 <source bridge="br0"/>
612 <target dev='$veth_pair'/>
614 <console type='pty' />
617 <name>host-bridge</name>
618 <forward mode="bridge"/>
624 # define lxc container for libvirt
625 virsh -c lxc:// define $config_path/$tmpl_name
628 chroot $rootfs_path /bin/rpm --rebuilddb
630 configure_yum_in_lxc $lxc $fcdistro $pldistro
635 function devel_or_vtest_tools () {
644 personality=$1; shift
646 pkg_method=$(package_method $fcdistro)
648 # check for .pkgs file based on pldistro
649 if [ -n "$VBUILD_MODE" ] ; then
654 pkgsfile=$(pl_locateDistroFile $DIRNAME $pldistro $pkgsname)
656 ### install individual packages, then groups
657 # get target arch - use uname -i here (we want either x86_64 or i386)
659 lxc_arch=$(chroot $rootfs_path /bin/uname -i)
660 # on debian systems we get arch through the 'arch' command
661 [ "$lxc_arch" = "unknown" ] && lxc_arch=$(chroot $rootfs_path /bin/arch)
663 packages=$(pl_getPackages -a $lxc_arch $fcdistro $pldistro $pkgsfile)
664 groups=$(pl_getGroups -a $lxc_arch $fcdistro $pldistro $pkgsfile)
666 case "$pkg_method" in
668 [ -n "$packages" ] && chroot $rootfs_path /usr/bin/yum -y install $packages
669 for group_plus in $groups; do
670 group=$(echo $group_plus | sed -e "s,+++, ,g")
671 chroot $rootfs_path /usr/bin/yum -y groupinstall "$group"
673 # store current rpm list in /init-lxc.rpms in case we need to check the contents
674 chroot $rootfs_path /bin/rpm -aq > $rootfs_path/init-lxc.rpms
677 chroot $rootfs_path /usr/bin/apt-get update
678 for package in $packages ; do
679 chroot $rootfs_path /usr/bin/apt-get install -y $package
681 ### xxx todo install groups with apt..
684 echo "unknown pkg_method $pkg_method"
691 function post_install () {
692 if [ -n "$VBUILD_MODE" ] ; then
693 post_install_vbuild "$@"
695 post_install_myplc "$@"
697 # setup localtime from the host
699 cp /etc/localtime $rootfs_path/etc/localtime
702 function post_install_vbuild () {
709 personality=$1; shift
711 ### From myplc-devel-native.spec
712 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
713 cat << EOF | chroot $rootfs_path /bin/bash -x
714 # set up /dev/loop* in lxc
715 for i in \$(seq 0 255) ; do
716 /bin/mknod -m 640 /dev/loop\$i b 7 \$i
719 # create symlink for /dev/fd
720 [ ! -e "/dev/fd" ] && /bin/ln -s /proc/self/fd /dev/fd
722 # modify /etc/rpm/macros to not use /sbin/new-kernel-pkg
723 /bin/sed -i 's,/sbin/new-kernel-pkg:,,' /etc/rpm/macros
724 if [ -h "/sbin/new-kernel-pkg" ] ; then
725 filename=\$(/bin/readlink -f /sbin/new-kernel-pkg)
726 if [ "\$filename" == "/sbin/true" ] ; then
727 /bin/echo "WARNING: /sbin/new-kernel-pkg symlinked to /sbin/true"
728 /bin/echo "\tmost likely /etc/rpm/macros has /sbin/new-kernel-pkg declared in _netsharedpath."
729 /bin/echo "\tPlease remove /sbin/new-kernel-pkg from _netsharedpath and reintall mkinitrd."
734 # customize root's prompt
735 /bin/cat << PROFILE > /root/.profile
736 export PS1="[$lxc] \\w # "
742 # add a "build" user to the system
743 builduser=\$(grep "^build:" /etc/passwd | wc -l)
744 if [ \$builduser -eq 0 ] ; then
745 groupadd -o -g \$gid build;
746 useradd -o -c 'Automated Build' -u \$uid -g \$gid -n -M -s /bin/bash build;
749 # Allow build user to build certain RPMs as root
750 if [ -f /etc/sudoers ] ; then
751 buildsudo=\$(grep "^build.*ALL=(ALL).*NOPASSWD:.*ALL" /etc/sudoers | wc -l)
752 if [ \$buildsudo -eq 0 ] ; then
753 echo "build ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
755 sed -i 's,^Defaults.*requiretty,#Defaults requiretty,' /etc/sudoers
762 function post_install_myplc () {
768 personality=$1; shift
770 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
771 cat << EOF | chroot $rootfs_path /bin/bash -x
773 # create /etc/sysconfig/network if missing
774 [ -f /etc/sysconfig/network ] || /bin/echo NETWORKING=yes > /etc/sysconfig/network
776 # create symlink for /dev/fd
777 [ ! -e "/dev/fd" ] && /bin/ln -s /proc/self/fd /dev/fd
779 # turn off regular crond, as plc invokes plc_crond
780 /sbin/chkconfig crond off
782 # take care of loginuid in /etc/pam.d
783 /bin/sed -i "s,#*\(.*loginuid.*\),#\1," /etc/pam.d/*
785 # customize root's prompt
786 /bin/cat << PROFILE > /root/.profile
787 export PS1="[$lxc] \\w # "
793 function start_lxc() {
797 #trap failure ERR INT
801 virsh -c lxc:// start $lxc
803 echo $IP is up, waiting for ssh...
805 #wait max 5 min for sshd to start
807 stop_time=$(($(date +%s) + 300))
808 current_time=$(date +%s)
811 while [ "$current_time" -lt "$stop_time" ] ; do
812 echo "$counter-th attempt to reach sshd in container $lxc ..."
813 ssh -o "StrictHostKeyChecking no" $IP 'uname -i' && { ssh_up=true; echo "SSHD in container $lxc is UP"; break ; } || :
815 current_time=$(($current_time + 10))
816 counter=$(($counter+1))
819 # Thierry: this is fatal, let's just exit with a failure here
820 [ -z $ssh_up ] && { echo "SSHD in container $lxc is not running" ; exit 1 ; }
827 echo "Usage: $COMMAND_VBUILD [options] lxc-name"
828 echo "Usage: $COMMAND_MYPLC [options] lxc-name repo-url [ -- lxc-options ]"
830 echo " This command creates a fresh lxc instance, for building, or running, myplc"
831 echo "Supported options"
832 echo " -f fcdistro - for creating the root filesystem - defaults to $DEFAULT_FCDISTRO"
833 echo " -d pldistro - defaults to $DEFAULT_PLDISTRO"
834 echo " -p personality - defaults to $DEFAULT_PERSONALITY"
835 echo " -i ifname: determines ip and netmask attached to ifname, and passes it to the lxc"
836 echo "-- lxc-options"
837 echo " --netdev : interface to be defined inside lxc"
838 echo " --interface : IP to be defined for the lxc"
839 echo " --hostname : Hostname to be defined for the lxc"
840 echo "With $COMMAND_MYPLC you can give 'none' as the URL, in which case"
841 echo " myplc.repo does not get created"
849 #trap failure ERR INT
864 while getopts "f:d:p:i:" opt ; do
866 f) fcdistro=$OPTARG;;
867 d) pldistro=$OPTARG;;
868 p) personality=$OPTARG;;
874 shift $(($OPTIND - 1))
876 # parse fixed arguments
877 [[ -z "$@" ]] && usage
879 if [ -n "$MYPLC_MODE" ] ; then
880 [[ -z "$@" ]] && usage
884 # parse vserver options
885 if [[ -n "$@" ]] ; then
886 if [ "$1" == "--" ] ; then
894 eval set -- "$LXC_OPTIONS"
899 --netdev) IFNAME=$2; shift 2;;
900 --interface) IP=$2; shift 2;;
901 --hostname) HOSTNAME=$2; shift 2;;
907 if [ -n "$VBUILD_MODE" ] ; then
908 [ -z "$IFNAME" ] && IFNAME=$DEFAULT_IFNAME
909 [ -z "$HOSTNAME" ] && HOSTNAME=$lxc
912 [ -z "$fcdistro" ] && fcdistro=$DEFAULT_FCDISTRO
913 [ -z "$pldistro" ] && pldistro=$DEFAULT_PLDISTRO
914 [ -z "$personality" ] && personality=$DEFAULT_PERSONALITY
916 release=$(echo $fcdistro | cut -df -f2)
918 if [ "$personality" == "linux32" ]; then
921 elif [ "$personality" == "linux64" ]; then
925 echo "Unknown personality: $personality"
928 # need lxc installed before we can run lxc-ls
929 # need bridge installed
932 if [ -n "$VBUILD_MODE" ] ; then
934 # Bridge IP affectation
935 x=$(echo $personality | cut -dx -f2)
936 y=$(echo $fcdistro | cut -df -f2)
940 NETMASK="255.255.255.0"
941 GATEWAY="192.168.122.1"
943 lxc_network_type=veth
944 lxc_network_link=virbr0
946 echo "the IP address of container $lxc is $IP "
948 [[ -z "$REPO_URL" ]] && usage
949 [[ -z "$IP" ]] && usage
951 NETMASK=$(ifconfig br0 | grep 'inet ' | awk '{print $4}' | sed -e 's/.*://')
952 GATEWAY=$(route -n | grep 'UG' | awk '{print $2}')
953 [[ -z "$HOSTNAME" ]] && usage
954 lxc_network_type=veth
956 veth_pair="i$(echo $HOSTNAME | cut -d. -f1)"
959 CIDR=$(cidr_notation $NETMASK)
962 if [ "$(id -u)" != "0" ]; then
963 echo "This script should be run as 'root'"
968 [ ! -d $path ] && mkdir $path
969 rootfs_path=$path/$lxc/rootfs
970 config_path=$path/$lxc
971 cache_base=/var/cache/lxc/fedora/$arch
972 cache=$cache_base/$release
975 # check whether the rootfs directory is created to know if the container exists
976 # bacause /var/lib/lxc/$lxc is already created while putting $lxc.timestamp
977 [ -d $rootfs_path ] && { echo "container $lxc already exists - exiting" ; exit 1 ; }
979 setup_lxc $lxc $fcdistro $pldistro $personality
981 devel_or_vtest_tools $lxc $fcdistro $pldistro $personality
983 post_install $lxc $personality