]> TLD Linux GIT Repositories - rc-scripts.git/blob - rc.d/init.d/random
- version 0.5.4
[rc-scripts.git] / rc.d / init.d / random
1 #!/bin/sh
2 #
3 # random        Script to snapshot random state and reload it at boot time.
4 #
5 # chkconfig:    12345 20 80
6 #
7 # description:  Saves and restores system entropy pool for higher quality \
8 #               random number generation.
9 #
10
11 # Source function library.
12 . /etc/rc.d/init.d/functions
13
14 random_seed=/var/run/random-seed
15
16 poolfile=/proc/sys/kernel/random/poolsize
17 [ -r $poolfile ] && bytes="$(cat $poolfile)" || bytes=512
18
19 start() {
20         # Check if the service is already running?
21         if [ -f /var/lock/subsys/random ]; then
22                 return
23         fi
24
25         show "Initializing random number generator"
26         busy
27         # Carry a random seed from start-up to start-up
28         if [ -f $random_seed ]; then
29                 cat $random_seed >/dev/urandom
30         else
31                 touch $random_seed
32         fi
33         chmod 600 $random_seed
34         dd if=/dev/urandom of=$random_seed count=1 bs=$bytes 2>/dev/null
35         touch /var/lock/subsys/random
36         deltext
37         ok
38 }
39
40 stop() {
41         if [ ! -f /var/lock/subsys/random ]; then
42                 return
43         fi
44
45         # Carry a random seed from shut-down to start-up
46         show "Saving random seed"
47         busy
48         touch $random_seed
49         chmod 600 $random_seed
50         dd if=/dev/urandom of=$random_seed count=1 bs=$bytes 2>/dev/null
51
52         rm -f /var/lock/subsys/random >/dev/null 2>&1
53         deltext
54         ok
55 }
56
57 status() {
58         entropy_avail="$(cat /proc/sys/kernel/random/entropy_avail)"
59         if [ "$entropy_avail" -eq 0 -o ! -c /dev/random ] ; then
60                 nls "The random data source is missing"
61                 RETVAL=1
62         else
63                 nls "%d bytes of entropy available" $entropy_avail
64         fi
65 }
66
67 # See how we were called.
68 case "$1" in
69   start)
70         start
71         ;;
72   stop)
73         stop
74         ;;
75   status)
76         status
77         ;;
78   *)
79         msg_usage "$0 {start|stop|status}"
80         exit 3
81 esac
82
83 exit 0