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 ################################################################################
9 export PATH=/sbin:/bin:/usr/bin:/usr/sbin
12 . /etc/grml/lsb-functions
18 [ -c "$CONSOLE" ] && exec >"$CONSOLE" 2>&1 <"$CONSOLE"
24 NOPROMPT=${NOPROMPT:-false}
25 NOEJECT=${NOEJECT:-false}
26 CMDLINE="$(cat /proc/cmdline 2>/dev/null)"
28 # Check if we are running from a GRML-CD or HD
29 [ -e /etc/grml_cd ] || INSTALLED=true
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
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)"
48 # try to remove only removable devices
50 if [ -n "$CDROM" ] ; then
51 DEVICE="${CDROM##/dev/}"
52 [ -d /sys/block/${DEVICE} ] || DEVICE="${DEVICE%%[0-9]*}"
54 if [ -n "$DEVICE" ] ; then
55 # is it a removable device?
56 [ "$(cat /sys/block/${DEVICE}/removable 2>/dev/null)" = "1" ] && REMOVABLE=true
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))"
67 message="grml system will be halted..."
72 message="Preparing for reboot..."
76 echo "Usage: call this script as \"halt\" or \"reboot\" please.">&2
82 for i in $(seq 1 40) ; do
84 echo -n "$1.${NORMAL}"
89 # Disable kernel messages
90 echo "0" > /proc/sys/kernel/printk
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
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
108 log_begin_msg_nn "Running /etc/init.d/rc ${level}: "
109 /etc/init.d/rc $level 1>/dev/null 2>&1 &
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
127 log_begin_msg_nn "Sending all processes the TERM signal: "
128 mysleep "$BLUE" ; log_end_msg $?
132 log_begin_msg_nn "Sending all processes the KILL signal: "
133 mysleep "$RED" ; log_end_msg $?
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 $?
140 log_begin_msg "Deactivating udev:"
141 echo -n " ${GREEN}-${NORMAL} "
142 /etc/init.d/udev stop ; log_end_msg $?
144 # noprompt and noeject handling
146 *noprompt*) NOPROMPT=true ; ;;
147 *noeject*) NOEJECT=true ; ;;
149 [ -r /etc/noprompt ] && NOPROMPT=true
150 [ -r /etc/noeject ] && NOPROMPT=true && NOEJECT=true
152 # Turn on autoeject of CD-ROMs
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
158 # Umount file systems
159 log_begin_msg "Syncing local filesystems..."
160 sync && sleep 1 ; log_end_msg $?
162 log_begin_msg "Unmounting file systems."
164 # Be safe in case someone messed with /etc/mtab
165 if ! $INSTALLED ; then
167 ln -snf /proc/mounts /etc/mtab
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
173 umount -t notmpfs,nosysfs,noproc,nousbfs -adrvf 1>/dev/null 2>&1
178 log_begin_msg "Unmounting network filesystems"
179 umount -t nfs,nfs4,smbfs -alvf 2>/dev/null ; log_end_msg $?
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
190 done ; log_end_msg $?
193 # For a harddisk installation: mount / ro
194 $INSTALLED && mount -n -o remount,ro / 2>/dev/null
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."
202 [ -d "$MOUNTPATH" ] && umount -l "$MOUNTPATH"
203 if ! $NOEJECT && [ -n "$CDROM" ] ; then
204 $REMOVABLE && eject -p "$CDROM" 2>/dev/null
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
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
219 echo "$message" >/dev/console
221 # Finally halt or reboot
222 /etc/init.d/$command stop
224 ## END OF FILE #################################################################