/rc6.d/S30urandom

http://github.com/brinkman83/bashrc · Shell · 78 lines · 55 code · 6 blank · 17 comment · 10 complexity · d0385e199d51b19181b77dc55211ac02 MD5 · raw file

  1. #! /bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides: urandom
  4. # Required-Start: $remote_fs
  5. # Required-Stop: $remote_fs
  6. # Default-Start: S
  7. # Default-Stop: 0 6
  8. # Short-Description: Save and restore random seed between restarts.
  9. # Description: This script saves the random seed between restarts.
  10. # It is called from the boot, halt and reboot scripts.
  11. ### END INIT INFO
  12. [ -c /dev/urandom ] || exit 0
  13. PATH=/sbin:/usr/sbin:/bin:/usr/bin
  14. SAVEDFILE=/var/lib/urandom/random-seed
  15. POOLSIZE=512
  16. [ -f /proc/sys/kernel/random/poolsize ] && POOLSIZE="$(cat /proc/sys/kernel/random/poolsize)"
  17. . /lib/init/vars.sh
  18. . /lib/lsb/init-functions
  19. do_status () {
  20. if [ -f $SAVEDFILE ] ; then
  21. return 0
  22. else
  23. return 4
  24. fi
  25. }
  26. case "$1" in
  27. start|"")
  28. [ "$VERBOSE" = no ] || log_action_begin_msg "Initializing random number generator"
  29. # Load and then save $POOLSIZE bytes,
  30. # which is the size of the entropy pool
  31. if [ -f "$SAVEDFILE" ]
  32. then
  33. # Handle locally increased pool size
  34. SAVEDSIZE="$(find "$SAVEDFILE" -printf "%s")"
  35. if [ "$SAVEDSIZE" -gt "$POOLSIZE" ]
  36. then
  37. [ -w /proc/sys/kernel/random/poolsize ] && echo $POOLSIZE > /proc/sys/kernel/random/poolsize
  38. POOLSIZE=$SAVEDSIZE
  39. fi
  40. cat "$SAVEDFILE" >/dev/urandom
  41. fi
  42. rm -f $SAVEDFILE
  43. # Hm, why is the saved pool re-created at boot? [pere 2009-09-03]
  44. umask 077
  45. dd if=/dev/urandom of=$SAVEDFILE bs=$POOLSIZE count=1 >/dev/null 2>&1
  46. ES=$?
  47. umask 022
  48. [ "$VERBOSE" = no ] || log_action_end_msg $ES
  49. ;;
  50. stop)
  51. # Carry a random seed from shut-down to start-up;
  52. # see documentation in linux/drivers/char/random.c
  53. [ "$VERBOSE" = no ] || log_action_begin_msg "Saving random seed"
  54. umask 077
  55. dd if=/dev/urandom of=$SAVEDFILE bs=$POOLSIZE count=1 >/dev/null 2>&1
  56. ES=$?
  57. [ "$VERBOSE" = no ] || log_action_end_msg $ES
  58. ;;
  59. status)
  60. do_status
  61. exit $?
  62. ;;
  63. restart|reload|force-reload)
  64. echo "Error: argument '$1' not supported" >&2
  65. exit 3
  66. ;;
  67. *)
  68. echo "Usage: urandom start|stop" >&2
  69. exit 3
  70. ;;
  71. esac
  72. :