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 f15 and higher
189 # not working with f15 anyways, systemd requires 2.6.36 to work
191 f1[5-9]) echo plain > /etc/vservers/$vserver/apps/init/style ;;
194 if [ "$pkg_method" = "yum" ] ; then
195 $personality vyum $vserver -- -y install yum
197 for i in $(seq 20) ; do
198 $personality vserver $VERBOSE $vserver pkgmgmt internalize && break || true
199 echo "* ${i}-th attempt to 'vserver pkgmgmt internalize' failed - waiting for 3 seconds"
204 # start the vserver so we can do the following operations
205 # redirect out/err to protect against the vserver's init sequence getting stalled
206 # mostly used for f10 vservers created remotely through ssh
207 $personality vserver $VERBOSE $vserver start >& /dev/null
209 if [ "$pkg_method" == "yum" ] ; then
210 $personality vserver $VERBOSE $vserver exec sh -c "rm -f /var/lib/rpm/__db*"
212 # run the host rpmdb_dump and restore with the guest rpmdb_load
213 function translate_rpm_hashes () {
216 local personality="$1"; shift
217 local vserver="$1"; shift
218 # need to have utilities installed
222 guest_dir=/var/lib/rpm
223 host_dir=/vservers/$vserver/$guest_dir
224 files=$(cd $host_dir ; file * | grep Hash | cut -d: -f 1)
225 for file in $files; do
226 (cd $host_dir && mv $file ${file}-foreign)
227 /usr/lib/rpm/rpmdb_dump $host_dir/${file}-foreign | $personality vserver $VERBOSE $vserver exec /usr/lib/rpm/rpmdb_load $guest_dir/$file
229 $personality vserver $VERBOSE $vserver exec rpm --rebuilddb
233 # try the simple way, if that fails try to cross fix the rpm hashes
234 $personality vserver $VERBOSE $vserver exec rpm --rebuilddb || translate_rpm_hashes $personality $vserver
237 # check if the vserver kernel is using VSERVER_DEVICE (vdevmap) support
238 need_vdevmap=$(grep "CONFIG_VSERVER_DEVICE=y" /boot/config-$(uname -r) | wc -l)
240 if [ $need_vdevmap -eq 1 ] ; then
241 ctx=$(cat /etc/vservers/$vserver/context)
242 vdevmap --set --xid $ctx --open --create --target /dev/null
243 vdevmap --set --xid $ctx --open --create --target /dev/root
246 # minimal config in the vserver for yum to work
247 [ "$pkg_method" = "yum" ] && configure_yum_in_vserver $vserver $fcdistro
250 cp /etc/resolv.conf /vservers/$vserver/etc/resolv.conf
251 # and /etc/hosts for at least localhost
252 [ -f /vservers/$vserver/etc/hosts ] || echo "127.0.0.1 localhost localhost.localdomain" > /vservers/$vserver/etc/hosts
256 function devel_or_vtest_tools () {
265 personality=$1; shift
267 pkg_method=$(package_method $fcdistro)
269 # check for .pkgs file based on pldistro
270 if [ -n "$VBUILD_MODE" ] ; then
275 pkgsfile=$(pl_locateDistroFile $DIRNAME $pldistro $pkgsname)
277 ### install individual packages, then groups
278 # get target arch - use uname -i here (we want either x86_64 or i386)
279 vserver_arch=$($personality vserver $vserver exec uname -i)
280 # on debian systems we get arch through the 'arch' command
281 [ "$vserver_arch" = "unknown" ] && vserver_arch=$($personality vserver $vserver exec arch)
283 packages=$(pl_getPackages -a $vserver_arch $fcdistro $pldistro $pkgsfile)
284 groups=$(pl_getGroups -a $vserver_arch $fcdistro $pldistro $pkgsfile)
286 case "$pkg_method" in
288 [ -n "$packages" ] && $personality vserver $vserver exec yum -y install $packages
289 for group_plus in $groups; do
290 group=$(echo $group_plus | sed -e "s,+++, ,g")
291 $personality vserver $vserver exec yum -y groupinstall "$group"
293 # store current rpm list in /init-vserver.rpms in case we need to check the contents
294 $personality vserver $vserver exec rpm -aq > /vservers/$vserver/init-vserver.rpms
297 $personality vserver $vserver exec apt-get update
298 for package in $packages ; do
299 $personality vserver $vserver exec apt-get install -y $package
301 ### xxx todo install groups with apt..
304 echo "unknown pkg_method $pkg_method"
311 function post_install () {
312 if [ -n "$VBUILD_MODE" ] ; then
313 post_install_vbuild "$@"
315 post_install_myplc "$@"
317 # setup localtime from the host
319 cp /etc/localtime /vservers/$vserver/etc/localtime
322 function post_install_vbuild () {
329 personality=$1; shift
331 ### From myplc-devel-native.spec
332 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
333 cat << EOF | $personality vserver $VERBOSE $vserver exec bash -x
334 # set up /dev/loop* in vserver
335 for i in \$(seq 0 255) ; do
336 mknod -m 640 /dev/loop\$i b 7 \$i
339 # create symlink for /dev/fd
340 [ ! -e "/dev/fd" ] && ln -s /proc/self/fd /dev/fd
342 # modify /etc/rpm/macros to not use /sbin/new-kernel-pkg
343 sed -i 's,/sbin/new-kernel-pkg:,,' /etc/rpm/macros
344 if [ -h "/sbin/new-kernel-pkg" ] ; then
345 filename=\$(readlink -f /sbin/new-kernel-pkg)
346 if [ "\$filename" == "/sbin/true" ] ; then
347 echo "WARNING: /sbin/new-kernel-pkg symlinked to /sbin/true"
348 echo "\tmost likely /etc/rpm/macros has /sbin/new-kernel-pkg declared in _netsharedpath."
349 echo "\tPlease remove /sbin/new-kernel-pkg from _netsharedpath and reintall mkinitrd."
354 # customize root's prompt
355 cat << PROFILE > /root/.profile
356 export PS1="[$vserver] \\w # "
362 # add a "build" user to the system
363 builduser=\$(grep "^build:" /etc/passwd | wc -l)
364 if [ \$builduser -eq 0 ] ; then
365 groupadd -o -g \$gid build;
366 useradd -o -c 'Automated Build' -u \$uid -g \$gid -n -M -s /bin/bash build;
369 # Allow build user to build certain RPMs as root
370 if [ -f /etc/sudoers ] ; then
371 buildsudo=\$(grep "^build.*ALL=(ALL).*NOPASSWD:.*ALL" /etc/sudoers | wc -l)
372 if [ \$buildsudo -eq 0 ] ; then
373 echo "build ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
375 sed -i 's,^Defaults.*requiretty,#Defaults requiretty,' /etc/sudoers
382 function post_install_myplc () {
388 personality=$1; shift
390 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
391 cat << EOF | $personality vserver $VERBOSE $vserver exec bash -x
393 # create /etc/sysconfig/network if missing
394 [ -f /etc/sysconfig/network ] || echo NETWORKING=yes > /etc/sysconfig/network
396 # create symlink for /dev/fd
397 [ ! -e "/dev/fd" ] && ln -s /proc/self/fd /dev/fd
399 # turn off regular crond, as plc invokes plc_crond
402 # take care of loginuid in /etc/pam.d
403 sed -i "s,#*\(.*loginuid.*\),#\1," /etc/pam.d/*
405 # customize root's prompt
406 cat << PROFILE > /root/.profile
407 export PS1="[$vserver] \\w # "
413 # parses ifconfig's output to find out ip address and mask
414 # will then be passed to vserver as e.g. --interface 138.96.250.126/255.255.0.0
415 # default is to use lo, that's enough for local mirrors
416 # use -i eth0 in case your fedora mirror is on a separate box on the network
417 function vserverIfconfig () {
420 line=$(ifconfig $ifname 2> /dev/null | grep 'inet addr')
421 if [ -n "$line" ] ; then
423 for word in "$@" ; do
424 addr=$(echo $word | sed -e s,[aA][dD][dD][rR]:,,)
425 mask=$(echo $word | sed -e s,[mM][aA][sS][kK]:,,)
426 if [ "$word" != "$addr" ] ; then
428 elif [ "$word" != "$mask" ] ; then
429 result="${result}/${mask}"
433 if [ -z "$result" ] ; then
434 echo "vserverIfconfig failed to locate $ifname"
443 echo "Usage: $COMMAND_VBUILD [options] vserver-name [ -- vserver-options ]"
444 echo "Usage: $COMMAND_MYPLC [options] vserver-name repo-url [ -- vserver-options ]"
445 echo "Requirements: you need to have a vserver-compliant kernel,"
446 echo " as well as the util-vserver RPM installed"
448 echo " This command creates a fresh vserver instance, for building, or running, myplc"
449 echo "Supported options"
450 echo " -f fcdistro - for creating the root filesystem - defaults to $DEFAULT_FCDISTRO"
451 echo " -d pldistro - defaults to $DEFAULT_PLDISTRO"
452 echo " -p personality - defaults to $DEFAULT_PERSONALITY"
453 echo " -i ifname: determines ip and netmask attached to ifname, and passes it to the vserver"
454 echo " -v : verbose - passes -v to calls to vserver"
455 echo "vserver-options"
456 echo " all args after the optional -- are passed to vserver <name> build <options>"
457 echo " typical usage is e.g. --interface eth0:200.150.100.10/24"
458 echo "With $COMMAND_MYPLC you can give 'none' as the URL, in which case"
459 echo " myplc.repo does not get created"
481 while getopts "f:d:p:i:v" opt ; do
483 f) fcdistro=$OPTARG;;
484 d) pldistro=$OPTARG;;
485 p) personality=$OPTARG;;
492 shift $(($OPTIND - 1))
494 # parse fixed arguments
495 [[ -z "$@" ]] && usage
497 if [ -n "$MYPLC_MODE" ] ; then
498 [[ -z "$@" ]] && usage
502 # parse vserver options
503 if [[ -n "$@" ]] ; then
504 if [ "$1" == "--" ] ; then
512 # with new util-vserver, it is mandatory to provide an IP even for building
513 if [ -n "$VBUILD_MODE" ] ; then
514 [ -z "$IFNAME" ] && IFNAME=$DEFAULT_IFNAME
516 if [ -n "$IFNAME" ] ; then
517 localip=$(vserverIfconfig $IFNAME)
518 VSERVER_OPTIONS="$VSERVER_OPTIONS --interface $localip"
521 [ -z "$fcdistro" ] && fcdistro=$DEFAULT_FCDISTRO
522 [ -z "$pldistro" ] && pldistro=$DEFAULT_PLDISTRO
523 [ -z "$personality" ] && personality=$DEFAULT_PERSONALITY
525 setup_vserver $vserver $fcdistro $personality
526 devel_or_vtest_tools $vserver $fcdistro $pldistro $personality
527 post_install $vserver $personality