rc.shutdown 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #! /bin/sh -
  2. #
  3. # Prepare to halt or reboot the system
  4. #
  5. # Copyright (c) 2017-2021 Matias Fonzo, <selk@dragora.org>.
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. # To reflect the name of this script
  19. RC="[${0##*/}]"
  20. umask 022
  21. IFS='
  22. '
  23. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/usr/sbin:/bin:/usr/bin
  24. LC_ALL=C
  25. # Set linefeed mode to avoid staircase effect
  26. stty onlcr 0>&1
  27. # Be immune to the following signals
  28. trap "" HUP INT QUIT ABRT TERM
  29. # Save system clock
  30. if grep -q '^RC_HWCLOCK=local' /etc/rc.conf
  31. then
  32. echo "${RC}: Saving hardware clock in local time"
  33. hwclock --systohc --localtime
  34. else
  35. echo "${RC}: Saving hardware clock in UTC"
  36. hwclock --systohc --utc
  37. fi
  38. # Stop any quota file system (if needed)
  39. if grep -E -q -m 1 '(usr|grp)quota' /etc/fstab
  40. then
  41. echo "${RC}: Setting any quota file system to OFF"
  42. quotaoff -va
  43. fi
  44. # Save the random number generator, see random(4)
  45. echo "${RC}: Saving random number generator"
  46. echo " /dev/urandom <-> /etc/random-seed ..."
  47. # Get the pool size from /proc or use a default value
  48. size="$(cat /proc/sys/kernel/random/poolsize)" || size=512
  49. # Save seed file containing the whole entropy pool
  50. dd if=/dev/urandom of=/etc/random-seed count=1 bs=$size
  51. chmod 600 /etc/random-seed; unset -v size
  52. # Try to write a login record (at /var/log/wtmp)
  53. halt -w
  54. # Flush file system buffers, update super block
  55. sync
  56. # Stop FUSE filesystem
  57. if test -x /etc/rc.d/rc.fuse
  58. then
  59. /etc/rc.d/rc.fuse stop > /dev/null
  60. fi
  61. # Kill all processes, except the one that is currently running
  62. echo "${RC}: Sending the TERM signal to all the processes"
  63. killall5 -15 -o $$
  64. sleep 9
  65. echo "${RC}: Deactivating swap devices"
  66. swapoff -v -a
  67. echo "${RC}: Unmounting local and remote file systems"
  68. umount -v -a -r
  69. sleep 1
  70. sync
  71. echo "${RC}: Remounting root filesystem in read-only mode"
  72. mount -v -n -o remount,ro /
  73. # Wait for completion of all the processes
  74. wait
  75. # Reboot, or halt the system
  76. case $0 in
  77. *.reboot)
  78. echo "Rebooting ..."
  79. exec reboot -d -f
  80. ;;
  81. *)
  82. exec halt -d -f -p
  83. ;;
  84. esac