Skip to content

Commit fed001f

Browse files
committed
Add script to create an .img to be flashed to raspberry pi
1 parent 2e4a474 commit fed001f

1 file changed

Lines changed: 105 additions & 0 deletions

File tree

scripts/build_img.sh

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# --- settings ---
5+
IMG=${IMG:-archarm-rpi-aarch64.img}
6+
SIZE=${SIZE:-4G} # total image size
7+
BOOT_MB=${BOOT_MB:-768} # FAT32 /boot size in MiB
8+
ROOT_LABEL=${ROOT_LABEL:-ALARM_ROOT}
9+
BOOT_LABEL=${BOOT_LABEL:-ALARM_BOOT}
10+
ROOTFS_TAR=${ROOTFS_TAR:-rootfs.tar}
11+
12+
# sanity
13+
[ -f "$ROOTFS_TAR" ] || { echo "Missing $ROOTFS_TAR"; exit 1; }
14+
15+
# tools needed: sfdisk, losetup, mkfs.vfat, mkfs.ext4, tar, rsync or cp -a
16+
command -v sfdisk >/dev/null
17+
command -v losetup >/dev/null
18+
19+
# --- create sparse disk file ---
20+
truncate -s "$SIZE" "$IMG"
21+
22+
# --- partition table: 1MiB align, 768MiB FAT32 boot, rest ext4 root ---
23+
BOOT_START=2048 # 1MiB (512b sectors)
24+
BOOT_SIZE=$(( BOOT_MB * 2048 )) # sectors (MiB * 2048)
25+
sfdisk "$IMG" <<EOF
26+
label: dos
27+
unit: sectors
28+
${IMG}1 : start=${BOOT_START}, size=${BOOT_SIZE}, type=c
29+
${IMG}2 : start=$((BOOT_START+BOOT_SIZE)), type=83
30+
EOF
31+
32+
# --- map loop with partitions ---
33+
LOOP=$(sudo losetup --find --show --partscan "$IMG")
34+
BOOT_DEV=${LOOP}p1
35+
ROOT_DEV=${LOOP}p2
36+
PARTUUID=$(sudo blkid -s PARTUUID -o value "$ROOT_DEV")
37+
38+
# --- mkfs ---
39+
sudo mkfs.vfat -F 32 -n "$BOOT_LABEL" "$BOOT_DEV"
40+
sudo mkfs.ext4 -F -L "$ROOT_LABEL" "$ROOT_DEV"
41+
42+
BOOT_UUID=$(sudo blkid -s UUID -o value "$BOOT_DEV")
43+
ROOT_UUID=$(sudo blkid -s UUID -o value "$ROOT_DEV")
44+
echo "BOOT UUID is $BOOT_UUID"
45+
echo "ROOT UUID is $ROOT_UUID"
46+
47+
# --- mount ---
48+
sudo mkdir -p /mnt/arch-root /mnt/arch-boot
49+
sudo mount "$ROOT_DEV" /mnt/arch-root
50+
sudo mkdir -p /mnt/arch-root/boot
51+
sudo mount "$BOOT_DEV" /mnt/arch-boot
52+
53+
# --- extract rootfs (preserve xattrs/owners) ---
54+
sudo tar --numeric-owner -xpf "$ROOTFS_TAR" -C /mnt/arch-root
55+
56+
# --- move/copy boot files to the FAT32 partition ---
57+
# Official instructions literally "move root/boot/* to boot" when using their tarball.
58+
# We do the equivalent from our extracted rootfs.
59+
if [ -d /mnt/arch-root/boot ] && [ -n "$(ls -A /mnt/arch-root/boot)" ]; then
60+
sudo cp -a /mnt/arch-root/boot/* /mnt/arch-boot/
61+
fi
62+
63+
# --- minimal boot config depending on strategy ---
64+
# If you installed linux-rpi (+ raspberrypi-bootloader), firmware boots kernel*.img via config.txt/cmdline.txt
65+
if [ -f /mnt/arch-boot/kernel8.img ]; then
66+
printf 'arm_64bit=1\nenable_uart=1\n' | sudo tee /mnt/arch-boot/config.txt >/dev/null
67+
# root= by PARTUUID (safest). Discover it:
68+
sudo tee /mnt/arch-boot/cmdline.txt >/dev/null <<EOF
69+
console=serial0,115200 console=ttyAMA0,115200 root=PARTUUID=${PARTUUID} rw rootwait
70+
EOF
71+
fi
72+
73+
# If you installed linux-aarch64 + uboot-raspberrypi, ensure extlinux.conf exists
74+
if [ -d /mnt/arch-boot/extlinux ]; then
75+
:
76+
elif [ -f /mnt/arch-boot/u-boot.bin ] || [ -f /mnt/arch-root/boot/u-boot.bin ]; then
77+
sudo install -d /mnt/arch-boot/extlinux
78+
sudo tee /mnt/arch-boot/extlinux/extlinux.conf >/dev/null <<EOF
79+
DEFAULT arch
80+
MENU TITLE Arch Linux ARM
81+
TIMEOUT 3
82+
83+
LABEL arch
84+
LINUX /Image
85+
INITRD /initramfs-linux.img
86+
FDTDIR /dtbs
87+
APPEND root=PARTUUID=${PARTUUID} rw rootwait console=ttyAMA0,115200 console=serial0,115200
88+
EOF
89+
fi
90+
91+
# --- fstab (Pi 4 aarch64 note: ALARM docs use mmcblk1) ---
92+
# Use PARTUUIDs so device names don’t matter.
93+
sudo tee /mnt/arch-root/etc/fstab <<EOF
94+
UUID=${ROOT_UUID} / ext4 defaults,noatime 0 1
95+
UUID=${BOOT_UUID} /boot vfat defaults,noatime 0 2
96+
EOF
97+
98+
sync
99+
100+
# --- unmount & detach ---
101+
sudo umount /mnt/arch-boot || true
102+
sudo umount /mnt/arch-root || true
103+
sudo losetup -d "$LOOP"
104+
105+
echo "OK: ${IMG} is ready."

0 commit comments

Comments
 (0)