Skip to content

Commit 69c98fc

Browse files
authored
Merge pull request #185 from gardenlinux/improve-root-hints-1877
root-hints: improve installation to disk
2 parents 899c425 + 22834d4 commit 69c98fc

8 files changed

Lines changed: 110 additions & 25 deletions

File tree

.github/actions/test/integration/build/action.yml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,21 @@ runs:
9999
chmod 600 /opt/ssh_host_ed25519_key
100100
SSH_KEY=$(cat /opt/ssh_host_ed25519_key.pub)
101101
echo "SSH_KEY=$SSH_KEY" >> $GITHUB_ENV
102-
sed -i "s|SSH_KEY_GOES_HERE|$SSH_KEY|g" ./.github/actions/test/integration/build/dev-user-butane.yaml
102+
cp ./.github/actions/test/integration/build/dev-user-butane.yaml ./.github/actions/test/integration/build/dev-user-butane-hv1.yaml
103+
yq '.storage.files += load("./.github/actions/test/integration/build/root-hints.yaml")' ./.github/actions/test/integration/build/dev-user-butane.yaml > ./.github/actions/test/integration/build/dev-user-butane-hv2.yaml
104+
105+
sed -i "s|SSH_KEY_GOES_HERE|$SSH_KEY|g" ./.github/actions/test/integration/build/dev-user-butane-hv1.yaml
106+
sed -i "s|SSH_KEY_GOES_HERE|$SSH_KEY|g" ./.github/actions/test/integration/build/dev-user-butane-hv2.yaml
107+
103108
PASSWORD=$(openssl passwd "password")
104109
echo "PASSWORD=$PASSWORD" >> $GITHUB_ENV
105-
sed -i "s|PASSWORD_GOES_HERE|$PASSWORD|g" ./.github/actions/test/integration/build/dev-user-butane.yaml
106-
sed -i "s|TAG_GOES_HERE|${{ inputs.image_tag }}|g" ./.github/actions/test/integration/build/dev-user-butane.yaml
107-
butane --pretty --strict ./.github/actions/test/integration/build/dev-user-butane.yaml > "/opt/${TAG}.ign"
110+
sed -i "s|PASSWORD_GOES_HERE|$PASSWORD|g" ./.github/actions/test/integration/build/dev-user-butane-hv1.yaml
111+
sed -i "s|PASSWORD_GOES_HERE|$PASSWORD|g" ./.github/actions/test/integration/build/dev-user-butane-hv2.yaml
112+
sed -i "s|TAG_GOES_HERE|${{ inputs.image_tag }}|g" ./.github/actions/test/integration/build/dev-user-butane-hv1.yaml
113+
sed -i "s|TAG_GOES_HERE|${{ inputs.image_tag }}|g" ./.github/actions/test/integration/build/dev-user-butane-hv2.yaml
114+
115+
butane --pretty --strict ./.github/actions/test/integration/build/dev-user-butane-hv1.yaml > "/opt/${TAG}-hv1.ign"
116+
butane --pretty --strict ./.github/actions/test/integration/build/dev-user-butane-hv2.yaml > "/opt/${TAG}-hv2.ign"
108117
109118
- name: Download ubuntu cloud image
110119
shell: bash

.github/actions/test/integration/build/dev-user-butane.yaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@ storage:
3737
# custom
3838
192.168.122.2 hv1
3939
192.168.122.3 hv2
40-
- path: /opt/persist/root-hints.yaml
41-
mode: 0644
42-
contents:
43-
inline: |
44-
hints:
45-
- size: lt 500G
4640
# turn off hugepages, not required for this test
4741
# also SIGNIFICANTLY reduces test runtime
4842
- path: /opt/persist/hugepages.env
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
- path: /opt/persist/root-hints.yaml
2+
mode: 0644
3+
contents:
4+
inline: |
5+
hints:
6+
- size: lt 500G

.github/actions/test/integration/setup/action.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ runs:
1515
- name: Copy ignition file
1616
shell: bash
1717
run: |
18-
sudo cp "/opt/${TAG}.ign" /var/lib/libvirt/images/hv.ign
18+
sudo cp "/opt/${TAG}-hv1.ign" /var/lib/libvirt/images/HV1.ign
19+
sudo cp "/opt/${TAG}-hv2.ign" /var/lib/libvirt/images/HV2.ign
1920
2021
- name: Create HyperVisor 1
2122
shell: bash

.github/actions/test/integration/setup/hv.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@
3232
</devices>
3333
<qemu:commandline>
3434
<qemu:arg value='-fw_cfg' />
35-
<qemu:arg value='name=opt/com.coreos/config,file=/var/lib/libvirt/images/hv.ign' />
35+
<qemu:arg value='name=opt/com.coreos/config,file=/var/lib/libvirt/images/HV_NAME_GOES_HERE.ign' />
3636
</qemu:commandline>
3737
</domain>

features/_usi/initrd.include/usr/bin/detect_disk

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ set -Eeuo pipefail
44

55
ROOT_HINTS_FILE="/sysroot/opt/persist/root-hints.yaml"
66

7-
if ! [[ -f $ROOT_HINTS_FILE ]]; then
8-
echo "no root-hints.yaml provided, error"
9-
exit 1
10-
fi
117
if kname=$(/usr/bin/root-hints $ROOT_HINTS_FILE); then
128
echo "/dev/${kname}"
139
else

features/_usi/initrd.include/usr/bin/persist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ if ! disk_dev=$(/usr/bin/detect_disk); then
66
exit 1
77
fi
88

9+
if [[ ! -b "$disk_dev" ]]; then
10+
echo "Device $disk_dev not found, exiting"
11+
exit 1
12+
fi
13+
914
# clean up the disk
1015
sgdisk -Z "$disk_dev"
1116
sgdisk -o "$disk_dev"

features/_usi/initrd.include/usr/bin/root-hints

Lines changed: 83 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
set -Eeuo pipefail
44

5+
# setup error trap
6+
trap 'err "Error occurred at line $LINENO while executing: $BASH_COMMAND"' ERR
7+
58
function yqroot() {
69
cat "$1" | chroot /sysroot /usr/bin/yq -c '.hints | .[]'
710
}
811

912
function err() {
1013
echo "$@" 1>&2
14+
15+
lsblk -bOJ | jq
1116
}
1217

1318
function 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+
3748
yamlFile="$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

Comments
 (0)