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