]> git.pld-linux.org Git - projects/rc-scripts.git/blob - rc.d/init.d/sys-chroots
673730043106101fed4a4119b03a06bd085d57b0
[projects/rc-scripts.git] / rc.d / init.d / sys-chroots
1 #!/bin/sh
2 #
3 # sys-chroots   Starts and stops services in chroots
4 #
5 # chkconfig:    2345 99 01
6 # description:  This shell script starts and stops services in chroots
7
8 [ -r /etc/sysconfig/system ] && . /etc/sysconfig/system
9 [ -n "$2" ] && SYSTEM_CHROOTS="$2"
10
11 if [ -z "$SYSTEM_CHROOTS" ]; then
12         case "$1" in
13         start|stop|restart)
14                 exit 0
15                 ;;
16         esac
17 fi
18
19 CMD="$1"
20
21 set $(runlevel)
22 runlevel=$2
23 previous=$1
24 export runlevel previous
25
26 [ -z "$runlevel" -o -z "$previous" -o "$runlevel" = "$previous" ] && exit 0
27
28 # Source function library.
29 . /etc/rc.d/init.d/functions
30
31 upstart_controlled
32
33 start() {
34         if [ "$previous" = "N" ]; then
35                 runlevel=5
36                 previous=0
37                 export runlevel previous
38         fi
39
40         if [ -f /var/lock/subsys/sys-chroots ]; then
41                 msg_already_running "System chroots services"
42                 return
43         fi
44
45         msg_starting "System chroots services"; started
46
47         for dir in $SYSTEM_CHROOTS; do
48                 [ ! -x "$dir/etc/rc.d/rc" ] && continue
49                 msg_starting "System chroots services for $dir"; started
50
51                 # Cleaning part, keep in sync with rc.sysinit
52
53                 chroot $dir sh -c '
54                 . /etc/rc.d/init.d/functions
55                 # Clear mtab
56                 > /etc/mtab
57                 [ -f /etc/cryptomtab ] && > /etc/cryptomtab
58
59                 # Remove stale backups
60                 rm -f /etc/mtab~ /etc/mtab~~ /etc/cryptomtab~ /etc/cryptomtab~~
61
62                 # Clean up /var
63                 # I would d use find, but /usr may not be mounted.
64                 for afile in /var/lock/* /var/run/*; do
65                         bafile=$(basename $afile)
66                         if [ -d "$afile" ]; then
67                                 [ "$bafile" != "news" -a "$bafile" != "sudo" -a "$bafile" != "mon" ] && rm -rf $afile/*
68                         else
69                                 [ "$bafile" != "hwprofile" ] && rm -f $afile 2> /dev/null
70                         fi
71                 done
72                 # Delete stale files
73                 rm -f /var/lib/rpm/__db* /var/spool/postoffice/.pid.* /tmp/.X*-lock \
74                         /tmp/.lock.* /tmp/.gdm_socket /tmp/.s.PGSQL.*
75                 rm -rf /tmp/.X*-unix /tmp/.ICE-unix /tmp/.font-unix /tmp/hsperfdata_* \
76                         /tmp/kde-* /tmp/ksocket-* /tmp/mc-* /tmp/mcop-* /tmp/orbit-* \
77                         /tmp/scrollkeeper-* /tmp/ssh-*
78
79                 # Clean up utmp/wtmp
80                 rm -f /var/run/utmpx
81                 > /var/run/utmp
82                 if [ -e /var/log/wtmpx ]; then
83                         if [ -e /var/log/wtmp ]; then
84                                 rm -f /var/log/wtmpx
85                         else
86                                 mv /var/log/wtmpx /var/log/wtmp
87                         fi
88                 fi
89                 touch /var/log/wtmp
90                 chown root:utmp /var/run/utmp /var/log/wtmp
91                 chmod 0664 /var/run/utmp /var/log/wtmp
92                 # Clean /tmp
93                 if is_yes "$CLEAN_TMP"; then
94                         rm -rf /tmp/* /tmp/.[a-zA-Z0-9]*
95                 fi
96                 '
97
98                 # Do our things
99
100                 # proc
101                 chroot $dir mount -o gid=17 -t proc proc /proc
102                 # usbfs, if available
103                 if [ -f /proc/bus/usb/devices ]; then
104                         chroot $dir mount -t usbfs usbfs /proc/bus/usb
105                 fi
106                 # sysfs is also needed before any other things (under kernel > 2.5)
107                 if grep -q sysfs /proc/filesystems ; then
108                         chroot $dir mount -o gid=17 -t sysfs sysfs /sys
109                 fi
110                 # selinux
111                 if grep -q selinuxfs /proc/filesystems ; then
112                         chroot $dir mount -o gid=17 -t selinuxfs selinuxfs /selinux
113                 fi
114                 chroot $dir mount -a
115                 # network
116                 [ -f /var/lock/subsys/network ] && touch $dir/var/lock/subsys/network
117                 # other
118                 chroot $dir /sbin/chkconfig single off
119                 rm -f $dir/etc/rc.d/rc*.d/K*single
120                 # run it
121                 chroot $dir /etc/rc.d/rc $runlevel chroot
122         done
123
124         touch /var/lock/subsys/sys-chroots
125 }
126
127 stop() {
128         # Stop daemons.
129         if [ "$previous" = "N" ]; then
130                 runlevel=0
131                 previous=5
132                 export runlevel previous
133         fi
134
135         if [ ! -f /var/lock/subsys/sys-chroots ]; then
136                 msg_not_running "System chroots services"
137                 return
138         fi
139
140         msg_stopping "System chroots services"; started
141         for dir in $SYSTEM_CHROOTS; do
142                 [ ! -x "$dir/etc/rc.d/rc" ] && continue
143                 msg_stopping "System chroots services for $dir"; started
144                 rm -f $dir/var/lock/subsys/network
145                 chroot $dir /etc/rc.d/rc $runlevel chroot
146                 chroot $dir sh -c "grep -q selinuxfs /proc/filesystems && umount /selinux"
147                 chroot $dir sh -c "grep -q sysfs /proc/filesystems && umount /sys"
148                 chroot $dir umount -a
149                 chroot $dir umount /proc
150         done
151         rm -f /var/lock/subsys/sys-chroots >/dev/null 2>&1
152 }
153
154 RETVAL=0
155 # See how we were called.
156 case "$CMD" in
157   start)
158         start
159         ;;
160   stop)
161         stop
162         ;;
163   restart)
164         stop
165         start
166         ;;
167   status)
168         # TODO: running each service with status parameter
169         for dir in $SYSTEM_CHROOTS; do
170                 echo "System chroots services for $dir"
171                 chroot $dir /sbin/chkconfig --list
172         done
173         exit $?
174         ;;
175   *)
176         msg_usage "$0 {start|stop|restart|status}"
177         exit 3
178 esac
179
180 exit $RETVAL
This page took 0.050259 seconds and 2 git commands to generate.