rc.shutdown 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. unset -v size
  52. chmod 600 /etc/random-seed
  53. # Try to write a login record (at /var/log/wtmp)
  54. halt -w
  55. # Flush file system buffers, update super block
  56. sync
  57. # Stop FUSE filesystem
  58. if test -x /etc/rc.d/rc.fuse
  59. then
  60. /etc/rc.d/rc.fuse stop > /dev/null
  61. fi
  62. # Kill all processes, except the one that is currently running
  63. echo "${RC}: Sending the TERM signal to all the processes"
  64. killall5 -15 -o $$
  65. sleep 9
  66. echo "${RC}: Deactivating swap devices"
  67. swapoff -v -a
  68. echo "${RC}: Unmounting local and remote file systems"
  69. umount -v -a -r
  70. sleep 1
  71. sync
  72. echo "${RC}: Remounting root filesystem in read-only mode"
  73. mount -v -n -o remount,ro /
  74. # Wait for completion of all the processes
  75. wait
  76. # Reboot, or halt the system
  77. case $0 in
  78. *.reboot)
  79. echo "Rebooting ..."
  80. exec reboot -d -f
  81. ;;
  82. *)
  83. exec halt -d -f -p
  84. ;;
  85. esac