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