731128828c23c6028dfdd71574bd66e35532c81f
[util-vserver.git] / scripts / vserver.start
1 # $Id: vserver.start,v 1.45 2005/07/15 19:01:06 ensc Exp $  --*- sh -*--
2
3 # Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 #  
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; version 2 of the License.
8 #  
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #  
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18
19 lock "$__LOCKDIR"/vserver."$($_VSERVER_INFO "$VSERVER_DIR" CANONIFY)".startup
20
21 if isVserverRunning "$VSERVER_DIR"; then
22     warning $"vserver '$VSERVER_NAME' already running"
23     exit 0
24 fi
25
26 function cleanup()
27 {
28     set +e
29
30     test -z "$startsync_pipe"  || $_RM -f "$startsync_pipe"
31     test -z "$is_started"      || { unlock; return 0; }
32
33     if test -n "$is_configured" -a -z "$is_executed"; then
34         warning $"
35 An error occured while executing the vserver startup sequence; when
36 there are no other messages, it is very likely that the init-script
37 (${INITCMD_START[@]}) failed.
38
39 Common causes are:
40 * /etc/rc.d/rc on Fedora Core 1 and RH9 fails always; the 'apt-rpm' build
41   method knows how to deal with this, but on existing installations,
42   appending 'true' to this file will help."
43     fi
44
45     if test -n "$is_executed"; then
46         warning $"
47 An error occured after executing the vserver startup sequence. This
48 means that some processes may exist in the created context and the
49 manual execution of
50
51   $_VSERVER '$VSERVER_DIR' stop
52
53 is recommended to fix this."
54     else
55         warning $"
56
57 Failed to start vserver '$VSERVER_NAME'"
58     fi
59
60     test -n "$OPTION_DEBUG"    || exec 2>/dev/null >/dev/null
61     test -z "$is_mounted"      || umountVserver     "$VSERVER_DIR" || :
62     test -z "$have_interfaces" || disableInterfaces "$VSERVER_DIR" || :
63
64     unlock || :
65 }
66
67 is_configured=
68 is_executed=
69 is_started=
70 is_mounted=
71 is_vshelper_init=
72 have_interfaces=
73 startsync_pipe=
74
75 while test "$#" -gt 0; do
76     case "$1" in
77         (--rescue)      INITSTYLE=rescue;;
78         (--rescue-cmd)  INITCMD_RESCUE=( $2 ); shift;;
79         (*)             panic $"vserver ... start: unknown option '$1'";;
80     esac
81     shift
82 done
83
84 set -e
85 trap "cleanup" EXIT
86
87 sanityCheck      "$VSERVER_DIR"
88
89 mountRootFS      "$VSERVER_DIR"
90 generateOptions  "$VSERVER_DIR"
91
92 pushd "$VSERVER_DIR" >/dev/null
93 execScriptlets   "$VSERVER_DIR" "$VSERVER_NAME" prepre-start
94 popd >/dev/null
95
96 enableInterfaces "$VSERVER_DIR" && have_interfaces=1
97
98 mountVserver "$VSERVER_DIR"     && is_mounted=1
99 prepareInit  "$VSERVER_DIR"
100
101 pushd "$VSERVER_DIR"/vdir/ >/dev/null
102 execScriptlets   "$VSERVER_DIR" "$VSERVER_NAME" pre-start
103 popd >/dev/null
104
105 test -z "$OPTION_DEFAULTTTY" || setDefaultTTY "$VSERVER_DIR"
106
107 pushd "$VSERVER_DIR"/vdir/ >/dev/null
108 is_configured=1
109 if $_VSERVER_INFO - FEATURE migrate; then
110     ${NICE_CMD[@]} \
111     $_CHBIND      "${CHBIND_OPTS[@]}" -- \
112     $_EXEC_ULIMIT "$VSERVER_DIR"/ulimits \
113     $_VCONTEXT   --create "${OPTS_VCONTEXT_CREATE[@]}" -- \
114     ${USE_VNAMESPACE:+$_VNAMESPACE --set -- } \
115     $_VLIMIT     --dir "$VSERVER_DIR"/rlimits --missingok -- \
116     $_VSCHED     --xid self "${OPTS_VSCHED[@]}" -- \
117     $_VUNAME     --xid self --dir "$VSERVER_DIR"/uts     --missingok -- \
118     "${VSERVER_EXTRA_CMDS[@]}" \
119     $_VUNAME     --xid self --set -t context="$VSERVER_DIR" -- \
120     $_VATTRIBUTE --set "${OPTS_VATTRIBUTE[@]}" -- \
121     $_SAVE_CTXINFO "$VSERVER_DIR" \
122     $_ENV -i -- \
123     $_VCONTEXT   --migrate-self --endsetup --chroot $SILENT_OPT \
124     "${OPTS_VCONTEXT_MIGRATE[@]}" "${OPTS_VCONTEXT_ENTER[@]}" -- \
125     "${INITCMD_START[@]}"
126 else
127     if test -n "$_IS_FAKEINIT"; then
128         startsync_pipe=$($_MKTEMP /tmp/vserver-start.XXXXXX)
129         $_RM -f "$startsync_pipe"
130         $_MKFIFO -m600 "$startsync_pipe"        ## safe, since mkfifo does not follow symlinks
131     fi
132     
133     ${NICE_CMD[@]} \
134     $_CHBIND           "${CHBIND_OPTS[@]}" \
135     $_EXEC_ULIMIT      "$VSERVER_DIR/ulimits" \
136     $_CHCONTEXT_COMPAT "${CHCONTEXT_OPTS[@]}" "${CHCONTEXT_INIT_OPTS[@]}" \
137     $_SAVE_CTXINFO     "$VSERVER_DIR" \
138     $_ENV -i -- \
139     $_CHAINECHO "${_IS_FAKEINIT:+$startsync_pipe}" "" \
140     $_CAPCHROOT        "${CAPCHROOT_OPTS[@]}" . \
141     "${INITCMD_START[@]}"
142
143     ## the additional synchronization step is needed since vshelper.doInit
144     ## below might be executed before needed context information were
145     ## saved by $_SAVE_CTXINFO
146     ${_IS_FAKEINIT:+cat "$startsync_pipe"} >/dev/null
147 fi
148 is_executed=1
149
150 "${INITCMD_START_SYNC[@]}"
151 popd >/dev/null
152
153 pushd "$VSERVER_DIR"/vdir/ >/dev/null
154 execScriptlets   "$VSERVER_DIR" "$VSERVER_NAME" post-start
155 popd >/dev/null
156
157 is_vshelper_init=1
158 ! vshelper.isEnabled "" "$VSERVER_DIR" || vshelper.doInit "$VSERVER_DIR"
159
160 is_started=1