21ccf6c51c708cb82fce64f110523ec9a4222e09
[grml-etc.git] / etc / init.d / grml-reboot
1 #!/bin/bash
2 # Filename:      grml-reboot
3 # Purpose:       reboot grml system
4 # Authors:       grml-team (grml.org)
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 ################################################################################
8 ### BEGIN INIT INFO
9 # Provides:          grml-reboot
10 # Required-Start:
11 # Required-Stop:
12 # Default-Start:
13 # Default-Stop:
14 ### END INIT INFO
15
16 export PATH=/sbin:/bin:/usr/bin:/usr/sbin
17
18 . /etc/grml_colors
19 . /etc/grml/lsb-functions
20
21 cd /
22
23 # Clean input/output
24 CONSOLE=/dev/console
25 [ -c "$CONSOLE" ] && exec >"$CONSOLE" 2>&1 <"$CONSOLE"
26
27 # default variables
28 INSTALLED=false
29 REMOVABLE=false
30 USB_DEVICE=false
31 NOPROMPT=${NOPROMPT:-false}
32 NOEJECT=${NOEJECT:-false}
33 CMDLINE="$(cat /proc/cmdline 2>/dev/null)"
34
35 # Check if we are running from a GRML-CD or HD
36 [ -e /etc/grml_cd ] || INSTALLED=true
37
38 # Assume that we don't want the eject+prompt stuff when running in one
39 # of the special modes:
40 grep -qe 'toram' -qe '2ram' -qe 'usb' -qe 'serial' \
41      -qe 'fromhd' -qe 'isofrom' -qe 'fromiso' -qe 'findiso' \
42      /proc/cmdline && INSTALLED=true
43
44 # detect cdrom device
45 if ! $INSTALLED ; then
46    # live-boot layout since December 2012:
47    if [ -d /lib/live/mount/medium ] ; then
48      CDROM="$(awk '/ \/lib\/live\/mount\/medium /{print $1;exit 0;}' /proc/mounts)"
49      MOUNTPATH="/lib/live/mount/medium"
50    # live-boot layout until December 2012:
51    elif [ -d /live/image ] ; then
52      CDROM="$(awk '/ \/live\/image /{print $1;exit 0;}' /proc/mounts)"
53      MOUNTPATH="/live/image"
54    else # old layout:
55      CDROM="$(awk '/ \/cdrom /{print $1;exit 0;}' /proc/mounts)"
56      MOUNTPATH="/cdrom"
57    fi
58
59    # try to remove only removable devices
60    DEVICE=""
61    if [ -n "$CDROM" ] ; then
62      DEVICE="${CDROM##/dev/}"
63      [ -d /sys/block/${DEVICE} ] || DEVICE="${DEVICE%%[0-9]*}"
64
65      if [ -n "$DEVICE" ] ; then
66        # is it a removable device?
67        [ "$(cat /sys/block/${DEVICE}/removable 2>/dev/null)" = "1" ] && REMOVABLE=true
68        # is it a usb device?
69        readlink /sys/block/${DEVICE} 2>/dev/null | grep -q '/usb' && USB_DEVICE=true
70        $USB_DEVICE && USB_INFO=" ($(cat /sys/block/${DEVICE}/device/model 2>/dev/null))"
71      fi
72    fi
73 fi
74
75 case "$0" in
76   *halt)
77         level=0
78         message="grml system will be halted..."
79         command="halt"
80         ;;
81   *reboot)
82         level=6
83         message="Preparing for reboot..."
84         command="reboot"
85         ;;
86   *)
87         echo "Usage: call this script as \"halt\" or \"reboot\" please.">&2
88         exit 1
89         ;;
90 esac
91
92 mysleep() {
93   for i in $(seq 1 30) ; do
94     sleep 0.1
95     echo -n "$1.${NORMAL}"
96   done
97   echo ""
98 }
99
100 # Disable kernel messages
101 echo "0" > /proc/sys/kernel/printk
102
103 # make sure halt/reboot commands are available even if
104 # someone is using shutdown command or nfsroot is used:
105 cat /sbin/halt /sbin/reboot /etc/init.d/$command /etc/init.d/ups-monitor \
106        /etc/apcupsd/powerfail >/dev/null 2>&1
107
108 # We may kill our network connection here before unmounting NFS. Bad luck.
109 if [ -d /sys/bus/pcmcia -a -x /sbin/pccardctl ] ; then
110    # make sure we don't lose any data, see issue219 for details
111    log_begin_msg "Syncing devices..."
112    sync ; log_end_msg $?
113    log_begin_msg "Shutting down PCMCIA devices..."
114    pccardctl eject >/dev/null 2>&1
115    log_end_msg $?
116 fi
117
118 if $INSTALLED ; then
119    log_begin_msg_nn "Running /etc/init.d/rc ${level}: "
120    /etc/init.d/rc $level 1>/dev/null 2>&1 &
121    mysleep "$GREEN"
122    log_end_msg 0
123 fi
124
125 # Remove all truecrypt mappings...
126 if [ -x /usr/sbin/truecrypt ] ; then
127    if grep -q truecrypt /proc/modules ; then
128       log_begin_msg "Unmapping truecrypt volumes."
129       MSG=$(truecrypt -d 2>1)
130       echo $MSG | grep "No volumes mapped" && eend 0
131       [ -z "$MSG" ] && eend 0 || eend 1
132    fi
133 fi
134
135 # Now kill them all
136 killall5 -15
137 sleep 1
138 log_begin_msg_nn "Sending all processes the TERM signal: "
139 mysleep "$BLUE" ; log_end_msg $?
140
141 killall5 -9
142 sleep 1
143 log_begin_msg_nn "Sending all processes the KILL signal: "
144 mysleep "$RED" ; log_end_msg $?
145
146 # Turn off swap, then unmount file systems.
147 log_begin_msg "Turning off swap."
148 swapoff -a >/dev/null 2>&1 ; log_end_msg $?
149
150 # Udev
151 log_begin_msg "Deactivating udev:"
152 echo -n "   ${GREEN}-${NORMAL} "
153 /etc/init.d/udev stop ; log_end_msg $?
154
155 # noprompt and noeject handling
156 case "$CMDLINE" in
157   *noprompt*) NOPROMPT=true ; ;;
158   *noeject*)  NOEJECT=true ; ;;
159 esac
160 [ -r /etc/noprompt ] && NOPROMPT=true
161 [ -r /etc/noeject ]  && NOPROMPT=true && NOEJECT=true
162
163 VIRT_WHAT=$(/usr/sbin/virt-what 2>/dev/null)
164 if [ -n "$VIRT_WHAT" ] ; then
165   log_begin_msg "System seems to be a virtual machine, assuming noprompt as default."
166   NOPROMPT=true
167   log_end_msg 0
168 fi
169
170 # Turn on autoeject of CD-ROMs
171 if $NOEJECT ; then
172    for dev in /proc/sys/dev/cdrom*/lock;      do [ -f "$dev" ] && echo 0 > "$dev"; done
173    for dev in /proc/sys/dev/cdrom*/autoeject; do [ -f "$dev" ] && echo 1 > "$dev"; done
174 fi
175
176 # Umount file systems
177 log_begin_msg "Syncing local filesystems..."
178 sync && sleep 1 ; log_end_msg $?
179
180 log_begin_msg "Unmounting file systems."
181
182 # Be safe in case someone messed with /etc/mtab
183 if ! $INSTALLED ; then
184    rm -f /etc/mtab
185    ln -snf /proc/mounts /etc/mtab
186 fi
187
188 # Free loopback devices if necessary, so we can unmount the host media
189 for i in /dev/loop*; do losetup -d $i 2>/dev/null; done
190
191 umount -t notmpfs,nosysfs,noproc,nousbfs -adrvf 1>/dev/null 2>&1
192
193 log_end_msg 0
194
195 # Network stuff
196 log_begin_msg "Unmounting network filesystems"
197 umount -t nfs,nfs4,smbfs -alvf 2>/dev/null ; log_end_msg $?
198
199 NETDEVICES="$(/sbin/ifconfig | awk '/^[^ ]+/ {print $1}' | grep -v '^lo$')"
200 if [ -n "$NETDEVICES" ]; then
201    pidof pump >/dev/null 2>&1 && { pump -k ; sleep 2; }
202    killall dhclient dhclient3 2>/dev/null
203    log_begin_msg "Shutting down network device..."
204    for n in $NETDEVICES; do
205        echo "${SUBMSG} ${WHITE}$n${NORMAL}"
206        ifdown $n 1>/dev/null 2>&1
207        ifconfig $n down
208    done ; log_end_msg $?
209 fi
210
211 # For a harddisk installation: mount / ro
212 $INSTALLED && mount -n -o remount,ro / 2>/dev/null
213
214 # Prompt for removal of live system device
215 if ! $INSTALLED && ! $NOPROMPT ; then
216    # do not prompt for removal when running in grml2ram mode:
217    if ! mount | grep -qe 'on /cdrom' -qe 'on /live/image' -qe 'on /lib/live/mount/medium' ; then
218       echo "Live System not mounted, nothing to eject therefore."
219    else
220       [ -d "$MOUNTPATH" ] && umount -l "$MOUNTPATH"
221       if ! $NOEJECT && [ -n "$CDROM" ]  ; then
222          $REMOVABLE && eject -p "$CDROM" 2>/dev/null
223       fi
224       echo
225       if $USB_DEVICE ; then
226         # if we do not have any information about the usb model then fallback to the device name instead
227         [ -n "$USB_INFO" ] || USB_INFO=" (${CDROM})"
228         read -s -p "${GREEN}Please remove the USB device${USB_INFO} and press ENTER to continue [auto 2 minutes].${NORMAL}" -t 120 a
229       else
230         [ -n "$CDROM" ] && CDROM=" (${CDROM})"
231         read -s -p "${GREEN}Please remove the live system${CDROM}, close the tray (if any) and press ENTER to continue [auto 2 minutes].${NORMAL}" -t 120 a
232       fi
233    fi
234 fi
235
236 echo
237 echo "$message" >/dev/console
238
239 # Finally halt or reboot
240 /etc/init.d/$command stop
241
242 ## END OF FILE #################################################################