Skip to content

Commit 5df7b0e

Browse files
authored
Merge pull request #16 from frostyard/scriptrobust
fix: make install more robust
2 parents a597f08 + 944f1b7 commit 5df7b0e

File tree

1 file changed

+118
-14
lines changed

1 file changed

+118
-14
lines changed

snow_first_setup/scripts/install-to-disk

Lines changed: 118 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
11
#!/bin/bash
2+
set -euo pipefail
3+
4+
# Cleanup function to ensure mounted filesystems are unmounted
5+
cleanup() {
6+
local exit_code=$?
7+
if [ -n "${MOUNTPOINT:-}" ] && [ -d "$MOUNTPOINT" ]; then
8+
if mountpoint -q "$MOUNTPOINT" 2>/dev/null; then
9+
echo "Unmounting $MOUNTPOINT..."
10+
umount "$MOUNTPOINT" 2>/dev/null || true
11+
fi
12+
rmdir "$MOUNTPOINT" 2>/dev/null || true
13+
fi
14+
if [ $exit_code -ne 0 ]; then
15+
echo "Script failed with exit code $exit_code" >&2
16+
fi
17+
exit $exit_code
18+
}
19+
20+
# Set up trap to call cleanup on exit
21+
trap cleanup EXIT INT TERM
22+
223
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
324
echo "usage:"
425
echo "install-to-disk <image> <filesystem> <device> [fde]"
@@ -38,7 +59,14 @@ if [ "$FDE" == "true" ]; then
3859
fi
3960
fi
4061

41-
RUST_LOG=debug bootc \
62+
# Check that bootc is available
63+
if ! command -v bootc &> /dev/null; then
64+
echo "bootc command not found, cannot proceed with installation"
65+
exit 14
66+
fi
67+
68+
echo "Starting bootc installation to $3..."
69+
if ! RUST_LOG=debug bootc \
4270
install \
4371
to-disk \
4472
--composefs-backend \
@@ -48,7 +76,11 @@ RUST_LOG=debug bootc \
4876
--target-imgref "$1" \
4977
--wipe \
5078
--bootloader systemd \
51-
"$3"
79+
"$3"; then
80+
echo "bootc installation failed"
81+
exit 15
82+
fi
83+
echo "bootc installation completed successfully"
5284

5385
# HACK: fix secure boot in bootc
5486
# now that the install is done, we can fix the efi binaries
@@ -64,7 +96,14 @@ RUST_LOG=debug bootc \
6496
# Mount the EFI partition from the target device ($3)
6597
# EFI partition is the second partition, so we use partprobe
6698
# to ensure the kernel sees it
67-
partprobe "$3"
99+
echo "Probing partitions on $3..."
100+
if ! partprobe "$3"; then
101+
echo "Failed to probe partitions on $3"
102+
exit 16
103+
fi
104+
105+
# Give the kernel a moment to recognize the new partitions
106+
sleep 2
68107

69108
DEVICE="$3"
70109

