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 function setup_vserver () {
113 personality=$1; shift
115 if [ -d /vservers/$vserver ] ; then
116 echo "$COMMAND : vserver $vserver seems to exist - bailing out"
120 pkg_method=$(package_method $fcdistro)
123 build_options="-m yum -- -d $fcdistro"
126 arch=$(canonical_arch $personality $fcdistro)
127 build_options="-m debootstrap -- -d $fcdistro -- --arch $arch"
130 build_options="something wrong" ;;
134 # try to work around the vserver issue:
135 # vc_ctx_migrate: No such process
136 # rpm-fake.so: failed to initialize communication with resolver
137 for i in $(seq 20) ; do
138 $personality vserver $VERBOSE $vserver build $VSERVER_OPTIONS $build_options && break || true
139 echo "* ${i}-th attempt to 'vserver build' failed - waiting for 3 seconds"
143 [ -d /vservers/$vserver ]
145 if [ ! -z "$personality" ] ; then
146 if [ -f "/etc/vservers/$vserver/personality" ] ; then
147 registered_personality=$(grep $personality /etc/vservers/$vserver/personality | wc -l)
149 registered_personality=0
151 if [ $registered_personality -eq 0 -a "$personality" != "linux64" ] ; then
152 echo $personality >> /etc/vservers/$vserver/personality
156 BCAPFILE=/etc/vservers/$vserver/bcapabilities
158 if [ -n "$VBUILD_MODE" ] ; then
159 ### capabilities required for a build vserver
160 # set up appropriate vserver capabilities to mount, mknod and IPC_LOCK
161 grep -q ^CAP_SYS_ADMIN $BCAPFILE || echo CAP_SYS_ADMIN >> $BCAPFILE
162 grep -q ^CAP_MKNOD $BCAPFILE || echo CAP_MKNOD >> $BCAPFILE
163 grep -q ^CAP_IPC_LOCK $BCAPFILE || echo CAP_IPC_LOCK >> $BCAPFILE
164 # useful for f15 guests that use set_cap_file
165 grep -q ^CAP_SETFCAP $BCAPFILE || echo CAP_SETFCAP >> $BCAPFILE
167 ### capabilities required for a myplc vserver
168 # for /etc/plc.d/gpg - need to init /dev/random
169 grep -q ^CAP_MKNOD $BCAPFILE || echo CAP_MKNOD >> $BCAPFILE
170 grep -q ^CAP_NET_BIND_SERVICE $BCAPFILE || echo CAP_NET_BIND_SERVICE >> $BCAPFILE
171 # useful for f15 guests that use set_cap_file
172 grep -q ^CAP_SETFCAP $BCAPFILE || echo CAP_SETFCAP >> $BCAPFILE
175 # Set persistent for the network context
176 # Thierry: Daniel's kernels come with single_ip turned off by default, let's make this explicit
177 echo "persistent,lback_allow,~single_ip" > /etc/vservers/$vserver/nflags
180 echo -e "persistent\n~info_init" > /etc/vservers/$vserver/cflags
183 mkdir /etc/vservers/$vserver/cgroup
185 # Start Vserver automatically on boot
186 echo "default" > /etc/vservers/$vserver/apps/init/mark
188 # Set the init style of your vserver to plain for f13 and higher
190 f1[3-9]) echo plain > /etc/vservers/$vserver/apps/init/style ;;
193 if [ "$pkg_method" = "yum" ] ; then
194 $personality vyum $vserver -- -y install yum
196 for i in $(seq 20) ; do
197 $personality vserver $VERBOSE $vserver pkgmgmt internalize && break || true
198 echo "* ${i}-th attempt to 'vserver pkgmgmt internalize' failed - waiting for 3 seconds"
203 # start the vserver so we can do the following operations
204 # redirect out/err to protect against the vserver's init sequence getting stalled
205 # mostly used for f10 vservers created remotely through ssh
206 $personality vserver $VERBOSE $vserver start >& /dev/null
208 if [ "$pkg_method" == "yum" ] ; then
209 $personality vserver $VERBOSE $vserver exec sh -c "rm -f /var/lib/rpm/__db*"
211 # run the host rpmdb_dump and restore with the guest rpmdb_load
212 function translate_rpm_hashes () {
215 local personality="$1"; shift
216 local vserver="$1"; shift
217 # need to have utilities installed
221 guest_dir=/var/lib/rpm
222 host_dir=/vservers/$vserver/$guest_dir
223 files=$(cd $host_dir ; file * | grep Hash | cut -d: -f 1)
224 for file in $files; do
225 (cd $host_dir && mv $file ${file}-foreign)
226 /usr/lib/rpm/rpmdb_dump $host_dir/${file}-foreign | $personality vserver $VERBOSE $vserver exec /usr/lib/rpm/rpmdb_load $guest_dir/$file
228 $personality vserver $VERBOSE $vserver exec rpm --rebuilddb
232 # try the simple way, if that fails try to cross fix the rpm hashes
233 $personality vserver $VERBOSE $vserver exec rpm --rebuilddb || translate_rpm_hashes $personality $vserver
236 # check if the vserver kernel is using VSERVER_DEVICE (vdevmap) support
237 need_vdevmap=$(grep "CONFIG_VSERVER_DEVICE=y" /boot/config-$(uname -r) | wc -l)
239 if [ $need_vdevmap -eq 1 ] ; then
240 ctx=$(cat /etc/vservers/$vserver/context)
241 vdevmap --set --xid $ctx --open --create --target /dev/null
242 vdevmap --set --xid $ctx --open --create --target /dev/root
245 # minimal config in the vserver for yum to work
246 [ "$pkg_method" = "yum" ] && configure_yum_in_vserver $vserver $fcdistro
249 cp /etc/resolv.conf /vservers/$vserver/etc/resolv.conf
250 # and /etc/hosts for at least localhost
251 [ -f /vservers/$vserver/etc/hosts ] || echo "127.0.0.1 localhost localhost.localdomain" > /vservers/$vserver/etc/hosts
255 function devel_or_vtest_tools () {
264 personality=$1; shift
266 pkg_method=$(package_method $fcdistro)
268 # check for .pkgs file based on pldistro
269 if [ -n "$VBUILD_MODE" ] ; then
274 pkgsfile=$(pl_locateDistroFile $DIRNAME $pldistro $pkgsname)
276 ### install individual packages, then groups
277 # get target arch - use uname -i here (we want either x86_64 or i386)
278 vserver_arch=$($personality vserver $vserver exec uname -i)
279 # on debian systems we get arch through the 'arch' command
280 [ "$vserver_arch" = "unknown" ] && vserver_arch=$($personality vserver $vserver exec arch)
282 packages=$(pl_getPackages -a $vserver_arch $fcdistro $pldistro $pkgsfile)
283 groups=$(pl_getGroups -a $vserver_arch $fcdistro $pldistro $pkgsfile)
285 case "$pkg_method" in
287 [ -n "$packages" ] && $personality vserver $vserver exec yum -y install $packages
288 for group_plus in $groups; do
289 group=$(echo $group_plus | sed -e "s,+++, ,g")
290 $personality vserver $vserver exec yum -y groupinstall "$group"
292 # store current rpm list in /init-vserver.rpms in case we need to check the contents
293 $personality vserver $vserver exec rpm -aq > /vservers/$vserver/init-vserver.rpms
296 $personality vserver $vserver exec apt-get update
297 for package in $packages ; do
298 $personality vserver $vserver exec apt-get install -y $package
300 ### xxx todo install groups with apt..
303 echo "unknown pkg_method $pkg_method"
310 function post_install () {
311 if [ -n "$VBUILD_MODE" ] ; then
312 post_install_vbuild "$@"
314 post_install_myplc "$@"
316 # setup localtime from the host
318 cp /etc/localtime /vservers/$vserver/etc/localtime
321 function post_install_vbuild () {
328 personality=$1; shift
330 ### From myplc-devel-native.spec
331 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
332 cat << EOF | $personality vserver $VERBOSE $vserver exec bash -x
333 # set up /dev/loop* in vserver
334 for i in \$(seq 0 255) ; do
335 mknod -m 640 /dev/loop\$i b 7 \$i
338 # create symlink for /dev/fd
339 [ ! -e "/dev/fd" ] && ln -s /proc/self/fd /dev/fd
341 # modify /etc/rpm/macros to not use /sbin/new-kernel-pkg
342 sed -i 's,/sbin/new-kernel-pkg:,,' /etc/rpm/macros
343 if [ -h "/sbin/new-kernel-pkg" ] ; then
344 filename=\$(readlink -f /sbin/new-kernel-pkg)
345 if [ "\$filename" == "/sbin/true" ] ; then
346 echo "WARNING: /sbin/new-kernel-pkg symlinked to /sbin/true"
347 echo "\tmost likely /etc/rpm/macros has /sbin/new-kernel-pkg declared in _netsharedpath."
348 echo "\tPlease remove /sbin/new-kernel-pkg from _netsharedpath and reintall mkinitrd."
353 # customize root's prompt
354 cat << PROFILE > /root/.profile
355 export PS1="[$vserver] \\w # "
361 # add a "build" user to the system
362 builduser=\$(grep "^build:" /etc/passwd | wc -l)
363 if [ \$builduser -eq 0 ] ; then
364 groupadd -o -g \$gid build;
365 useradd -o -c 'Automated Build' -u \$uid -g \$gid -n -M -s /bin/bash build;
368 # Allow build user to build certain RPMs as root
369 if [ -f /etc/sudoers ] ; then
370 buildsudo=\$(grep "^build.*ALL=(ALL).*NOPASSWD:.*ALL" /etc/sudoers | wc -l)
371 if [ \$buildsudo -eq 0 ] ; then
372 echo "build ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
374 sed -i 's,^Defaults.*requiretty,#Defaults requiretty,' /etc/sudoers
381 function post_install_myplc () {
387 personality=$1; shift
389 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
390 cat << EOF | $personality vserver $VERBOSE $vserver exec bash -x
392 # create /etc/sysconfig/network if missing
393 [ -f /etc/sysconfig/network ] || echo NETWORKING=yes > /etc/sysconfig/network
395 # create symlink for /dev/fd
396 [ ! -e "/dev/fd" ] && ln -s /proc/self/fd /dev/fd
398 # turn off regular crond, as plc invokes plc_crond
401 # take care of loginuid in /etc/pam.d
402 sed -i "s,#*\(.*loginuid.*\),#\1," /etc/pam.d/*
404 # customize root's prompt
405 cat << PROFILE > /root/.profile
406 export PS1="[$vserver] \\w # "
412 # parses ifconfig's output to find out ip address and mask
413 # will then be passed to vserver as e.g. --interface 138.96.250.126/255.255.0.0
414 # default is to use lo, that's enough for local mirrors
415 # use -i eth0 in case your fedora mirror is on a separate box on the network
416 function vserverIfconfig () {
419 line=$(ifconfig $ifname 2> /dev/null | grep 'inet addr')
420 if [ -n "$line" ] ; then
422 for word in "$@" ; do
423 addr=$(echo $word | sed -e s,[aA][dD][dD][rR]:,,)
424 mask=$(echo $word | sed -e s,[mM][aA][sS][kK]:,,)
425 if [ "$word" != "$addr" ] ; then
427 elif [ "$word" != "$mask" ] ; then
428 result="${result}/${mask}"
432 if [ -z "$result" ] ; then
433 echo "vserverIfconfig failed to locate $ifname"
442 echo "Usage: $COMMAND_VBUILD [options] vserver-name [ -- vserver-options ]"
443 echo "Usage: $COMMAND_MYPLC [options] vserver-name repo-url [ -- vserver-options ]"
444 echo "Requirements: you need to have a vserver-compliant kernel,"
445 echo " as well as the util-vserver RPM installed"
447 echo " This command creates a fresh vserver instance, for building, or running, myplc"
448 echo "Supported options"
449 echo " -f fcdistro - for creating the root filesystem - defaults to $DEFAULT_FCDISTRO"
450 echo " -d pldistro - defaults to $DEFAULT_PLDISTRO"
451 echo " -p personality - defaults to $DEFAULT_PERSONALITY"
452 echo " -i ifname: determines ip and netmask attached to ifname, and passes it to the vserver"
453 echo " -v : verbose - passes -v to calls to vserver"
454 echo "vserver-options"
455 echo " all args after the optional -- are passed to vserver <name> build <options>"
456 echo " typical usage is e.g. --interface eth0:200.150.100.10/24"
457 echo "With $COMMAND_MYPLC you can give 'none' as the URL, in which case"
458 echo " myplc.repo does not get created"
480 while getopts "f:d:p:i:v" opt ; do
482 f) fcdistro=$OPTARG;;
483 d) pldistro=$OPTARG;;
484 p) personality=$OPTARG;;
491 shift $(($OPTIND - 1))
493 # parse fixed arguments
494 [[ -z "$@" ]] && usage
496 if [ -n "$MYPLC_MODE" ] ; then
497 [[ -z "$@" ]] && usage
501 # parse vserver options
502 if [[ -n "$@" ]] ; then
503 if [ "$1" == "--" ] ; then
511 # with new util-vserver, it is mandatory to provide an IP even for building
512 if [ -n "$VBUILD_MODE" ] ; then
513 [ -z "$IFNAME" ] && IFNAME=$DEFAULT_IFNAME
515 if [ -n "$IFNAME" ] ; then
516 localip=$(vserverIfconfig $IFNAME)
517 VSERVER_OPTIONS="$VSERVER_OPTIONS --interface $localip"
520 [ -z "$fcdistro" ] && fcdistro=$DEFAULT_FCDISTRO
521 [ -z "$pldistro" ] && pldistro=$DEFAULT_PLDISTRO
522 [ -z "$personality" ] && personality=$DEFAULT_PERSONALITY
524 setup_vserver $vserver $fcdistro $personality
525 devel_or_vtest_tools $vserver $fcdistro $pldistro $personality
526 post_install $vserver $personality