9 # pkgs parsing utilities
10 PATH=$(dirname $0):$PATH export PATH
14 DEFAULT_PLDISTRO=planetlab
15 DEFAULT_PERSONALITY=linux32
18 COMMAND_VBUILD="vbuild-init-vserver.sh"
19 COMMAND_MYPLC="vtest-init-vserver.sh"
22 echo "$COMMAND : Bailing out"
26 # overwrite vserver's internal yum config from what is in
27 # .distributions/<distrib>/yum/yum.conf and /yum.repos.d
29 function configure_yum_in_vserver () {
38 templates=/etc/vservers/.distributions/${fcdistro}
39 if [ -f ${templates}/yum/yum.conf ] ; then
40 echo "Initializing yum.conf in $vserver from ${templates}/yum"
41 sed -e "s!@YUMETCDIR@!/etc!g;
42 s!@YUMCACHEDIR@!/var/cache/yum!g;
43 s!@YUMLOGDIR@!/var/log!g;
44 s!@YUMLOCKDIR@!/var/lock!g;
45 " ${templates}/yum/yum.conf > /vservers/$vserver/etc/yum.conf
47 # post process the various @...@ variables from this yum.conf file.
49 echo "Using $fcdistro default for yum.conf"
52 if [ -d ${templates}/yum.repos.d ] ; then
53 echo "Initializing yum.repos.d in $vserver from ${templates}/yum.repos.d"
54 rm -rf /vservers/$vserver/etc/yum.repos.d
55 tar -C ${templates} -cf - yum.repos.d | tar -C /vservers/$vserver/etc -xvf -
57 echo "Cannot initialize yum.repos.d in $vserver"
60 # for using vtest-init-vserver.sh as a general-purpose vserver creation wrapper
61 # just mention 'none' as the repo url
62 if [ -n "$MYPLC_MODE" -a "$REPO_URL" != "none" ] ; then
63 if [ ! -d /vservers/$vserver/etc/yum.repos.d ] ; then
64 echo "WARNING : cannot create myplc repo"
66 # exclude kernel from fedora repos
67 yumexclude=$(pl_plcyumexclude $fcdistro $pldistro $DIRNAME)
68 for repo in /vservers/$vserver/etc/yum.repos.d/* ; do
69 [ -f $repo ] && yumconf_exclude $repo "exclude=$yumexclude"
71 # the build repo is not signed at this stage
72 cat > /vservers/$vserver/etc/yum.repos.d/myplc.repo <<EOF
83 # return yum or debootstrap
84 function package_method () {
87 f[0-9]*|centos[0-9]*|sl[0-9]*) echo yum ;;
88 lenny|etch) echo debootstrap ;;
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 function setup_vserver () {
125 personality=$1; shift
127 # check that this is a new one - see above
128 almost_empty /vservers/$vserver || {
129 echo "$COMMAND : vserver $vserver seems to exist - bailing out"
133 pkg_method=$(package_method $fcdistro)
136 build_options="-m yum -- -d $fcdistro"
139 arch=$(canonical_arch $personality $fcdistro)
140 build_options="-m debootstrap -- -d $fcdistro -- --arch $arch"
143 build_options="something wrong" ;;
147 # try to work around the vserver issue:
148 # vc_ctx_migrate: No such process
149 # rpm-fake.so: failed to initialize communication with resolver
150 for i in $(seq 20) ; do
151 $personality vserver $VERBOSE $vserver build $VSERVER_OPTIONS $build_options && break || true
152 echo "* ${i}-th attempt to 'vserver build' failed - waiting for 3 seconds"
155 # check success - not enough to check for the directory, let's assume /etc/ in image
156 [ -d /vservers/$vserver/etc ]
158 if [ ! -z "$personality" ] ; then
159 if [ -f "/etc/vservers/$vserver/personality" ] ; then
160 registered_personality=$(grep $personality /etc/vservers/$vserver/personality | wc -l)
162 registered_personality=0
164 if [ $registered_personality -eq 0 -a "$personality" != "linux64" ] ; then
165 echo $personality >> /etc/vservers/$vserver/personality
169 BCAPFILE=/etc/vservers/$vserver/bcapabilities
171 if [ -n "$VBUILD_MODE" ] ; then
172 ### capabilities required for a build vserver
173 # set up appropriate vserver capabilities to mount, mknod and IPC_LOCK
174 grep -q ^CAP_SYS_ADMIN $BCAPFILE || echo CAP_SYS_ADMIN >> $BCAPFILE
175 grep -q ^CAP_MKNOD $BCAPFILE || echo CAP_MKNOD >> $BCAPFILE
176 grep -q ^CAP_IPC_LOCK $BCAPFILE || echo CAP_IPC_LOCK >> $BCAPFILE
177 # useful for f16 guests that use set_cap_file
178 grep -q ^CAP_SETFCAP $BCAPFILE || echo CAP_SETFCAP >> $BCAPFILE
180 ### capabilities required for a myplc vserver
181 # for /etc/plc.d/gpg - need to init /dev/random
182 grep -q ^CAP_MKNOD $BCAPFILE || echo CAP_MKNOD >> $BCAPFILE
183 grep -q ^CAP_NET_BIND_SERVICE $BCAPFILE || echo CAP_NET_BIND_SERVICE >> $BCAPFILE
184 # useful for f16 guests that use set_cap_file
185 grep -q ^CAP_SETFCAP $BCAPFILE || echo CAP_SETFCAP >> $BCAPFILE
188 # Set persistent for the network context
189 # Thierry: Daniel's kernels come with single_ip turned off by default, let's make this explicit
190 echo "persistent,lback_allow,~single_ip" > /etc/vservers/$vserver/nflags
193 echo -e "persistent\n~info_init" > /etc/vservers/$vserver/cflags
196 mkdir /etc/vservers/$vserver/cgroup
198 # Set the init style of your vserver to plain for f16 and higher
199 # not working with f16 anyways, systemd requires 2.6.36 to work
201 f1[5-9]) echo plain > /etc/vservers/$vserver/apps/init/style ;;
204 if [ "$pkg_method" = "yum" ] ; then
205 $personality vyum $vserver -- -y install yum
207 for i in $(seq 20) ; do
208 $personality vserver $VERBOSE $vserver pkgmgmt internalize && break || true
209 echo "* ${i}-th attempt to 'vserver pkgmgmt internalize' failed - waiting for 3 seconds"
214 # start the vserver so we can do the following operations
215 # redirect out/err to protect against the vserver's init sequence getting stalled
216 # mostly used for f10 vservers created remotely through ssh
217 $personality vserver $VERBOSE $vserver start >& /dev/null
219 if [ "$pkg_method" == "yum" ] ; then
220 $personality vserver $VERBOSE $vserver exec sh -c "rm -f /var/lib/rpm/__db*"
222 # run the host rpmdb_dump and restore with the guest rpmdb_load
223 function translate_rpm_hashes () {
226 local personality="$1"; shift
227 local vserver="$1"; shift
228 # need to have utilities installed
232 guest_dir=/var/lib/rpm
233 host_dir=/vservers/$vserver/$guest_dir
234 files=$(cd $host_dir ; file * | grep Hash | cut -d: -f 1)
235 for file in $files; do
236 (cd $host_dir && mv $file ${file}-foreign)
237 /usr/lib/rpm/rpmdb_dump $host_dir/${file}-foreign | $personality vserver $VERBOSE $vserver exec /usr/lib/rpm/rpmdb_load $guest_dir/$file
239 $personality vserver $VERBOSE $vserver exec rpm --rebuilddb
243 # try the simple way, if that fails try to cross fix the rpm hashes
244 $personality vserver $VERBOSE $vserver exec rpm --rebuilddb || translate_rpm_hashes $personality $vserver
247 # check if the vserver kernel is using VSERVER_DEVICE (vdevmap) support
248 need_vdevmap=$(grep "CONFIG_VSERVER_DEVICE=y" /boot/config-$(uname -r) | wc -l)
250 if [ $need_vdevmap -eq 1 ] ; then
251 ctx=$(cat /etc/vservers/$vserver/context)
252 vdevmap --set --xid $ctx --open --create --target /dev/null
253 vdevmap --set --xid $ctx --open --create --target /dev/root
256 # minimal config in the vserver for yum to work
257 [ "$pkg_method" = "yum" ] && configure_yum_in_vserver $vserver $fcdistro $pldistro
260 cp /etc/resolv.conf /vservers/$vserver/etc/resolv.conf
261 # and /etc/hosts for at least localhost
262 [ -f /vservers/$vserver/etc/hosts ] || echo "127.0.0.1 localhost localhost.localdomain" > /vservers/$vserver/etc/hosts
266 function devel_or_vtest_tools () {
275 personality=$1; shift
277 pkg_method=$(package_method $fcdistro)
279 # check for .pkgs file based on pldistro
280 if [ -n "$VBUILD_MODE" ] ; then
285 pkgsfile=$(pl_locateDistroFile $DIRNAME $pldistro $pkgsname)
287 ### install individual packages, then groups
288 # get target arch - use uname -i here (we want either x86_64 or i386)
289 vserver_arch=$($personality vserver $vserver exec uname -i)
290 # on debian systems we get arch through the 'arch' command
291 [ "$vserver_arch" = "unknown" ] && vserver_arch=$($personality vserver $vserver exec arch)
293 packages=$(pl_getPackages -a $vserver_arch $fcdistro $pldistro $pkgsfile)
294 groups=$(pl_getGroups -a $vserver_arch $fcdistro $pldistro $pkgsfile)
296 case "$pkg_method" in
298 [ -n "$packages" ] && $personality vserver $vserver exec yum -y install $packages
299 for group_plus in $groups; do
300 group=$(echo $group_plus | sed -e "s,+++, ,g")
301 $personality vserver $vserver exec yum -y groupinstall "$group"
303 # store current rpm list in /init-vserver.rpms in case we need to check the contents
304 $personality vserver $vserver exec rpm -aq > /vservers/$vserver/init-vserver.rpms
307 $personality vserver $vserver exec apt-get update
308 for package in $packages ; do
309 $personality vserver $vserver exec apt-get install -y $package
311 ### xxx todo install groups with apt..
314 echo "unknown pkg_method $pkg_method"
321 function post_install () {
322 if [ -n "$VBUILD_MODE" ] ; then
323 post_install_vbuild "$@"
325 post_install_myplc "$@"
327 # setup localtime from the host
329 cp /etc/localtime /vservers/$vserver/etc/localtime
332 function post_install_vbuild () {
339 personality=$1; shift
341 ### From myplc-devel-native.spec
342 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
343 cat << EOF | $personality vserver $VERBOSE $vserver exec bash -x
344 # set up /dev/loop* in vserver
345 for i in \$(seq 0 255) ; do
346 mknod -m 640 /dev/loop\$i b 7 \$i
349 # create symlink for /dev/fd
350 [ ! -e "/dev/fd" ] && ln -s /proc/self/fd /dev/fd
352 # modify /etc/rpm/macros to not use /sbin/new-kernel-pkg
353 sed -i 's,/sbin/new-kernel-pkg:,,' /etc/rpm/macros
354 if [ -h "/sbin/new-kernel-pkg" ] ; then
355 filename=\$(readlink -f /sbin/new-kernel-pkg)
356 if [ "\$filename" == "/sbin/true" ] ; then
357 echo "WARNING: /sbin/new-kernel-pkg symlinked to /sbin/true"
358 echo "\tmost likely /etc/rpm/macros has /sbin/new-kernel-pkg declared in _netsharedpath."
359 echo "\tPlease remove /sbin/new-kernel-pkg from _netsharedpath and reintall mkinitrd."
364 # customize root's prompt
365 cat << PROFILE > /root/.profile
366 export PS1="[$vserver] \\w # "
372 # add a "build" user to the system
373 builduser=\$(grep "^build:" /etc/passwd | wc -l)
374 if [ \$builduser -eq 0 ] ; then
375 groupadd -o -g \$gid build;
376 useradd -o -c 'Automated Build' -u \$uid -g \$gid -n -M -s /bin/bash build;
379 # Allow build user to build certain RPMs as root
380 if [ -f /etc/sudoers ] ; then
381 buildsudo=\$(grep "^build.*ALL=(ALL).*NOPASSWD:.*ALL" /etc/sudoers | wc -l)
382 if [ \$buildsudo -eq 0 ] ; then
383 echo "build ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
385 sed -i 's,^Defaults.*requiretty,#Defaults requiretty,' /etc/sudoers
392 function post_install_myplc () {
398 personality=$1; shift
400 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
401 cat << EOF | $personality vserver $VERBOSE $vserver exec bash -x
403 # create /etc/sysconfig/network if missing
404 [ -f /etc/sysconfig/network ] || echo NETWORKING=yes > /etc/sysconfig/network
406 # create symlink for /dev/fd
407 [ ! -e "/dev/fd" ] && ln -s /proc/self/fd /dev/fd
409 # turn off regular crond, as plc invokes plc_crond
412 # take care of loginuid in /etc/pam.d
413 sed -i "s,#*\(.*loginuid.*\),#\1," /etc/pam.d/*
415 # customize root's prompt
416 cat << PROFILE > /root/.profile
417 export PS1="[$vserver] \\w # "
423 # parses ifconfig's output to find out ip address and mask
424 # will then be passed to vserver as e.g. --interface 138.96.250.126/255.255.0.0
425 # default is to use lo, that's enough for local mirrors
426 # use -i eth0 in case your fedora mirror is on a separate box on the network
427 function vserverIfconfig () {
430 line=$(ifconfig $ifname 2> /dev/null | grep 'inet addr')
431 if [ -n "$line" ] ; then
433 for word in "$@" ; do
434 addr=$(echo $word | sed -e s,[aA][dD][dD][rR]:,,)
435 mask=$(echo $word | sed -e s,[mM][aA][sS][kK]:,,)
436 if [ "$word" != "$addr" ] ; then
438 elif [ "$word" != "$mask" ] ; then
439 result="${result}/${mask}"
443 if [ -z "$result" ] ; then
444 echo "vserverIfconfig failed to locate $ifname"
453 echo "Usage: $COMMAND_VBUILD [options] vserver-name [ -- vserver-options ]"
454 echo "Usage: $COMMAND_MYPLC [options] vserver-name repo-url [ -- vserver-options ]"
455 echo "Requirements: you need to have a vserver-compliant kernel,"
456 echo " as well as the util-vserver RPM installed"
458 echo " This command creates a fresh vserver instance, for building, or running, myplc"
459 echo "Supported options"
460 echo " -f fcdistro - for creating the root filesystem - defaults to $DEFAULT_FCDISTRO"
461 echo " -d pldistro - defaults to $DEFAULT_PLDISTRO"
462 echo " -p personality - defaults to $DEFAULT_PERSONALITY"
463 echo " -i ifname: determines ip and netmask attached to ifname, and passes it to the vserver"
464 echo " -r : set apps/init/mark to default so the vserver restart upon reboot"
465 echo " -v : verbose - passes -v to calls to vserver"
466 echo "vserver-options"
467 echo " all args after the optional -- are passed to vserver <name> build <options>"
468 echo " typical usage is e.g. --interface eth0:200.150.100.10/24"
469 echo "With $COMMAND_MYPLC you can give 'none' as the URL, in which case"
470 echo " myplc.repo does not get created"
493 while getopts "f:d:p:i:rv" opt ; do
495 f) fcdistro=$OPTARG;;
496 d) pldistro=$OPTARG;;
497 p) personality=$OPTARG;;
499 r) RESISTANT="true";;
505 shift $(($OPTIND - 1))
507 # parse fixed arguments
508 [[ -z "$@" ]] && usage
510 if [ -n "$MYPLC_MODE" ] ; then
511 [[ -z "$@" ]] && usage
515 # parse vserver options
516 if [[ -n "$@" ]] ; then
517 if [ "$1" == "--" ] ; then
525 # with new util-vserver, it is mandatory to provide an IP even for building
526 if [ -n "$VBUILD_MODE" ] ; then
527 [ -z "$IFNAME" ] && IFNAME=$DEFAULT_IFNAME
529 if [ -n "$IFNAME" ] ; then
530 localip=$(vserverIfconfig $IFNAME)
531 VSERVER_OPTIONS="$VSERVER_OPTIONS --interface $localip"
534 [ -z "$fcdistro" ] && fcdistro=$DEFAULT_FCDISTRO
535 [ -z "$pldistro" ] && pldistro=$DEFAULT_PLDISTRO
536 [ -z "$personality" ] && personality=$DEFAULT_PERSONALITY
538 setup_vserver $vserver $fcdistro $pldistro $personality
539 devel_or_vtest_tools $vserver $fcdistro $pldistro $personality
540 post_install $vserver $personality
542 # Start Vserver automatically on boot
543 [ -n "$RESISTANt" ] && echo "default" > /etc/vservers/$vserver/apps/init/mark