9 # pkgs parsing utilities
10 PATH=$(dirname $0):$PATH export PATH
14 DEFAULT_PLDISTRO=planetlab
15 DEFAULT_PERSONALITY=linux64
18 COMMAND_VBUILD="vbuild-init-lxc.sh"
19 COMMAND_MYPLC="vtest-init-lxc.sh"
21 lxc_version="0.8.0-rc2"
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
87 #Reconfigure the routing table
88 echo "Configuring gateway=$gateway"
89 route add default gw $gateway
91 echo "========== $COMMAND: exiting start - beg"
94 echo "========== $COMMAND: exiting start - end"
102 function failure () {
103 echo "$COMMAND : Bailing out"
107 function cidr_notation () {
111 for i in $(seq 1 4) ; do
112 part=$(echo $netmask | cut -d. -f $i)
114 "255") cidr=$((cidr + 8));;
115 "254") cidr=$((cidr + 7));;
116 "252") cidr=$((cidr + 6));;
117 "248") cidr=$((cidr + 5));;
118 "240") cidr=$((cidr + 4));;
119 "224") cidr=$((cidr + 3));;
120 "192") cidr=$((cidr + 2));;
121 "128") cidr=$((cidr + 1));;
122 "0") cidr=$((cidr + 0));;
129 function check_yum_installed () {
131 rpm -q $package >& /dev/null || yum -y install $package
134 function check_yumgroup_installed () {
136 yum grouplist "$group" | grep -q Installed || { yum -y groupinstall "$group" ; }
139 function prepare_host() {
141 #################### lxc-tools : rebuild as current fedora release has flaws
142 #install development tools
143 check_yumgroup_installed "Development Tools"
144 #install libcap-devel, libvirt
145 check_yum_installed libcap-devel
146 check_yum_installed libvirt
148 #retrieve and install lxc from sources
149 raw_version=$(lxc-version ||: )
150 lxc_installed_version=$(echo $raw_version | sed -e 's,.*: ,,')
151 if [ "$lxc_installed_version" != "$lxc_version" ] ; then
152 echo "Expecting version" '['$lxc_version']'
153 echo "Found version" '['$lxc_installed_version']'
154 echo "Installing lxc ..."
156 [ -d lxc ] || git clone git://lxc.git.sourceforge.net/gitroot/lxc/lxc
159 git checkout $lxc_version
161 ./configure --prefix=/usr --exec-prefix=/usr
166 # #create a placeholder (just a hack to make lxc works)
167 # [ -d "/usr/local/var/lib" ] || mkdir -p /usr/local/var/lib
169 #################### bride initialization
170 check_yum_installed bridge-utils
172 isInstalled=$(netstat -rn | grep '^0.0.0.0' | awk '{print $8;}')
173 if [ "$isInstalled" != "br0" ] ; then
183 function configure_fedora() {
185 # disable selinux in fedora
186 mkdir -p $rootfs_path/selinux
187 echo 0 > $rootfs_path/selinux/enforce
189 # configure the network
191 cat <<EOF > ${rootfs_path}/etc/sysconfig/network-scripts/ifcfg-$IFNAME
205 cat <<EOF > ${rootfs_path}/etc/sysconfig/network
211 # cat <<EOF > $rootfs_path/etc/hosts
212 #127.0.0.1 localhost $HOSTNAME
215 dev_path="${rootfs_path}/dev"
218 mknod -m 666 ${dev_path}/null c 1 3
219 mknod -m 666 ${dev_path}/zero c 1 5
220 mknod -m 666 ${dev_path}/random c 1 8
221 mknod -m 666 ${dev_path}/urandom c 1 9
222 mkdir -m 755 ${dev_path}/pts
223 mkdir -m 1777 ${dev_path}/shm
224 mknod -m 666 ${dev_path}/tty c 5 0
225 mknod -m 666 ${dev_path}/tty0 c 4 0
226 mknod -m 666 ${dev_path}/tty1 c 4 1
227 mknod -m 666 ${dev_path}/tty2 c 4 2
228 mknod -m 666 ${dev_path}/tty3 c 4 3
229 mknod -m 666 ${dev_path}/tty4 c 4 4
230 mknod -m 600 ${dev_path}/console c 5 1
231 mknod -m 666 ${dev_path}/full c 1 7
232 mknod -m 600 ${dev_path}/initctl p
233 mknod -m 666 ${dev_path}/ptmx c 5 2
235 #echo "setting root passwd to $root_password"
236 #echo "root:$root_password" | chroot $rootfs_path chpasswd
242 function configure_fedora_init() {
244 sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.sysinit
245 sed -i 's|.sbin.start_udev||' ${rootfs_path}/etc/rc.d/rc.sysinit
246 chroot ${rootfs_path} chkconfig udev-post off
247 chroot ${rootfs_path} chkconfig network on
251 function configure_fedora_systemd() {
253 unlink ${rootfs_path}/etc/systemd/system/default.target
254 touch ${rootfs_path}/etc/fstab
255 chroot ${rootfs_path} ln -s /dev/null //etc/systemd/system/udev.service
256 chroot ${rootfs_path} ln -s /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
257 #dependency on a device unit fails it specially that we disabled udev
258 sed -i 's/After=dev-%i.device/After=/' ${rootfs_path}/lib/systemd/system/getty\@.service
259 chroot ${rootfs_path} chkconfig network on
262 function download_fedora() {
264 # check the mini fedora was not already downloaded
265 INSTALL_ROOT=$cache/partial
267 mkdir -p $INSTALL_ROOT
268 if [ $? -ne 0 ]; then
269 echo "Failed to create '$INSTALL_ROOT' directory"
273 # download a mini fedora into a cache
274 echo "Downloading fedora minimal ..."
275 YUM="yum --installroot $INSTALL_ROOT -y --nogpgcheck --releasever=$release"
276 PKG_LIST="yum initscripts passwd rsyslog vim-minimal dhclient chkconfig rootfiles policycoreutils openssh-server openssh-clients"
279 MIRROR_URL=http://mirror.onelab.eu/fedora/releases/$release/Everything/$arch/os
280 RELEASE_URL="$MIRROR_URL/Packages/fedora-release-$release-1.noarch.rpm"
281 echo "Fetching from $RELEASE_URL"
282 curl -f "$RELEASE_URL" > $INSTALL_ROOT/fedora-release-$release.noarch.rpm
284 mkdir -p $INSTALL_ROOT/var/lib/rpm
285 rpm --root $INSTALL_ROOT --initdb
286 rpm --root $INSTALL_ROOT -ivh $INSTALL_ROOT/fedora-release-$release.noarch.rpm
287 echo "$YUM install $PKG_LIST"
288 $YUM install $PKG_LIST
290 if [ $? -ne 0 ]; then
291 echo "Failed to download the rootfs, aborting."
295 mv "$INSTALL_ROOT" "$cache/rootfs"
296 echo "Download complete."
302 function copy_fedora() {
304 # make a local copy of the minifedora
305 echo -n "Copying rootfs to $rootfs_path ..."
306 mkdir -p $rootfs_path
307 rsync -a $cache/rootfs/ $rootfs_path/
312 function update_fedora() {
314 YUM="yum --installroot $cache/rootfs -y --nogpgcheck"
319 function install_fedora() {
322 mkdir -p /var/lock/subsys/
325 if [ $? -ne 0 ]; then
326 echo "Cache repository is busy."
330 echo "Checking cache download in $cache/rootfs ... "
331 if [ ! -e "$cache/rootfs" ]; then
333 if [ $? -ne 0 ]; then
334 echo "Failed to download 'fedora base'"
338 echo "Cache found. Updating..."
340 if [ $? -ne 0 ]; then
341 echo "Failed to update 'fedora base', continuing with last known good cache"
343 echo "Update finished"
347 echo "Copy $cache/rootfs to $rootfs_path ... "
349 if [ $? -ne 0 ]; then
350 echo "Failed to copy rootfs"
356 ) 200>/var/lock/subsys/lxc
362 function copy_configuration() {
364 mkdir -p $config_path
365 cat <<EOF >> $config_path/config
370 lxc.rootfs = $rootfs_path
371 lxc.mount = $config_path/fstab
373 lxc.network.type = $lxc_network_type
374 lxc.network.flags = up
375 lxc.network.link = $lxc_network_link
376 lxc.network.name = $IFNAME
377 lxc.network.mtu = 1500
378 lxc.network.ipv4 = $IP/$CIDR
380 #lxc.cgroup.devices.deny = a
382 lxc.cgroup.devices.allow = c 1:3 rwm
383 lxc.cgroup.devices.allow = c 1:5 rwm
385 lxc.cgroup.devices.allow = c 5:1 rwm
386 lxc.cgroup.devices.allow = c 5:0 rwm
387 lxc.cgroup.devices.allow = c 4:0 rwm
388 lxc.cgroup.devices.allow = c 4:1 rwm
390 lxc.cgroup.devices.allow = c 1:9 rwm
391 lxc.cgroup.devices.allow = c 1:8 rwm
392 lxc.cgroup.devices.allow = c 136:* rwm
393 lxc.cgroup.devices.allow = c 5:2 rwm
395 lxc.cgroup.devices.allow = c 254:0 rwm
396 lxc.cgroup.devices.allow = b 255:0 rwm
401 cat <<EOF > $config_path/fstab
402 proc $rootfs_path/proc proc nodev,noexec,nosuid 0 0
403 devpts $rootfs_path/dev/pts devpts defaults 0 0
404 sysfs $rootfs_path/sys sysfs defaults 0 0
406 if [ $? -ne 0 ]; then
407 echo "Failed to add configuration"
417 # overwrite lxc's internal yum config
418 function configure_yum_in_lxc () {
427 echo "Initializing yum.repos.d in $lxc"
428 rm -f $rootfs_path/etc/yum.repos.d/*
430 cat > $rootfs_path/etc/yum.repos.d/building.repo <<EOF
432 name=Fedora $release - \$basearch
433 baseurl=http://mirror.onelab.eu/fedora/releases/$release/Everything/\$basearch/os/
437 gpgkey=http://mirror.onelab.eu/keys/RPM-GPG-KEY-fedora-$release-primary
440 name=Fedora $release - \$basearch - Updates
441 baseurl=http://mirror.onelab.eu/fedora/updates/$release/\$basearch/
445 gpgkey=http://mirror.onelab.eu/keys/RPM-GPG-KEY-fedora-$release-primary
448 # for using vtest-init-lxc.sh as a general-purpose lxc creation wrapper
449 # just mention 'none' as the repo url
450 if [ -n "$MYPLC_MODE" -a "$REPO_URL" != "none" ] ; then
451 if [ ! -d $rootfs_path/etc/yum.repos.d ] ; then
452 echo "WARNING : cannot create myplc repo"
454 # exclude kernel from fedora repos
455 yumexclude=$(pl_plcyumexclude $fcdistro $pldistro $DIRNAME)
456 for repo in $rootfs_path/etc/yum.repos.d/* ; do
457 [ -f $repo ] && yumconf_exclude $repo "exclude=$yumexclude"
459 # the build repo is not signed at this stage
460 cat > $rootfs_path/etc/yum.repos.d/myplc.repo <<EOF
471 # return yum or debootstrap
472 function package_method () {
475 f[0-9]*|centos[0-9]*|sl[0-9]*) echo yum ;;
476 lenny|etch) echo debootstrap ;;
477 *) echo Unknown distro $fcdistro ;;
481 # return arch from debian distro and personality
482 function canonical_arch () {
483 personality=$1; shift
485 case $(package_method $fcdistro) in
487 case $personality in *32) echo i386 ;; *64) echo x86_64 ;; *) echo Unknown-arch-1 ;; esac ;;
489 case $personality in *32) echo i386 ;; *64) echo amd64 ;; *) echo Unknown-arch-2 ;; esac ;;
491 echo Unknown-arch-3 ;;
495 # the new test framework creates /timestamp in /vservers/<name> *before* populating it
496 function almost_empty () {
498 # non existing is fine
499 [ ! -d $dir ] && return 0;
500 # need to have at most one file
501 count=$(cd $dir; ls | wc -l); [ $count -le 1 ];
504 function setup_lxc() {
508 #trap failure ERR INT
513 personality=$1; shift
515 # create lxc container
517 if [ $? -ne 0 ]; then
518 echo "failed write configuration file"
523 if [ $? -ne 0 ]; then
524 echo "failed to install fedora"
529 if [ $? -ne 0 ]; then
530 echo "failed to configure fedora for a container"
534 type /bin/systemd >/dev/null 2>&1
535 if [ $? -ne 0 ]; then
536 configure_fedora_init
538 configure_fedora_systemd
542 mkdir $rootfs_path/cgroup
545 cp /etc/resolv.conf $rootfs_path/etc/resolv.conf
546 # and /etc/hosts for at least localhost
547 [ -f $rootfs_path/etc/hosts ] || echo "127.0.0.1 localhost localhost.localdomain" > $rootfs_path/etc/hosts
550 mkdir $rootfs_path/root/.ssh
551 cat /root/.ssh/id_rsa.pub >> $rootfs_path/root/.ssh/authorized_keys
558 chroot $rootfs_path rpm --rebuilddb
559 #ssh -o "StrictHostKeyChecking no" $IP "rpm --rebuilddb"
561 configure_yum_in_lxc $lxc $fcdistro $pldistro
566 function devel_or_vtest_tools () {
575 personality=$1; shift
577 pkg_method=$(package_method $fcdistro)
579 # check for .pkgs file based on pldistro
580 if [ -n "$VBUILD_MODE" ] ; then
585 pkgsfile=$(pl_locateDistroFile $DIRNAME $pldistro $pkgsname)
587 ### install individual packages, then groups
588 # get target arch - use uname -i here (we want either x86_64 or i386)
590 lxc_arch=$(chroot $rootfs_path uname -i)
591 # on debian systems we get arch through the 'arch' command
592 [ "$lxc_arch" = "unknown" ] && lxc_arch=$(chroot $rootfs_path arch)
594 packages=$(pl_getPackages -a $lxc_arch $fcdistro $pldistro $pkgsfile)
595 groups=$(pl_getGroups -a $lxc_arch $fcdistro $pldistro $pkgsfile)
597 case "$pkg_method" in
599 [ -n "$packages" ] && chroot $rootfs_path yum -y install $packages
600 for group_plus in $groups; do
601 group=$(echo $group_plus | sed -e "s,+++, ,g")
602 chroot $rootfs_path yum -y groupinstall "$group"
604 # store current rpm list in /init-lxc.rpms in case we need to check the contents
605 chroot $rootfs_path rpm -aq > $rootfs_path/init-lxc.rpms
608 chroot $rootfs_path apt-get update
609 for package in $packages ; do
610 chroot $rootfs_path apt-get install -y $package
612 ### xxx todo install groups with apt..
615 echo "unknown pkg_method $pkg_method"
622 function post_install () {
623 if [ -n "$VBUILD_MODE" ] ; then
624 post_install_vbuild "$@"
626 post_install_myplc "$@"
628 # setup localtime from the host
630 cp /etc/localtime $rootfs_path/etc/localtime
633 function post_install_vbuild () {
640 personality=$1; shift
642 ### From myplc-devel-native.spec
643 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
644 cat << EOF | chroot $rootfs_path bash -x
645 # set up /dev/loop* in lxc
646 for i in \$(seq 0 255) ; do
647 mknod -m 640 /dev/loop\$i b 7 \$i
650 # create symlink for /dev/fd
651 [ ! -e "/dev/fd" ] && ln -s /proc/self/fd /dev/fd
653 # modify /etc/rpm/macros to not use /sbin/new-kernel-pkg
654 sed -i 's,/sbin/new-kernel-pkg:,,' /etc/rpm/macros
655 if [ -h "/sbin/new-kernel-pkg" ] ; then
656 filename=\$(readlink -f /sbin/new-kernel-pkg)
657 if [ "\$filename" == "/sbin/true" ] ; then
658 echo "WARNING: /sbin/new-kernel-pkg symlinked to /sbin/true"
659 echo "\tmost likely /etc/rpm/macros has /sbin/new-kernel-pkg declared in _netsharedpath."
660 echo "\tPlease remove /sbin/new-kernel-pkg from _netsharedpath and reintall mkinitrd."
665 # customize root's prompt
666 cat << PROFILE > /root/.profile
667 export PS1="[$lxc] \\w # "
673 # add a "build" user to the system
674 builduser=\$(grep "^build:" /etc/passwd | wc -l)
675 if [ \$builduser -eq 0 ] ; then
676 groupadd -o -g \$gid build;
677 useradd -o -c 'Automated Build' -u \$uid -g \$gid -n -M -s /bin/bash build;
680 # Allow build user to build certain RPMs as root
681 if [ -f /etc/sudoers ] ; then
682 buildsudo=\$(grep "^build.*ALL=(ALL).*NOPASSWD:.*ALL" /etc/sudoers | wc -l)
683 if [ \$buildsudo -eq 0 ] ; then
684 echo "build ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
686 sed -i 's,^Defaults.*requiretty,#Defaults requiretty,' /etc/sudoers
693 function post_install_myplc () {
699 personality=$1; shift
701 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
702 cat << EOF | chroot $rootfs_path bash -x
704 # create /etc/sysconfig/network if missing
705 [ -f /etc/sysconfig/network ] || echo NETWORKING=yes > /etc/sysconfig/network
707 # create symlink for /dev/fd
708 [ ! -e "/dev/fd" ] && ln -s /proc/self/fd /dev/fd
710 # turn off regular crond, as plc invokes plc_crond
713 # take care of loginuid in /etc/pam.d
714 sed -i "s,#*\(.*loginuid.*\),#\1," /etc/pam.d/*
716 # customize root's prompt
717 cat << PROFILE > /root/.profile
718 export PS1="[$lxc] \\w # "
726 echo "Usage: $COMMAND_VBUILD [options] lxc-name"
727 echo "Usage: $COMMAND_MYPLC [options] lxc-name repo-url [ -- lxc-options ]"
729 echo " This command creates a fresh lxc instance, for building, or running, myplc"
730 echo "Supported options"
731 echo " -f fcdistro - for creating the root filesystem - defaults to $DEFAULT_FCDISTRO"
732 echo " -d pldistro - defaults to $DEFAULT_PLDISTRO"
733 echo " -p personality - defaults to $DEFAULT_PERSONALITY"
734 echo " -i ifname: determines ip and netmask attached to ifname, and passes it to the lxc"
735 echo "-- lxc-options"
736 echo " --netdev : interface to be defined inside lxc"
737 echo " --interface : IP to be defined for the lxc"
738 echo " --hostname : Hostname to be defined for the lxc"
739 echo "With $COMMAND_MYPLC you can give 'none' as the URL, in which case"
740 echo " myplc.repo does not get created"
748 #trap failure ERR INT
763 while getopts "f:d:p:i:" opt ; do
765 f) fcdistro=$OPTARG;;
766 d) pldistro=$OPTARG;;
767 p) personality=$OPTARG;;
773 shift $(($OPTIND - 1))
775 # parse fixed arguments
776 [[ -z "$@" ]] && usage
778 if [ -n "$MYPLC_MODE" ] ; then
779 [[ -z "$@" ]] && usage
783 # parse vserver options
784 if [[ -n "$@" ]] ; then
785 if [ "$1" == "--" ] ; then
793 eval set -- "$LXC_OPTIONS"
798 --netdev) IFNAME=$2; shift 2;;
799 --interface) IP=$2; shift 2;;
800 --hostname) HOSTNAME=$2; shift 2;;
806 if [ -n "$VBUILD_MODE" ] ; then
807 [ -z "$IFNAME" ] && IFNAME=$DEFAULT_IFNAME
808 [ -z "$HOSTNAME" ] && HOSTNAME=$lxc
811 [ -z "$fcdistro" ] && fcdistro=$DEFAULT_FCDISTRO
812 [ -z "$pldistro" ] && pldistro=$DEFAULT_PLDISTRO
813 [ -z "$personality" ] && personality=$DEFAULT_PERSONALITY
815 release=$(echo $fcdistro | cut -df -f2)
817 if [ "$personality" == "linux32" ]; then
820 elif [ "$personality" == "linux64" ]; then
824 echo "Unknown personality: $personality"
828 if [ -n "$VBUILD_MODE" ] ; then
830 # Bridge IP affectation
831 x=$(echo $personality | cut -dx -f2)
832 y=$(echo $fcdistro | cut -df -f2)
836 NETMASK="255.255.255.0"
837 GATEWAY="192.168.122.1"
839 lxc_network_type=veth
840 lxc_network_link=virbr0
841 echo "the IP address of container $lxc is $IP "
843 [[ -z "$REPO_URL" ]] && usage
844 [[ -z "$IP" ]] && usage
845 NETMASK=$(ifconfig br0 | grep 'inet addr' | awk '{print $4}' | sed -e 's/.*://')
846 GATEWAY=$(route -n | grep 'UG' | awk '{print $2}')
847 [[ -z "$HOSTNAME" ]] && usage
848 lxc_network_type=veth
852 CIDR=$(cidr_notation $NETMASK)
855 if [ "$(id -u)" != "0" ]; then
856 echo "This script should be run as 'root'"
860 # need lxc installed before we can run lxc-ls
863 # be strict on lxc-lines matching name exactly using ^<name>$
864 # as otherwise the timestamps may show up here
865 lxc-ls | grep -q '^'"$lxc"'$' && { echo "container $lxc already exists - exiting" ; exit 1 ; }
868 rootfs_path=$path/$lxc/rootfs
869 config_path=$path/$lxc
870 cache_base=/var/cache/lxc/fedora/$arch
871 cache=$cache_base/$release
875 setup_lxc $lxc $fcdistro $pldistro $personality
877 devel_or_vtest_tools $lxc $fcdistro $pldistro $personality
879 post_install $lxc $personality