#! /bin/sh
# chkconfig: 2345 05 95
### BEGIN INIT INFO
# Provides:          urandom
# Required-Start:    modules $local_fs
# Required-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: save random seed between boots
# Description:       save random seed between boots
# X-Start-Before:    network
# X-Stop-After:      
# X-Timesys-Start-Number:  05
# X-Timesys-Stop-Number:  95
### END INIT INFO

[ -c /dev/urandom ] || exit 0

case "$1" in
	start)
		echo -n "Initializing random number generator... "

		# Load and then save 512 bytes,
		# which is the size of the entropy pool
		if [ -f /etc/random-seed ]; then
			cat /etc/random-seed >/dev/urandom
		fi

		# check for read only file system
		touch /etc/random-seed 2>/dev/null || exit

		rm -f /etc/random-seed
		if dd if=/dev/urandom of=/etc/random-seed count=1 >/dev/null 2>&1; then
		  echo "[ OK ]"
    else
      echo "[ FAIL ]"
      exit 1
    fi
		;;
	stop)
		# check for read only file system
		touch /etc/random-seed 2>/dev/null || exit

		# Carry a random seed from shut-down to start-up;
		# see documentation in linux/drivers/char/random.c
		echo -n "Saving random seed... "
		if dd if=/dev/urandom of=/etc/random-seed count=1 >/dev/null 2>&1; then
		  echo "[ OK ]"
    else
      echo "[ FAIL ]"
      exit 1
    fi
  ;;
	*)
		echo "Usage: $0 [start|stop]"
		;;
esac
