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 # don't mount devpts, for pete's sake
246 sed -i 's/^.*dev.pts.*$/#\0/' ${rootfs_path}/etc/rc.sysinit
247 sed -i 's/^.*dev.pts.*$/#\0/' ${rootfs_path}/etc/rc.d/rc.sysinit
248 chroot ${rootfs_path} /sbin/chkconfig udev-post off
249 chroot ${rootfs_path} /sbin/chkconfig network on
252 # this code of course is for guests that do run on systemd
253 function configure_fedora_systemd() {
254 # so ignore if we can't find /etc/systemd at all
255 [ -d ${rootfs_path}/etc/systemd ] || return 0
256 # otherwise let's proceed
257 unlink ${rootfs_path}/etc/systemd/system/default.target
258 ln -s /lib/systemd/system/multi-user.target ${rootfs_path}/etc/systemd/system/default.target
259 touch ${rootfs_path}/etc/fstab
260 ln -s /dev/null ${rootfs_path}/etc/systemd/system/udev.service
262 # this was intended for f16 initially, in order to enable getty that otherwise would not start
263 # having a getty running is helpful only if ssh won't start though, and we see a correlation between
264 # VM's that refuse to lxc-stop and VM's that run crazy getty's
265 # so, turning getty off for now instead
266 # #dependency on a device unit fails it specially that we disabled udev
267 # sed -i 's/After=dev-%i.device/After=/' ${rootfs_path}/lib/systemd/system/getty\@.service
268 ln -s /dev/null ${rootfs_path}/etc/systemd/system/"getty@.service"
269 rm -f ${rootfs_path}/etc/systemd/system/getty.target.wants/*service || :
270 # can't seem to handle this one with systemctl
271 chroot ${rootfs_path} /sbin/chkconfig network on
274 function download_fedora() {
276 # check the mini fedora was not already downloaded
277 INSTALL_ROOT=$cache/partial
279 mkdir -p $INSTALL_ROOT
280 if [ $? -ne 0 ]; then
281 echo "Failed to create '$INSTALL_ROOT' directory"
285 mkdir -p $INSTALL_ROOT/etc/yum.repos.d
286 mkdir -p $INSTALL_ROOT/dev
287 mknod -m 0444 $INSTALL_ROOT/dev/random c 1 8
288 mknod -m 0444 $INSTALL_ROOT/dev/urandom c 1 9
290 # copy yum config and repo files
291 cp /etc/yum.conf $INSTALL_ROOT/etc/
292 cp /etc/yum.repos.d/fedora* $INSTALL_ROOT/etc/yum.repos.d/
294 # append fedora repo files with desired $release and $basearch
295 for f in $INSTALL_ROOT/etc/yum.repos.d/*
297 sed -i "s/\$basearch/$arch/g; s/\$releasever/$release/g;" $f
301 # PKG_LIST="yum initscripts passwd rsyslog vim-minimal dhclient chkconfig rootfiles policycoreutils openssh-server openssh-clients"
302 # yum --installroot=$INSTALL_ROOT --nogpgcheck install fedora-release
303 # yum --installroot=$INSTALL_ROOT --nogpgcheck install $PKG_LIST
307 MIRROR_URL=http://mirror.onelab.eu/fedora/releases/$release/Everything/$arch/os
308 RELEASE_URL1="$MIRROR_URL/Packages/fedora-release-$release-1.noarch.rpm"
309 # with fedora18 the rpms are scattered by first name
310 RELEASE_URL2="$MIRROR_URL/Packages/f/fedora-release-$release-1.noarch.rpm"
311 RELEASE_TARGET=$INSTALL_ROOT/fedora-release-$release.noarch.rpm
313 for attempt in $RELEASE_URL1 $RELEASE_URL2; do
314 if curl -f $attempt -o $RELEASE_TARGET ; then
315 echo "Retrieved $attempt"
319 echo "Failed attempt $attempt"
322 [ -n "$found" ] || { echo "Could not retrieve fedora-release rpm - exiting" ; exit 1; }
324 mkdir -p $INSTALL_ROOT/var/lib/rpm
325 rpm --root $INSTALL_ROOT --initdb
326 # when installing f12 this apparently is already present, so ignore result
327 rpm --root $INSTALL_ROOT -ivh $INSTALL_ROOT/fedora-release-$release.noarch.rpm || :
328 # however f12 root images won't get created on a f18 host
329 # (the issue here is the same as the one we ran into when dealing with a vs-box)
330 # in a nutshell, in f12 the glibc-common and filesystem rpms have an apparent conflict
331 # >>> file /usr/lib/locale from install of glibc-common-2.11.2-3.x86_64 conflicts
332 # with file from package filesystem-2.4.30-2.fc12.x86_64
333 # in fact this was - of course - allowed by f12's rpm but later on a fix was made
334 # http://rpm.org/gitweb?p=rpm.git;a=commitdiff;h=cf1095648194104a81a58abead05974a5bfa3b9a
335 # So ideally if we want to be able to build f12 images from f18 we need an rpm that has
336 # this patch undone, like we have in place on our f14 boxes (our f14 boxes need a f18-like rpm)
338 YUM="yum --installroot=$INSTALL_ROOT --nogpgcheck -y"
339 PKG_LIST="yum initscripts passwd rsyslog vim-minimal dhclient chkconfig rootfiles policycoreutils openssh-server openssh-clients"
340 echo "$YUM install $PKG_LIST"
341 $YUM install $PKG_LIST
343 if [ $? -ne 0 ]; then
344 echo "Failed to download the rootfs, aborting."
348 mv "$INSTALL_ROOT" "$cache/rootfs"
349 echo "Download complete."
355 function copy_fedora() {
357 # make a local copy of the minifedora
358 echo -n "Copying rootfs to $rootfs_path ..."
359 mkdir -p $rootfs_path
360 rsync -a $cache/rootfs/ $rootfs_path/
365 function update_fedora() {
367 YUM="yum --installroot $cache/rootfs -y --nogpgcheck"
372 function install_fedora() {
375 mkdir -p /var/lock/subsys/
378 if [ $? -ne 0 ]; then
379 echo "Cache repository is busy."
383 echo "Checking cache download in $cache/rootfs ... "
384 if [ ! -e "$cache/rootfs" ]; then
386 if [ $? -ne 0 ]; then
387 echo "Failed to download 'fedora base'"
391 echo "Cache found. Updating..."
393 if [ $? -ne 0 ]; then
394 echo "Failed to update 'fedora base', continuing with last known good cache"
396 echo "Update finished"
400 echo "Copy $cache/rootfs to $rootfs_path ... "
402 if [ $? -ne 0 ]; then
403 echo "Failed to copy rootfs"
409 ) 200>/var/lock/subsys/lxc
415 function copy_configuration() {
417 mkdir -p $config_path
418 cat <<EOF >> $config_path/config
423 lxc.rootfs = $rootfs_path
424 lxc.mount = $config_path/fstab
426 lxc.network.type = $lxc_network_type
427 lxc.network.flags = up
428 lxc.network.link = $lxc_network_link
429 lxc.network.name = $IFNAME
430 lxc.network.mtu = 1500
431 lxc.network.ipv4 = $IP/$CIDR
432 lxc.network.veth.pair = $veth_pair
434 #lxc.cgroup.devices.deny = a
436 lxc.cgroup.devices.allow = c 1:3 rwm
437 lxc.cgroup.devices.allow = c 1:5 rwm
439 lxc.cgroup.devices.allow = c 5:1 rwm
440 lxc.cgroup.devices.allow = c 5:0 rwm
441 lxc.cgroup.devices.allow = c 4:0 rwm
442 lxc.cgroup.devices.allow = c 4:1 rwm
444 lxc.cgroup.devices.allow = c 1:9 rwm
445 lxc.cgroup.devices.allow = c 1:8 rwm
446 lxc.cgroup.devices.allow = c 136:* rwm
447 lxc.cgroup.devices.allow = c 5:2 rwm
449 lxc.cgroup.devices.allow = c 254:0 rwm
450 lxc.cgroup.devices.allow = b 255:0 rwm
455 cat <<EOF > $config_path/fstab
456 proc $rootfs_path/proc proc nodev,noexec,nosuid 0 0
457 devpts $rootfs_path/dev/pts devpts defaults 0 0
458 sysfs $rootfs_path/sys sysfs defaults 0 0
460 if [ $? -ne 0 ]; then
461 echo "Failed to add configuration"
471 # overwrite lxc's internal yum config
472 function configure_yum_in_lxc () {
481 echo "Initializing yum.repos.d in $lxc"
482 rm -f $rootfs_path/etc/yum.repos.d/*
484 cat > $rootfs_path/etc/yum.repos.d/building.repo <<EOF
486 name=Fedora $release - $arch
487 baseurl=http://mirror.onelab.eu/fedora/releases/$release/Everything/$arch/os/
491 gpgkey=http://mirror.onelab.eu/keys/RPM-GPG-KEY-fedora-$release-primary
494 name=Fedora $release - $arch - Updates
495 baseurl=http://mirror.onelab.eu/fedora/updates/$release/$arch/
499 gpgkey=http://mirror.onelab.eu/keys/RPM-GPG-KEY-fedora-$release-primary
502 # for using vtest-init-lxc.sh as a general-purpose lxc creation wrapper
503 # just mention 'none' as the repo url
504 if [ -n "$MYPLC_MODE" -a "$REPO_URL" != "none" ] ; then
505 if [ ! -d $rootfs_path/etc/yum.repos.d ] ; then
506 echo "WARNING : cannot create myplc repo"
508 # exclude kernel from fedora repos
509 yumexclude=$(pl_plcyumexclude $fcdistro $pldistro $DIRNAME)
510 for repo in $rootfs_path/etc/yum.repos.d/* ; do
511 [ -f $repo ] && yumconf_exclude $repo "exclude=$yumexclude"
513 # the build repo is not signed at this stage
514 cat > $rootfs_path/etc/yum.repos.d/myplc.repo <<EOF
525 # return yum or debootstrap
526 function package_method () {
529 f[0-9]*|centos[0-9]*|sl[0-9]*) echo yum ;;
530 lenny|etch) echo debootstrap ;;
531 *) echo Unknown distro $fcdistro ;;
535 # return arch from debian distro and personality
536 function canonical_arch () {
537 personality=$1; shift
539 case $(package_method $fcdistro) in
541 case $personality in *32) echo i386 ;; *64) echo x86_64 ;; *) echo Unknown-arch-1 ;; esac ;;
543 case $personality in *32) echo i386 ;; *64) echo amd64 ;; *) echo Unknown-arch-2 ;; esac ;;
545 echo Unknown-arch-3 ;;
549 # the new test framework creates /timestamp in /vservers/<name> *before* populating it
550 function almost_empty () {
552 # non existing is fine
553 [ ! -d $dir ] && return 0;
554 # need to have at most one file
555 count=$(cd $dir; ls | wc -l); [ $count -le 1 ];
558 function setup_lxc() {
562 #trap failure ERR INT
567 personality=$1; shift
569 # create lxc container
571 if [ $? -ne 0 ]; then
572 echo "failed write configuration file"
577 if [ $? -ne 0 ]; then
578 echo "failed to install fedora"
583 if [ $? -ne 0 ]; then
584 echo "failed to configure fedora for a container"
588 if [ "$(echo $fcdistro | cut -d"f" -f2)" -le "14" ]; then
589 configure_fedora_init
591 configure_fedora_systemd
595 mkdir $rootfs_path/cgroup
598 cp /etc/resolv.conf $rootfs_path/etc/resolv.conf
599 # and /etc/hosts for at least localhost
600 [ -f $rootfs_path/etc/hosts ] || echo "127.0.0.1 localhost localhost.localdomain" > $rootfs_path/etc/hosts
603 mkdir $rootfs_path/root/.ssh
604 cat /root/.ssh/id_rsa.pub >> $rootfs_path/root/.ssh/authorized_keys
606 # copy libvirt xml template
607 veth_pair="i$(echo $HOSTNAME | cut -d. -f1)"
609 cat > $config_path/$tmpl_name<<EOF
612 <memory>524288</memory>
614 <type arch='$arch2'>exe</type>
615 <init>/sbin/init</init>
621 <clock offset='utc'/>
622 <on_poweroff>destroy</on_poweroff>
623 <on_reboot>restart</on_reboot>
624 <on_crash>destroy</on_crash>
626 <emulator>/usr/libexec/libvirt_lxc</emulator>
627 <filesystem type='mount'>
628 <source dir='$rootfs_path'/>
631 <interface type="bridge">
632 <source bridge="br0"/>
633 <target dev='$veth_pair'/>
635 <console type='pty' />
638 <name>host-bridge</name>
639 <forward mode="bridge"/>
645 # define lxc container for libvirt
646 virsh -c lxc:// define $config_path/$tmpl_name
649 chroot $rootfs_path /bin/rpm --rebuilddb
651 configure_yum_in_lxc $lxc $fcdistro $pldistro
656 function devel_or_vtest_tools () {
665 personality=$1; shift
667 pkg_method=$(package_method $fcdistro)
669 # check for .pkgs file based on pldistro
670 if [ -n "$VBUILD_MODE" ] ; then
675 pkgsfile=$(pl_locateDistroFile $DIRNAME $pldistro $pkgsname)
677 ### install individual packages, then groups
678 # get target arch - use uname -i here (we want either x86_64 or i386)
680 lxc_arch=$(chroot $rootfs_path /bin/uname -i)
681 # on debian systems we get arch through the 'arch' command
682 [ "$lxc_arch" = "unknown" ] && lxc_arch=$(chroot $rootfs_path /bin/arch)
684 packages=$(pl_getPackages -a $lxc_arch $fcdistro $pldistro $pkgsfile)
685 groups=$(pl_getGroups -a $lxc_arch $fcdistro $pldistro $pkgsfile)
687 case "$pkg_method" in
689 [ -n "$packages" ] && chroot $rootfs_path /usr/bin/yum -y install $packages
690 for group_plus in $groups; do
691 group=$(echo $group_plus | sed -e "s,+++, ,g")
692 chroot $rootfs_path /usr/bin/yum -y groupinstall "$group"
694 # store current rpm list in /init-lxc.rpms in case we need to check the contents
695 chroot $rootfs_path /bin/rpm -aq > $rootfs_path/init-lxc.rpms
698 chroot $rootfs_path /usr/bin/apt-get update
699 for package in $packages ; do
700 chroot $rootfs_path /usr/bin/apt-get install -y $package
702 ### xxx todo install groups with apt..
705 echo "unknown pkg_method $pkg_method"
712 function post_install () {
713 if [ -n "$VBUILD_MODE" ] ; then
714 post_install_vbuild "$@"
716 post_install_myplc "$@"
718 # setup localtime from the host
720 cp /etc/localtime $rootfs_path/etc/localtime
723 function post_install_vbuild () {
730 personality=$1; shift
732 ### From myplc-devel-native.spec
733 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
734 cat << EOF | chroot $rootfs_path /bin/bash -x
735 # set up /dev/loop* in lxc
736 for i in \$(seq 0 255) ; do
737 /bin/mknod -m 640 /dev/loop\$i b 7 \$i
740 # create symlink for /dev/fd
741 [ ! -e "/dev/fd" ] && /bin/ln -s /proc/self/fd /dev/fd
743 # modify /etc/rpm/macros to not use /sbin/new-kernel-pkg
744 /bin/sed -i 's,/sbin/new-kernel-pkg:,,' /etc/rpm/macros
745 if [ -h "/sbin/new-kernel-pkg" ] ; then
746 filename=\$(/bin/readlink -f /sbin/new-kernel-pkg)
747 if [ "\$filename" == "/sbin/true" ] ; then
748 /bin/echo "WARNING: /sbin/new-kernel-pkg symlinked to /sbin/true"
749 /bin/echo "\tmost likely /etc/rpm/macros has /sbin/new-kernel-pkg declared in _netsharedpath."
750 /bin/echo "\tPlease remove /sbin/new-kernel-pkg from _netsharedpath and reintall mkinitrd."
755 # customize root's prompt
756 /bin/cat << PROFILE > /root/.profile
757 export PS1="[$lxc] \\w # "
763 # add a "build" user to the system
764 builduser=\$(grep "^build:" /etc/passwd | wc -l)
765 if [ \$builduser -eq 0 ] ; then
766 groupadd -o -g \$gid build;
767 useradd -o -c 'Automated Build' -u \$uid -g \$gid -n -M -s /bin/bash build;
770 # Allow build user to build certain RPMs as root
771 if [ -f /etc/sudoers ] ; then
772 buildsudo=\$(grep "^build.*ALL=(ALL).*NOPASSWD:.*ALL" /etc/sudoers | wc -l)
773 if [ \$buildsudo -eq 0 ] ; then
774 echo "build ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
776 sed -i 's,^Defaults.*requiretty,#Defaults requiretty,' /etc/sudoers
783 function post_install_myplc () {
789 personality=$1; shift
791 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
792 cat << EOF | chroot $rootfs_path /bin/bash -x
794 # create /etc/sysconfig/network if missing
795 [ -f /etc/sysconfig/network ] || /bin/echo NETWORKING=yes > /etc/sysconfig/network
797 # create symlink for /dev/fd
798 [ ! -e "/dev/fd" ] && /bin/ln -s /proc/self/fd /dev/fd
800 # turn off regular crond, as plc invokes plc_crond
801 /sbin/chkconfig crond off
803 # take care of loginuid in /etc/pam.d
804 /bin/sed -i "s,#*\(.*loginuid.*\),#\1," /etc/pam.d/*
806 # customize root's prompt
807 /bin/cat << PROFILE > /root/.profile
808 export PS1="[$lxc] \\w # "
814 function start_lxc() {
818 #trap failure ERR INT
822 virsh -c lxc:// start $lxc
824 echo $IP is up, waiting for ssh...
826 #wait max 5 min for sshd to start
828 stop_time=$(($(date +%s) + 300))
829 current_time=$(date +%s)
832 while [ "$current_time" -lt "$stop_time" ] ; do
833 echo "$counter-th attempt to reach sshd in container $lxc ..."
834 ssh -o "StrictHostKeyChecking no" $IP 'uname -i' && { ssh_up=true; echo "SSHD in container $lxc is UP"; break ; } || :
836 current_time=$(($current_time + 10))
837 counter=$(($counter+1))
840 # Thierry: this is fatal, let's just exit with a failure here
841 [ -z $ssh_up ] && { echo "SSHD in container $lxc is not running" ; exit 1 ; }
848 echo "Usage: $COMMAND_VBUILD [options] lxc-name"
849 echo "Usage: $COMMAND_MYPLC [options] lxc-name repo-url [ -- lxc-options ]"
851 echo " This command creates a fresh lxc instance, for building, or running, myplc"
852 echo "Supported options"
853 echo " -f fcdistro - for creating the root filesystem - defaults to $DEFAULT_FCDISTRO"
854 echo " -d pldistro - defaults to $DEFAULT_PLDISTRO"
855 echo " -p personality - defaults to $DEFAULT_PERSONALITY"
856 echo " -i ifname: determines ip and netmask attached to ifname, and passes it to the lxc"
857 echo "-- lxc-options"
858 echo " --netdev : interface to be defined inside lxc"
859 echo " --interface : IP to be defined for the lxc"
860 echo " --hostname : Hostname to be defined for the lxc"
861 echo "With $COMMAND_MYPLC you can give 'none' as the URL, in which case"
862 echo " myplc.repo does not get created"
870 #trap failure ERR INT
885 while getopts "f:d:p:i:" opt ; do
887 f) fcdistro=$OPTARG;;
888 d) pldistro=$OPTARG;;
889 p) personality=$OPTARG;;
895 shift $(($OPTIND - 1))
897 # parse fixed arguments
898 [[ -z "$@" ]] && usage
900 if [ -n "$MYPLC_MODE" ] ; then
901 [[ -z "$@" ]] && usage
905 # parse vserver options
906 if [[ -n "$@" ]] ; then
907 if [ "$1" == "--" ] ; then
915 eval set -- "$LXC_OPTIONS"
920 --netdev) IFNAME=$2; shift 2;;
921 --interface) IP=$2; shift 2;;
922 --hostname) HOSTNAME=$2; shift 2;;
928 if [ -n "$VBUILD_MODE" ] ; then
929 [ -z "$IFNAME" ] && IFNAME=$DEFAULT_IFNAME
930 [ -z "$HOSTNAME" ] && HOSTNAME=$lxc
933 [ -z "$fcdistro" ] && fcdistro=$DEFAULT_FCDISTRO
934 [ -z "$pldistro" ] && pldistro=$DEFAULT_PLDISTRO
935 [ -z "$personality" ] && personality=$DEFAULT_PERSONALITY
937 release=$(echo $fcdistro | cut -df -f2)
939 if [ "$personality" == "linux32" ]; then
942 elif [ "$personality" == "linux64" ]; then
946 echo "Unknown personality: $personality"
949 # need lxc installed before we can run lxc-ls
950 # need bridge installed
953 if [ -n "$VBUILD_MODE" ] ; then
955 # Bridge IP affectation
956 x=$(echo $personality | cut -dx -f2)
957 y=$(echo $fcdistro | cut -df -f2)
961 NETMASK="255.255.255.0"
962 GATEWAY="192.168.122.1"
964 lxc_network_type=veth
965 lxc_network_link=virbr0
967 echo "the IP address of container $lxc is $IP "
969 [[ -z "$REPO_URL" ]] && usage
970 [[ -z "$IP" ]] && usage
972 NETMASK=$(ifconfig br0 | grep 'inet ' | awk '{print $4}' | sed -e 's/.*://')
973 GATEWAY=$(route -n | grep 'UG' | awk '{print $2}')
974 [[ -z "$HOSTNAME" ]] && usage
975 lxc_network_type=veth
977 veth_pair="i$(echo $HOSTNAME | cut -d. -f1)"
980 CIDR=$(cidr_notation $NETMASK)
983 if [ "$(id -u)" != "0" ]; then
984 echo "This script should be run as 'root'"
989 [ ! -d $path ] && mkdir $path
990 rootfs_path=$path/$lxc/rootfs
991 config_path=$path/$lxc
992 cache_base=/var/cache/lxc/fedora/$arch
993 cache=$cache_base/$release
996 # check whether the rootfs directory is created to know if the container exists
997 # bacause /var/lib/lxc/$lxc is already created while putting $lxc.timestamp
998 [ -d $rootfs_path ] && { echo "container $lxc already exists - exiting" ; exit 1 ; }
1000 setup_lxc $lxc $fcdistro $pldistro $personality
1002 devel_or_vtest_tools $lxc $fcdistro $pldistro $personality
1004 post_install $lxc $personality