diff options
author | Natanael Copa <ncopa@alpinelinux.org> | 2022-11-09 16:17:41 +0100 |
---|---|---|
committer | Natanael Copa <ncopa@alpinelinux.org> | 2022-11-09 16:45:43 +0100 |
commit | 44bdb55ce2a8d835c18028fc47bac647f8b2fa96 (patch) | |
tree | b7acebf90d921615164c5c533b3076f5a2392f6d | |
parent | 5eb8b1ae7c5f5d1ec8de2c17ede97b54a66f52e8 (diff) | |
download | alpine-conf-44bdb55ce2a8d835c18028fc47bac647f8b2fa96.zip |
setup-disk: refactor and test find_disks
-rw-r--r-- | setup-disk.in | 20 | ||||
-rwxr-xr-x | tests/setup_disk_test | 50 |
2 files changed, 58 insertions, 12 deletions
diff --git a/setup-disk.in b/setup-disk.in index d56a572..a748b98 100644 --- a/setup-disk.in +++ b/setup-disk.in @@ -727,7 +727,7 @@ has_mounted_part() { local sysfsdev="$(echo ${1#/dev/} | sed 's:/:!:g')" # parse /proc/mounts for mounted devices for p in $(awk '$1 ~ /^\/dev\// {gsub("/dev/", "", $1); gsub("/", "!", $1); print $1}' \ - /proc/mounts); do + "$ROOT"/proc/mounts 2>/dev/null); do [ "$p" = "$sysfsdev" ] && return 0 [ -e "$ROOT"/sys/block/$sysfsdev/$p ] && return 0 done @@ -745,31 +745,27 @@ has_holders() { is_available_disk() { local dev="$1" - local b="$(echo $p | sed 's:/:!:g')" - - # check if its a "root" block device and not a partition - [ -e "$ROOT/sys/block/$b" ] || return 1 # check so it does not have mounted partitions has_mounted_part $dev && return 1 # check so its not part of an md setup - if has_holders "$ROOT"/sys/block/$b; then + if has_holders "$ROOT"/sys/block/$dev; then [ -n "$USE_RAID" ] && echo "Warning: $dev is part of a running raid" >&2 return 1 fi - # check so its not an md device - [ -e "$ROOT"/sys/block/$b/md ] && return 1 - return 0 } find_disks() { local p= - # filter out ramdisks (major=1) - for p in $(awk '$1 != 1 && $1 ~ /[0-9]+/ {print $4}' /proc/partitions); do - is_available_disk $p && printf %s " $p" + for p in "$ROOT"/sys/block/*/device; do + local dev="${p%/device}" + dev=${dev#*/sys/block/} + if is_available_disk "$dev"; then + printf %s " $dev" + fi done } diff --git a/tests/setup_disk_test b/tests/setup_disk_test index c3af040..65b7ada 100755 --- a/tests/setup_disk_test +++ b/tests/setup_disk_test @@ -5,6 +5,7 @@ init_tests \ setup_disk_usage \ setup_disk_mode_none \ setup_disk_none \ + setup_disk_func_find_disks setup_disk_usage_body() { test_usage setup-disk @@ -22,3 +23,52 @@ setup_disk_none_body() { setup-disk none } +fake_disk() { + mkdir -p sys/block/$1/device \ + sys/block/$1/holders +} + +fake_partition() { + mkdir -p sys/block/$1/$1$2/holders +} + +fake_mount() { + mkdir -p proc + echo "$1" >> proc/mounts +} + +fake_raid() { + local md="$1" + shift + for dev; do + for p in sys/block/*/$dev/holders sys/block/$dev/holders; do + if [ -d "$p" ]; then + touch "$p"/$md + fi + done + done +} + +setup_disk_func_find_disks_body() { + init_env + fake_disk vda + fake_disk vdb + fake_partition vdb 1 + fake_disk sda + fake_disk nvme0n1 + fake_partition nvme0n1 p2 + + # simulate vda and vdb1 being part of md0 raid + fake_raid md0 vda vdb1 + + # simulate nvme0n1p2 being mounted + fake_mount '/dev/nvme0n1p2 /boot vfat rw,relatime,fmask=0022 0 0' + + SETUP_DISK_TESTFUNC=find_disks USE_RAID=1 \ + atf_check -s exit:0 \ + -o match:"^ sda$" \ + -e match:"vda is part of a running raid" \ + -e match:"vdb is part of a running raid" \ + setup-disk +} + |