@@ -31,21 +31,35 @@ function filter() {
3131 else
3232 echo " and ${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+ 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+ f=$( filter " $r " )
49+ flsblk=" $flsblk $f "
50+ done < <( yqroot " $yamlFile " )
51+ fi
52+
53+ LSBLK_JSON=" "
54+ # if filter was added prepend it with "TYPE eq \"disk\"
55+ if [ -n " $flsblk " ]; then
56+ flsblk=" TYPE eq \" disk\" $flsblk "
57+ LSBLK_JSON=$( lsblk --filter " $flsblk " -OJ)
58+ else
59+ # no hints, hence we choose the smallest disk we find. we exclude ram disk,
60+ # floppy, ide, dynamically allocated, loopback and scsi cd-rom devices
61+ LSBLK_JSON=$( lsblk --sort size --exclude 1,2,3,4,7,11 -OJ)
62+ fi
4963
50- # echo "$flsblk"
51- lsblk --filter " $flsblk " --noheadings -o kname
64+ # from the resulting list of disks we pick the first disk that are writeable
65+ echo " $LSBLK_JSON " | jq -r ' [.blockdevices.[]|select(.type? == "disk" and .ro? == false)][0]. kname // "" '
0 commit comments