merge with 0.30.213
[util-vserver.git] / scripts / vserver-build.fai
1 #
2 # Copyright (C) 2006 Sam Vilain <sam.vilain@catalyst.net.nz>
3 #  
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; version 2 of the License.
7 #  
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #  
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16
17 tmp=$(getopt -o '+d:f:n:a' --long debug,pkgmgmt -n "$0" -- "$@") || exit 1
18 eval set -- "$tmp"
19
20 . "$_LIB_VSERVER_BUILD_FUNCTIONS_PKGMGMT"
21 . "$__PKGLIBDIR/vserver.functions"
22
23 DISTRIBUTION=:
24
25 FAI_VSERVER=
26 FAI_NFSROOT=/usr/lib/fai/nfsroot
27 FAI_DIR=/usr/local/share/fai
28 AUTO=1
29
30 use_pkgmgmt=
31 while true; do
32     case "$1" in
33         -f)             FAI_VSERVER=$2; shift; ;;
34         -n)             FAI_NFSROOT=$2; shift; ;;
35         -d)             FAI_DIR=$2; shift; ;;
36         -a)             AUTO=;;
37         --debug)        DEBUG=1; SH_DEBUG=-x; set -x;;
38         --)             shift; break ;;
39         *)              echo "vserver-build.fai: internal error: unrecognized option '$1'" >&2
40                         exit 1
41                         ;;
42     esac
43     shift
44 done
45
46 getDistribution '' 1
47
48 base.init
49 base.initFilesystem "$OPTION_FORCE"
50
51 setup_writeOption "$VSERVER_NAME"
52 setup_writeInitialFstab
53
54 test -z "$BUILD_INITPRE"  || "$BUILD_INITPRE" "$SETUP_CONFDIR"  "$UTIL_VSERVER_VARS"
55
56 # migrate is a 2.6 feature, we need 2.6 for the secure_mount ccap
57 if ! $_VSERVER_INFO - FEATURE migrate; then
58     panic $"\
59 The fai build method requires kernel features that are not present
60 in your kernel."
61 fi
62
63 # if they specified a vserver where FAI is installed, then prefix its
64 # root to the FAI_NFSROOT etc (unless they have already specified a
65 # $ROOTDIR-relative path)
66 REAL_ROOTDIR=`getPhysicalDir "$ROOTDIR"`
67 REAL_VDIR=`getPhysicalDir "$VDIR"`
68 echo $"ROOTDIR is $REAL_ROOTDIR"
69 echo $"VDIR    is $REAL_VDIR"
70 if [ -n "$FAI_VSERVER" ]
71 then
72     echo "FAI_VSERVER is $FAI_VSERVER"
73     if [ `expr "$FAI_NFSROOT" : "$REAL_ROOTDIR"` -eq 0 ]
74     then
75         FAI_NFSROOT="$REAL_ROOTDIR/$FAI_VSERVER$FAI_NFSROOT"
76     fi
77     if [ `expr "$FAI_DIR" : "$REAL_ROOTDIR"` -eq 0 ]
78     then
79         FAI_DIR="$REAL_ROOTDIR/$FAI_VSERVER$FAI_DIR"
80     fi
81 fi
82 echo $"FAI_NFSROOT is $FAI_NFSROOT"
83 echo $"FAI_DIR     is $FAI_DIR"
84 FAI_ROOT=/tmp/target
85
86 if [ ! -e "$FAI_NFSROOT/usr/sbin/fai" ]
87 then
88     panic $"NFSROOT not found - did you run fai-setup?"
89 fi
90
91 if ! grep -qw VIRTUAL "$FAI_NFSROOT/usr/sbin/fai"
92 then
93     panic $"\
94 ERROR: Looks like you don't have the patched FAI.
95 See http://faiwiki.informatik.uni-koeln.de/index.php/Using_FAI_as_a_vserver_build_method
96 for information on where to get it."
97 fi
98
99 set -e
100
101 # suppresses color logo, as it does not leave the terminal in a nice
102 # state.
103 console=dummy
104
105 killprocs() {
106     set +e
107     S_CONTEXT=
108     if isVserverRunning "$SETUP_CONFDIR" S_CONTEXT
109     then
110         $_VKILL -c "$S_CONTEXT" -s 15
111         sleep 2
112         $_VKILL -c "$S_CONTEXT" -s 9
113         sleep 2
114         isVserverRunning "$SETUP_CONFDIR" && warning $"\
115 Vserver '$VSERVER_NAME' still running unexpectedly; please investigate it
116 manually."
117     fi
118     [ -n "$HAVE_INTERFACES" ] && disableInterfaces "$SETUP_CONFDIR"
119     base.__cleanup
120 }
121
122 trap killprocs INT EXIT
123
124 # last chance to abort!
125 if [ -z "$AUTO" ]
126 then
127     echo -n "Press ENTER to begin, or Ctrl+C to cancel: "
128     read junk
129 fi
130
131 # set up interfaces, so that the vserver can be built with restricted
132 # network access.
133 generateOptions "$SETUP_CONFDIR"
134 enableInterfaces "$SETUP_CONFDIR"
135 HAVE_INTERFACES=1
136
137 rwpaths="/var/lock /var/log /dev /var/run /etc/sysconfig"
138 [ -d "$FAI_NFSROOT/etc/network/run" ] && rwpaths="$rwpaths /etc/network/run"
139
140 export VDIR ROOTDIR FAI_VSERVER FAI_DIR FAI_NFSROOT FAI_ROOT console
141
142 [ -z "$AUTO" ] && FINAL_SHELL="exec /bin/sh -i"
143
144 # set up /mnt with all the mounts required
145 $_SECURE_MOUNT -n -o ro,dev --bind "$FAI_NFSROOT" /mnt
146 $_SECURE_MOUNT -n -t tmpfs -o size=64m,mode=1777 vm /mnt/tmp
147 $_MKDIR -p /mnt/tmp/target
148 $_SECURE_MOUNT -n -o rw,dev --bind "$VDIR" /mnt/tmp/target
149 $_MKDIR -p /mnt/tmp/target/proc
150 $_SECURE_MOUNT -n -t proc proc /mnt/tmp/target/proc
151 for x in $rwpaths
152 do
153     /mnt/usr/lib/fai/mkrw -n "/mnt$x"
154 done
155 $_SECURE_MOUNT -n -o ro --bind "$FAI_DIR" /mnt/fai
156 $_SECURE_MOUNT -n -t proc proc /mnt/proc
157
158 cd /mnt
159 $_CHBIND "${CHBIND_OPTS[@]}" \
160     $_VCONTEXT --create "${OPTS_VCONTEXT_CREATE[@]}" -- \
161     $_VUNAME --xid self --set -t nodename="${SETUP_HOSTNAME:-fai-build}" \
162         -t context="$SETUP_CONFDIR" -- \
163     $_VATTRIBUTE --set --xid self "${OPTS_VATTRIBUTE[@]}" --ccap secure_mount \
164     $_SAVE_CTXINFO "$SETUP_CONFDIR" \
165     $_VCONTEXT --migrate-self --endsetup --chroot $SILENT_OPT \
166         "${OPTS_VCONTEXT_MIGRATE[@]}" "${OPTS_VCONTEXT_ENTER[@]}" -- \
167     /bin/sh -c "/bin/sh $SH_DEBUG /etc/init.d/rcS -N vschild_build; $FINAL_SHELL"
168
169 test -z "$BUILD_INITPOST" || "$BUILD_INITPOST" "$SETUP_CONFDIR" "$UTIL_VSERVER_VARS"
170
171 base.setSuccess