grml-reboot: detect virtual machines + assume noprompt for them, add imvirt to Depends
[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 if [ "$(/usr/bin/imvirt 2>/dev/null)" != "Physical" ] ; then
153   log_begin_msg "System seems to be a virtual machine, assuming noprompt as default."
154   NOPROMPT=true
155   log_end_msg 0
156 fi
157
158 # Turn on autoeject of CD-ROMs
159 if $NOEJECT ; then
160    for dev in /proc/sys/dev/cdrom*/lock;      do [ -f "$dev" ] && echo 0 > "$dev"; done
161    for dev in /proc/sys/dev/cdrom*/autoeject; do [ -f "$dev" ] && echo 1 > "$dev"; done
162 fi
163
164 # Umount file systems
165 log_begin_msg "Syncing local filesystems..."
166 sync && sleep 1 ; log_end_msg $?
167
168 log_begin_msg "Unmounting file systems."
169
170 # Be safe in case someone messed with /etc/mtab
171 if ! $INSTALLED ; then
172    rm -f /etc/mtab
173    ln -snf /proc/mounts /etc/mtab
174 fi
175
176 # Free loopback devices if necessary, so we can unmount the host media
177 for i in /dev/loop*; do losetup -d $i 2>/dev/null; done
178
179 umount -t notmpfs,nosysfs,noproc,nousbfs -adrvf 1>/dev/null 2>&1
180
181 log_end_msg 0
182
183 # Network stuff
184 log_begin_msg "Unmounting network filesystems"
185 umount -t nfs,nfs4,smbfs -alvf 2>/dev/null ; log_end_msg $?
186
187 NETDEVICES="$(/sbin/ifconfig | awk '/^[^ ]+/ {print $1}' | grep -v '^lo$')"
188 if [ -n "$NETDEVICES" ]; then
189    pidof pump >/dev/null 2>&1 && { pump -k ; sleep 2; }
190    killall dhclient dhclient3 2>/dev/null
191    log_begin_msg "Shutting down network device..."
192    for n in $NETDEVICES; do
193        echo "${SUBMSG} ${WHITE}$n${NORMAL}"
194        ifdown $n 1>/dev/null 2>&1
195        ifconfig $n down
196    done ; log_end_msg $?
197 fi
198
199 # For a harddisk installation: mount / ro
200 $INSTALLED && mount -n -o remount,ro / 2>/dev/null
201
202 # Prompt for removal of live system device
203 if ! $INSTALLED && ! $NOPROMPT ; then
204    # do not prompt for removal when running in grml2ram mode:
205    if ! mount | grep -qe 'on /cdrom' -qe 'on /live/image' ; then
206       echo "Live System not mounted, nothing to eject therefore."
207    else
208       [ -d "$MOUNTPATH" ] && umount -l "$MOUNTPATH"
209       if ! $NOEJECT && [ -n "$CDROM" ]  ; then
210          $REMOVABLE && eject -p "$CDROM" 2>/dev/null
211       fi
212       echo
213       if $USB_DEVICE ; then
214         # if we do not have any information about the usb model then fallback to the device name instead
215         [ -n "$USB_INFO" ] || USB_INFO=" (${CDROM})"
216         read -s -p "${GREEN}Please remove the USB device${USB_INFO} and press ENTER to continue [auto 2 minutes].${NORMAL}" -t 120 a
217       else
218         [ -n "$CDROM" ] && CDROM=" (${CDROM})"
219         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
220       fi
221    fi
222 fi
223
224 echo
225 echo "$message" >/dev/console
226
227 # Finally halt or reboot
228 /etc/init.d/$command stop
229
230 ## END OF FILE #################################################################