Skip to contentSkip to conversation

package/initscripts: refactor S20urandom

PATCH New unixmania at gmail.com 3 participants 4 messages Oct 23, 2019 – Oct 24, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 package/initscripts: refactor S20urandom

From: Carlos Santos <[email protected]>

Adapt the format to the current template, used in other init scripts.

Read /proc/sys/kernel/random/poolsize to calculate the pool size, as suggestred by the urandom manual page.

Save the seed at /var/lib/ as other non-systemd distributions do (e.g. RHEL6), since /etc can be in a red-only filesystem and the Filesystem Hierarchy Standard defines that /var/lib holds persistent data modified by programs as they run.

Users willing to use a different path just need to redefine URANDOM_SEED in /etc/default/urandom instead of rewriting the init script.

---
 package/initscripts/init.d/S20urandom | 105 ++++++++++++++++----------
 1 file changed, 64 insertions(+), 41 deletions(-)
package/initscripts/init.d/S20urandom +64 -41
index cababe1023..94e46cff10 100644
@@ -1,51 +1,74 @@
 #! /bin/sh
 #
-# urandom	This script saves the random seed between reboots.
-#		It is called from the boot, halt and reboot scripts.
-#
-# Version:	@(#)urandom  1.33  22-Jun-1998  miquels at cistron.nl
+# Save the random seed between reboots. See urandom(4).
 #
 
+# Quietly do nothing if /dev/urandom does not exist
 [ -c /dev/urandom ] || exit 0
-#. /etc/default/rcS
 
-case "$1" in
-	start|"")
-		# check for read only file system
-		if ! touch /etc/random-seed 2>/dev/null
-		then
-			echo "read-only file system detected...done"
-			exit
-		fi
-		if [ "$VERBOSE" != no ]
-		then
-			printf "Initializing random number generator... "
-		fi
-		# Load and then save 512 bytes,
-		# which is the size of the entropy pool
-		cat /etc/random-seed >/dev/urandom
-		rm -f /etc/random-seed
-		umask 077
-		dd if=/dev/urandom of=/etc/random-seed count=1 \
-			>/dev/null 2>&1 || echo "urandom start: failed."
-		umask 022
-		[ "$VERBOSE" != no ] && echo "done."
-		;;
-	stop)
-		if ! touch /etc/random-seed 2>/dev/null
-		then
-			exit
+URANDOM_SEED="/var/lib/random-seed"
+
+# shellcheck source=/dev/null
+[ -r "/etc/default/urandom" ] && . "/etc/default/urandom"
+
+pool_size_file="/proc/sys/kernel/random/poolsize"
+
+start() {
+	# Carry a random seed from start-up to start-up
+	# Load and then save the whole entropy pool
+	printf 'Initializing random number generator: '
+	if [ -f "$URANDOM_SEED" ]; then
+		dd if="$URANDOM_SEED" of=/dev/urandom status=none
+		status=$?
+		if [ "$status" -ne 0 ]; then
+			echo "FAIL (can't dump $URANDOM_SEED to /dev/urandom)"
+			return "$status"
 		fi
-		# Carry a random seed from shut-down to start-up;
-		# see documentation in linux/drivers/char/random.c
-		[ "$VERBOSE" != no ] && printf "Saving random seed... "
-		umask 077
-		dd if=/dev/urandom of=/etc/random-seed count=1 \
-			>/dev/null 2>&1 || echo "urandom stop: failed."
-		[ "$VERBOSE" != no ] && echo "done."
-		;;
+	fi
+	if bits=$(cat "$pool_size_file" 2> /dev/null); then
+		bytes=$((bits/8))
+	else
+		bytes=512
+	fi
+	umask 077
+	dd if=/dev/urandom of="$URANDOM_SEED" bs="$bytes" count=1 status=none
+	status=$?
+	umask 022
+	if [ "$status" -eq 0 ]; then
+		echo "OK"
+	else
+		echo "FAIL"
+	fi
+	return "$status"
+}
+
+stop() {
+	# Carry a random seed from shut-down to start-up
+	# Save the whole entropy pool
+	printf "Saving random seed: "
+	if bits=$(cat "$pool_size_file" 2> /dev/null); then
+		bytes=$((bits/8))
+	else
+		bytes=512
+	fi
+	umask 077
+	dd if=/dev/urandom of="$URANDOM_SEED" bs="$bytes" count=1 status=none
+	status=$?
+	umask 022
+	if [ "$status" -eq 0 ]; then
+		echo "OK"
+	else
+		echo "FAIL"
+	fi
+	return "$status"
+}
+
+case "$1" in
+	start|stop)
+		"$1";;
+	restart|reload)
+		:;;
 	*)
