9 # pkgs parsing utilities
10 PATH=$(dirname $0):$PATH export PATH
14 DEFAULT_PLDISTRO=planetlab
15 DEFAULT_PERSONALITY=linux32
18 COMMAND_VBUILD="vbuild-init-lxc.sh"
19 COMMAND_MYPLC="vtest-init-lxc.sh"
21 function bridge_init () {
29 # Default Value for INTERFACE_LAN
33 echo "========== $COMMAND: entering start - beg"
38 echo "========== $COMMAND: entering start - end"
40 # disable netfilter calls for bridge interface (they cause panick on 2.6.35 anyway)
42 # another option would be to accept the all forward packages for
43 # bridged interface like: -A FORWARD -m physdev --physdev-is-bridged -j ACCEPT
44 sysctl net.bridge.bridge-nf-call-iptables=0
45 sysctl net.bridge.bridge-nf-call-ip6tables=0
46 sysctl net.bridge.bridge-nf-call-arptables=0
48 # take extra arg for ifname, if provided
49 [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
52 type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
54 #if we have already configured the same host_box no need to do it again
55 /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null && {
56 echo "Bridge interface $INTERFACE_BRIDGE already set up - $COMMAND start exiting"
59 /sbin/ifconfig $INTERFACE_LAN &>/dev/null || {
60 echo "Cannot use interface $INTERFACE_LAN - exiting"
65 #Getting host IP/masklen
66 address=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $2;}')
67 [ -z "$address" ] && { echo "ERROR: Could not determine IP address for $INTERFACE_LAN" ; exit 1 ; }
69 broadcast=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $4;}')
70 [ -z "$broadcast" ] && echo "WARNING: Could not determine broadcast address for $INTERFACE_LAN"
72 gateway=$(netstat -rn | grep '^0.0.0.0' | awk '{print $2;}')
73 [ -z "$gateway" ] && echo "WARNING: Could not determine gateway IP"
76 echo "Creating bridge INTERFACE_BRIDGE=$INTERFACE_BRIDGE"
77 brctl addbr $INTERFACE_BRIDGE
78 #brctl stp $INTERFACE_BRIDGE yes
79 brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
80 echo "Activating promiscuous mode INTERFACE_LAN=$INTERFACE_LAN"
81 /sbin/ifconfig $INTERFACE_LAN 0.0.0.0 promisc up
83 echo "Setting bridge address=$address broadcast=$broadcast"
85 /sbin/ifconfig $INTERFACE_BRIDGE $address broadcast $broadcast up
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 prepare_host() {
135 #install development tools
136 isInstalled=$(yum grouplist "Development Tools" | grep Installed)
137 if [ -z "$isInstalled" ] ; then
138 echo "Installing Development Tools ..."
139 yum -y groupinstall "Development Tools"
142 #install libcap-devel, libvirt
143 isInstalled=$(rpm -qa | grep libcap-devel)
144 if [ -z "$isInstalled" ] ; then
145 echo "Installing libcap-devel ..."
146 yum -y install libcap-devel
149 isInstalled=$(rpm -qa | grep libvirt)
150 if [ -z "$isInstalled" ] ; then
151 echo "Installing libvirt ..."
152 yum -y install libvirt
155 #retreive and install lxc from sources
156 isInstalled=$(lxc-version | cut -d: -f2 | grep "0.8.0-rc1")
157 if [ -z "$isInstalled" ] ; then
158 echo "Installing lxc ..."
160 git clone git://lxc.git.sourceforge.net/gitroot/lxc/lxc
168 #create a symlink (just a hack to make lxc works)
169 [ ! -d "/usr/local/var/lib" ] && mkdir -p /usr/local/var/lib
170 #[ ! -f "/usr/local/var/lib/lxc" ] && ln -s /var/lib/lxc /usr/local/var/lib/lxc
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 cat <<EOF > ${rootfs_path}/etc/sysconfig/network
205 # cat <<EOF > $rootfs_path/etc/hosts
206 #127.0.0.1 localhost $HOSTNAME
209 dev_path="${rootfs_path}/dev"
212 mknod -m 666 ${dev_path}/null c 1 3
213 mknod -m 666 ${dev_path}/zero c 1 5
214 mknod -m 666 ${dev_path}/random c 1 8
215 mknod -m 666 ${dev_path}/urandom c 1 9
216 mkdir -m 755 ${dev_path}/pts
217 mkdir -m 1777 ${dev_path}/shm
218 mknod -m 666 ${dev_path}/tty c 5 0
219 mknod -m 666 ${dev_path}/tty0 c 4 0
220 mknod -m 666 ${dev_path}/tty1 c 4 1
221 mknod -m 666 ${dev_path}/tty2 c 4 2
222 mknod -m 666 ${dev_path}/tty3 c 4 3
223 mknod -m 666 ${dev_path}/tty4 c 4 4
224 mknod -m 600 ${dev_path}/console c 5 1
225 mknod -m 666 ${dev_path}/full c 1 7
226 mknod -m 600 ${dev_path}/initctl p
227 mknod -m 666 ${dev_path}/ptmx c 5 2
229 #echo "setting root passwd to $root_password"
230 #echo "root:$root_password" | chroot $rootfs_path chpasswd
236 function configure_fedora_init() {
238 sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.sysinit
239 sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.d/rc.sysinit
240 chroot ${rootfs_path} chkconfig udev-post off
241 chroot ${rootfs_path} chkconfig network on
245 function configure_fedora_systemd() {
247 unlink ${rootfs_path}/etc/systemd/system/default.target
248 touch ${rootfs_path}/etc/fstab
249 chroot ${rootfs_path} ln -s /dev/null //etc/systemd/system/udev.service
250 chroot ${rootfs_path} ln -s /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
251 #dependency on a device unit fails it specially that we disabled udev
252 sed -i 's/After=dev-%i.device/After=/' ${rootfs_path}/lib/systemd/system/getty\@.service
253 chroot ${rootfs_path} chkconfig network on
256 function download_fedora() {
258 # check the mini fedora was not already downloaded
259 INSTALL_ROOT=$cache/partial
261 mkdir -p $INSTALL_ROOT
262 if [ $? -ne 0 ]; then
263 echo "Failed to create '$INSTALL_ROOT' directory"
267 # download a mini fedora into a cache
268 echo "Downloading fedora minimal ..."
269 YUM="yum --installroot $INSTALL_ROOT -y --nogpgcheck --releasever=$release"
270 PKG_LIST="yum initscripts passwd rsyslog vim-minimal dhclient chkconfig rootfiles policycoreutils openssh-server openssh-clients"
273 MIRROR_URL=http://mirror.onelab.eu/fedora/releases/$release/Everything/$arch/os
274 RELEASE_URL="$MIRROR_URL/Packages/fedora-release-$release-1.noarch.rpm"
275 echo "Fetching from $RELEASE_URL"
276 curl -f "$RELEASE_URL" > $INSTALL_ROOT/fedora-release-$release.noarch.rpm
278 mkdir -p $INSTALL_ROOT/var/lib/rpm
279 rpm --root $INSTALL_ROOT --initdb
280 rpm --root $INSTALL_ROOT -ivh $INSTALL_ROOT/fedora-release-$release.noarch.rpm
281 echo "$YUM install $PKG_LIST"
282 $YUM install $PKG_LIST
284 if [ $? -ne 0 ]; then
285 echo "Failed to download the rootfs, aborting."
289 mv "$INSTALL_ROOT" "$cache/rootfs"
290 echo "Download complete."
296 function copy_fedora() {
298 # make a local copy of the minifedora
299 echo -n "Copying rootfs to $rootfs_path ..."
300 mkdir -p $rootfs_path
301 rsync -a $cache/rootfs/ $rootfs_path/
306 function update_fedora() {
308 YUM="yum --installroot $cache/rootfs -y --nogpgcheck"
313 function install_fedora() {
316 mkdir -p /var/lock/subsys/
319 if [ $? -ne 0 ]; then
320 echo "Cache repository is busy."
324 echo "Checking cache download in $cache/rootfs ... "
325 if [ ! -e "$cache/rootfs" ]; then
327 if [ $? -ne 0 ]; then
328 echo "Failed to download 'fedora base'"
332 echo "Cache found. Updating..."
334 if [ $? -ne 0 ]; then
335 echo "Failed to update 'fedora base', continuing with last known good cache"
337 echo "Update finished"
341 echo "Copy $cache/rootfs to $rootfs_path ... "
343 if [ $? -ne 0 ]; then
344 echo "Failed to copy rootfs"
350 ) 200>/var/lock/subsys/lxc
356 function copy_configuration() {
358 mkdir -p $config_path
359 cat <<EOF >> $config_path/config
364 lxc.rootfs = $rootfs_path
365 lxc.mount = $config_path/fstab
367 lxc.network.type = $lxc_network_type
368 lxc.network.flags = up
369 lxc.network.link = $lxc_network_link
370 lxc.network.name = $IFNAME
371 lxc.network.mtu = 1500
372 lxc.network.ipv4 = $IP/$CIDR
374 #lxc.cgroup.devices.deny = a
376 lxc.cgroup.devices.allow = c 1:3 rwm
377 lxc.cgroup.devices.allow = c 1:5 rwm
379 lxc.cgroup.devices.allow = c 5:1 rwm
380 lxc.cgroup.devices.allow = c 5:0 rwm
381 lxc.cgroup.devices.allow = c 4:0 rwm
382 lxc.cgroup.devices.allow = c 4:1 rwm
384 lxc.cgroup.devices.allow = c 1:9 rwm
385 lxc.cgroup.devices.allow = c 1:8 rwm
386 lxc.cgroup.devices.allow = c 136:* rwm
387 lxc.cgroup.devices.allow = c 5:2 rwm
389 lxc.cgroup.devices.allow = c 254:0 rwm
390 lxc.cgroup.devices.allow = b 255:0 rwm
395 cat <<EOF > $config_path/fstab
396 proc $rootfs_path/proc proc nodev,noexec,nosuid 0 0
397 devpts $rootfs_path/dev/pts devpts defaults 0 0
398 sysfs $rootfs_path/sys sysfs defaults 0 0
400 if [ $? -ne 0 ]; then
401 echo "Failed to add configuration"
411 # overwrite lxc's internal yum config
412 function configure_yum_in_lxc () {
421 echo "Initializing yum.repos.d in $lxc"
422 rm -f $rootfs_path/etc/yum.repos.d/*
424 cat > $rootfs_path/etc/yum.repos.d/building.repo <<EOF
426 name=Fedora $release - \$basearch
427 baseurl=http://mirror.onelab.eu/fedora/releases/$release/Everything/\$basearch/os/
431 gpgkey=http://mirror.onelab.eu/keys/RPM-GPG-KEY-fedora-$release-primary
434 name=Fedora $release - \$basearch - Updates
435 baseurl=http://mirror.onelab.eu/fedora/updates/$release/\$basearch/
439 gpgkey=http://mirror.onelab.eu/keys/RPM-GPG-KEY-fedora-$release-primary
442 # for using vtest-init-lxc.sh as a general-purpose lxc creation wrapper
443 # just mention 'none' as the repo url
444 if [ -n "$MYPLC_MODE" -a "$REPO_URL" != "none" ] ; then
445 if [ ! -d $rootfs_path/etc/yum.repos.d ] ; then
446 echo "WARNING : cannot create myplc repo"
448 # exclude kernel from fedora repos
449 yumexclude=$(pl_plcyumexclude $fcdistro $pldistro $DIRNAME)
450 for repo in $rootfs_path/etc/yum.repos.d/* ; do
451 [ -f $repo ] && yumconf_exclude $repo "exclude=$yumexclude"
453 # the build repo is not signed at this stage
454 cat > $rootfs_path/etc/yum.repos.d/myplc.repo <<EOF
465 # return yum or debootstrap
466 function package_method () {
469 f[0-9]*|centos[0-9]*|sl[0-9]*) echo yum ;;
470 lenny|etch) echo debootstrap ;;
471 *) echo Unknown distro $fcdistro ;;
475 # return arch from debian distro and personality
476 function canonical_arch () {
477 personality=$1; shift
479 case $(package_method $fcdistro) in
481 case $personality in *32) echo i386 ;; *64) echo x86_64 ;; *) echo Unknown-arch-1 ;; esac ;;
483 case $personality in *32) echo i386 ;; *64) echo amd64 ;; *) echo Unknown-arch-2 ;; esac ;;
485 echo Unknown-arch-3 ;;
489 # the new test framework creates /timestamp in /vservers/<name> *before* populating it
490 function almost_empty () {
492 # non existing is fine
493 [ ! -d $dir ] && return 0;
494 # need to have at most one file
495 count=$(cd $dir; ls | wc -l); [ $count -le 1 ];
498 function setup_lxc() {
502 #trap failure ERR INT
507 personality=$1; shift
511 # create lxc container
513 if [ $? -ne 0 ]; then
514 echo "failed write configuration file"
519 if [ $? -ne 0 ]; then
520 echo "failed to install fedora"
525 if [ $? -ne 0 ]; then
526 echo "failed to configure fedora for a container"
530 type /bin/systemd >/dev/null 2>&1
531 if [ $? -ne 0 ]; then
532 configure_fedora_init
534 configure_fedora_systemd
539 mkdir $rootfs_path/cgroup
542 cp /etc/resolv.conf $rootfs_path/etc/resolv.conf
543 # and /etc/hosts for at least localhost
544 [ -f $rootfs_path/etc/hosts ] || echo "127.0.0.1 localhost localhost.localdomain" > $rootfs_path/etc/hosts
547 mkdir $rootfs_path/root/.ssh
548 cat /root/.ssh/id_rsa.pub >> $rootfs_path/root/.ssh/authorized_keys
555 chroot $rootfs_path rpm --rebuilddb
557 configure_yum_in_lxc $lxc $fcdistro $pldistro
562 function devel_or_vtest_tools () {
571 personality=$1; shift
573 pkg_method=$(package_method $fcdistro)
575 # check for .pkgs file based on pldistro
576 if [ -n "$VBUILD_MODE" ] ; then
581 pkgsfile=$(pl_locateDistroFile $DIRNAME $pldistro $pkgsname)
583 ### install individual packages, then groups
584 # get target arch - use uname -i here (we want either x86_64 or i386)
586 lxc_arch=$(chroot $rootfs_path uname -i)
587 # on debian systems we get arch through the 'arch' command
588 [ "$lxc_arch" = "unknown" ] && lxc_arch=$(chroot $rootfs_path arch)
590 packages=$(pl_getPackages -a $lxc_arch $fcdistro $pldistro $pkgsfile)
591 groups=$(pl_getGroups -a $lxc_arch $fcdistro $pldistro $pkgsfile)
593 case "$pkg_method" in
595 [ -n "$packages" ] && chroot $rootfs_path yum -y install $packages
596 for group_plus in $groups; do
597 group=$(echo $group_plus | sed -e "s,+++, ,g")
598 chroot $rootfs_path yum -y groupinstall "$group"
600 # store current rpm list in /init-lxc.rpms in case we need to check the contents
601 chroot $rootfs_path rpm -aq > $rootfs_path/init-lxc.rpms
604 chroot $rootfs_path apt-get update
605 for package in $packages ; do
606 chroot $rootfs_path apt-get install -y $package
608 ### xxx todo install groups with apt..
611 echo "unknown pkg_method $pkg_method"
618 function post_install () {
619 if [ -n "$VBUILD_MODE" ] ; then
620 post_install_vbuild "$@"
622 post_install_myplc "$@"
624 # setup localtime from the host
626 cp /etc/localtime $rootfs_path/etc/localtime
629 function post_install_vbuild () {
636 personality=$1; shift
638 ### From myplc-devel-native.spec
639 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
640 cat << EOF | chroot $rootfs_path bash -x
641 # set up /dev/loop* in lxc
642 for i in \$(seq 0 255) ; do
643 mknod -m 640 /dev/loop\$i b 7 \$i
646 # create symlink for /dev/fd
647 [ ! -e "/dev/fd" ] && ln -s /proc/self/fd /dev/fd
649 # modify /etc/rpm/macros to not use /sbin/new-kernel-pkg
650 sed -i 's,/sbin/new-kernel-pkg:,,' /etc/rpm/macros
651 if [ -h "/sbin/new-kernel-pkg" ] ; then
652 filename=\$(readlink -f /sbin/new-kernel-pkg)
653 if [ "\$filename" == "/sbin/true" ] ; then
654 echo "WARNING: /sbin/new-kernel-pkg symlinked to /sbin/true"
655 echo "\tmost likely /etc/rpm/macros has /sbin/new-kernel-pkg declared in _netsharedpath."
656 echo "\tPlease remove /sbin/new-kernel-pkg from _netsharedpath and reintall mkinitrd."
661 # customize root's prompt
662 cat << PROFILE > /root/.profile
663 export PS1="[$lxc] \\w # "
669 # add a "build" user to the system
670 builduser=\$(grep "^build:" /etc/passwd | wc -l)
671 if [ \$builduser -eq 0 ] ; then
672 groupadd -o -g \$gid build;
673 useradd -o -c 'Automated Build' -u \$uid -g \$gid -n -M -s /bin/bash build;
676 # Allow build user to build certain RPMs as root
677 if [ -f /etc/sudoers ] ; then
678 buildsudo=\$(grep "^build.*ALL=(ALL).*NOPASSWD:.*ALL" /etc/sudoers | wc -l)
679 if [ \$buildsudo -eq 0 ] ; then
680 echo "build ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
682 sed -i 's,^Defaults.*requiretty,#Defaults requiretty,' /etc/sudoers
689 function post_install_myplc () {
695 personality=$1; shift
697 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
698 cat << EOF | chroot $rootfs_path bash -x
700 # create /etc/sysconfig/network if missing
701 [ -f /etc/sysconfig/network ] || echo NETWORKING=yes > /etc/sysconfig/network
703 # create symlink for /dev/fd
704 [ ! -e "/dev/fd" ] && ln -s /proc/self/fd /dev/fd
706 # turn off regular crond, as plc invokes plc_crond
709 # take care of loginuid in /etc/pam.d
710 sed -i "s,#*\(.*loginuid.*\),#\1," /etc/pam.d/*
712 # customize root's prompt
713 cat << PROFILE > /root/.profile
714 export PS1="[$lxc] \\w # "
722 echo "Usage: $COMMAND_VBUILD [options] lxc-name"
723 echo "Usage: $COMMAND_MYPLC [options] lxc-name repo-url [ -- lxc-options ]"
725 echo " This command creates a fresh lxc instance, for building, or running, myplc"
726 echo "Supported options"
727 echo " -f fcdistro - for creating the root filesystem - defaults to $DEFAULT_FCDISTRO"
728 echo " -d pldistro - defaults to $DEFAULT_PLDISTRO"
729 echo " -p personality - defaults to $DEFAULT_PERSONALITY"
730 echo " -i ifname: determines ip and netmask attached to ifname, and passes it to the lxc"
731 echo "-- lxc-options"
732 echo " --netdev : interface to be defined inside lxc"
733 echo " --interface : IP to be defined for the lxc"
734 echo " --hostname : Hostname to be defined for the lxc"
735 echo "With $COMMAND_MYPLC you can give 'none' as the URL, in which case"
736 echo " myplc.repo does not get created"
744 #trap failure ERR INT
759 while getopts "f:d:p:i:" opt ; do
761 f) fcdistro=$OPTARG;;
762 d) pldistro=$OPTARG;;
763 p) personality=$OPTARG;;
769 shift $(($OPTIND - 1))
771 # parse fixed arguments
772 [[ -z "$@" ]] && usage
774 if [ -n "$MYPLC_MODE" ] ; then
775 [[ -z "$@" ]] && usage
779 # parse vserver options
780 if [[ -n "$@" ]] ; then
781 if [ "$1" == "--" ] ; then
789 eval set -- "$LXC_OPTIONS"
794 --netdev) IFNAME=$2; shift 2;;
795 --interface) IP=$2; shift 2;;
796 --hostname) HOSTNAME=$2; shift 2;;
802 if [ -n "$VBUILD_MODE" ] ; then
803 [ -z "$IFNAME" ] && IFNAME=$DEFAULT_IFNAME
804 [ -z "$HOSTNAME" ] && HOSTNAME=$lxc
807 [ -z "$fcdistro" ] && fcdistro=$DEFAULT_FCDISTRO
808 [ -z "$pldistro" ] && pldistro=$DEFAULT_PLDISTRO
809 [ -z "$personality" ] && personality=$DEFAULT_PERSONALITY
811 release=$(echo $fcdistro | cut -df -f2)
813 if [ "$personality" == "linux32" ]; then
816 elif [ "$personality" == "linux64" ]; then
820 echo "Unknown personality: $personality"
824 if [ -n "$VBUILD_MODE" ] ; then
826 # Bridge IP affectation
827 x=$(echo $personality | cut -dx -f2)
828 y=$(echo $fcdistro | cut -df -f2)
832 NETMASK="255.255.255.0"
833 GATEWAY="192.168.122.1"
835 lxc_network_type=veth
836 lxc_network_link=virbr0
837 echo "the IP address of container $lxc is $IP "
839 [[ -z "$REPO_URL" ]] && usage
840 [[ -z "$IP" ]] && usage
841 NETMASK=$(ifconfig br0 | grep 'inet addr' | awk '{print $4}' | sed -e 's/.*://')
842 GATEWAY=$(route -n | grep 'UG' | awk '{print $2}')
843 [[ -z "$HOSTNAME" ]] && usage
844 lxc_network_type=veth
848 CIDR=$(cidr_notation $NETMASK)
851 if [ "$(id -u)" != "0" ]; then
852 echo "This script should be run as 'root'"
856 if [ ! -z "$(lxc-ls | grep $lxc)" ];then
857 echo "container $lxc exists"
863 rootfs_path=$path/$lxc/rootfs
864 config_path=$path/$lxc
865 cache_base=/var/cache/lxc/fedora/$arch
866 cache=$cache_base/$release
872 setup_lxc $lxc $fcdistro $pldistro $personality
874 devel_or_vtest_tools $lxc $fcdistro $pldistro $personality
876 post_install $lxc $personality