merge with 0.30.213
[util-vserver.git] / distrib / redhat / initpost
1 #! /bin/bash
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 ## HACK: this script is full of races...
19
20 vdir=$1/vdir
21 . "$2"
22
23 function relink
24 {
25     test ! -e "$2" -o -L "$2" || mv -f "$2"{,.orig}
26     ln -sf "$1" "$2"
27 }
28
29 ## Usage: subst <file> <regexp>
30 function subst
31 {
32     tmp=$(mktemp /tmp/initpost-subst.XXXXXX)
33
34     case "$1" in
35         (/*|./*)        cat "$1";;
36         (*)             $_CHROOT_SH cat "$1";;
37     esac              | sed -e "$2"          >$tmp
38     cmp -s $tmp "$1" || $_CHROOT_SH truncate "$1" <$tmp
39
40     rm -f $tmp
41 }
42
43 vserver=$1
44
45 ## Do some magic to set the vserver into a defined state
46 $_VSERVER "$vserver" stop  &>/dev/null || :
47 $_VSERVER "$vserver" start --rescue --rescue-init bash -c '
48     exec  >/dev/null
49     exec 2>/dev/null
50
51     set -x
52     f=/tmp/startwait
53     trap "rm -f $f" EXIT
54     mkfifo $f
55     ( sleep 15; kill -s 9 -- -1 ) &
56     cat "$f" >/dev/null
57     kill -s 9 -- -1
58     wait
59 '
60
61 pushd "$vdir"/etc/init.d &>/dev/null ||
62     pushd "$vdir"/etc/rc.d/init.d &>/dev/null || exit 1
63
64     for i in *; do
65         test -e "$i" || continue
66         case "$i" in
67             functions|halt|killall|single) ;;
68             syslog) ;;
69             *)
70                 $_VSERVER "$1" chkconfig "$i" off
71                 ;;
72         esac
73     done
74
75 popd >/dev/null
76
77 pushd "$vdir"/sbin &>/dev/null
78     relink /bin/true new-kernel-pkg
79 popd >/dev/null
80
81 pushd "$vdir"/usr/bin &>/dev/null
82     relink /bin/true rhgb-client
83 popd >/dev/null
84
85 pushd "$vdir" &>/dev/null
86     echo 'NETWORKING=yes' | $_CHROOT_SH append /etc/sysconfig/network
87 popd >/dev/null
88
89 pushd "$vdir" &>/dev/null
90     cat <<EOF | $_CHROOT_SH truncate /etc/init.d/halt
91 #! /bin/bash
92
93 exec /sbin/killall5 -15
94 EOF
95     $_CHROOT_SH chmod 0755 /etc/init.d/halt
96     
97     : | $_CHROOT_SH truncate /etc/sysctl.conf
98     echo "none    /       none    defaults" | $_CHROOT_SH truncate /etc/fstab
99     cat "$__DISTRIBDIR"/redhat/rc.sysinit   | $_CHROOT_SH truncate /etc/rc.d/rc.sysinit.vserver
100     $_CHROOT_SH chmod 0755 /etc/rc.d/rc.sysinit.vserver
101     subst etc/inittab 's!^\([^#].*:respawn:.* tty\)!#\1!;
102                        s!si::sysinit:/etc/rc.d/rc.sysinit!si::sysinit:/etc/rc.d/rc.sysinit.vserver!'
103 popd >/dev/null
104
105 $_VSERVER "$vserver" exec bash -c ': >/tmp/startwait' &>/dev/null || :
106 $_VSERVER "$vserver" stop &>/dev/null || :