22
33set -Eeuo pipefail
44
5+ # setup error trap
6+ trap ' err "Error occurred at line $LINENO while executing: $BASH_COMMAND"' ERR
7+
58function yqroot() {
69 cat " $1 " | chroot /sysroot /usr/bin/yq -c ' .hints | .[]'
710}
811
912function err() {
1013 echo " $@ " 1>&2
14+
15+ lsblk -bOJ | jq
1116}
1217
1318function filter() {
@@ -27,25 +32,94 @@ function filter() {
2732 fi
2833 if [[ $val =~ ^[0-9]+[KMGT]+$ ]]; then
2934 val=$( echo " $val " | numfmt --from=iec)
30- echo " and ${col^^} $op ${val} "
35+ echo " ${col^^} $op ${val} "
3136 else
32- echo " and ${col^^} $op \" ${val} \" "
37+ echo " ${col^^} $op \" ${val} \" "
3338 fi
39+ }
3440
41+ function blockdevicesize() {
42+ local kname=" $1 "
43+ lsblk -bOJ | jq --arg kname " $kname " -r ' .blockdevices.[] | select(.kname == $kname).size'
3544}
3645
46+ udevadm settle
47+
3748yamlFile=" $1 "
3849
50+ esp_disk=" "
51+ if [ -e " /dev/disk/by-label/ESP" ]; then
52+ esp_partition=$( basename " $( readlink -f /dev/disk/by-label/ESP) " )
53+ esp_disk=$( basename " $( readlink -f " /sys/class/block/$esp_partition /.." ) " )
54+ fi
55+
56+ # TODO: likely go rather with device uuid or alike
3957# if dev=$(yq -e .dev "$yamlFile" 2> /dev/null); then
4058# echo "$dev"
4159# exit 0
4260# fi
4361
44- flsblk=" TYPE eq \" disk\" "
45- while IFS= read -r r; do
46- f=$( filter " $r " )
47- flsblk=" $flsblk $f "
48- done < <( yqroot " $yamlFile " )
62+ declare -a flsblk=()
63+ # check if yaml file exists and is valid, if so, use it to build the filter for lsblk
64+ if [ -f " $yamlFile " ] && yq -e . " $yamlFile " > /dev/null 2>&1 ; then
65+ while IFS= read -r r; do
66+ flsblk+=(" $( filter " $r " ) " )
67+ done < <( yqroot " $yamlFile " )
68+ fi
69+
70+ LSBLK_JSON=" "
71+
72+ function join {
73+ local SEPARATOR=" $1 "
74+ shift
75+ printf " %s${SEPARATOR} " " $@ " | sed --unbuffered " s/${SEPARATOR} $//"
76+ }
77+
78+ # if flsblk contains elements, we join them by 'and' and use the filter with lsblk
79+ if [ ${# flsblk[@]} -gt 0 ]; then
80+ flsfilter=$( join " and " " ${flsblk[@]} " )
81+ echo " Using filter: $flsfilter " 1>&2
82+ LSBLK_JSON=$( lsblk --sort size --exclude 1,2,3,4,7,11 --filter " $flsfilter " -OJ)
83+ else
84+ # no hints, hence we choose the smallest disk we find. we exclude ram disk,
85+ # floppy, ide, dynamically allocated, loopback and scsi cd-rom devices
86+ LSBLK_JSON=$( lsblk --sort size --exclude 1,2,3,4,7,11 -OJ)
87+ fi
4988
50- # echo "$flsblk"
51- lsblk --filter " $flsblk " --noheadings -o kname
89+ # from the resulting list of disks we pick the first disk that are writeable
90+ kname=$( echo " $LSBLK_JSON " | jq -r ' [.blockdevices.[]|select(.type? == "disk" and .ro? == false)][0].kname // ""' )
91+
92+ # check which disk to use.
93+ # 1. if kname is empty:
94+ # * we check if we have an esp_disk, if so we use it
95+ # * otherwise we fail
96+ # 2. if kname is not empty:
97+ # * we check if we have an esp_disk, if not we use
98+ # * kname, otherwise we check if they are the same, if not we fail, otherwise we use kname/esp_disk
99+ if [ -z " $kname " ]; then
100+ if [ -z " $esp_disk " ]; then
101+ err " No suitable disk found"
102+ exit 1
103+ fi
104+ echo " Warning: No suitable disk found, falling back to ESP disk ${esp_disk} " 1>&2
105+ echo " $esp_disk "
106+ else
107+ if [ -z " $esp_disk " ]; then
108+ # no esp_disk using kname
109+ echo " $kname "
110+ exit 0
111+ fi
112+
113+ if [ " $kname " != " $esp_disk " ]; then
114+ # check if desvice size are the same
115+ err " Error: Found disk ${kname} does not match ESP disk ${esp_disk} ." 1>&2
116+ kname_size=$( blockdevicesize " $kname " )
117+ esp_disk_size=$( blockdevicesize " $esp_disk " )
118+ if [ " $kname_size " != " $esp_disk_size " ]; then
119+ err " Error: Found disk ${kname} size ${kname_size} does not match ESP disk ${esp_disk} size ${esp_disk_size} ." 1>&2
120+ exit 1
121+ fi
122+ fi
123+ # kname == esp_disk or esp_disk has similar size to kname
124+ echo " $esp_disk "
125+ fi
0 commit comments