From ac6116c3bad1256ac1f85da7bbceb636833498d2 Mon Sep 17 00:00:00 2001 From: Aaron Ball Date: Thu, 27 Apr 2017 17:51:59 -0600 Subject: Added wait_dev support Previously, the device wait was hardcoded, regardless of device presence. Now we wait 10 seconds, but if the device is present early, the script can proceed before the threshold is reached. This should speed up boot time by a few seconds. --- init | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/init b/init index b0e2828..288f77b 100755 --- a/init +++ b/init @@ -113,6 +113,38 @@ function setup_encrypted { } +# +# Waits up to a threshold (in seconds) for the specified path to appear as a +# block device. A check for device presence occurs once per second. +# +# If the device appears within the threshold time, the function returns early +# with error code 0 (all is well - no need to wait more). +# +# If the block device fails to appear within the threshold time, the function +# returns code 2 (not found) +# +# @param dev Path to the device to wait for +# @param timeout Maximum time (in seconds) to wait for the device to appear +# +wait_dev() { + local dev=${1} + local timeout=${2} + + local wait=0 + + while [ ${wait} -lt ${timeout} ]; do + # If the path exists as a block device, exit the wait loop + [ -b "${dev}" ] && return 0 + # Increment the wait counter + wait=$(( wait + 1 )) + # Wait another second for the device to appear + sleep 1 + done + + return 2 +} + + # # Main function to keep the main operations code nicely separated from the # rest. @@ -140,11 +172,11 @@ function main { log "Root device: ${ROOTDEV}" - log "Waiting a few seconds for devices to settle." - sleep 2 + log "Waiting up to 10 seconds for root device to appear." + wait_dev "${ROOTDEV}" 10 # Drop to maintenance shell if root device does not exist. - if [[ ! -b ${ROOTDEV} ]]; then + if [[ $? -ne 0 ]]; then log "ERROR: Root device ${ROOTDEV} does not exist or can not be accessed." log " Dropping to maintenance shell for troubleshooting." log " (You may just need to wait longer for the root device)" -- cgit v1.2.3