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