| 1 | #!/bin/bash |
|---|
| 2 | |
|---|
| 3 | ### BEGIN INIT INFO |
|---|
| 4 | # Provides: fll-reboot |
|---|
| 5 | # Required-Start: |
|---|
| 6 | # Required-Stop: |
|---|
| 7 | # Default-Start: |
|---|
| 8 | # Default-Stop: 0 6 |
|---|
| 9 | # Short-Description: shutdown and eject live media |
|---|
| 10 | # Description: This script provides the equivalent of umountfs, |
|---|
| 11 | # umountroot and halt on a stadard debian system. It umounts |
|---|
| 12 | # live mounts and ejects the live media at shutdown/reboot. |
|---|
| 13 | ### END INIT INFO |
|---|
| 14 | |
|---|
| 15 | ### |
|---|
| 16 | # F.U.L.L.S.T.O.R.Y init script |
|---|
| 17 | # |
|---|
| 18 | # Copyright: (C) 2007 F.U.L.L.S.T.O.R.Y Project |
|---|
| 19 | # Copyright: (C) 2007 Kel Modderman <kel@otaku42.de> |
|---|
| 20 | # Copyright: (C) 2007 Alan Baghumian <alan@technotux.org> |
|---|
| 21 | # License: GPLv2 |
|---|
| 22 | # |
|---|
| 23 | # F.U.L.L.S.T.O.R.Y Project Homepage: |
|---|
| 24 | # http://developer.berlios.de/projects/fullstory |
|---|
| 25 | ### |
|---|
| 26 | |
|---|
| 27 | PATH=/sbin:/usr/sbin:/bin:/usr/bin |
|---|
| 28 | NAME="fll-reboot" |
|---|
| 29 | |
|---|
| 30 | ### |
|---|
| 31 | # source distro-defaults, no-op unless in live mode |
|---|
| 32 | ### |
|---|
| 33 | FLL_DISTRO_MODE="installed" |
|---|
| 34 | |
|---|
| 35 | if [ -s /etc/default/distro ]; then |
|---|
| 36 | . /etc/default/distro |
|---|
| 37 | fi |
|---|
| 38 | |
|---|
| 39 | if [ "${FLL_DISTRO_MODE}" != "live" ]; then |
|---|
| 40 | exit 0 |
|---|
| 41 | fi |
|---|
| 42 | |
|---|
| 43 | ### |
|---|
| 44 | # VERBOSE setting and other rcS variables |
|---|
| 45 | ### |
|---|
| 46 | . /lib/init/vars.sh |
|---|
| 47 | |
|---|
| 48 | ### |
|---|
| 49 | # source lsb functions |
|---|
| 50 | ### |
|---|
| 51 | . /lib/lsb/init-functions |
|---|
| 52 | |
|---|
| 53 | ### |
|---|
| 54 | # ANSI escape sequences (N, R, G, Y, B, M, C, W) |
|---|
| 55 | ### |
|---|
| 56 | . /lib/fll/fll-init-cols |
|---|
| 57 | |
|---|
| 58 | ### |
|---|
| 59 | # define this for copy_exec |
|---|
| 60 | ### |
|---|
| 61 | DESTDIR="/live-hack" |
|---|
| 62 | |
|---|
| 63 | if [ -f /proc/cmdline ]; then |
|---|
| 64 | for param in $(cat /proc/cmdline); do |
|---|
| 65 | case "${param}" in |
|---|
| 66 | flldebug=*) |
|---|
| 67 | [ "${param#flldebug=}" = "${NAME#fll-}" ] && set -x |
|---|
| 68 | ;; |
|---|
| 69 | noeject) |
|---|
| 70 | NOEJECT="1" |
|---|
| 71 | ;; |
|---|
| 72 | noprompt) |
|---|
| 73 | NOPROMPT="1" |
|---|
| 74 | ;; |
|---|
| 75 | esac |
|---|
| 76 | done |
|---|
| 77 | fi |
|---|
| 78 | |
|---|
| 79 | ############################################################################### |
|---|
| 80 | # Adapted from initscripts: /etc/init.d/umountfs (2.86.ds1-38) |
|---|
| 81 | ############################################################################### |
|---|
| 82 | # Print in order of decreasing length |
|---|
| 83 | # |
|---|
| 84 | # Algorithm: Find and print longest argument, then call self |
|---|
| 85 | # to print remaining arguments in order of decreasing length |
|---|
| 86 | # |
|---|
| 87 | # This function runs at one tenth the speed of the sort program |
|---|
| 88 | # but we use the function because we don't want to rely on any |
|---|
| 89 | # programs in /usr/. |
|---|
| 90 | # |
|---|
| 91 | # N.B.: Arguments must not be null and must not contain whitespace |
|---|
| 92 | # |
|---|
| 93 | pioodl() { |
|---|
| 94 | [ "$1" ] || return 0 |
|---|
| 95 | |
|---|
| 96 | ARGNUM=1 |
|---|
| 97 | ARGNUM_LONGEST=0 |
|---|
| 98 | ARGLENGTH_LONGEST=0 |
|---|
| 99 | for ARG in "$@" |
|---|
| 100 | do |
|---|
| 101 | ARGLENGTH="${#ARG}" |
|---|
| 102 | if [ "$ARGLENGTH" -gt "$ARGLENGTH_LONGEST" ] |
|---|
| 103 | then |
|---|
| 104 | ARGLENGTH_LONGEST="$ARGLENGTH" |
|---|
| 105 | ARGNUM_LONGEST="$ARGNUM" |
|---|
| 106 | fi |
|---|
| 107 | ARGNUM=$(($ARGNUM + 1)) |
|---|
| 108 | done |
|---|
| 109 | |
|---|
| 110 | # The method of passing prevargs assumes that args can be |
|---|
| 111 | # delimited with spaces |
|---|
| 112 | ARGNUM=1 |
|---|
| 113 | PREVARGS="" |
|---|
| 114 | while [ "$ARGNUM" -lt "$ARGNUM_LONGEST" ] |
|---|
| 115 | do |
|---|
| 116 | PREVARGS="$PREVARGS $1" |
|---|
| 117 | shift |
|---|
| 118 | ARGNUM=$(($ARGNUM + 1)) |
|---|
| 119 | done |
|---|
| 120 | echo "$1" |
|---|
| 121 | shift |
|---|
| 122 | |
|---|
| 123 | pioodl $PREVARGS "$@" |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | do_umount() { |
|---|
| 127 | exec 9<&0 < /proc/mounts |
|---|
| 128 | |
|---|
| 129 | REG_MTPTS="" |
|---|
| 130 | TMPFS_MTPTS="" |
|---|
| 131 | while read DEV MTPT FSTYPE OPTS REST |
|---|
| 132 | do |
|---|
| 133 | case "$MTPT" in |
|---|
| 134 | # |
|---|
| 135 | # live hack |
|---|
| 136 | # |
|---|
| 137 | /fll/*) |
|---|
| 138 | case "$FSTYPE" in |
|---|
| 139 | iso9660) |
|---|
| 140 | if [ "$MTPT" = /fll/fromiso ]; then |
|---|
| 141 | LIVE_MTPTS="$LIVE_MTPTS $MTPT" |
|---|
| 142 | else |
|---|
| 143 | LIVE_CDROM="$DEV" |
|---|
| 144 | fi |
|---|
| 145 | ;; |
|---|
| 146 | *) |
|---|
| 147 | LIVE_MTPTS="$LIVE_MTPTS $MTPT" |
|---|
| 148 | ;; |
|---|
| 149 | esac |
|---|
| 150 | continue |
|---|
| 151 | ;; |
|---|
| 152 | # |
|---|
| 153 | # live hack |
|---|
| 154 | # |
|---|
| 155 | /|/proc|/dev|/.dev|/dev/pts|/dev/shm|/dev/.static/dev|/proc/*|/sys|/lib/init/rw) |
|---|
| 156 | continue |
|---|
| 157 | ;; |
|---|
| 158 | /var/run) |
|---|
| 159 | if [ yes = "$RAMRUN" ] ; then |
|---|
| 160 | continue |
|---|
| 161 | fi |
|---|
| 162 | ;; |
|---|
| 163 | /var/lock) |
|---|
| 164 | if [ yes = "$RAMLOCK" ] ; then |
|---|
| 165 | continue |
|---|
| 166 | fi |
|---|
| 167 | ;; |
|---|
| 168 | esac |
|---|
| 169 | case "$FSTYPE" in |
|---|
| 170 | proc|procfs|linprocfs|devfs|sysfs|usbfs|usbdevfs|devpts) |
|---|
| 171 | continue |
|---|
| 172 | ;; |
|---|
| 173 | # |
|---|
| 174 | # live hack |
|---|
| 175 | # |
|---|
| 176 | aufs|unionfs) |
|---|
| 177 | UNION_MTPTS="$UNION_MTPTS $MTPT" |
|---|
| 178 | ;; |
|---|
| 179 | squashfs) |
|---|
| 180 | SQUSH_MTPTS="$SQUSH_MTPTS $MTPT" |
|---|
| 181 | ;; |
|---|
| 182 | # |
|---|
| 183 | # live hack |
|---|
| 184 | # |
|---|
| 185 | tmpfs) |
|---|
| 186 | TMPFS_MTPTS="$TMPFS_MTPTS $MTPT" |
|---|
| 187 | ;; |
|---|
| 188 | *) |
|---|
| 189 | REG_MTPTS="$REG_MTPTS $MTPT" |
|---|
| 190 | ;; |
|---|
| 191 | esac |
|---|
| 192 | done |
|---|
| 193 | |
|---|
| 194 | exec 0<&9 9<&- |
|---|
| 195 | |
|---|
| 196 | # |
|---|
| 197 | # Make sure tmpfs file systems are umounted before turning off |
|---|
| 198 | # swap, to avoid running out of memory if the tmpfs filesystems |
|---|
| 199 | # use a lot of space. |
|---|
| 200 | # |
|---|
| 201 | if [ "$TMPFS_MTPTS" ] |
|---|
| 202 | then |
|---|
| 203 | TMPFS_MTPTS="$(pioodl $TMPFS_MTPTS)" |
|---|
| 204 | if [ "$VERBOSE" = no ] |
|---|
| 205 | then |
|---|
| 206 | log_action_begin_msg "Unmounting temporary filesystems" |
|---|
| 207 | umount $TMPFS_MTPTS |
|---|
| 208 | log_action_end_msg $? |
|---|
| 209 | else |
|---|
| 210 | log_daemon_msg "Will now unmount temporary filesystems" |
|---|
| 211 | umount -v $TMPFS_MTPTS |
|---|
| 212 | log_end_msg $? |
|---|
| 213 | fi |
|---|
| 214 | fi |
|---|
| 215 | |
|---|
| 216 | # |
|---|
| 217 | # Deactivate swap |
|---|
| 218 | # |
|---|
| 219 | if [ "$VERBOSE" = no ] |
|---|
| 220 | then |
|---|
| 221 | log_action_begin_msg "Deactivating swap" |
|---|
| 222 | swapoff -a >/dev/null |
|---|
| 223 | log_action_end_msg $? |
|---|
| 224 | else |
|---|
| 225 | log_daemon_msg "Will now deactivate swap" |
|---|
| 226 | swapoff -a -v |
|---|
| 227 | log_end_msg $? |
|---|
| 228 | fi |
|---|
| 229 | |
|---|
| 230 | # |
|---|
| 231 | # Unmount local filesystems |
|---|
| 232 | # |
|---|
| 233 | if [ "$REG_MTPTS" ] |
|---|
| 234 | then |
|---|
| 235 | REG_MTPTS="$(pioodl $REG_MTPTS)" |
|---|
| 236 | if [ "$VERBOSE" = no ] |
|---|
| 237 | then |
|---|
| 238 | log_action_begin_msg "Unmounting local filesystems" |
|---|
| 239 | umount -n -f -r -d $REG_MTPTS |
|---|
| 240 | log_action_end_msg $? |
|---|
| 241 | else |
|---|
| 242 | log_daemon_msg "Will now unmount local filesystems" |
|---|
| 243 | umount -n -f -v -r -d $REG_MTPTS |
|---|
| 244 | log_end_msg $? |
|---|
| 245 | fi |
|---|
| 246 | fi |
|---|
| 247 | |
|---|
| 248 | # |
|---|
| 249 | # live hack: any command after this point must be prefixed with ${BINDIR} |
|---|
| 250 | # |
|---|
| 251 | if [ "$UNION_MTPTS" ] |
|---|
| 252 | then |
|---|
| 253 | UNION_MTPTS="$(pioodl $UNION_MTPTS)" |
|---|
| 254 | if [ "$VERBOSE" = no ] |
|---|
| 255 | then |
|---|
| 256 | log_action_begin_msg "Unmounting union filesystems" |
|---|
| 257 | ${BINDIR}umount -l -n -f -d $UNION_MTPTS |
|---|
| 258 | log_action_end_msg $? |
|---|
| 259 | else |
|---|
| 260 | log_daemon_msg "Will now unmount union filesystems" |
|---|
| 261 | ${BINDIR}umount -l -n -f -v -d $UNION_MTPTS |
|---|
| 262 | log_end_msg $? |
|---|
| 263 | fi |
|---|
| 264 | fi |
|---|
| 265 | |
|---|
| 266 | if [ "$SQSH_MTPTS" ] |
|---|
| 267 | then |
|---|
| 268 | SQSH_MTPTS="$(pioodl $SQSH_MTPTS)" |
|---|
| 269 | if [ "$VERBOSE" = no ] |
|---|
| 270 | then |
|---|
| 271 | log_action_begin_msg "Unmounting squashfs filesystems" |
|---|
| 272 | ${BINDIR}umount -l -n -f -d $SQSH_MTPTS |
|---|
| 273 | log_action_end_msg $? |
|---|
| 274 | else |
|---|
| 275 | log_daemon_msg "Will now unmount squashfs filesystems" |
|---|
| 276 | ${BINDIR}umount -l -n -f -v -d $SQSH_MTPTS |
|---|
| 277 | log_end_msg $? |
|---|
| 278 | fi |
|---|
| 279 | fi |
|---|
| 280 | |
|---|
| 281 | if [ "$LIVE_MTPTS" ] |
|---|
| 282 | then |
|---|
| 283 | LIVE_MTPTS="$(pioodl $LIVE_MTPTS)" |
|---|
| 284 | if [ "$VERBOSE" = no ] |
|---|
| 285 | then |
|---|
| 286 | log_action_begin_msg "Unmounting live filesystems" |
|---|
| 287 | ${BINDIR}umount -l -n -f -d $LIVE_MTPTS |
|---|
| 288 | log_action_end_msg $? |
|---|
| 289 | else |
|---|
| 290 | log_daemon_msg "Will now unmount live filesystems" |
|---|
| 291 | ${BINDIR}umount -l -n -f -v -d $LIVE_MTPTS |
|---|
| 292 | log_end_msg $? |
|---|
| 293 | fi |
|---|
| 294 | fi |
|---|
| 295 | |
|---|
| 296 | if [ "$LIVE_CDROM" ]; then |
|---|
| 297 | export LIVE_CDROM |
|---|
| 298 | fi |
|---|
| 299 | } |
|---|
| 300 | ############################################################################### |
|---|
| 301 | # Adapted from initramfs-tools hook-functions (0.87b) |
|---|
| 302 | ############################################################################### |
|---|
| 303 | # $1 is source |
|---|
| 304 | # $2 is relative destination |
|---|
| 305 | copy_exec() { |
|---|
| 306 | local verbose="${VERBOSE}" |
|---|
| 307 | local final_destination=${DESTDIR}/${2}/`basename ${1}` |
|---|
| 308 | |
|---|
| 309 | if [ -L "$final_destination" ]; then |
|---|
| 310 | if ! [ `readlink ${final_destination}` = "${1}" ]; then |
|---|
| 311 | return |
|---|
| 312 | fi |
|---|
| 313 | else |
|---|
| 314 | cp ${1} ${DESTDIR}/${2} |
|---|
| 315 | if [ -n "${verbose}" ] && [ "${verbose}" = "y" ]; then |
|---|
| 316 | echo "Adding binary ${1}" |
|---|
| 317 | fi |
|---|
| 318 | fi |
|---|
| 319 | |
|---|
| 320 | # Copy the dependant libraries |
|---|
| 321 | for x in $(ldd ${1} 2>/dev/null | sed -e ' |
|---|
| 322 | /\//!d; |
|---|
| 323 | /linux-gate/d; |
|---|
| 324 | /=>/ {s/.*=>[[:blank:]]*\([^[:blank:]]*\).*/\1/}; |
|---|
| 325 | s/[[:blank:]]*\([^[:blank:]]*\) (.*)/\1/' 2>/dev/null); do |
|---|
| 326 | |
|---|
| 327 | # Try to use non-optimised libraries where possible. |
|---|
| 328 | # We assume that all HWCAP libraries will be in tls. |
|---|
| 329 | nonoptlib=$(echo ${x} | sed -e 's#/lib/tls.*/\(lib.*\)#/lib/\1#') |
|---|
| 330 | |
|---|
| 331 | if [ -e ${nonoptlib} ]; then |
|---|
| 332 | x=${nonoptlib} |
|---|
| 333 | fi |
|---|
| 334 | |
|---|
| 335 | libname=$(basename ${x}) |
|---|
| 336 | dirname=$(dirname ${x}) |
|---|
| 337 | |
|---|
| 338 | mkdir -p ${DESTDIR}/${dirname} |
|---|
| 339 | if [ ! -e ${DESTDIR}/${dirname}/${libname} ]; then |
|---|
| 340 | cp ${x} ${DESTDIR}/${dirname} |
|---|
| 341 | if [ -n "${verbose}" ] && [ "${verbose}" = "y" ]; then |
|---|
| 342 | echo "Adding library ${x}" |
|---|
| 343 | fi |
|---|
| 344 | fi |
|---|
| 345 | done |
|---|
| 346 | } |
|---|
| 347 | ############################################################################### |
|---|
| 348 | # End adapted functions |
|---|
| 349 | ############################################################################### |
|---|
| 350 | |
|---|
| 351 | live_hack() { |
|---|
| 352 | mkdir -p ${DESTDIR}/bin |
|---|
| 353 | copy_exec /bin/umount /bin |
|---|
| 354 | copy_exec /sbin/halt /bin |
|---|
| 355 | copy_exec /sbin/reboot /bin |
|---|
| 356 | copy_exec /usr/bin/eject /bin |
|---|
| 357 | |
|---|
| 358 | LD_LIBRARY_PATH="${DESTDIR}/lib" |
|---|
| 359 | if [ -e "${DESTDIR}/lib64" ]; then |
|---|
| 360 | LD_LIBRARY_PATH="${DESTDIR}/lib64:${LD_LIBRARY_PATH}" |
|---|
| 361 | fi |
|---|
| 362 | export LD_LIBRARY_PATH |
|---|
| 363 | |
|---|
| 364 | LD_LINUX="${DESTDIR}/lib/ld-linux.so.2" |
|---|
| 365 | if [ -e "${DESTDIR}/lib64/ld-linux-x86-64.so.2" ]; then |
|---|
| 366 | LD_LINUX="${DESTDIR}/lib64/ld-linux-x86-64.so.2" |
|---|
| 367 | fi |
|---|
| 368 | export LD_LINUX |
|---|
| 369 | |
|---|
| 370 | PATH="${DESTDIR}/bin:${PATH}" |
|---|
| 371 | export PATH |
|---|
| 372 | |
|---|
| 373 | BINDIR="${LD_LINUX} ${DESTDIR}/bin/" |
|---|
| 374 | export BINDIR |
|---|
| 375 | } |
|---|
| 376 | |
|---|
| 377 | pop_live_cd() |
|---|
| 378 | { |
|---|
| 379 | if [ -z "${LIVE_CDROM}" ] || [ "${NOEJECT}" ]; then |
|---|
| 380 | return 0 |
|---|
| 381 | fi |
|---|
| 382 | |
|---|
| 383 | # |
|---|
| 384 | # disable kernel messages while ejecting cdrom (libata noise) |
|---|
| 385 | # |
|---|
| 386 | echo "0" > /proc/sys/kernel/printk |
|---|
| 387 | |
|---|
| 388 | if [ -b "${LIVE_CDROM}" ]; then |
|---|
| 389 | ${BINDIR}eject -m -p ${LIVE_CDROM} |
|---|
| 390 | fi |
|---|
| 391 | |
|---|
| 392 | echo "6" > /proc/sys/kernel/printk |
|---|
| 393 | |
|---|
| 394 | if [ -z "${NOPROMPT}" ]; then |
|---|
| 395 | echo "${Y}Please remove CD, close cdrom drive and hit return.${N}" > /dev/console |
|---|
| 396 | read |
|---|
| 397 | fi |
|---|
| 398 | } |
|---|
| 399 | |
|---|
| 400 | do_stop() { |
|---|
| 401 | local command message options |
|---|
| 402 | |
|---|
| 403 | # prepare binaries for post-umount of live media |
|---|
| 404 | live_hack |
|---|
| 405 | # umount the live media and other mount points |
|---|
| 406 | do_umount |
|---|
| 407 | |
|---|
| 408 | case "${0}" in |
|---|
| 409 | *halt) |
|---|
| 410 | message="${G}${FLL_DISTRO_SNAME} halted.${N}" |
|---|
| 411 | command="halt" |
|---|
| 412 | options="-h -n -p -i -f" |
|---|
| 413 | ;; |
|---|
| 414 | *reboot) |
|---|
| 415 | message="${G}Preparing for reboot...${N}" |
|---|
| 416 | command="reboot" |
|---|
| 417 | options="-n -i -f" |
|---|
| 418 | ;; |
|---|
| 419 | *) |
|---|
| 420 | echo "${0}: call this script as \"halt\" or \"reboot\" please!" |
|---|
| 421 | exit 1 |
|---|
| 422 | ;; |
|---|
| 423 | esac |
|---|
| 424 | |
|---|
| 425 | echo "${message}" > /dev/console |
|---|
| 426 | |
|---|
| 427 | pop_live_cd |
|---|
| 428 | |
|---|
| 429 | exec ${BINDIR}${command} ${options} > /dev/console 2>&1 < /dev/console |
|---|
| 430 | } |
|---|
| 431 | |
|---|
| 432 | case "${1}" in |
|---|
| 433 | start) |
|---|
| 434 | # no-op |
|---|
| 435 | ;; |
|---|
| 436 | restart|reload|force-reload) |
|---|
| 437 | echo "Error: argument '${1}' not supported" >&2 |
|---|
| 438 | exit 3 |
|---|
| 439 | ;; |
|---|
| 440 | stop) |
|---|
| 441 | # live-hack, umount and eject |
|---|
| 442 | do_stop |
|---|
| 443 | ;; |
|---|
| 444 | *) |
|---|
| 445 | echo "Usage: ${NAME} {start|stop}" >&2 |
|---|
| 446 | exit 3 |
|---|
| 447 | ;; |
|---|
| 448 | esac |
|---|