diff options
author | Cem Keylan <cem@ckyln.com> | 2020-03-02 13:36:29 +0300 |
---|---|---|
committer | Cem Keylan <cem@ckyln.com> | 2020-03-02 13:36:29 +0300 |
commit | 633608ead6cd30f416050c121ae06f3500367603 (patch) | |
tree | fe5ac63bf1fb4c8142f547ec159bd92f9067c8b0 | |
parent | 33a56bf63d38681ed9e00a941f4767c028a8fc63 (diff) | |
download | init-633608ead6cd30f416050c121ae06f3500367603.tar.gz |
add parse_crypttab to rc.lib
-rwxr-xr-x | rc.boot | 47 | ||||
-rw-r--r-- | rc.lib | 43 |
2 files changed, 46 insertions, 44 deletions
@@ -48,50 +48,9 @@ out "Remounting rootfs as ro..."; { mount -o remount,ro / || emergency_shell } -out "Activating encrypted devices (if any exist)..."; { - [ -e /etc/crypttab ] && [ -x /bin/cryptsetup ] && { - exec 3<&0 - - while read -r name dev pass opts err; do - [ "${name##\#*}" ] || continue - - # Break on invalid crypttab. - [ "$err" ] && { - printf 'error: A valid crypttab has only 4 columns.\n' - break - } - - # Turn 'UUID=*' lines into device names. - [ "${dev##UUID*}" ] || dev=$(blkid -l -o device -t "$dev") - - # Parse options by turning list into a pseudo array. - IFS=, - set -- $opts - IFS=$old_ifs - - copts="cryptsetup luksOpen" - - # Create an argument list (no other way to do this in sh). - for opt; do case $opt in - discard) copts="$copts --allow-discards" ;; - readonly|read-only) copts="$copts -r" ;; - tries=*) copts="$copts -T ${opt##*=}" ;; - esac; done - - # If password is 'none', '-' or empty ask for it. - case $pass in - none|-|"") $copts "$dev" "$name" <&3 ;; - *) $copts -d "$pass" "$dev" "$name" ;; - esac - done < /etc/crypttab - - exec 3>&- - - [ "$copts" ] && [ -x /bin/vgchance ] && { - out "Activating LVM devices for dm-crypt..." - vgchange --sysinit -a y || emergency_shell - } - } +[ -e /etc/crypttab ] && [ -x /bin/cryptsetup ] && { + out "Activating encrypted devices..." + parse_crypttab } out "Checking filesystems..."; { @@ -25,3 +25,46 @@ emergency_shell() { /bin/sh -l } + +parse_crypttab() { + # This is a pure shell crypttab parser which supports + # a subset of the format. + + # Function by Dylan Araps + exec 3<&0 + + # shellcheck disable=2086 + while read -r name dev pass opts err; do + # Skip comments. + [ "${name##\#*}" ] || continue + + # Break on invalid crypttab (> 5 columns). + [ "$err" ] && break + + # Turn 'UUID=*' lines into device names. + [ "${dev##UUID*}" ] || dev=$(blkid -l -o device -t "$dev") + + # Parse options by turning the list into a pseudo array. + { old_ifs=$IFS; IFS=,; set -f; set +f -- $opts; IFS=$old_ifs; } + + # Create an argument list (no other way to do this in sh). + for opt; do case $opt in + discard) copts="$copts --allow-discards" ;; + readonly|read-only) copts="$copts -r" ;; + tries=*) copts="$copts -T ${opt##*=}" ;; + esac; done + + # If password is 'none', '-' or empty ask for it. + case $pass in + none|-|"") cryptsetup luksOpen $copts "$dev" "$name" <&3 ;; + *) cryptsetup luksOpen $copts -d "$pass" "$dev" "$name" ;; + esac + done < /etc/crypttab + + exec 3>&- + + [ "$copts" ] && [ -x /bin/vgchange ] && { + out "Activating LVM devices for dm-crypt..." + vgchange --sysinit -a y || emergency_shell + } +} |