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