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 () {
37 templates=/etc/vservers/.distributions/${fcdistro}
38 if [ -f ${templates}/yum/yum.conf ] ; then
39 echo "Initializing yum.conf in $vserver from ${templates}/yum"
40 sed -e "s!@YUMETCDIR@!/etc!g;
41 s!@YUMCACHEDIR@!/var/cache/yum!g;
42 s!@YUMLOGDIR@!/var/log!g;
43 s!@YUMLOCKDIR@!/var/lock!g;
44 " ${templates}/yum/yum.conf > /vservers/$vserver/etc/yum.conf
46 # post process the various @...@ variables from this yum.conf file.
48 echo "Using $fcdistro default for yum.conf"
51 if [ -d ${templates}/yum.repos.d ] ; then
52 echo "Initializing yum.repos.d in $vserver from ${templates}/yum.repos.d"
53 rm -rf /vservers/$vserver/etc/yum.repos.d
54 tar -C ${templates} -cf - yum.repos.d | tar -C /vservers/$vserver/etc -xvf -
56 echo "Cannot initialize yum.repos.d in $vserver"
59 # for using vtest-init-vserver.sh as a general-purpose vserver creation wrapper
60 # just mention 'none' as the repo url
61 if [ -n "$MYPLC_MODE" -a "$REPO_URL" != "none" ] ; then
62 if [ ! -d /vservers/$vserver/etc/yum.repos.d ] ; then
63 echo "WARNING : cannot create myplc repo"
65 # exclude kernel from fedora repos
66 for repo in /vservers/$vserver/etc/yum.repos.d/* ; do
67 [ -f $repo ] && yumconf_exclude $repo "exclude=$pl_KEXCLUDES"
69 # the build repo is not signed at this stage
70 cat > /vservers/$vserver/etc/yum.repos.d/myplc.repo <<EOF
81 # return yum or debootstrap
82 function package_method () {
85 f[0-9]*|centos[0-9]*|sl[0-9]*) echo yum ;;
86 lenny|etch) echo debootstrap ;;
87 *) echo Unknown distro $fcdistro ;;
91 # return arch from debian distro and personality
92 function canonical_arch () {
95 case $(package_method $fcdistro) in
97 case $personality in *32) echo i386 ;; *64) echo x86_64 ;; *) echo Unknown-arch-1 ;; esac ;;
99 case $personality in *32) echo i386 ;; *64) echo amd64 ;; *) echo Unknown-arch-2 ;; esac ;;
101 echo Unknown-arch-3 ;;
105 # the new test framework creates /timestamp in /vservers/<name> *before* populating it
106 function almost_empty () {
108 # non existing is fine
109 [ ! -d $dir ] && return 0;
110 # need to have at most one file
111 count=$(cd $dir; ls | wc -l); [ $count -le 1 ];
114 function setup_vserver () {
122 personality=$1; shift
124 # check that this is a new one - see above
125 almost_empty /vservers/$vserver || {
126 echo "$COMMAND : vserver $vserver seems to exist - bailing out"
130 pkg_method=$(package_method $fcdistro)
133 build_options="-m yum -- -d $fcdistro"
136 arch=$(canonical_arch $personality $fcdistro)
137 build_options="-m debootstrap -- -d $fcdistro -- --arch $arch"
140 build_options="something wrong" ;;
144 # try to work around the vserver issue:
145 # vc_ctx_migrate: No such process
146 # rpm-fake.so: failed to initialize communication with resolver
147 for i in $(seq 20) ; do
148 $personality vserver $VERBOSE $vserver build $VSERVER_OPTIONS $build_options && break || true
149 echo "* ${i}-th attempt to 'vserver build' failed - waiting for 3 seconds"
152 # check success - not enough to check for the directory, let's assume /etc/ in image
153 [ -d /vservers/$vserver/etc ]
155 if [ ! -z "$personality" ] ; then
156 if [ -f "/etc/vservers/$vserver/personality" ] ; then
157 registered_personality=$(grep $personality /etc/vservers/$vserver/personality | wc -l)
159 registered_personality=0
161 if [ $registered_personality -eq 0 -a "$personality" != "linux64" ] ; then
162 echo $personality >> /etc/vservers/$vserver/personality
166 BCAPFILE=/etc/vservers/$vserver/bcapabilities
168 if [ -n "$VBUILD_MODE" ] ; then
169 ### capabilities required for a build vserver
170 # set up appropriate vserver capabilities to mount, mknod and IPC_LOCK
171 grep -q ^CAP_SYS_ADMIN $BCAPFILE || echo CAP_SYS_ADMIN >> $BCAPFILE
172 grep -q ^CAP_MKNOD $BCAPFILE || echo CAP_MKNOD >> $BCAPFILE
173 grep -q ^CAP_IPC_LOCK $BCAPFILE || echo CAP_IPC_LOCK >> $BCAPFILE
174 # useful for f15 guests that use set_cap_file
175 grep -q ^CAP_SETFCAP $BCAPFILE || echo CAP_SETFCAP >> $BCAPFILE
177 ### capabilities required for a myplc vserver
178 # for /etc/plc.d/gpg - need to init /dev/random
179 grep -q ^CAP_MKNOD $BCAPFILE || echo CAP_MKNOD >> $BCAPFILE
180 grep -q ^CAP_NET_BIND_SERVICE $BCAPFILE || echo CAP_NET_BIND_SERVICE >> $BCAPFILE
181 # useful for f15 guests that use set_cap_file
182 grep -q ^CAP_SETFCAP $BCAPFILE || echo CAP_SETFCAP >> $BCAPFILE
185 # Set persistent for the network context
186 # Thierry: Daniel's kernels come with single_ip turned off by default, let's make this explicit
187 echo "persistent,lback_allow,~single_ip" > /etc/vservers/$vserver/nflags
190 echo -e "persistent\n~info_init" > /etc/vservers/$vserver/cflags
193 mkdir /etc/vservers/$vserver/cgroup
195 # Set the init style of your vserver to plain for f15 and higher
196 # not working with f15 anyways, systemd requires 2.6.36 to work
198 f1[5-9]) echo plain > /etc/vservers/$vserver/apps/init/style ;;
201 if [ "$pkg_method" = "yum" ] ; then
202 $personality vyum $vserver -- -y install yum
204 for i in $(seq 20) ; do
205 $personality vserver $VERBOSE $vserver pkgmgmt internalize && break || true
206 echo "* ${i}-th attempt to 'vserver pkgmgmt internalize' failed - waiting for 3 seconds"
211 # start the vserver so we can do the following operations
212 # redirect out/err to protect against the vserver's init sequence getting stalled
213 # mostly used for f10 vservers created remotely through ssh
214 $personality vserver $VERBOSE $vserver start >& /dev/null
216 if [ "$pkg_method" == "yum" ] ; then
217 $personality vserver $VERBOSE $vserver exec sh -c "rm -f /var/lib/rpm/__db*"
219 # run the host rpmdb_dump and restore with the guest rpmdb_load
220 function translate_rpm_hashes () {
223 local personality="$1"; shift
224 local vserver="$1"; shift
225 # need to have utilities installed
229 guest_dir=/var/lib/rpm
230 host_dir=/vservers/$vserver/$guest_dir
231 files=$(cd $host_dir ; file * | grep Hash | cut -d: -f 1)
232 for file in $files; do
233 (cd $host_dir && mv $file ${file}-foreign)
234 /usr/lib/rpm/rpmdb_dump $host_dir/${file}-foreign | $personality vserver $VERBOSE $vserver exec /usr/lib/rpm/rpmdb_load $guest_dir/$file
236 $personality vserver $VERBOSE $vserver exec rpm --rebuilddb
240 # try the simple way, if that fails try to cross fix the rpm hashes
241 $personality vserver $VERBOSE $vserver exec rpm --rebuilddb || translate_rpm_hashes $personality $vserver
244 # check if the vserver kernel is using VSERVER_DEVICE (vdevmap) support
245 need_vdevmap=$(grep "CONFIG_VSERVER_DEVICE=y" /boot/config-$(uname -r) | wc -l)
247 if [ $need_vdevmap -eq 1 ] ; then
248 ctx=$(cat /etc/vservers/$vserver/context)
249 vdevmap --set --xid $ctx --open --create --target /dev/null
250 vdevmap --set --xid $ctx --open --create --target /dev/root
253 # minimal config in the vserver for yum to work
254 [ "$pkg_method" = "yum" ] && configure_yum_in_vserver $vserver $fcdistro
257 cp /etc/resolv.conf /vservers/$vserver/etc/resolv.conf
258 # and /etc/hosts for at least localhost
259 [ -f /vservers/$vserver/etc/hosts ] || echo "127.0.0.1 localhost localhost.localdomain" > /vservers/$vserver/etc/hosts
263 function devel_or_vtest_tools () {
272 personality=$1; shift
274 pkg_method=$(package_method $fcdistro)
276 # check for .pkgs file based on pldistro
277 if [ -n "$VBUILD_MODE" ] ; then
282 pkgsfile=$(pl_locateDistroFile $DIRNAME $pldistro $pkgsname)
284 ### install individual packages, then groups
285 # get target arch - use uname -i here (we want either x86_64 or i386)
286 vserver_arch=$($personality vserver $vserver exec uname -i)
287 # on debian systems we get arch through the 'arch' command
288 [ "$vserver_arch" = "unknown" ] && vserver_arch=$($personality vserver $vserver exec arch)
290 packages=$(pl_getPackages -a $vserver_arch $fcdistro $pldistro $pkgsfile)
291 groups=$(pl_getGroups -a $vserver_arch $fcdistro $pldistro $pkgsfile)
293 case "$pkg_method" in
295 [ -n "$packages" ] && $personality vserver $vserver exec yum -y install $packages
296 for group_plus in $groups; do
297 group=$(echo $group_plus | sed -e "s,+++, ,g")
298 $personality vserver $vserver exec yum -y groupinstall "$group"
300 # store current rpm list in /init-vserver.rpms in case we need to check the contents
301 $personality vserver $vserver exec rpm -aq > /vservers/$vserver/init-vserver.rpms
304 $personality vserver $vserver exec apt-get update
305 for package in $packages ; do
306 $personality vserver $vserver exec apt-get install -y $package
308 ### xxx todo install groups with apt..
311 echo "unknown pkg_method $pkg_method"
318 function post_install () {
319 if [ -n "$VBUILD_MODE" ] ; then
320 post_install_vbuild "$@"
322 post_install_myplc "$@"
324 # setup localtime from the host
326 cp /etc/localtime /vservers/$vserver/etc/localtime
329 function post_install_vbuild () {
336 personality=$1; shift
338 ### From myplc-devel-native.spec
339 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
340 cat << EOF | $personality vserver $VERBOSE $vserver exec bash -x
341 # set up /dev/loop* in vserver
342 for i in \$(seq 0 255) ; do
343 mknod -m 640 /dev/loop\$i b 7 \$i
346 # create symlink for /dev/fd
347 [ ! -e "/dev/fd" ] && ln -s /proc/self/fd /dev/fd
349 # modify /etc/rpm/macros to not use /sbin/new-kernel-pkg
350 sed -i 's,/sbin/new-kernel-pkg:,,' /etc/rpm/macros
351 if [ -h "/sbin/new-kernel-pkg" ] ; then
352 filename=\$(readlink -f /sbin/new-kernel-pkg)
353 if [ "\$filename" == "/sbin/true" ] ; then
354 echo "WARNING: /sbin/new-kernel-pkg symlinked to /sbin/true"
355 echo "\tmost likely /etc/rpm/macros has /sbin/new-kernel-pkg declared in _netsharedpath."
356 echo "\tPlease remove /sbin/new-kernel-pkg from _netsharedpath and reintall mkinitrd."
361 # customize root's prompt
362 cat << PROFILE > /root/.profile
363 export PS1="[$vserver] \\w # "
369 # add a "build" user to the system
370 builduser=\$(grep "^build:" /etc/passwd | wc -l)
371 if [ \$builduser -eq 0 ] ; then
372 groupadd -o -g \$gid build;
373 useradd -o -c 'Automated Build' -u \$uid -g \$gid -n -M -s /bin/bash build;
376 # Allow build user to build certain RPMs as root
377 if [ -f /etc/sudoers ] ; then
378 buildsudo=\$(grep "^build.*ALL=(ALL).*NOPASSWD:.*ALL" /etc/sudoers | wc -l)
379 if [ \$buildsudo -eq 0 ] ; then
380 echo "build ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
382 sed -i 's,^Defaults.*requiretty,#Defaults requiretty,' /etc/sudoers
389 function post_install_myplc () {
395 personality=$1; shift
397 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
398 cat << EOF | $personality vserver $VERBOSE $vserver exec bash -x
400 # create /etc/sysconfig/network if missing
401 [ -f /etc/sysconfig/network ] || echo NETWORKING=yes > /etc/sysconfig/network
403 # create symlink for /dev/fd
404 [ ! -e "/dev/fd" ] && ln -s /proc/self/fd /dev/fd
406 # turn off regular crond, as plc invokes plc_crond
409 # take care of loginuid in /etc/pam.d
410 sed -i "s,#*\(.*loginuid.*\),#\1," /etc/pam.d/*
412 # customize root's prompt
413 cat << PROFILE > /root/.profile
414 export PS1="[$vserver] \\w # "
420 # parses ifconfig's output to find out ip address and mask
421 # will then be passed to vserver as e.g. --interface 138.96.250.126/255.255.0.0
422 # default is to use lo, that's enough for local mirrors
423 # use -i eth0 in case your fedora mirror is on a separate box on the network
424 function vserverIfconfig () {
427 line=$(ifconfig $ifname 2> /dev/null | grep 'inet addr')
428 if [ -n "$line" ] ; then
430 for word in "$@" ; do
431 addr=$(echo $word | sed -e s,[aA][dD][dD][rR]:,,)
432 mask=$(echo $word | sed -e s,[mM][aA][sS][kK]:,,)
433 if [ "$word" != "$addr" ] ; then
435 elif [ "$word" != "$mask" ] ; then
436 result="${result}/${mask}"
440 if [ -z "$result" ] ; then
441 echo "vserverIfconfig failed to locate $ifname"
450 echo "Usage: $COMMAND_VBUILD [options] vserver-name [ -- vserver-options ]"
451 echo "Usage: $COMMAND_MYPLC [options] vserver-name repo-url [ -- vserver-options ]"
452 echo "Requirements: you need to have a vserver-compliant kernel,"
453 echo " as well as the util-vserver RPM installed"
455 echo " This command creates a fresh vserver instance, for building, or running, myplc"
456 echo "Supported options"
457 echo " -f fcdistro - for creating the root filesystem - defaults to $DEFAULT_FCDISTRO"
458 echo " -d pldistro - defaults to $DEFAULT_PLDISTRO"
459 echo " -p personality - defaults to $DEFAULT_PERSONALITY"
460 echo " -i ifname: determines ip and netmask attached to ifname, and passes it to the vserver"
461 echo " -r : set apps/init/mark to default so the vserver restart upon reboot"
462 echo " -v : verbose - passes -v to calls to vserver"
463 echo "vserver-options"
464 echo " all args after the optional -- are passed to vserver <name> build <options>"
465 echo " typical usage is e.g. --interface eth0:200.150.100.10/24"
466 echo "With $COMMAND_MYPLC you can give 'none' as the URL, in which case"
467 echo " myplc.repo does not get created"
490 while getopts "f:d:p:i:rv" opt ; do
492 f) fcdistro=$OPTARG;;
493 d) pldistro=$OPTARG;;
494 p) personality=$OPTARG;;
496 r) RESISTANT="true";;
502 shift $(($OPTIND - 1))
504 # parse fixed arguments
505 [[ -z "$@" ]] && usage
507 if [ -n "$MYPLC_MODE" ] ; then
508 [[ -z "$@" ]] && usage
512 # parse vserver options
513 if [[ -n "$@" ]] ; then
514 if [ "$1" == "--" ] ; then
522 # with new util-vserver, it is mandatory to provide an IP even for building
523 if [ -n "$VBUILD_MODE" ] ; then
524 [ -z "$IFNAME" ] && IFNAME=$DEFAULT_IFNAME
526 if [ -n "$IFNAME" ] ; then
527 localip=$(vserverIfconfig $IFNAME)
528 VSERVER_OPTIONS="$VSERVER_OPTIONS --interface $localip"
531 [ -z "$fcdistro" ] && fcdistro=$DEFAULT_FCDISTRO
532 [ -z "$pldistro" ] && pldistro=$DEFAULT_PLDISTRO
533 [ -z "$personality" ] && personality=$DEFAULT_PERSONALITY
535 setup_vserver $vserver $fcdistro $personality
536 devel_or_vtest_tools $vserver $fcdistro $pldistro $personality
537 post_install $vserver $personality
539 # Start Vserver automatically on boot
540 [ -n "$RESISTANt" ] && echo "default" > /etc/vservers/$vserver/apps/init/mark