8 # pkgs parsing utilities
9 PATH=$(dirname $0):$PATH . build.common
11 DEFAULT_FCDISTRO=centos5
12 DEFAULT_PLDISTRO=planetlab
13 DEFAULT_PERSONALITY=linux32
16 COMMAND_VBUILD="vbuild-init-vserver.sh"
17 COMMAND_MYPLC="vtest-init-vserver.sh"
20 echo "$COMMAND : Bailing out"
24 # overwrite vserver's internal yum config from what is in
25 # .distributions/<distrib>/yum/yum.conf and /yum.repos.d
27 function configure_yum_in_vserver () {
35 templates=/etc/vservers/.distributions/${fcdistro}
36 if [ -f ${templates}/yum/yum.conf ] ; then
37 echo "Initializing yum.conf in $vserver from ${templates}/yum"
38 sed -e "s!@YUMETCDIR@!/etc!g;
39 s!@YUMCACHEDIR@!/var/cache/yum!g;
40 s!@YUMLOGDIR@!/var/log!g;
41 s!@YUMLOCKDIR@!/var/lock!g;
42 " ${templates}/yum/yum.conf > /vservers/$vserver/etc/yum.conf
44 # post process the various @...@ variables from this yum.conf file.
46 echo "Using $fcdistro default for yum.conf"
49 if [ -d ${templates}/yum.repos.d ] ; then
50 echo "Initializing yum.repos.d in $vserver from ${templates}/yum.repos.d"
51 rm -rf /vservers/$vserver/etc/yum.repos.d
52 tar -C ${templates} -cf - yum.repos.d | tar -C /vservers/$vserver/etc -xvf -
54 echo "Cannot initialize yum.repos.d in $vserver"
57 if [ -n "$MYPLC_MODE" ] ; then
58 if [ ! -d /vservers/$vserver/etc/yum.repos.d ] ; then
59 echo "WARNING : cannot create myplc repo"
61 # exclude kernel from fedora repos
62 for repo in /vservers/$vserver/etc/yum.repos.d/* ; do
63 [ -f $repo ] && yumconf_exclude $repo "exclude=$pl_KEXCLUDES"
65 # the build repo is not signed at this stage
66 cat > /vservers/$vserver/etc/yum.repos.d/myplc.repo <<EOF
77 # return yum or debootstrap
78 function package_method () {
81 f[0-9]*|centos[0-9]*) echo yum ;;
82 lenny|etch) echo debootstrap ;;
83 *) echo Unknown distro $fcdistro ;;
87 # return arch from debian distro and personality
88 function canonical_arch () {
91 case $(package_method $fcdistro) in
93 case $personality in *32) echo i386 ;; *64) echo x86_64 ;; *) echo Unknown-arch-1 ;; esac ;;
95 case $personality in *32) echo i386 ;; *64) echo amd64 ;; *) echo Unknown-arch-2 ;; esac ;;
97 echo Unknown-arch-3 ;;
101 function setup_vserver () {
109 personality=$1; shift
111 if [ -d /vservers/$vserver ] ; then
112 echo "$COMMAND : vserver $vserver seems to exist - bailing out"
116 pkg_method=$(package_method $fcdistro)
119 build_options="-m yum -- -d $fcdistro"
122 arch=$(canonical_arch $personality $fcdistro)
123 build_options="-m debootstrap -- -d $fcdistro -- --arch $arch"
126 build_options="something wrong" ;;
130 # try to work around the vserver issue:
131 # vc_ctx_migrate: No such process
132 # rpm-fake.so: failed to initialize communication with resolver
133 for i in $(seq 20) ; do
134 $personality vserver $VERBOSE $vserver build $VSERVER_OPTIONS $build_options && break || true
135 echo "* ${i}-th attempt to 'vserver build' failed - waiting for 3 seconds"
139 [ -d /vservers/$vserver ]
141 if [ ! -z "$personality" ] ; then
142 if [ -f "/etc/vservers/$vserver/personality" ] ; then
143 registered_personality=$(grep $personality /etc/vservers/$vserver/personality | wc -l)
145 registered_personality=0
147 if [ $registered_personality -eq 0 -a "$personality" != "linux64" ] ; then
148 echo $personality >> /etc/vservers/$vserver/personality
152 if [ -n "$VBUILD_MODE" ] ; then
153 ### capabilities required for a build vserver
154 # set up appropriate vserver capabilities to mount, mknod and IPC_LOCK
155 BCAPFILE=/etc/vservers/$vserver/bcapabilities
157 cap=$(grep ^CAP_SYS_ADMIN /etc/vservers/$vserver/bcapabilities | wc -l)
158 [ $cap -eq 0 ] && echo 'CAP_SYS_ADMIN' >> /etc/vservers/$vserver/bcapabilities
159 cap=$(grep ^CAP_MKNOD /etc/vservers/$vserver/bcapabilities | wc -l)
160 [ $cap -eq 0 ] && echo 'CAP_MKNOD' >> /etc/vservers/$vserver/bcapabilities
161 cap=$(grep ^CAP_IPC_LOCK /etc/vservers/$vserver/bcapabilities | wc -l)
162 [ $cap -eq 0 ] && echo 'CAP_IPC_LOCK' >> /etc/vservers/$vserver/bcapabilities
164 ### capabilities required for a myplc vserver
165 # for /etc/plc.d/gpg - need to init /dev/random
166 cap=$(grep ^CAP_MKNOD /etc/vservers/$vserver/bcapabilities | wc -l)
167 [ $cap -eq 0 ] && echo 'CAP_MKNOD' >> /etc/vservers/$vserver/bcapabilities
168 cap=$(grep ^CAP_NET_BIND_SERVICE /etc/vservers/$vserver/bcapabilities | wc -l)
169 [ $cap -eq 0 ] && echo 'CAP_NET_BIND_SERVICE' >> /etc/vservers/$vserver/bcapabilities
172 if [ "$pkg_method" = "yum" ] ; then
173 $personality vyum $vserver -- -y install yum
175 for i in $(seq 20) ; do
176 $personality vserver $VERBOSE $vserver pkgmgmt internalize && break || true
177 echo "* ${i}-th attempt to 'vserver pkgmgmt internalize' failed - waiting for 3 seconds"
182 # start the vserver so we can do the following operations
183 $personality vserver $VERBOSE $vserver start
184 [ "$pkg_method" = "yum" ] && $personality vserver $VERBOSE $vserver exec sh -c "rm -f /var/lib/rpm/__db*"
185 [ "$pkg_method" = "yum" ] && $personality vserver $VERBOSE $vserver exec rpm --rebuilddb
187 # with vserver 2.3, granting the vserver CAP_MKNOD is not enough
188 # check whether we run vs2.3 or above
189 vs_version=$(uname -a | sed -e 's,.*[\.\-]vs\([0-9]\)\.\([0-9]\)\..*,\1\2,')
190 # at this stage we have here 22 or 23
191 need_vdevmap=$(( $vs_version >= 23 ))
193 if [ "$need_vdevmap" == 1 ] ; then
194 ctx=$(cat /etc/vservers/$vserver/context)
195 vdevmap --set --xid $ctx --open --create --target /dev/null
196 vdevmap --set --xid $ctx --open --create --target /dev/root
199 # minimal config in the vserver for yum to work
200 [ "$pkg_method" = "yum" ] && configure_yum_in_vserver $vserver $fcdistro
203 cp /etc/resolv.conf /vservers/$vserver/etc/resolv.conf
204 # and /etc/hosts for at least localhost
205 [ -f /vservers/$vserver/etc/hosts ] || echo "127.0.0.1 localhost localhost.localdomain" > /vservers/$vserver/etc/hosts
208 function devel_or_vtest_tools () {
217 personality=$1; shift
219 pkg_method=$(package_method $fcdistro)
221 # check for .pkgs file based on pldistro
222 if [ -n "$VBUILD_MODE" ] ; then
227 pkgsfile=$(pl_locateDistroFile $DIRNAME $pldistro $pkgsname)
229 ### install individual packages, then groups
230 # get target arch - use uname -i here (we want either x86_64 or i386)
231 vserver_arch=$($personality vserver $vserver exec uname -i)
232 # on debian systems we get arch through the 'arch' command
233 [ "$vserver_arch" = "unknown" ] && vserver_arch=$($personality vserver $vserver exec arch)
235 packages=$(pl_getPackages -a $vserver_arch $fcdistro $pldistro $pkgsfile)
236 groups=$(pl_getGroups -a $vserver_arch $fcdistro $pldistro $pkgsfile)
238 [ "$pkg_method" = yum ] && [ -n "$packages" ] && $personality vserver $vserver exec yum -y install $packages
239 [ "$pkg_method" = yum ] && [ -n "$groups" ] && $personality vserver $vserver exec yum -y groupinstall $groups
241 [ "$pkg_method" = debootstrap ] && $personality vserver $vserver exec apt-get update
242 [ "$pkg_method" = debootstrap ] && for package in $packages ; do
243 $personality vserver $vserver exec apt-get install -y $package
249 function post_install () {
250 if [ -n "$VBUILD_MODE" ] ; then
251 post_install_vbuild "$@"
253 post_install_myplc "$@"
255 # setup localtime from the host
257 cp /etc/localtime /vservers/$vserver/etc/localtime
260 function post_install_vbuild () {
267 personality=$1; shift
269 ### From myplc-devel-native.spec
270 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
271 cat << EOF | $personality vserver $VERBOSE $vserver exec bash -x
272 # set up /dev/loop* in vserver
273 for i in \$(seq 0 255) ; do
274 mknod -m 640 /dev/loop\$i b 7 \$i
277 # create symlink for /dev/fd
278 [ ! -e "/dev/fd" ] && ln -s /proc/self/fd /dev/fd
280 # modify /etc/rpm/macros to not use /sbin/new-kernel-pkg
281 sed -i 's,/sbin/new-kernel-pkg:,,' /etc/rpm/macros
282 if [ -h "/sbin/new-kernel-pkg" ] ; then
283 filename=\$(readlink -f /sbin/new-kernel-pkg)
284 if [ "\$filename" == "/sbin/true" ] ; then
285 echo "WARNING: /sbin/new-kernel-pkg symlinked to /sbin/true"
286 echo "\tmost likely /etc/rpm/macros has /sbin/new-kernel-pkg declared in _netsharedpath."
287 echo "\tPlease remove /sbin/new-kernel-pkg from _netsharedpath and reintall mkinitrd."
292 # customize root's prompt
293 cat << PROFILE > /root/.profile
294 export PS1="[$vserver] \\w # "
300 # add a "build" user to the system
301 builduser=\$(grep "^build:" /etc/passwd | wc -l)
302 if [ \$builduser -eq 0 ] ; then
303 groupadd -o -g \$gid build;
304 useradd -o -c 'Automated Build' -u \$uid -g \$gid -n -M -s /bin/bash build;
307 # Allow build user to build certain RPMs as root
308 if [ -f /etc/sudoers ] ; then
309 buildsudo=\$(grep "^build.*ALL=(ALL).*NOPASSWD:.*ALL" /etc/sudoers | wc -l)
310 if [ \$buildsudo -eq 0 ] ; then
311 echo "build ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
313 sed -i 's,^Defaults.*requiretty,#Defaults requiretty,' /etc/sudoers
320 function post_install_myplc () {
326 personality=$1; shift
328 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
329 cat << EOF | $personality vserver $VERBOSE $vserver exec bash -x
331 # create /etc/sysconfig/network if missing
332 [ -f /etc/sysconfig/network ] || echo NETWORKING=yes > /etc/sysconfig/network
334 # create symlink for /dev/fd
335 [ ! -e "/dev/fd" ] && ln -s /proc/self/fd /dev/fd
337 # turn off regular crond, as plc invokes plc_crond
340 # take care of loginuid in /etc/pam.d
341 sed -i "s,#*\(.*loginuid.*\),#\1," /etc/pam.d/*
343 # customize root's prompt
344 cat << PROFILE > /root/.profile
345 export PS1="[$vserver] \\w # "
351 # parses ifconfig's output to find out ip address and mask
352 # will then be passed to vserver as e.g. --interface 138.96.250.126/255.255.0.0
353 # default is to use lo, that's enough for local mirrors
354 # use -i eth0 in case your fedora mirror is on a separate box on the network
355 function vserverIfconfig () {
358 line=$(ifconfig $ifname 2> /dev/null | grep 'inet addr')
359 if [ -n "$line" ] ; then
361 for word in "$@" ; do
362 addr=$(echo $word | sed -e s,[aA][dD][dD][rR]:,,)
363 mask=$(echo $word | sed -e s,[mM][aA][sS][kK]:,,)
364 if [ "$word" != "$addr" ] ; then
366 elif [ "$word" != "$mask" ] ; then
367 result="${result}/${mask}"
371 if [ -z "$result" ] ; then
372 echo "vserverIfconfig failed to locate $ifname"
381 echo "Usage: $COMMAND_VBUILD [options] vserver-name [ -- vserver-options ]"
382 echo "Usage: $COMMAND_MYPLC [options] vserver-name repo-url [ -- vserver-options ]"
383 echo "Requirements: you need to have a vserver-compliant kernel,"
384 echo " as well as the util-vserver RPM installed"
386 echo " This command creates a fresh vserver instance, for building, or running, myplc"
387 echo "Supported options"
388 echo " -f fcdistro - for creating the root filesystem - defaults to $DEFAULT_FCDISTRO"
389 echo " -d pldistro - defaults to $DEFAULT_PLDISTRO"
390 echo " -p personality - defaults to $DEFAULT_PERSONALITY"
391 echo " -i ifname: determines ip and netmask attached to ifname, and passes it to the vserver"
392 echo " -v : verbose - passes -v to calls to vserver"
393 echo "vserver-options"
394 echo " all args after the optional -- are passed to vserver <name> build <options>"
395 echo " typical usage is e.g. --interface eth0:200.150.100.10/24"
417 while getopts "f:d:p:i:v" opt ; do
419 f) fcdistro=$OPTARG;;
420 d) pldistro=$OPTARG;;
421 p) personality=$OPTARG;;
428 shift $(($OPTIND - 1))
430 # parse fixed arguments
431 [[ -z "$@" ]] && usage
433 if [ -n "$MYPLC_MODE" ] ; then
434 [[ -z "$@" ]] && usage
438 # parse vserver options
439 if [[ -n "$@" ]] ; then
440 if [ "$1" == "--" ] ; then
448 # with new util-vserver, it is mandatory to provide an IP even for building
449 if [ -n "$VBUILD_MODE" ] ; then
450 [ -z "$IFNAME" ] && IFNAME=$DEFAULT_IFNAME
452 if [ -n "$IFNAME" ] ; then
453 localip=$(vserverIfconfig $IFNAME)
454 VSERVER_OPTIONS="$VSERVER_OPTIONS --interface $localip"
457 [ -z "$fcdistro" ] && fcdistro=$DEFAULT_FCDISTRO
458 [ -z "$pldistro" ] && pldistro=$DEFAULT_PLDISTRO
459 [ -z "$personality" ] && personality=$DEFAULT_PERSONALITY
461 setup_vserver $vserver $fcdistro $personality
462 devel_or_vtest_tools $vserver $fcdistro $pldistro $personality
463 post_install $vserver $personality