]>
Commit | Line | Data |
---|---|---|
791f7cd2 JR |
1 | #!/bin/sh |
2 | # | |
af55fe6b | 3 | # shutdown Common script for system halt/reboot. |
791f7cd2 | 4 | # |
5e6dfc29 | 5 | # Author: Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org> |
791f7cd2 | 6 | # |
791f7cd2 JR |
7 | |
8 | # Set the path. | |
9 | PATH=/sbin:/bin:/usr/bin:/usr/sbin | |
10 | ||
de160ea3 AM |
11 | # move to root dir |
12 | cd / | |
13 | ||
d84e0c78 JR |
14 | IN_SHUTDOWN=yes |
15 | ||
791f7cd2 JR |
16 | . /etc/rc.d/init.d/functions |
17 | ||
b6509675 AM |
18 | # avoid keyboard interruption |
19 | trap "echo" INT SEGV QUIT TERM | |
20 | set +e | |
21 | ||
22 | rc_splash "reboot" | |
23 | ||
791f7cd2 JR |
24 | # Kill all processes. |
25 | [ "${BASH+bash}" = bash ] && enable kill | |
26 | ||
d0f42664 | 27 | runlevel=$1 |
cc10e704 | 28 | previous=$2 |
d0f42664 | 29 | |
0868c9a6 | 30 | # Stop blogd before umounting /var |
b171122f ER |
31 | if [ -x /sbin/blogd ]; then |
32 | killall -q -QUIT blogd | |
0868c9a6 ER |
33 | fi |
34 | ||
cf360554 ER |
35 | if [ -e /dev/rtc -o -e /dev/rtc0 ]; then |
36 | run_cmd "$(nls 'Syncing hardware clock to system time')" hwclock --systohc | |
37 | fi | |
38 | ||
74c57003 AM |
39 | # check for other processes. |
40 | # there could be none if all services were terminated properly | |
41 | # pgrep -g 0 requires procps >= 3.2.6-1.1 | |
bcff88d5 | 42 | # XXX: pgrep is on /usr! |
74c57003 | 43 | pids=$(pgrep -g 0 -l -v | grep -v '^1 ') |
bcff88d5 | 44 | if [ -n "$pids" ] || [ ! -x /usr/bin/pgrep ]; then |
74c57003 AM |
45 | run_cmd "Sending all processes the TERM signal" killall5 -15 |
46 | fi | |
47 | ||
48 | pids=$(pgrep -g 0 -l -v | grep -v '^1 ') | |
bcff88d5 | 49 | if [ -n "$pids" ] || [ ! -x /usr/bin/pgrep ]; then |
74c57003 AM |
50 | sleep 5 |
51 | run_cmd "Sending all processes the KILL signal" killall5 -9 | |
52 | fi | |
791f7cd2 JR |
53 | |
54 | # Write to wtmp file before unmounting /var | |
1822a5a6 | 55 | halt -w |
791f7cd2 | 56 | |
1aaf8c92 JR |
57 | if ! is_yes "$VSERVER"; then |
58 | # Turn off swap, then unmount file systems. | |
59 | run_cmd "Turning off swap" swapoff -a | |
7d7fd6c1 | 60 | |
1aaf8c92 | 61 | [ -x /etc/rc.d/rc.acct ] && /etc/rc.d/rc.acct stop |
791f7cd2 | 62 | |
1aaf8c92 JR |
63 | if [ -x /sbin/quotaoff ]; then |
64 | run_cmd "Turning off quotas for local filesystems" /sbin/quotaoff -a | |
65 | fi | |
2eec3807 | 66 | |
6b30816c ER |
67 | # grab kexec_loaded state before we umount /sys |
68 | kexec_loaded=`cat /sys/kernel/kexec_loaded 2>/dev/null` | |
69 | ||
cc10e704 JR |
70 | # Unmount file systems, killing processes if we have to. |
71 | sig=-15 | |
72 | retry=3 | |
73 | force= | |
4cc198ff JR |
74 | if [ -z "$UMOUNT_IGNORE" ]; then |
75 | UMOUNT_IGNORE="/" | |
df82ecd4 | 76 | else |
4cc198ff | 77 | UMOUNT_IGNORE="/ $UMOUNT_IGNORE" |
df82ecd4 | 78 | fi |
4cc198ff | 79 | remaining=$(awk -v ig="^($UMOUNT_IGNORE)$" 'BEGIN { gsub(/[\t ]+/, "|", ig); } \ |
8f507628 | 80 | $3 !~ /^(proc|loopfs|devpts|devtmpfs|shm|iso9660|ramfs|tmpfs|cgroup|cgroup2|sysfs|securityfs|squashfs)$/ \ |
2850ca3d | 81 | && $1 !~ /^(none|\/dev\/root)$/ \ |
4cc198ff | 82 | && $2 !~ ig {print $2}' /proc/mounts) |
2a57d1ce | 83 | while [ -n "$remaining" -a "$retry" -gt 0 ]; do |
5a02578c AM |
84 | for mnt in $remaining; do |
85 | run_cmd "$(nls 'Umounting file system: %s' $mnt)" umount $force -R $mnt | |
86 | ||
87 | # we might had unmounted /usr, recheck $TPUT availability | |
88 | # but well. we need tput only for show() and busy() (ok() and fail() messages are already cached) | |
89 | # TODO: look ahead the messages? | |
90 | if is_yes "$TPUT"; then | |
91 | TPUT= | |
92 | rc_gettext_init | |
93 | fi | |
94 | done | |
95 | ||
96 | show "Unmounting remainging file systems"; busy | |
97 | ERRORS=$(umount -a $force -t noproc,nosysfs,notmpfs,nodevpts,nodevtmpfs 2>&1); rc=$? | |
2a57d1ce ER |
98 | |
99 | # we might had unmounted /usr, recheck $TPUT availability | |
100 | # but well. we need tput only for show() and busy() (ok() and fail() messages are already cached) | |
101 | # TODO: look ahead the messages? | |
102 | if is_yes "$TPUT"; then | |
103 | TPUT= | |
104 | rc_gettext_init | |
105 | fi | |
106 | ||
107 | if [ $rc = 0 ]; then | |
cc10e704 JR |
108 | ok |
109 | else | |
110 | fail | |
5a02578c AM |
111 | if [ -n "$ERRORS" ]; then |
112 | nls "Umount errors:" | |
113 | echo "$ERRORS" | |
114 | fi | |
cc10e704 | 115 | fi |
5e6dfc29 | 116 | |
cc10e704 | 117 | sleep 2 |
4cc198ff | 118 | remaining=$(awk -v ig="^($UMOUNT_IGNORE)$" 'BEGIN { gsub(/[\t ]+/, "|", ig); } \ |
8f507628 | 119 | $3 !~ /^(proc|loopfs|devpts|devtmpfs|shm|iso9660|ramfs|tmpfs|cgroup|cgroup2|sysfs|securityfs|squashfs)$/ \ |
2850ca3d | 120 | && $1 !~ /^(none|\/dev\/root)$/ \ |
4cc198ff | 121 | && $2 !~ ig {print $2}' /proc/mounts) |
cc10e704 | 122 | [ -z "$remaining" ] && break |
4b0d64e2 | 123 | fuser -k -m $sig $remaining > /dev/null |
cc10e704 JR |
124 | sleep 5 |
125 | retry=$(($retry-1)) | |
126 | sig=-9 | |
127 | force="-f" | |
128 | done | |
129 | ||
130 | run_cmd "Remounting root filesystem in ro mode" mount -n -o remount,ro / | |
131 | ||
9bec7e9f | 132 | if [ -x /sbin/vgchange ]; then |
0174ae89 | 133 | lvmversion=$(LC_ALL=C /sbin/vgchange --version 2>/dev/null | awk '/LVM version:/{if ($3 >= 2) print "2"}') |
9bec7e9f | 134 | if [ "$lvmversion" = "2" ]; then |
6371a9cf | 135 | lvmsysinit="--sysinit --noudevsync" |
cc10e704 | 136 | else |
ffa93fa2 | 137 | lvmsysinit="" |
cc10e704 | 138 | fi |
ffa93fa2 | 139 | /sbin/vgchange -a n $lvmsysinit > /dev/null 2>&1 |
41781ded | 140 | fi |
41781ded | 141 | |
cc10e704 | 142 | if [ -x /sbin/mdadm -a -f /etc/mdadm.conf ]; then |
62119804 | 143 | if grep -qE "^([[:blank:]]|)ARRAY[[:blank:]]" /etc/mdadm.conf 2>/dev/null; then |
9045fdf6 | 144 | /sbin/mdadm --stop --scan > /dev/null 2>&1 |
5e6dfc29 | 145 | rc=$? |
cc10e704 | 146 | fi |
8fb3abd8 | 147 | fi |
5e6dfc29 | 148 | |
cc10e704 | 149 | show "Remounting remaining filesystems ro mode"; busy |
62119804 | 150 | if mount | awk '/ext2|ext3|reiserfs|xfs|jfs/ { print $3 }' | while read line; do mount -n -o ro,remount $line; done; then |
cc10e704 JR |
151 | ok |
152 | else | |
153 | fail | |
154 | fi | |
2e33f3ce JR |
155 | _rebootwhat="system" |
156 | else | |
157 | _rebootwhat="vserver" | |
158 | fi | |
159 | ||
6b496866 | 160 | if [ -f /etc/crypttab ] && ! is_empty_file /etc/crypttab; then |
8710c4b5 ER |
161 | . /etc/rc.d/init.d/cryptsetup |
162 | ||
163 | show "Stopping disk encryption" | |
878e1414 ER |
164 | halt_crypto && ok || fail |
165 | fi | |
166 | ||
d0f42664 | 167 | if [ "$runlevel" = "0" ] ; then |
2e33f3ce | 168 | show "The $_rebootwhat is halted"; ok |
62119804 ER |
169 | if [ -f /fastboot ]; then |
170 | show "On the next boot fsck will be skipped."; ok | |
171 | fi | |
bc451b91 | 172 | |
d0b6372d | 173 | if [ -f /etc/killpower -a -f /etc/sysconfig/ups ] ; then |
5e6dfc29 | 174 | . /etc/sysconfig/ups |
d0b6372d AM |
175 | if is_yes "$POWEROFF_UPS"; then |
176 | if [ -d /etc/ups.d ]; then | |
03ef5439 | 177 | for i in /etc/ups.d/*.sh; do |
d0b6372d AM |
178 | [ ! -f $i -o ! -x $i ] && continue |
179 | $i poweroff | |
180 | done | |
181 | fi | |
182 | [ -x /sbin/poweroff-ups ] && /sbin/poweroff-ups | |
183 | fi | |
cc10e704 | 184 | fi |
554bf775 | 185 | [ "$previous" != "unknown" ] && halt -d -p -f |
d0f42664 | 186 | else |
2e33f3ce | 187 | show "Please stand by while rebooting the $_rebootwhat"; ok |
62119804 ER |
188 | if [ -f /fastboot ]; then |
189 | show "On the next boot fsck will be skipped."; ok | |
190 | fi | |
69d7c00f | 191 | |
6b30816c ER |
192 | if [ -x /sbin/kexec ] && [ "$kexec_loaded" = "1" ]; then |
193 | show "Will now restart with kexec" | |
69d7c00f | 194 | /sbin/kexec -e |
6b30816c | 195 | fail |
69d7c00f ER |
196 | fi |
197 | ||
554bf775 | 198 | [ "$previous" != "unknown" ] && reboot -d -f |
d0f42664 | 199 | fi |