10 # pkgs parsing utilities
11 PATH=$(dirname $0):$PATH . build.common
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 if [ -n "$MYPLC_MODE" ] ; then
60 if [ ! -d /vservers/$vserver/etc/yum.repos.d ] ; then
61 echo "WARNING : cannot create myplc repo"
63 # exclude kernel from fedora repos
64 for repo in /vservers/$vserver/etc/yum.repos.d/* ; do
65 [ -f $repo ] && yumconf_exclude $repo "exclude=$pl_KEXCLUDES"
67 # the build repo is not signed at this stage
68 cat > /vservers/$vserver/etc/yum.repos.d/myplc.repo <<EOF
79 # return yum or debootstrap
80 function package_method () {
83 f[0-9]*|centos[0-9]*) echo yum ;;
84 lenny|etch) echo debootstrap ;;
85 *) echo Unknown distro $fcdistro ;;
89 # return arch from debian distro and personality
90 function canonical_arch () {
93 case $(package_method $fcdistro) in
95 case $personality in *32) echo i386 ;; *64) echo x86_64 ;; *) echo Unknown-arch-1 ;; esac ;;
97 case $personality in *32) echo i386 ;; *64) echo amd64 ;; *) echo Unknown-arch-2 ;; esac ;;
99 echo Unknown-arch-3 ;;
103 function setup_vserver () {
111 personality=$1; shift
113 if [ -d /vservers/$vserver ] ; then
114 echo "$COMMAND : vserver $vserver seems to exist - bailing out"
118 pkg_method=$(package_method $fcdistro)
121 build_options="-m yum -- -d $fcdistro"
124 arch=$(canonical_arch $personality $fcdistro)
125 build_options="-m debootstrap -- -d $fcdistro -- --arch $arch"
128 build_options="something wrong" ;;
132 # try to work around the vserver issue:
133 # vc_ctx_migrate: No such process
134 # rpm-fake.so: failed to initialize communication with resolver
135 for i in $(seq 20) ; do
136 $personality vserver $VERBOSE $vserver build $VSERVER_OPTIONS $build_options && break || true
137 echo "* ${i}-th attempt to 'vserver build' failed - waiting for 3 seconds"
141 [ -d /vservers/$vserver ]
143 if [ ! -z "$personality" ] ; then
144 if [ -f "/etc/vservers/$vserver/personality" ] ; then
145 registered_personality=$(grep $personality /etc/vservers/$vserver/personality | wc -l)
147 registered_personality=0
149 if [ $registered_personality -eq 0 -a "$personality" != "linux64" ] ; then
150 echo $personality >> /etc/vservers/$vserver/personality
154 if [ -n "$VBUILD_MODE" ] ; then
155 ### capabilities required for a build vserver
156 # set up appropriate vserver capabilities to mount, mknod and IPC_LOCK
157 BCAPFILE=/etc/vservers/$vserver/bcapabilities
159 cap=$(grep ^CAP_SYS_ADMIN /etc/vservers/$vserver/bcapabilities | wc -l)
160 [ $cap -eq 0 ] && echo 'CAP_SYS_ADMIN' >> /etc/vservers/$vserver/bcapabilities
161 cap=$(grep ^CAP_MKNOD /etc/vservers/$vserver/bcapabilities | wc -l)
162 [ $cap -eq 0 ] && echo 'CAP_MKNOD' >> /etc/vservers/$vserver/bcapabilities
163 cap=$(grep ^CAP_IPC_LOCK /etc/vservers/$vserver/bcapabilities | wc -l)
164 [ $cap -eq 0 ] && echo 'CAP_IPC_LOCK' >> /etc/vservers/$vserver/bcapabilities
166 ### capabilities required for a myplc vserver
167 # for /etc/plc.d/gpg - need to init /dev/random
168 cap=$(grep ^CAP_MKNOD /etc/vservers/$vserver/bcapabilities | wc -l)
169 [ $cap -eq 0 ] && echo 'CAP_MKNOD' >> /etc/vservers/$vserver/bcapabilities
170 cap=$(grep ^CAP_NET_BIND_SERVICE /etc/vservers/$vserver/bcapabilities | wc -l)
171 [ $cap -eq 0 ] && echo 'CAP_NET_BIND_SERVICE' >> /etc/vservers/$vserver/bcapabilities
174 if [ "$pkg_method" = "yum" ] ; then
175 $personality vyum $vserver -- -y install yum
177 for i in $(seq 20) ; do
178 $personality vserver $VERBOSE $vserver pkgmgmt internalize && break || true
179 echo "* ${i}-th attempt to 'vserver pkgmgmt internalize' failed - waiting for 3 seconds"
184 # start the vserver so we can do the following operations
186 # echo -n ' about to start - WAITING for /tmp/go1' ; while true ; do [ -f /tmp/go1 ] && break || : ; done
187 $personality vserver $VERBOSE $vserver start
188 # echo -n ' started - WAITING for /tmp/go2' ; while true ; do [ -f /tmp/go2 ] && break || : ; done
190 # echo SKIPPING for debug --- vserver was started
192 [ "$pkg_method" = "yum" ] && $personality vserver $VERBOSE $vserver exec sh -c "rm -f /var/lib/rpm/__db*"
193 [ "$pkg_method" = "yum" ] && $personality vserver $VERBOSE $vserver exec rpm --rebuilddb
195 # check if the vserver kernel is using VSERVER_DEVICE (vdevmap) support
196 need_vdevmap=$(grep "CONFIG_VSERVER_DEVICE=y" /boot/config-$(uname -r) | wc -l)
198 if [ $need_vdevmap -eq 1 ] ; then
199 ctx=$(cat /etc/vservers/$vserver/context)
200 vdevmap --set --xid $ctx --open --create --target /dev/null
201 vdevmap --set --xid $ctx --open --create --target /dev/root
204 # minimal config in the vserver for yum to work
205 [ "$pkg_method" = "yum" ] && configure_yum_in_vserver $vserver $fcdistro
208 cp /etc/resolv.conf /vservers/$vserver/etc/resolv.conf
209 # and /etc/hosts for at least localhost
210 [ -f /vservers/$vserver/etc/hosts ] || echo "127.0.0.1 localhost localhost.localdomain" > /vservers/$vserver/etc/hosts
216 function devel_or_vtest_tools () {
225 personality=$1; shift
227 pkg_method=$(package_method $fcdistro)
229 # check for .pkgs file based on pldistro
230 if [ -n "$VBUILD_MODE" ] ; then
235 pkgsfile=$(pl_locateDistroFile $DIRNAME $pldistro $pkgsname)
237 ### install individual packages, then groups
238 # get target arch - use uname -i here (we want either x86_64 or i386)
239 vserver_arch=$($personality vserver $vserver exec uname -i)
240 # on debian systems we get arch through the 'arch' command
241 [ "$vserver_arch" = "unknown" ] && vserver_arch=$($personality vserver $vserver exec arch)
243 packages=$(pl_getPackages -a $vserver_arch $fcdistro $pldistro $pkgsfile)
244 groups=$(pl_getGroups -a $vserver_arch $fcdistro $pldistro $pkgsfile)
246 [ "$pkg_method" = yum ] && [ -n "$packages" ] && $personality vserver $vserver exec yum -y install $packages
247 [ "$pkg_method" = yum ] && [ -n "$groups" ] && $personality vserver $vserver exec yum -y groupinstall $groups
249 [ "$pkg_method" = debootstrap ] && $personality vserver $vserver exec apt-get update
250 [ "$pkg_method" = debootstrap ] && for package in $packages ; do
251 $personality vserver $vserver exec apt-get install -y $package
257 function post_install () {
258 if [ -n "$VBUILD_MODE" ] ; then
259 post_install_vbuild "$@"
261 post_install_myplc "$@"
263 # setup localtime from the host
265 cp /etc/localtime /vservers/$vserver/etc/localtime
268 function post_install_vbuild () {
275 personality=$1; shift
277 ### From myplc-devel-native.spec
278 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
279 cat << EOF | $personality vserver $VERBOSE $vserver exec bash -x
280 # set up /dev/loop* in vserver
281 for i in \$(seq 0 255) ; do
282 mknod -m 640 /dev/loop\$i b 7 \$i
285 # create symlink for /dev/fd
286 [ ! -e "/dev/fd" ] && ln -s /proc/self/fd /dev/fd
288 # modify /etc/rpm/macros to not use /sbin/new-kernel-pkg
289 sed -i 's,/sbin/new-kernel-pkg:,,' /etc/rpm/macros
290 if [ -h "/sbin/new-kernel-pkg" ] ; then
291 filename=\$(readlink -f /sbin/new-kernel-pkg)
292 if [ "\$filename" == "/sbin/true" ] ; then
293 echo "WARNING: /sbin/new-kernel-pkg symlinked to /sbin/true"
294 echo "\tmost likely /etc/rpm/macros has /sbin/new-kernel-pkg declared in _netsharedpath."
295 echo "\tPlease remove /sbin/new-kernel-pkg from _netsharedpath and reintall mkinitrd."
300 # customize root's prompt
301 cat << PROFILE > /root/.profile
302 export PS1="[$vserver] \\w # "
308 # add a "build" user to the system
309 builduser=\$(grep "^build:" /etc/passwd | wc -l)
310 if [ \$builduser -eq 0 ] ; then
311 groupadd -o -g \$gid build;
312 useradd -o -c 'Automated Build' -u \$uid -g \$gid -n -M -s /bin/bash build;
315 # Allow build user to build certain RPMs as root
316 if [ -f /etc/sudoers ] ; then
317 buildsudo=\$(grep "^build.*ALL=(ALL).*NOPASSWD:.*ALL" /etc/sudoers | wc -l)
318 if [ \$buildsudo -eq 0 ] ; then
319 echo "build ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
321 sed -i 's,^Defaults.*requiretty,#Defaults requiretty,' /etc/sudoers
328 function post_install_myplc () {
334 personality=$1; shift
336 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
337 cat << EOF | $personality vserver $VERBOSE $vserver exec bash -x
339 # create /etc/sysconfig/network if missing
340 [ -f /etc/sysconfig/network ] || echo NETWORKING=yes > /etc/sysconfig/network
342 # create symlink for /dev/fd
343 [ ! -e "/dev/fd" ] && ln -s /proc/self/fd /dev/fd
345 # turn off regular crond, as plc invokes plc_crond
348 # take care of loginuid in /etc/pam.d
349 sed -i "s,#*\(.*loginuid.*\),#\1," /etc/pam.d/*
351 # customize root's prompt
352 cat << PROFILE > /root/.profile
353 export PS1="[$vserver] \\w # "
359 # parses ifconfig's output to find out ip address and mask
360 # will then be passed to vserver as e.g. --interface 138.96.250.126/255.255.0.0
361 # default is to use lo, that's enough for local mirrors
362 # use -i eth0 in case your fedora mirror is on a separate box on the network
363 function vserverIfconfig () {
366 line=$(ifconfig $ifname 2> /dev/null | grep 'inet addr')
367 if [ -n "$line" ] ; then
369 for word in "$@" ; do
370 addr=$(echo $word | sed -e s,[aA][dD][dD][rR]:,,)
371 mask=$(echo $word | sed -e s,[mM][aA][sS][kK]:,,)
372 if [ "$word" != "$addr" ] ; then
374 elif [ "$word" != "$mask" ] ; then
375 result="${result}/${mask}"
379 if [ -z "$result" ] ; then
380 echo "vserverIfconfig failed to locate $ifname"
389 echo "Usage: $COMMAND_VBUILD [options] vserver-name [ -- vserver-options ]"
390 echo "Usage: $COMMAND_MYPLC [options] vserver-name repo-url [ -- vserver-options ]"
391 echo "Requirements: you need to have a vserver-compliant kernel,"
392 echo " as well as the util-vserver RPM installed"
394 echo " This command creates a fresh vserver instance, for building, or running, myplc"
395 echo "Supported options"
396 echo " -f fcdistro - for creating the root filesystem - defaults to $DEFAULT_FCDISTRO"
397 echo " -d pldistro - defaults to $DEFAULT_PLDISTRO"
398 echo " -p personality - defaults to $DEFAULT_PERSONALITY"
399 echo " -i ifname: determines ip and netmask attached to ifname, and passes it to the vserver"
400 echo " -v : verbose - passes -v to calls to vserver"
401 echo "vserver-options"
402 echo " all args after the optional -- are passed to vserver <name> build <options>"
403 echo " typical usage is e.g. --interface eth0:200.150.100.10/24"
425 while getopts "f:d:p:i:v" opt ; do
427 f) fcdistro=$OPTARG;;
428 d) pldistro=$OPTARG;;
429 p) personality=$OPTARG;;
436 shift $(($OPTIND - 1))
438 # parse fixed arguments
439 [[ -z "$@" ]] && usage
441 if [ -n "$MYPLC_MODE" ] ; then
442 [[ -z "$@" ]] && usage
446 # parse vserver options
447 if [[ -n "$@" ]] ; then
448 if [ "$1" == "--" ] ; then
456 # with new util-vserver, it is mandatory to provide an IP even for building
457 if [ -n "$VBUILD_MODE" ] ; then
458 [ -z "$IFNAME" ] && IFNAME=$DEFAULT_IFNAME
460 if [ -n "$IFNAME" ] ; then
461 localip=$(vserverIfconfig $IFNAME)
462 VSERVER_OPTIONS="$VSERVER_OPTIONS --interface $localip"
465 [ -z "$fcdistro" ] && fcdistro=$DEFAULT_FCDISTRO
466 [ -z "$pldistro" ] && pldistro=$DEFAULT_PLDISTRO
467 [ -z "$personality" ] && personality=$DEFAULT_PERSONALITY
469 setup_vserver $vserver $fcdistro $personality
470 devel_or_vtest_tools $vserver $fcdistro $pldistro $personality
471 post_install $vserver $personality