@@ -73,15 +112,39 @@ if [[ "$DEVICE" == *"nvme"* || "$DEVICE" == *"mmcblk"* || "$DEVICE" == *"loop"*
73112
DEVICE="${DEVICE}p"
74113
fi
75114

115+
EFI_PARTITION="${DEVICE}2"
116+
117+
# Verify the EFI partition exists
118+
if ! [ -b "$EFI_PARTITION" ]; then
119+
echo "EFI partition $EFI_PARTITION does not exist or is not a block device"
120+
exit 17
121+
fi
122+
123+
echo "Creating temporary mount point..."
76124
MOUNTPOINT=$(mktemp -d)
77-
mount "${DEVICE}2" "$MOUNTPOINT"
125+
if [ ! -d "$MOUNTPOINT" ]; then
126+
echo "Failed to create temporary mount point"
127+
exit 18
128+
fi
129+
130+
echo "Mounting EFI partition $EFI_PARTITION to $MOUNTPOINT..."
131+
if ! mount "$EFI_PARTITION" "$MOUNTPOINT"; then
132+
echo "Failed to mount EFI partition $EFI_PARTITION"
133+
rmdir "$MOUNTPOINT" 2>/dev/null || true
134+
exit 19
135+
fi
78136

79137

80138
if [ ! -d "$MOUNTPOINT/EFI/BOOT" ]; then
81-
mkdir -p "$MOUNTPOINT/EFI/BOOT"
139+
echo "Creating $MOUNTPOINT/EFI/BOOT directory..."
140+
if ! mkdir -p "$MOUNTPOINT/EFI/BOOT"; then
141+
echo "Failed to create EFI/BOOT directory"
142+
exit 20
143+
fi
82144
fi
83145

84146
# make sure the source files exists
147+
echo "Verifying source EFI files..."
85148
if [ ! -f /usr/lib/systemd/boot/efi/systemd-bootx64.efi.signed ]; then
86149
echo "systemd-bootx64.efi.signed not found, cannot copy to EFI partition"
87150
exit 10
@@ -98,20 +161,61 @@ if [ ! -f /usr/lib/shim/mmx64.efi.signed ]; then
98161
echo "mmx64.efi.signed not found, cannot copy to EFI partition"
99162
exit 13
100163
fi
164+
101165
# replicate a debian secureboot efi setup
102-
mkdir -p "$MOUNTPOINT/EFI/snow"
103-
cp /usr/lib/shim/shimx64.efi.signed "$MOUNTPOINT/EFI/snow/shimx64.efi"
104-
cp /usr/lib/shim/fbx64.efi.signed "$MOUNTPOINT/EFI/snow/fbx64.efi"
105-
cp /usr/lib/shim/mmx64.efi.signed "$MOUNTPOINT/EFI/snow/mmx64.efi"
106-
cp /usr/lib/systemd/boot/efi/systemd-bootx64.efi.signed "$MOUNTPOINT/EFI/snow/grubx64.efi"
166+
echo "Creating EFI/snow directory..."
167+
if ! mkdir -p "$MOUNTPOINT/EFI/snow"; then
168+
echo "Failed to create EFI/snow directory"
169+
exit 21
170+
fi
171+
172+
echo "Copying secure boot EFI binaries..."
173+
if ! cp /usr/lib/shim/shimx64.efi.signed "$MOUNTPOINT/EFI/snow/shimx64.efi"; then
174+
echo "Failed to copy shimx64.efi"
175+
exit 22
176+
fi
177+
if ! cp /usr/lib/shim/fbx64.efi.signed "$MOUNTPOINT/EFI/snow/fbx64.efi"; then
178+
echo "Failed to copy fbx64.efi"
179+
exit 23
180+
fi
181+
if ! cp /usr/lib/shim/mmx64.efi.signed "$MOUNTPOINT/EFI/snow/mmx64.efi"; then
182+
echo "Failed to copy mmx64.efi"
183+
exit 24
184+
fi
185+
if ! cp /usr/lib/systemd/boot/efi/systemd-bootx64.efi.signed "$MOUNTPOINT/EFI/snow/grubx64.efi"; then
186+
echo "Failed to copy systemd-bootx64.efi as grubx64.efi"
187+
exit 25
188+
fi
107189

108190
# create a new boot entry for shim
109-
efibootmgr --create --disk "$3" --part 2 --loader '\EFI\snow\shimx64.efi' --label "Snow Secure Boot"
191+
echo "Creating EFI boot entry..."
192+
if command -v efibootmgr &> /dev/null; then
193+
if ! efibootmgr --create --disk "$3" --part 2 --loader '\EFI\snow\shimx64.efi' --label "Snow Secure Boot"; then
194+
echo "Warning: Failed to create EFI boot entry (continuing anyway)"
195+
fi
196+
else
197+
echo "Warning: efibootmgr not found, skipping boot entry creation"
198+
fi
199+
110200
# finally uncomment the line in loader.conf that sets the timeout
111201
# so that the boot menu appears, allowing the user to edit the kargs
112202
# if needed to unlock the disk
113-
sed -i 's/^#timeout/timeout/' "$MOUNTPOINT/loader/loader.conf"
203+
if [ -f "$MOUNTPOINT/loader/loader.conf" ]; then
204+
echo "Configuring bootloader timeout..."
205+
if ! sed -i 's/^#timeout/timeout/' "$MOUNTPOINT/loader/loader.conf"; then
206+
echo "Warning: Failed to update loader.conf (continuing anyway)"
207+
fi
208+
else
209+
echo "Warning: loader.conf not found at $MOUNTPOINT/loader/loader.conf"
210+
fi
114211

115212
# clean up
116-
umount "$MOUNTPOINT"
117-
rmdir "$MOUNTPOINT"
213+
echo "Unmounting EFI partition..."
214+
if ! umount "$MOUNTPOINT"; then
215+
echo "Warning: Failed to unmount $MOUNTPOINT cleanly"
216+
# Try force unmount as last resort
217+
umount -f "$MOUNTPOINT" 2>/dev/null || true
218+
fi
219+
rmdir "$MOUNTPOINT" 2>/dev/null || true
220+
221+
echo "Installation completed successfully!"

0 commit comments

Comments
 (0)