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