Files
pi-kit/flash_sd.sh
2025-12-10 18:51:31 -05:00

44 lines
1.1 KiB
Bash
Executable File

#!/bin/bash
set -euo pipefail
# Pi-Kit flashing helper (small wrapper around xzcat | dd).
# Usage: sudo ./flash_sd.sh /path/to/image.img.xz /dev/sdX
# Example: sudo ./flash_sd.sh images/output/DietPi_RPi5-ARMv8-Trixie-base.img.xz /dev/sdb
# Safety guardrails:
# - Requires root (sudo).
# - WILL wipe the target block device; prompt asks for YES before writing.
# - If a .sha256 sits next to the image, consider verifying it first.
img="${1:-}"
dev="${2:-}"
if [[ -z "$img" || -z "$dev" ]]; then
echo "Usage: sudo $0 image.img.xz /dev/sdX"
exit 1
fi
if [[ $EUID -ne 0 ]]; then
echo "Please run as root (sudo)."
exit 1
fi
if [[ ! -b "$dev" ]]; then
echo "Device $dev not found or not a block device."
exit 1
fi
echo "About to wipe and flash $dev with $img"
read -rp "Type YES to continue: " yn
[[ "$yn" == "YES" ]] || { echo "Aborted."; exit 1; }
echo "Decompressing and writing... this may take several minutes."
xzcat "$img" | pv | dd of="$dev" bs=4M conv=fsync status=none
echo "Syncing..."
sync
echo "Flashed $img to $dev successfully."
echo "Ejecting $dev..."
sudo eject "$dev" || true