xref: /openbmc/openbmc/poky/meta/recipes-core/initscripts/initscripts-1.0/urandom (revision c124f4f2e04dca16a428a76c89677328bc7bf908)
1#!/bin/sh
2#
3# SPDX-License-Identifier: GPL-2.0-only
4#
5
6### BEGIN INIT INFO
7# Provides:          urandom
8# Required-Start:    $local_fs mountvirtfs
9# Required-Stop:     $local_fs
10# Default-Start:     S
11# Default-Stop:      0 6
12# Short-Description: Save and restore the random seed
13# Description:       Save the random seed on shutdown and restore it on boot,
14#                    to ensure that the seed isn't predicable on startup
15#                    (because the boot process is predictable)
16### END INIT INFO
17
18test -c /dev/urandom || exit 0
19
20RANDOM_SEED_FILE=/var/lib/urandom/random-seed
21
22. /etc/default/rcS
23[ -f /etc/default/urandom ] && . /etc/default/urandom
24
25case "$1" in
26	start|"")
27		test "$VERBOSE" != no && echo "Initializing random number generator..."
28		# Load and then save 512 bytes, which is the size of the entropy
29		# pool. Also load the current date, in case the seed file is
30		# empty.
31		( date +%s.%N; [ -f "$RANDOM_SEED_FILE" ] && cat "$RANDOM_SEED_FILE" ) \
32			>/dev/urandom
33		rm -f "$RANDOM_SEED_FILE"
34		umask 077
35		dd if=/dev/urandom of=$RANDOM_SEED_FILE count=1 \
36			>/dev/null 2>&1 || echo "urandom start: failed."
37		umask 022
38		;;
39	stop)
40		# Carry a random seed from shut-down to start-up;
41		# see documentation in linux/drivers/char/random.c
42		test "$VERBOSE" != no && echo "Saving random seed..."
43		umask 077
44		dd if=/dev/urandom of=$RANDOM_SEED_FILE count=1 \
45			>/dev/null 2>&1 || echo "urandom stop: failed."
46		;;
47	*)
48		echo "Usage: urandom {start|stop}" >&2
49		exit 1
50		;;
51esac
52
53exit 0
54