/etc/init.d/grml-reboot: put reboot + halt binaries to the filesystem cache
[grml-etc.git] / etc / init.d / grml-reboot
1 #!/bin/sh
2 # Filename:      grml-reboot
3 # Purpose:       reboot grml system
4 # Authors:       grml-team (grml.org), (c) Klaus Knopper, (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 # Latest change: Mon Mär 10 18:45:36 CET 2008 [mika]
8 ################################################################################
9
10 export PATH=/sbin:/bin:/usr/bin:/usr/sbin
11
12 . /etc/grml_colors
13 . /etc/grml/lsb-functions
14
15 cd /
16
17 # Clean input/output
18 exec >/dev/console 2>&1 </dev/console
19
20 # Check if we are running from a GRML-CD or HD
21 INSTALLED=""
22 [ -e /etc/grml_cd ] || INSTALLED=yes
23 # Assume that we don't want the eject+prompt stuff when running in one
24 # of the special modes:
25 grep -qe ' toram' -qe ' usb' -qe 'serial' -qe 'fromhd' /proc/cmdline && INSTALLED=yes
26
27 case "$0" in
28   *halt)
29         level=0
30         message="grml system will be halted..."
31         command="halt"
32         ;;
33   *reboot)
34         level=6
35         message="Preparing for reboot..."
36         command="reboot"
37         ;;
38   *)
39         echo "Usage: call this script as \"halt\" or \"reboot\" please.">&2
40         exit 1
41         ;;
42 esac
43
44 mysleep() {
45   for i in `seq 1 40` ; do
46     usleep 75000
47     echo -n "$1.${NORMAL}"
48   done
49   echo ""
50 }
51
52 # Disable kernel messages
53 echo "0" > /proc/sys/kernel/printk
54
55 # make sure halt/reboot commands are available even if
56 # someone is using shutdown command:
57 cat /sbin/halt /sbin/reboot >/dev/null
58
59 # We may kill our network connection here before unmounting NFS. Bad luck.
60 # poweroff pcmcia devices
61 if [ -d /sys/bus/pcmcia -a -x /sbin/pccardctl ] ; then
62    # make sure we don't lose any data, see issue219 for details
63    log_begin_msg "Syncing devices..."
64    sync ; log_end_msg $?
65    log_begin_msg "Shutting down PCMCIA devices..."
66    pccardctl eject >/dev/null 2>&1
67    log_end_msg $?
68 fi
69
70 if [ -n "$INSTALLED" ] ; then
71    log_begin_msg_nn "Running /etc/init.d/rc ${level}: "
72    /etc/init.d/rc $level 1>/dev/null 2>&1 &
73    mysleep "$GREEN"
74    log_end_msg 0
75 fi
76
77 # Remove all truecrypt mappings...
78 if [ -x /usr/sbin/truecrypt ] ; then
79    if grep -q truecrypt /proc/modules ; then
80       log_begin_msg "Unmapping truecrypt volumes."
81       MSG=$(truecrypt -d 2>1)
82       echo $MSG | grep "No volumes mapped" && eend 0
83       [ -z "$MSG" ] && eend 0 || eend 1
84    fi
85 fi
86
87 # Now kill them all
88 killall5 -15
89 sleep 1
90 log_begin_msg_nn "Sending all processes the TERM signal: "
91 mysleep "$BLUE" ; log_end_msg $?
92
93 killall5 -9
94 sleep 1
95 log_begin_msg_nn "Sending all processes the KILL signal: "
96 mysleep "$RED" ; log_end_msg $?
97
98 # check for nfsdir
99 if grep -q nfsdir /proc/cmdline ; then
100    log_begin_msg "Bootoption nfsdir detected, syncing filesystems."
101    sync && sleep 1 ; log_end_msg $?
102 else
103    # Unmount network filesystems first before shutting down network
104    NETMOUNTS="$(awk '{if($1~/:/){print $2}}' /proc/mounts 2>/dev/null)"
105    if [ -n "$NETMOUNTS" ]; then
106       log_begin_msg "Unmounting network filesystems."
107       umount -t nfs,nfs4,smbfs -alvf 2>/dev/null
108    fi
109    # Shutdown network
110    NETDEVICES="$(/sbin/ifconfig | awk '/^[^ ]+/ {print $1}' | grep -v '^lo$')"
111    if [ -n "$NETDEVICES" ]; then
112       pidof pump >/dev/null 2>&1 && { pump -k ; sleep 2; }
113       killall dhclient dhclient3 2>/dev/null
114       log_begin_msg "Shutting down network device..."
115       for n in $NETDEVICES; do
116           echo "${SUBMSG} ${WHITE}$n${NORMAL}"
117           ifdown $n 1>/dev/null 2>&1
118           ifconfig $n down
119       done ; log_end_msg $?
120    fi
121    log_begin_msg "Syncing local filesystems..."
122    sync && sleep 1 ; log_end_msg $?
123 fi
124
125 # Turn off swap, then unmount file systems.
126 log_begin_msg "Turning off swap."
127 swapoff -a >/dev/null 2>&1 ; log_end_msg $?
128
129 # Udev
130 log_begin_msg "Deactivating udev:"
131 echo -n "   ${GREEN}-${NORMAL} "
132 /etc/init.d/udev stop ; log_end_msg $?
133 [ -n $INSTALLED ] || mkdir -p /dev/pts
134 [ -n $INSTALLED ] || cp -a /GRML/dev/console /GRML/dev/tty[0-9]* /GRML/dev/loop* /GRML/dev/initctl /GRML/dev/null /dev/
135 [ -n $INSTALLED ] || ln -s /GRML/dev/* /dev/ 2>/dev/null
136
137 # Read in boot parameters
138 CMDLINE="`cat /proc/cmdline 2>/dev/null`"
139 # noprompt or noeject option given?
140 NOPROMPT=${NOPROMPT:-''}
141 case "$CMDLINE" in *noprompt*) NOPROMPT="yes"; ;; esac
142 NOEJECT=${NOEJECT:-''}
143 case "$CMDLINE" in *noeject*) NOEJECT="yes"; ;; esac
144 [ -r /etc/noprompt ] && NOPROMPT='yes'
145 [ -r /etc/noeject ]  && NOPROMPT='yes' && NOEJECT='yes'
146
147 # Turn on autoeject of CD-ROMs
148 if [ -z "$NOEJECT" ]; then
149    for dev in /proc/sys/dev/cdrom*/lock;      do [ -f "$dev" ] && echo 0 > "$dev"; done
150    for dev in /proc/sys/dev/cdrom*/autoeject; do [ -f "$dev" ] && echo 1 > "$dev"; done
151 fi
152
153 # Detected cdrom device
154 if [ -z "$INSTALLED" ]; then
155    # new live-initramfs layout:
156    if [ -d /live/image ] ; then
157       CDROM="$(awk '/ \/live\/image /{print $1;exit 0;}' /proc/mounts)"
158    else # old unionfs layout:
159       CDROM="$(awk '/ \/cdrom /{print $1;exit 0;}' /proc/mounts)"
160    fi
161 fi
162
163 # Umount everything but root
164 log_begin_msg "Unmounting file systems."
165
166 # Be safe in case someone messed with /etc/mtab
167 if [ -z "$INSTALLED" ] ; then
168    rm -f /etc/mtab
169    ln -snf /proc/mounts /etc/mtab
170 fi
171
172 # Free loopback devices if necessary, so we can unmount the host media
173 for i in /dev/loop*; do losetup -d $i 2>/dev/null; done
174
175 # Remove remaining unused modules (Kernel 2.4)
176 # rmmod -a >/dev/null 2>&1
177
178 umount -t notmpfs,nosysfs,noproc,nousbfs -adrvf 1>/dev/null 2>&1
179
180 log_end_msg 0
181
182 # For a harddisk installation: mount / ro
183 [ -n "$INSTALLED" ] && mount -n -o remount,ro / 2>/dev/null
184
185 if [ -z "$INSTALLED" -a -z "$NOPROMPT" ]; then
186    # do not prompt for removal when running in grml2ram mode:
187    if ! mount | grep -qe 'on /cdrom' -qe 'on /live/image' ; then
188       echo "CD not mounted, nothing to eject therefore."
189    else
190       [ -d /live/image ] && umount -l /live/image
191       [ -d /cdrom ] && umount -l /cdrom
192       [ -n "$CDROM" -a -z "$NOEJECT" ] && eject -p "$CDROM" 2>/dev/null
193       echo
194       read -s -p "${GREEN}Please remove CD, close cdrom drive and hit return [auto 2 minutes].${NORMAL}" -t 120 a
195    fi
196 fi
197
198 echo
199 echo "$message" >/dev/console
200
201 # Finally halt or reboot
202 /etc/init.d/$command stop
203
204 ## END OF FILE #################################################################