Skip to content

Commit 388f7a9

Browse files
kernel tools: make build-dtb-image.sh root-only and remove sudo dependency
Refine build-dtb-image.sh to operate as a root-only tool and eliminate its runtime dependency on sudo, improving compatibility with minimal and containerized CI environments. Changes: - Add an explicit EUID check at startup and fail fast if the script is not executed as root, clarifying the requirement for losetup/mkfs/mount operations. - Remove all internal sudo invocations around: - losetup --show -fP - mkfs.vfat - mount / umount - loop-device detach - DTB copy into the mounted FAT filesystem - Keep the existing EXIT trap semantics intact, still guaranteeing: - sync() best-effort flush, - unmount of the temporary mountpoint (if present and mounted), - loop device detachment, - removal of the temporary sanitized manifest file. Behavior of the tool is otherwise unchanged: it still normalizes and validates DTB paths from the manifest, produces <DTB_SRC>/combined-dtb.dtb by concatenation in manifest order, and creates a FAT-formatted dtb.bin image containing only the combined DTB. The script is now better aligned with containerized CI usage patterns, where the entrypoint already runs as root and sudo is typically absent. Signed-off-by: Bjordis Collaku <[email protected]>
1 parent fc09a51 commit 388f7a9

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

kernel/scripts/build-dtb-image.sh

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@
4141
# * losetup
4242
# * mount / umount
4343
# * dd (with status=progress support is nice but not required)
44-
# - sudo access for:
45-
# * losetup
46-
# * mkfs.vfat
47-
# * mount / umount
44+
# - This script must be run as root (no internal sudo calls).
4845
#
4946
# Notes:
5047
# - The combined DTB is written to: <DTB_SRC>/combined-dtb.dtb
@@ -59,6 +56,12 @@
5956

6057
set -euo pipefail
6158

59+
# Require running as root (needed for losetup, mkfs, mount, etc.)
60+
if [[ "$EUID" -ne 0 ]]; then
61+
echo "[ERROR] This script must be run as root (no internal sudo calls)." >&2
62+
exit 1
63+
fi
64+
6265
# ----------------------------- Defaults --------------------------------------
6366

6467
DTB_BIN_SIZE=4 # Default FAT image size (MB)
@@ -79,7 +82,7 @@ Usage: $0 -dtb-src <path> -manifest <file> [-size <MB>] [-out <file>]
7982
8083
-dtb-src Path to DTB source directory (e.g. arch/arm64/boot/dts/qcom)
8184
-manifest Path to manifest file listing DTBs (one per line)
82-
-size FAT image size in MB (default: 7)
85+
-size FAT image size in MB (default: 4)
8386
-out Output image filename (default: dtb.bin)
8487
EOF
8588
exit 1
@@ -95,15 +98,15 @@ cleanup() {
9598
# Unmount the mountpoint if it exists and is currently mounted
9699
if [[ -n "${MNT_DIR:-}" && -d "$MNT_DIR" ]]; then
97100
if mountpoint -q "$MNT_DIR"; then
98-
sudo umount "$MNT_DIR" || true
101+
umount "$MNT_DIR" || true
99102
fi
100103
# Try to remove the temporary directory (ignore failures)
101104
rmdir "$MNT_DIR" 2>/dev/null || true
102105
fi
103106

104107
# Detach loop device if it was created
105108
if [[ -n "${LOOP_DEV:-}" ]]; then
106-
sudo losetup -d "$LOOP_DEV" || true
109+
losetup -d "$LOOP_DEV" || true
107110
fi
108111

109112
# Remove temporary sanitized list
@@ -208,7 +211,6 @@ OUT="${DTB_SRC}/combined-dtb.dtb"
208211
rm -f "$OUT"
209212

210213
# Concatenate in the order specified by the sanitized manifest.
211-
# xargs -a is GNU-specific but acceptable for typical Linux build hosts.
212214
xargs -a "$SANLIST" -r cat > "$OUT"
213215
echo "[INFO] Combined DTBs into: $OUT"
214216
ls -lh "$OUT"
@@ -219,23 +221,23 @@ echo "[INFO] Creating FAT image '$DTB_BIN' (${DTB_BIN_SIZE} MB)..."
219221
dd if=/dev/zero of="$DTB_BIN" bs=1M count="$DTB_BIN_SIZE" status=progress
220222

221223
# Attach a loop device to the image
222-
LOOP_DEV="$(sudo losetup --show -fP "$DTB_BIN")"
224+
LOOP_DEV="$(losetup --show -fP "$DTB_BIN")"
223225
echo "[INFO] Using loop device: $LOOP_DEV"
224226

225227
# Create a temporary mount directory for this run
226228
MNT_DIR="$(mktemp -d -t dtb-mnt-XXXXXX)"
227229

228230
# Format the loop device with FAT
229231
echo "[INFO] Formatting $LOOP_DEV as FAT..."
230-
sudo mkfs.vfat "$LOOP_DEV" >/dev/null
232+
mkfs.vfat "$LOOP_DEV" >/dev/null
231233

232234
# Mount the loop device
233235
echo "[INFO] Mounting $LOOP_DEV at $MNT_DIR..."
234-
sudo mount "$LOOP_DEV" "$MNT_DIR"
236+
mount "$LOOP_DEV" "$MNT_DIR"
235237

236238
# ----------------------- Deploy Combined DTB ---------------------------------
237239

238-
sudo cp "$OUT" "$MNT_DIR/"
240+
cp "$OUT" "$MNT_DIR/"
239241
echo "[INFO] Deployed combined DTB into FAT image."
240242
echo "[INFO] Files in image:"
241243
ls -lh "$MNT_DIR"

0 commit comments

Comments
 (0)