diff options
author | P. J. McDermott <pj@pehjota.net> | 2014-07-21 13:17:48 (EDT) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2014-07-21 13:17:48 (EDT) |
commit | 354b24ff32c66e2f4a6e989ccaf1a035f85c129a (patch) | |
tree | c5bab76b1408b5b9151087bf42078103b89ba5b7 /installers | |
parent | 5a90f803f5cc07f1863e0a0ad3cd871eafe0310e (diff) |
installers/pc: Prompt for block device
Diffstat (limited to 'installers')
-rwxr-xr-x | installers/pc.sh | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/installers/pc.sh b/installers/pc.sh index 52525e2..a9f5898 100755 --- a/installers/pc.sh +++ b/installers/pc.sh @@ -45,6 +45,10 @@ main() host_arch="${host_arch}-${install_arch#*-}" check_arch_compat "${install_arch}" "${host_arch}" + printf '\nSelect a block device' + printf '\n=====================\n\n' + dev="$(prompt_block_dev)" + printf '\nInstalling base system' printf '\n======================\n\n' @@ -177,4 +181,77 @@ check_arch_compat() fi } +pretty_print_size_dec() +{ + local size="${1}" + + awk -v size=${size} 'BEGIN { + split("B KB MB GB TB", units, " "); + i=1; + while (size >= 1000) { + size /= 1000; + ++i; + }; + printf("%.2f %s\n", size, units[i]); }' +} + +pretty_print_size_bin() +{ + local size="${1}" + + awk -v size=${size} 'BEGIN { + split("B KiB MiB GiB TiB", units, " "); + i=1; + while (size >= 1024) { + size /= 1024; + ++i; + }; + printf("%.2f %s\n", size, units[i]); }' +} + +prompt_block_dev() +{ + local dev= + local vendor= + local model= + local name= + local size= + local size_s= + local desc= + local devs= + + set -- + for dev in /sys/block/*; do + dev="${dev#/sys/block/}" + case "${dev}" in + loop*|sr*) continue;; + esac + if [ -e "/sys/block/${dev}/device/vendor" ]; then + vendor="$(cat "/sys/block/${dev}/device/vendor")" + model="$(cat "/sys/block/${dev}/device/model")" + name="${vendor} ${model}" + else + name="$(cat "/sys/block/${dev}/device/name")" + fi + size="$(cat "/sys/block/${dev}/size")" + size="$(expr ${size} \* 512)" + size_s="$(pretty_print_size_dec "${size}")" + size_s="${size_s} ($(pretty_print_size_bin "${size}"))" + desc="$(printf '%s (%s) - %s\n' "${name}" "${dev}" "${size_s}")" + set -- "${@}" "${desc}" + devs="$(printf '%s\n%s\t%s\n' "${devs}" "${desc}" "${dev}")" + done + + printf 'Select a block device:\n' >&2 + _select "${@}" + + while IFS=' ' read -r desc dev; do + if [ "x${desc}" = "x${select_result}" ]; then + printf '%s\n' "${dev}" + fi + done <<-EOF + ${devs} + EOF +} + main "${@}" |