@@ -27,25 +27,46 @@ function filter() {
2727 fi
2828 if [[ $val =~ ^[0-9]+[KMGT]+$ ]]; then
2929 val=$( echo " $val " | numfmt --from=iec)
30- echo " and ${col^^} $op ${val} "
30+ echo " ${col^^} $op ${val} "
3131 else
32- echo " and ${col^^} $op \" ${val} \" "
32+ echo " ${col^^} $op \" ${val} \" "
3333 fi
34-
3534}
3635
3736yamlFile=" $1 "
3837
38+ # TODO: likely go rather with device uuid or alike
3939# if dev=$(yq -e .dev "$yamlFile" 2> /dev/null); then
4040# echo "$dev"
4141# exit 0
4242# fi
4343
44- flsblk=" TYPE eq \" disk\" "
45- while IFS= read -r r; do
46- f=$( filter " $r " )
47- flsblk=" $flsblk $f "
48- done < <( yqroot " $yamlFile " )
44+ declare -a flsblk=()
45+ # check if yaml file exists and is valid, if so, use it to build the filter for lsblk
46+ if [ -f " $yamlFile " ] && yq -e . " $yamlFile " > /dev/null 2>&1 ; then
47+ while IFS= read -r r; do
48+ flsblk+=(" $( filter " $r " ) " )
49+ done < <( yqroot " $yamlFile " )
50+ fi
51+
52+ LSBLK_JSON=" "
53+
54+ function join {
55+ local SEPARATOR=" $1 "
56+ shift
57+ printf " %s${SEPARATOR} " " $@ " | sed --unbuffered " s/${SEPARATOR} $//"
58+ }
59+
60+ # if flsblk contains elements, we join them by 'and' and use the filter with lsblk
61+ if [ ${# flsblk[@]} -gt 0 ]; then
62+ flsfilter=$( join " and " " ${flsblk[@]} " )
63+ echo " Using filter: $flsfilter " 1>&2
64+ LSBLK_JSON=$( lsblk --sort size --exclude 1,2,3,4,7,11 --filter " $flsfilter " -OJ)
65+ else
66+ # no hints, hence we choose the smallest disk we find. we exclude ram disk,
67+ # floppy, ide, dynamically allocated, loopback and scsi cd-rom devices
68+ LSBLK_JSON=$( lsblk --sort size --exclude 1,2,3,4,7,11 -OJ)
69+ fi
4970
50- # echo "$flsblk"
51- lsblk --filter " $flsblk " --noheadings -o kname
71+ # from the resulting list of disks we pick the first disk that are writeable
72+ echo " $LSBLK_JSON " | jq -r ' [.blockdevices.[]|select(.type? == "disk" and .ro? == false)][0]. kname // "" '
0 commit comments