8 # pkgs parsing utilities
9 PATH=$(dirname $0):$PATH . build.common
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 pushd /etc/vservers/.distributions/${fcdistro}
36 if [ -f yum/yum.conf ] ; then
37 echo "Initializing yum.conf in $vserver from $(pwd)/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 " 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 yum.repos.d ] ; then
50 echo "Initializing yum.repos.d in $vserver from $(pwd)/yum.repos.d"
51 rm -rf /vservers/$vserver/etc/yum.repos.d
52 tar 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 i in /vservers/$vserver/etc/yum.repos.d/* ; do
63 [ -f $i ] && echo "exclude=kernel* ulogd iptables" >> $i
65 # the build repo is not signed at this stage
66 cat > /vservers/$vserver/etc/yum.repos.d/myplc.repo <<EOF
78 function setup_vserver () {
88 if [ -d /vservers/$vserver ] ; then
89 echo "$COMMAND : vserver $vserver seems to exist - bailing out"
94 # try to work around the vserver issue:
95 # vc_ctx_migrate: No such process
96 # rpm-fake.so: failed to initialize communication with resolver
97 for i in $(seq 20) ; do
98 $personality vserver $VERBOSE $vserver build $VSERVER_OPTIONS -m yum -- -d $fcdistro && break || true
99 echo "* ${i}-th attempt to 'vserver build' failed - waiting for 3 seconds"
103 [ -d /vservers/$vserver ]
105 if [ ! -z "$personality" ] ; then
106 if [ -f "/etc/vservers/$vserver/personality" ] ; then
107 registered_personality=$(grep $personality /etc/vservers/$vserver/personality | wc -l)
109 registered_personality=0
111 if [ $registered_personality -eq 0 -a "$personality" != "linux64" ] ; then
112 echo $personality >> /etc/vservers/$vserver/personality
116 if [ -n "$VBUILD_MODE" ] ; then
117 ### capabilities required for a build vserver
118 # set up appropriate vserver capabilities to mount, mknod and IPC_LOCK
119 BCAPFILE=/etc/vservers/$vserver/bcapabilities
121 cap=$(grep ^CAP_SYS_ADMIN /etc/vservers/$vserver/bcapabilities | wc -l)
122 [ $cap -eq 0 ] && echo 'CAP_SYS_ADMIN' >> /etc/vservers/$vserver/bcapabilities
123 cap=$(grep ^CAP_MKNOD /etc/vservers/$vserver/bcapabilities | wc -l)
124 [ $cap -eq 0 ] && echo 'CAP_MKNOD' >> /etc/vservers/$vserver/bcapabilities
125 cap=$(grep ^CAP_IPC_LOCK /etc/vservers/$vserver/bcapabilities | wc -l)
126 [ $cap -eq 0 ] && echo 'CAP_IPC_LOCK' >> /etc/vservers/$vserver/bcapabilities
128 ### capabilities required for a myplc vserver
129 # for /etc/plc.d/gpg - need to init /dev/random
130 cap=$(grep ^CAP_MKNOD /etc/vservers/$vserver/bcapabilities | wc -l)
131 [ $cap -eq 0 ] && echo 'CAP_MKNOD' >> /etc/vservers/$vserver/bcapabilities
132 cap=$(grep ^CAP_NET_BIND_SERVICE /etc/vservers/$vserver/bcapabilities | wc -l)
133 [ $cap -eq 0 ] && echo 'CAP_NET_BIND_SERVICE' >> /etc/vservers/$vserver/bcapabilities
136 $personality vyum $vserver -- -y install yum
138 for i in $(seq 20) ; do
139 $personality vserver $VERBOSE $vserver pkgmgmt internalize && break || true
140 echo "* ${i}-th attempt to 'vserver pkgmgmt internalize' failed - waiting for 3 seconds"
144 # start the vserver so we can do the following operations
145 $personality vserver $VERBOSE $vserver start
146 $personality vserver $VERBOSE $vserver exec sh -c "rm -f /var/lib/rpm/__db*"
147 $personality vserver $VERBOSE $vserver exec rpm --rebuilddb
149 # with vserver 2.3, granting the vserver CAP_MKNOD is not enough
150 # check whether we run vs2.3 or above
151 vs_version=$(uname -a | sed -e 's,.*[\.\-]vs\([0-9]\)\.\([0-9]\)\..*,\1\2,')
152 # at this stage we have here 22 or 23
153 need_vdevmap=$(( $vs_version >= 23 ))
155 if [ "$need_vdevmap" == 1 ] ; then
157 vdevmap --help | grep -- --set &> /dev/null && util_vserver_215=1
159 if [ "$util_vserver_215" == 1 ] ; then
160 ctx=$(cat /etc/vservers/$vserver/context)
161 vdevmap --set --xid $ctx --open --create --target /dev/null
162 vdevmap --set --xid $ctx --open --create --target /dev/root
164 echo "You seem to be running vs2.3 with util-vserver < 0.30.215"
165 echo "This combination is not supported by $COMMAND"
166 echo "Please upgrade your environment"
168 # this supposedly is an equivalent to using vdevmap as invoked above
169 # but it's not going to work in this case
170 # mkdir -p /etc/vservers/$vserver/apps/vdevmap/default-{block,char}
171 # touch /etc/vservers/$vserver/apps/vdevmap/default-{block,char}/{open,create}
172 # echo /dev/root > /etc/vservers/$vserver/apps/vdevmap/default-block/target
173 # echo /dev/null > /etc/vservers/$vserver/apps/vdevmap/default-char/target
177 # minimal config in the vserver for yum to work
178 configure_yum_in_vserver $vserver $fcdistro
181 cp /etc/resolv.conf /vservers/$vserver/etc/resolv.conf
182 # and /etc/hosts for at least localhost
183 [ -f /vservers/$vserver/etc/hosts ] || echo "127.0.0.1 localhost localhost.localdomain" > /vservers/$vserver/etc/hosts
186 function devel_or_vtest_tools () {
195 personality=$1; shift
197 # check for .pkgs file based on pldistro
198 if [ -n "$VBUILD_MODE" ] ; then
203 pkgsfile=$(pl_locateDistroFile $DIRNAME $pldistro $pkgsname)
205 ### install individual packages, then groups
206 # get target arch - use uname -i here (we want either x86_64 or i386)
207 vserver_arch=$($personality vserver $vserver exec uname -i)
209 packages=$(pl_getPackages -a $vserver_arch $fcdistro $pldistro $pkgsfile)
210 groups=$(pl_getGroups -a $vserver_arch $fcdistro $pldistro $pkgsfile)
212 [ -n "$packages" ] && $personality vserver $vserver exec yum -y install $packages
213 [ -n "$groups" ] && $personality vserver $vserver exec yum -y groupinstall $groups
217 function post_install () {
218 if [ -n "$VBUILD_MODE" ] ; then
219 post_install_vbuild "$@"
221 post_install_myplc "$@"
223 # setup localtime from the host
225 cp /etc/localtime /vservers/$vserver/etc/localtime
228 function post_install_vbuild () {
235 personality=$1; shift
237 ### From myplc-devel-native.spec
238 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
239 cat << EOF | $personality vserver $VERBOSE $vserver exec bash -x
240 # set up /dev/loop* in vserver
241 for i in \$(seq 0 255) ; do
242 mknod -m 640 /dev/loop\$i b 7 \$i
245 # create symlink for /dev/fd
246 [ ! -e "/dev/fd" ] && ln -s /proc/self/fd /dev/fd
248 # modify /etc/rpm/macros to not use /sbin/new-kernel-pkg
249 sed -i 's,/sbin/new-kernel-pkg:,,' /etc/rpm/macros
250 if [ -h "/sbin/new-kernel-pkg" ] ; then
251 filename=\$(readlink -f /sbin/new-kernel-pkg)
252 if [ "\$filename" == "/sbin/true" ] ; then
253 echo "WARNING: /sbin/new-kernel-pkg symlinked to /sbin/true"
254 echo "\tmost likely /etc/rpm/macros has /sbin/new-kernel-pkg declared in _netsharedpath."
255 echo "\tPlease remove /sbin/new-kernel-pkg from _netsharedpath and reintall mkinitrd."
260 # customize root's prompt
261 cat << PROFILE > /root/.profile
262 export PS1="[$vserver] \\w # "
268 # add a "build" user to the system
269 builduser=\$(grep "^build:" /etc/passwd | wc -l)
270 if [ \$builduser -eq 0 ] ; then
271 groupadd -o -g \$gid build;
272 useradd -o -c 'Automated Build' -u \$uid -g \$gid -n -M -s /bin/bash build;
275 # Allow build user to build certain RPMs as root
276 if [ -f /etc/sudoers ] ; then
277 buildsudo=\$(grep "^build.*ALL=(ALL).*NOPASSWD:.*ALL" /etc/sudoers | wc -l)
278 if [ \$buildsudo -eq 0 ] ; then
279 echo "build ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
281 sed -i 's,^Defaults.*requiretty,#Defaults requiretty,' /etc/sudoers
288 function post_install_myplc () {
294 personality=$1; shift
296 # be careful to backslash $ in this, otherwise it's the root context that's going to do the evaluation
297 cat << EOF | $personality vserver $VERBOSE $vserver exec bash -x
299 # create symlink for /dev/fd
300 [ ! -e "/dev/fd" ] && ln -s /proc/self/fd /dev/fd
302 # turn off regular crond, as plc invokes plc_crond
305 # customize root's prompt
306 cat << PROFILE > /root/.profile
307 export PS1="[$vserver] \\w # "
313 # parses ifconfig's output to find out ip address and mask
314 # will then be passed to vserver as e.g. --interface 138.96.250.126/255.255.0.0
315 # default is to use lo, that's enough for local mirrors
316 # use -i eth0 in case your fedora mirror is on a separate box on the network
317 function vserverIfconfig () {
320 line=$(ifconfig $ifname 2> /dev/null | grep 'inet addr')
321 if [ -n "$line" ] ; then
323 for word in "$@" ; do
324 addr=$(echo $word | sed -e s,[aA][dD][dD][rR]:,,)
325 mask=$(echo $word | sed -e s,[mM][aA][sS][kK]:,,)
326 if [ "$word" != "$addr" ] ; then
328 elif [ "$word" != "$mask" ] ; then
329 result="${result}/${mask}"
333 if [ -z "$result" ] ; then
334 echo "vserverIfconfig failed to locate $ifname"
343 echo "Usage: $COMMAND_VBUILD [options] vserver-name [ -- vserver-options ]"
344 echo "Usage: $COMMAND_MYPLC [options] vserver-name repo-url [ -- vserver-options ]"
345 echo "Requirements: you need to have a vserver-compliant kernel,"
346 echo " as well as the util-vserver RPM installed"
348 echo " This command creates a fresh vserver instance, for building, or running, myplc"
349 echo "Supported options"
350 echo " -f fcdistro - for creating the root filesystem - defaults to $DEFAULT_FCDISTRO"
351 echo " -d pldistro - defaults to $DEFAULT_PLDISTRO"
352 echo " -p personality - defaults to $DEFAULT_PERSONALITY"
353 echo " -i ifname: determines ip and netmask attached to ifname, and passes it to the vserver"
354 echo " -v : verbose - passes -v to calls to vserver"
355 echo "vserver-options"
356 echo " all args after the optional -- are passed to vserver <name> build <options>"
357 echo " typical usage is e.g. --interface eth0:200.150.100.10/24"
379 while getopts "f:d:p:i:v" opt ; do
381 f) fcdistro=$OPTARG;;
382 d) pldistro=$OPTARG;;
383 p) personality=$OPTARG;;
390 shift $(($OPTIND - 1))
392 # parse fixed arguments
393 [[ -z "$@" ]] && usage
395 if [ -n "$MYPLC_MODE" ] ; then
396 [[ -z "$@" ]] && usage
400 # parse vserver options
401 if [[ -n "$@" ]] ; then
402 if [ "$1" == "--" ] ; then
410 # with new util-vserver, it is mandatory to provide an IP even for building
411 if [ -n "$VBUILD_MODE" ] ; then
412 [ -z "$IFNAME" ] && IFNAME=$DEFAULT_IFNAME
414 if [ -n "$IFNAME" ] ; then
415 localip=$(vserverIfconfig $IFNAME)
416 VSERVER_OPTIONS="$VSERVER_OPTIONS --interface $localip"
419 [ -z "$fcdistro" ] && fcdistro=$DEFAULT_FCDISTRO
420 [ -z "$pldistro" ] && pldistro=$DEFAULT_PLDISTRO
421 [ -z "$personality" ] && personality=$DEFAULT_PERSONALITY
423 setup_vserver $vserver $fcdistro $personality
424 devel_or_vtest_tools $vserver $fcdistro $pldistro $personality
425 post_install $vserver $personality