10aab162c0d073b52d1938d4bec1ab6da8b4e5e4
[util-vserver.git] / util-vserver / scripts / vserver.start
1 # $Id$  --*- 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 if isVserverRunning "$VSERVER_DIR"; then
19     echo $"vserver '$VSERVER_NAME' already running; aborting..." >&2
20     exit 1
21 fi
22
23 function cleanup()
24 {
25     test -z "$startsync_pipe"  || rm -f "$startsync_pipe"
26     test -z "$is_started"      || return 0
27
28     if test "$is_configured" -a -z "$is_executed"; then
29         warning $"
30 An error occured while executing the vserver startup sequence; when
31 there are no other messages, it is very likely that the init-script
32 (${INITCMD_START[@]}) failed.
33
34 Common causes are:
35 * /etc/rc.d/rc on Fedora Core 1 and RH9 fails always; the 'apt-rpm' build
36   method knows how to deal with this, but on existing installations,
37   appending 'true' to this file will help."
38     fi
39
40     if test "$is_executed"; then
41         warning $"
42 An error occured after executing the vserver startup sequence. This
43 means that some processes may exist in the created context and the
44 manual execution of
45
46   $_VSERVER '$VSERVER_DIR' stop
47
48 is recommended to fix this."
49     else
50         warning $"
51
52 Failed to start vserver '$VSERVER_NAME'"
53     fi
54
55     test    "$OPTION_DEBUG"    || exec 2>/dev/null >/dev/null
56     test -z "$is_mounted"      || umountVserver     "$VSERVER_DIR" || :
57     test -z "$have_interfaces" || disableInterfaces "$VSERVER_DIR" || :
58 }
59
60 is_configured=
61 is_executed=
62 is_started=
63 is_mounted=
64 is_vshelper_init=
65 have_interfaces=
66 startsync_pipe=
67
68 set -e
69 trap "cleanup" EXIT
70
71 vshelper.doSanityCheck
72
73 generateOptions  "$VSERVER_DIR"
74 execScriptlets   "$VSERVER_DIR" "$VSERVER_NAME" prepre-start
75 enableInterfaces "$VSERVER_DIR" && have_interfaces=1
76
77 mountVserver "$VSERVER_DIR"     && is_mounted=1
78 prepareInit  "$VSERVER_DIR"
79
80 pushd "$VSERVER_DIR"/vdir/ >/dev/null
81 execScriptlets   "$VSERVER_DIR" "$VSERVER_NAME" pre-start
82 popd >/dev/null
83
84 test -z "$OPTION_DEFAULTTTY" || setDefaultTTY
85
86 pushd "$VSERVER_DIR"/vdir/ >/dev/null
87 is_configured=1
88 if $_VSERVER_INFO - FEATURE migrate; then
89     $_CHBIND     "${CHBIND_OPTS[@]}" -- \
90     $_VCONTEXT   --create "${OPTS_VCONTEXT_CREATE[@]}" -- \
91     ${USE_VNAMESPACE:+$_VNAMESPACE --set -- } \
92     $_VLIMIT     --dir "$VSERVER_DIR"/rlimits --missingok -- \
93     $_VSCHED     --xid self "${OPTS_VSCHED[@]}" -- \
94     $_VUNAME     --xid self --dir "$VSERVER_DIR"/uts     --missingok -- \
95     "${VSERVER_EXTRA_CMDS[@]}" \
96     $_VUNAME     --xid self --set -t context="$VSERVER_DIR" -- \
97     $_VATTRIBUTE --set "${OPTS_VATTRIBUTE[@]}" -- \
98     $_SAVE_CTXINFO "$VSERVER_DIR" \
99     $_CLEARENV \
100     $_VCONTEXT   --migrate-self --endsetup --chroot $SILENT_OPT "${OPTS_VCONTEXT_MIGRATE[@]}" -- \
101     "${INITCMD_START[@]}"
102 else
103     if test "$_IS_FAKEINIT"; then
104         startsync_pipe=$(mktemp /tmp/vserver-start.XXXXXX)
105         rm -f "$startsync_pipe"
106         mkfifo -m600 "$startsync_pipe"  ## safe, since mkfifo does not follow symlinks
107     fi
108     
109     ${NICE_CMD[@]} \
110     $_CHBIND           "${CHBIND_OPTS[@]}" \
111     $_EXEC_ULIMIT      "$VSERVER_DIR/ulimits" \
112     $_CHCONTEXT_COMPAT "${CHCONTEXT_OPTS[@]}" "${CHCONTEXT_INIT_OPTS[@]}" \
113     $_SAVE_CTXINFO     "$VSERVER_DIR" \
114     $_CLEARENV \
115     $_CHAINECHO "${_IS_FAKEINIT:+$startsync_pipe}" "" \
116     $_CAPCHROOT        "${CAPCHROOT_OPTS[@]}" . \
117     "${INITCMD_START[@]}"
118
119     ## the additional synchronization step is needed since vshelper.doInit
120     ## below might be executed before needed context information were
121     ## saved by $_SAVE_CTXINFO
122     ${_IS_FAKEINIT:+cat "$startsync_pipe"} >/dev/null
123 fi
124 is_executed=1
125
126 "${INITCMD_START_SYNC[@]}"
127 popd >/dev/null
128
129 pushd "$VSERVER_DIR"/vdir/ >/dev/null
130 execScriptlets   "$VSERVER_DIR" "$VSERVER_NAME" post-start
131 popd >/dev/null
132
133 is_vshelper_init=1
134 ! vshelper.isEnabled "" "$VSERVER_DIR" || vshelper.doInit "$VSERVER_DIR"
135
136 is_started=1