-		echo "Usage: urandom {start|stop}" >&2
+		echo "Usage: $0 {start|stop|restart|reload}"
 		exit 1
-		;;
 esac
Signature · 1 line
2.18.1
#1 lore raw

PATCH 1/1 package/initscripts: refactor S20urandom

Carlos,

Quoted 89 lines · On Wed, Oct 23, 2019 at 11:47 AM <[email protected]> wrote:

From: Carlos Santos <[email protected]>

Adapt the format to the current template, used in other init scripts.

Read /proc/sys/kernel/random/poolsize to calculate the pool size, as suggestred by the urandom manual page.

Save the seed at /var/lib/ as other non-systemd distributions do (e.g. RHEL6), since /etc can be in a red-only filesystem and the Filesystem Hierarchy Standard defines that /var/lib holds persistent data modified by programs as they run.

Users willing to use a different path just need to redefine URANDOM_SEED in /etc/default/urandom instead of rewriting the init script.

---
 package/initscripts/init.d/S20urandom | 105 ++++++++++++++++----------
 1 file changed, 64 insertions(+), 41 deletions(-)
package/initscripts/init.d/S20urandom +19 -37
index cababe1023..94e46cff10 100644
@@ -1,51 +1,74 @@
 #! /bin/sh
 #
-# urandom      This script saves the random seed between reboots.
-#              It is called from the boot, halt and reboot scripts.
-#
-# Version:     @(#)urandom  1.33  22-Jun-1998  miquels at cistron.nl
+# Save the random seed between reboots. See urandom(4).
 #
 
+# Quietly do nothing if /dev/urandom does not exist
 [ -c /dev/urandom ] || exit 0
-#. /etc/default/rcS
 
