-
Notifications
You must be signed in to change notification settings - Fork 3
/
initrc
87 lines (77 loc) · 1.86 KB
/
initrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/sh
PATH=/opt/bin:/opt/sbin:/sbin:/bin:/usr/sbin:/usr/bin
# Debian folder
CHROOT_DIR=/opt/debian
# Some folder outside of sandbox, will be mounted to /mnt folder in Debian
# Leave commented if not needed
#EXT_DIR=/media
CHROOT_SERVICES_LIST=$CHROOT_DIR/chroot-services.list
if [ ! -e "$CHROOT_SERVICES_LIST" ]; then
echo "Please, define Debian services to start in $CHROOT_SERVICES_LIST first!"
echo 'One service per line. Hint: this is a script names from Debian /etc/init.d/'
exit 1
fi
MountedDirCount="$(mount | grep $CHROOT_DIR | wc -l)"
start() {
if [ $MountedDirCount -gt 0 ]; then
logger 'Debian services seems to be already started, exiting...'
exit 1
fi
logger 'Starting Debian services...'
for dir in dev dev/pts proc sys opt/etc; do
mount -o bind /$dir $CHROOT_DIR/$dir
done
[ -z "$EXT_DIR" ] || mount -o bind $EXT_DIR $CHROOT_DIR/media
for item in $(cat $CHROOT_SERVICES_LIST); do
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/sbin \
LC_ALL=C \
LANGUAGE=C \
LANG=C \
chroot $CHROOT_DIR /etc/init.d/$item start
done
}
stop() {
if [ $MountedDirCount -eq 0 ]; then
logger 'Debian services seems to be already stopped, exiting...'
exit 1
fi
logger 'Stopping Debian services...'
for item in $(cat $CHROOT_SERVICES_LIST); do
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/sbin \
LC_ALL=C \
LANGUAGE=C \
LANG=C \
chroot $CHROOT_DIR /etc/init.d/$item stop
done
umount $CHROOT_DIR/dev/pts
mount | grep $CHROOT_DIR | awk "{print \$3}" | xargs umount
}
status() {
if [ $MountedDirCount -gt 0 ]; then
echo 'Debian services is running'
else
echo 'Debian services is stopped'
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 5
start
;;
status)
status
;;
*)
echo "Usage: $0 (start|stop|restart|status)"
exit 1
;;
esac
echo 'Done.'
exit 0