-case "$1" in
-       start|"")
-               # check for read only file system
-               if ! touch /etc/random-seed 2>/dev/null
-               then
-                       echo "read-only file system detected...done"
-                       exit
-               fi
-               if [ "$VERBOSE" != no ]
-               then
-                       printf "Initializing random number generator... "
-               fi
-               # Load and then save 512 bytes,
-               # which is the size of the entropy pool
-               cat /etc/random-seed >/dev/urandom
-               rm -f /etc/random-seed
-               umask 077
-               dd if=/dev/urandom of=/etc/random-seed count=1 \
-                       >/dev/null 2>&1 || echo "urandom start: failed."
-               umask 022
-               [ "$VERBOSE" != no ] && echo "done."
-               ;;
-       stop)
-               if ! touch /etc/random-seed 2>/dev/null
-               then
-                       exit
+URANDOM_SEED="/var/lib/random-seed"
+
+# shellcheck source=/dev/null
+[ -r "/etc/default/urandom" ] && . "/etc/default/urandom"
+
+pool_size_file="/proc/sys/kernel/random/poolsize"
+
+start() {
+       # Carry a random seed from start-up to start-up
+       # Load and then save the whole entropy pool
+       printf 'Initializing random number generator: '
+       if [ -f "$URANDOM_SEED" ]; then
+               dd if="$URANDOM_SEED" of=/dev/urandom status=none
+               status=$?
+               if [ "$status" -ne 0 ]; then
+                       echo "FAIL (can't dump $URANDOM_SEED to /dev/urandom)"
+                       return "$status"
                fi
-               # Carry a random seed from shut-down to start-up;
-               # see documentation in linux/drivers/char/random.c
-               [ "$VERBOSE" != no ] && printf "Saving random seed... "
-               umask 077
-               dd if=/dev/urandom of=/etc/random-seed count=1 \
-                       >/dev/null 2>&1 || echo "urandom stop: failed."

package/initscripts/init.d/S20urandom line 34

Quoted 10 lines
package/initscripts/init.d/S20urandom +8 -2
-               [ "$VERBOSE" != no ] && echo "done."
-               ;;
+       fi
+       if bits=$(cat "$pool_size_file" 2> /dev/null); then
+               bytes=$((bits/8))
+       else
+               bytes=512
+       fi
+       umask 077
+       dd if=/dev/urandom of="$URANDOM_SEED" bs="$bytes" count=1 status=none

Original script tested with a touch to see if the seed location was writable

Quoted 11 lines
package/initscripts/init.d/S20urandom +11 -0
+       status=$?
+       umask 022
+       if [ "$status" -eq 0 ]; then
+               echo "OK"
+       else
+               echo "FAIL"
+       fi
+       return "$status"
+}
+
+stop() {

package/initscripts/init.d/S20urandom line 55

Quoted 10 lines
package/initscripts/init.d/S20urandom +10 -0
+       # Carry a random seed from shut-down to start-up
+       # Save the whole entropy pool
+       printf "Saving random seed: "
+       if bits=$(cat "$pool_size_file" 2> /dev/null); then
+               bytes=$((bits/8))
+       else
+               bytes=512
+       fi
+       umask 077
+       dd if=/dev/urandom of="$URANDOM_SEED" bs="$bytes" count=1 status=none

Do we need to conditionally write this file for the case of a read-only rootfs? Looks like the previous script tested with a touch.

I've tested this in a basic busybox target (start/stop/restart and with/without readonly rootfs)

Regards,
Matt

#2 lore raw

PATCH 1/1 package/initscripts: refactor S20urandom

Quoted 97 lines · Matthew Weber

Carlos,

Quoted 93 lines · On Wed, Oct 23, 2019 at 11:47 AM <[email protected]> wrote:

From: Carlos Santos <[email protected]>

Adapt the format to the current template, used in other init scripts.

Read /proc/sys/kernel/random/poolsize to calculate the pool size, as suggestred by the urandom manual page.

Save the seed at /var/lib/ as other non-systemd distributions do (e.g. RHEL6), since /etc can be in a red-only filesystem and the Filesystem Hierarchy Standard defines that /var/lib holds persistent data modified by programs as they run.

Users willing to use a different path just need to redefine URANDOM_SEED in /etc/default/urandom instead of rewriting the init script.

---
 package/initscripts/init.d/S20urandom | 105 ++++++++++++++++----------
 1 file changed, 64 insertions(+), 41 deletions(-)
package/initscripts/init.d/S20urandom +21 -39
index cababe1023..94e46cff10 100644
@@ -1,51 +1,74 @@
 #! /bin/sh
 #
-# urandom      This script saves the random seed between reboots.
-#              It is called from the boot, halt and reboot scripts.
-#
-# Version:     @(#)urandom  1.33  22-Jun-1998  miquels at cistron.nl
+# Save the random seed between reboots. See urandom(4).
 #
 
+# Quietly do nothing if /dev/urandom does not exist
 [ -c /dev/urandom ] || exit 0
-#. /etc/default/rcS
 
-case "$1" in
-       start|"")
-               # check for read only file system
-               if ! touch /etc/random-seed 2>/dev/null
-               then
-                       echo "read-only file system detected...done"
-                       exit
-               fi
-               if [ "$VERBOSE" != no ]
-               then
-                       printf "Initializing random number generator... "
-               fi
-               # Load and then save 512 bytes,
-               # which is the size of the entropy pool
-               cat /etc/random-seed >/dev/urandom
-               rm -f /etc/random-seed
-               umask 077
-               dd if=/dev/urandom of=/etc/random-seed count=1 \
-                       >/dev/null 2>&1 || echo "urandom start: failed."
-               umask 022
-               [ "$VERBOSE" != no ] && echo "done."
-               ;;
-       stop)
-               if ! touch /etc/random-seed 2>/dev/null
-               then
-                       exit
+URANDOM_SEED="/var/lib/random-seed"
+
+# shellcheck source=/dev/null
+[ -r "/etc/default/urandom" ] && . "/etc/default/urandom"
+
+pool_size_file="/proc/sys/kernel/random/poolsize"
+
+start() {
+       # Carry a random seed from start-up to start-up
+       # Load and then save the whole entropy pool
+       printf 'Initializing random number generator: '
+       if [ -f "$URANDOM_SEED" ]; then
+               dd if="$URANDOM_SEED" of=/dev/urandom status=none
+               status=$?
+               if [ "$status" -ne 0 ]; then
+                       echo "FAIL (can't dump $URANDOM_SEED to /dev/urandom)"
+                       return "$status"
                fi
-               # Carry a random seed from shut-down to start-up;
-               # see documentation in linux/drivers/char/random.c
-               [ "$VERBOSE" != no ] && printf "Saving random seed... "
-               umask 077
-               dd if=/dev/urandom of=/etc/random-seed count=1 \
-                       >/dev/null 2>&1 || echo "urandom stop: failed."
-               [ "$VERBOSE" != no ] && echo "done."
-               ;;
+       fi
+       if bits=$(cat "$pool_size_file" 2> /dev/null); then
Quoted 8 lines

package/initscripts/init.d/S20urandom line 34

Quoted 6 lines
package/initscripts/init.d/S20urandom +6 -0
+               bytes=$((bits/8))
+       else
+               bytes=512
+       fi
+       umask 077
+       dd if=/dev/urandom of="$URANDOM_SEED" bs="$bytes" count=1 status=none

Original script tested with a touch to see if the seed location was writable

I think it's better to report an erroneous condition instead of fail graciously. Attempting to guess the user intention is dangerous.

Quoted 16 lines
Quoted 16 lines
package/initscripts/init.d/S20urandom +16 -0
+       status=$?
+       umask 022
+       if [ "$status" -eq 0 ]; then
+               echo "OK"
+       else
+               echo "FAIL"
+       fi
+       return "$status"
+}
+
+stop() {
+       # Carry a random seed from shut-down to start-up
+       # Save the whole entropy pool
+       printf "Saving random seed: "
+       if bits=$(cat "$pool_size_file" 2> /dev/null); then
+               bytes=$((bits/8))
Quoted 8 lines

package/initscripts/init.d/S20urandom line 34

Quoted 5 lines
package/initscripts/init.d/S20urandom +5 -0
+       else
+               bytes=512
+       fi
+       umask 077
+       dd if=/dev/urandom of="$URANDOM_SEED" bs="$bytes" count=1 status=none

Do we need to conditionally write this file for the case of a read-only rootfs? Looks like the previous script tested with a touch.

It was the same attempt to fail gracefully. Anyone willing to use a read-only rootfs must either put an "exit" in /etc/default/urandom or remove $TARGET_DIR/ets/init.d/S20urandom in a post-build script.

Quoted 5 lines

I've tested this in a basic busybox target (start/stop/restart and with/without readonly rootfs)

Regards,
Matt

Signature · 1 line
Carlos Santos <[email protected]>
#3 lore raw

Carlos,

Quoted 108 lines · Carlos Santos
Quoted 97 lines · Matthew Weber

Carlos,

Quoted 93 lines · On Wed, Oct 23, 2019 at 11:47 AM <[email protected]> wrote:

From: Carlos Santos <[email protected]>

Adapt the format to the current template, used in other init scripts.

Read /proc/sys/kernel/random/poolsize to calculate the pool size, as suggestred by the urandom manual page.

Save the seed at /var/lib/ as other non-systemd distributions do (e.g. RHEL6), since /etc can be in a red-only filesystem and the Filesystem Hierarchy Standard defines that /var/lib holds persistent data modified by programs as they run.

Users willing to use a different path just need to redefine URANDOM_SEED in /etc/default/urandom instead of rewriting the init script.

---
 package/initscripts/init.d/S20urandom | 105 ++++++++++++++++----------
 1 file changed, 64 insertions(+), 41 deletions(-)
package/initscripts/init.d/S20urandom +21 -39
index cababe1023..94e46cff10 100644
@@ -1,51 +1,74 @@
 #! /bin/sh
 #
-# urandom      This script saves the random seed between reboots.
-#              It is called from the boot, halt and reboot scripts.
-#
-# Version:     @(#)urandom  1.33  22-Jun-1998  miquels at cistron.nl
+# Save the random seed between reboots. See urandom(4).
 #
 
+# Quietly do nothing if /dev/urandom does not exist
 [ -c /dev/urandom ] || exit 0
-#. /etc/default/rcS
 
-case "$1" in
-       start|"")
-               # check for read only file system
-               if ! touch /etc/random-seed 2>/dev/null
-               then
-                       echo "read-only file system detected...done"
-                       exit
-               fi
-               if [ "$VERBOSE" != no ]
-               then
-                       printf "Initializing random number generator... "
-               fi
-               # Load and then save 512 bytes,
-               # which is the size of the entropy pool
-               cat /etc/random-seed >/dev/urandom
-               rm -f /etc/random-seed
-               umask 077
-               dd if=/dev/urandom of=/etc/random-seed count=1 \
-                       >/dev/null 2>&1 || echo "urandom start: failed."
-               umask 022
-               [ "$VERBOSE" != no ] && echo "done."
-               ;;
-       stop)
-               if ! touch /etc/random-seed 2>/dev/null
-               then
-                       exit
+URANDOM_SEED="/var/lib/random-seed"
+
+# shellcheck source=/dev/null
+[ -r "/etc/default/urandom" ] && . "/etc/default/urandom"
+
+pool_size_file="/proc/sys/kernel/random/poolsize"
+
+start() {
+       # Carry a random seed from start-up to start-up
+       # Load and then save the whole entropy pool
+       printf 'Initializing random number generator: '
+       if [ -f "$URANDOM_SEED" ]; then
+               dd if="$URANDOM_SEED" of=/dev/urandom status=none
+               status=$?
+               if [ "$status" -ne 0 ]; then
+                       echo "FAIL (can't dump $URANDOM_SEED to /dev/urandom)"
+                       return "$status"
                fi
-               # Carry a random seed from shut-down to start-up;
-               # see documentation in linux/drivers/char/random.c
-               [ "$VERBOSE" != no ] && printf "Saving random seed... "
-               umask 077
-               dd if=/dev/urandom of=/etc/random-seed count=1 \
-                       >/dev/null 2>&1 || echo "urandom stop: failed."
-               [ "$VERBOSE" != no ] && echo "done."
-               ;;
+       fi
+       if bits=$(cat "$pool_size_file" 2> /dev/null); then
Quoted 8 lines

package/initscripts/init.d/S20urandom line 34

Quoted 6 lines
package/initscripts/init.d/S20urandom +6 -0
+               bytes=$((bits/8))
+       else
+               bytes=512
+       fi
+       umask 077
+       dd if=/dev/urandom of="$URANDOM_SEED" bs="$bytes" count=1 status=none

Original script tested with a touch to see if the seed location was writable

Quoted 2 lines

I think it's better to report an erroneous condition instead of fail graciously. Attempting to guess the user intention is dangerous.

The original script didn't treat the rootfs being read-only as an error so the behavior changed with this update. With this update, I see having to either remove this script or carry a patch against it out of tree to point URANDOM_SEED at /tmp by default instead of /var/lib as it is fairly consistent that /tmp is tmpfs.

Quoted 38 lines
Quoted 17 lines
Quoted 17 lines
package/initscripts/init.d/S20urandom +16 -0
+       status=$?
+       umask 022
+       if [ "$status" -eq 0 ]; then
+               echo "OK"
+       else
+               echo "FAIL"
+       fi
+       return "$status"
+}
+
+stop() {
+       # Carry a random seed from shut-down to start-up
+       # Save the whole entropy pool
+       printf "Saving random seed: "
+       if bits=$(cat "$pool_size_file" 2> /dev/null); then
+               bytes=$((bits/8))
Quoted 8 lines

package/initscripts/init.d/S20urandom line 34

Quoted 5 lines
package/initscripts/init.d/S20urandom +5 -0
+       else
+               bytes=512
+       fi
+       umask 077
+       dd if=/dev/urandom of="$URANDOM_SEED" bs="$bytes" count=1 status=none

Do we need to conditionally write this file for the case of a read-only rootfs? Looks like the previous script tested with a touch.

It was the same attempt to fail gracefully. Anyone willing to use a read-only rootfs must either put an "exit" in /etc/default/urandom or remove $TARGET_DIR/ets/init.d/S20urandom in a post-build script.

Quoted 5 lines

I've tested this in a basic busybox target (start/stop/restart and with/without readonly rootfs)

Regards,
Matt

--
Carlos Santos <[email protected]>