Skip to content

Instantly share code, notes, and snippets.

@raghavauppuluri13
Last active October 17, 2025 00:32
Show Gist options
  • Save raghavauppuluri13/1b9cd1149132f6eff0d6296cee4ab5b7 to your computer and use it in GitHub Desktop.
Save raghavauppuluri13/1b9cd1149132f6eff0d6296cee4ab5b7 to your computer and use it in GitHub Desktop.
Flash Jetpack 6.2 on jetson orin nano
#!/usr/bin/env bash
# DietPi flash for OrangePi5/5Pro (Linux + macOS)
# - Auto-picks largest removable disk >=4GB
# - No prompts (env: WIFI_SSID, WIFI_PASSWORD, HOSTNAME); only confirm target (skip with -y)
set -Eeuo pipefail; shopt -s nullglob
YES=0; [[ "${1:-}" == "-y" ]] && YES=1
TARGET="${TARGET:-OrangePi5Pro}"
IMAGE="DietPi_${TARGET}-ARMv8-Bookworm.img.xz"
URL="https://dietpi.com/downloads/images/${IMAGE}"
rand_host(){ printf "bracketbot-%03d" "$((RANDOM%1000))"; }
HOSTNAME="${HOSTNAME:-$(rand_host)}"
WIFI_SSID="${WIFI_SSID:-}"; WIFI_PASSWORD="${WIFI_PASSWORD:-}"
die(){ echo "ERR: $*" >&2; exit 1; }
need(){ command -v "$1" >/dev/null 2>&1 || die "missing: $1"; }
human(){ local b=$1 u=(B KB MB GB TB) i=0; while ((b>=1024 && i<4)); do b=$((b/1024)); ((i++)); done; echo "${b}${u[$i]}"; }
OS="$(uname -s)"
need dd; need xz; need sed
DL=""; command -v curl >/dev/null && DL="curl -fL" || command -v wget >/dev/null && DL="wget -O-"
[[ -n "$DL" ]] || die "need curl or wget"
MIN=$((4*1024*1024*1024))
pick_linux(){
local best=0 bestdev=""
for b in /sys/block/*; do
[[ -r "$b/removable" && $(<"$b/removable") == 1 ]] || continue
local d="/dev/$(basename "$b")"; [[ -b "$d" ]] || continue
local sz=$(( $(<"$b/size") * 512 )); (( sz>=MIN )) || continue
(( sz>best )) && { best=$sz; bestdev="$d"; }
done
[[ -n "$bestdev" ]] || die "no removable disk >=4GB found"
echo "$bestdev|$best"
}
pick_macos(){
need diskutil
local best=0 bestdev=""
while read -r d; do
[[ "$(diskutil info "$d" | awk -F': *' '/^ *Whole:/{print $2}')" == "Yes" ]] || continue
[[ "$d" == "/dev/disk0" ]] && continue
local info sz proto rem
info="$(diskutil info "$d")"
proto="$(awk -F': *' '/^ *Protocol:/{print $2}' <<<"$info")"
rem="$(awk -F': *' '/^ *Removable Media:/{print $2}' <<<"$info")"
case "$proto" in USB*|SD*|Thunderbolt*) : ;; *) [[ "$rem" =~ Yes|Removable ]] || continue ;; esac
sz="$(awk -F': *' '/^ *Disk Size:/{gsub(/[^0-9]/,"",$2); print $2}' <<<"$info")"
[[ -n "$sz" && "$sz" -ge "$MIN" ]] || continue
(( sz>best )) && { best=$sz; bestdev="$d"; }
done < <(diskutil list | awk '/^\/dev\/disk[0-9]+/{print $1}')
[[ -n "$bestdev" ]] || die "no external/removable disk >=4GB found"
echo "$bestdev|$best"
}
sel="" bytes=0
if [[ "$OS" == "Linux" ]]; then
IFS="|" read -r sel bytes < <(pick_linux)
elif [[ "$OS" == "Darwin" ]]; then
IFS="|" read -r sel bytes < <(pick_macos)
else
die "unsupported OS: $OS"
fi
raw="$sel"; [[ "$OS" == "Darwin" ]] && raw="${sel/\/dev\/disk/\/dev\/rdisk}"
echo "Target: $sel ($(human "$bytes"))"
(( YES==1 )) || { read -r -p "Erase and flash this device? [y/N] " a; [[ "$a" =~ ^[Yy]$ ]] || die "aborted"; }
# Unmount
if [[ "$OS" == "Linux" ]]; then
for p in "${sel}"?*; do [[ -e "$p" ]] && umount -f "$p" || true; done; sync
else
diskutil unmountDisk "$sel" || true; sync
fi
# Flash (streaming; no temp .img on disk)
echo "Downloading & flashing ${IMAGE} ..."
$DL "$URL" | xz -dc | ( [[ "$OS" == "Linux" ]] && dd of="$raw" bs=4M status=progress conv=fsync oflag=direct || dd of="$raw" bs=4m ) || die "dd failed"
sync
# Mount BOOT partition & write configs
echo "Configuring DietPi (hostname & WiFi if provided) ..."
ESC_SSID=$(printf "%s" "$WIFI_SSID" | sed "s/'/'\\\\''/g")
ESC_PSK=$(printf "%s" "$WIFI_PASSWORD" | sed "s/'/'\\\\''/g")
if [[ "$OS" == "Linux" ]]; then
partprobe "$sel" || true; udevadm settle || true; sleep 2
bootp=""
# pick first vfat partition
while read -r n fstype; do [[ "$fstype" == "vfat" ]] && { bootp="$n"; break; }; done < <(lsblk -ln -o NAME,FSTYPE "$sel")
[[ -n "$bootp" ]] || die "could not find BOOT (vfat) partition"
sudo mkdir -p /mnt/boot_dietpi
sudo mount "/dev/$bootp" /mnt/boot_dietpi
sudo bash -c "sed -i \"s|^AUTO_SETUP_NET_HOSTNAME=.*|AUTO_SETUP_NET_HOSTNAME=$HOSTNAME|\" /mnt/boot_dietpi/dietpi.txt"
if [[ -n "$WIFI_SSID" ]]; then
sudo bash -c "sed -i \"s|^aWIFI_SSID\[0\]=.*|aWIFI_SSID[0]='$ESC_SSID'|; s|^aWIFI_KEY\[0\]=.*|aWIFI_KEY[0]='$ESC_PSK'|\" /mnt/boot_dietpi/dietpi-wifi.txt"
fi
# Optional: drop automation script if present in $TARGET/
[[ -f "${TARGET}/Automation_Custom_Script.sh" ]] && sudo cp -f "${TARGET}/Automation_Custom_Script.sh" /mnt/boot_dietpi/
sudo umount /mnt/boot_dietpi
else
# macOS: try native mount of FAT; fall back to anylinuxfs if needed
bootdev="${sel}s1"
if diskutil info "$bootdev" | grep -q "File System Personality: MS-DOS"; then
diskutil mount "$bootdev" >/dev/null
mnt="$(diskutil info "$bootdev" | awk -F': *' '/Mount Point/{print $2}')"
else
need anylinuxfs
anylinuxfs stop 2>/dev/null || true
sudo mkdir -p /tmp/mnt_dietpi
sudo anylinuxfs "$bootdev" /tmp/mnt_dietpi
mnt="/tmp/mnt_dietpi"
fi
sudo bash -c "sed -i '' \"s|^AUTO_SETUP_NET_HOSTNAME=.*|AUTO_SETUP_NET_HOSTNAME=$HOSTNAME|\" \"$mnt/boot/dietpi.txt\" 2>/dev/null || sed -i '' \"s|^AUTO_SETUP_NET_HOSTNAME=.*|AUTO_SETUP_NET_HOSTNAME=$HOSTNAME|\" \"$mnt/dietpi.txt\""
if [[ -n "$WIFI_SSID" ]]; then
sudo bash -c "sed -i '' \"s|^aWIFI_SSID\[0\]=.*|aWIFI_SSID[0]='$ESC_SSID'|; s|^aWIFI_KEY\[0\]=.*|aWIFI_KEY[0]='$ESC_PSK'|\" \"$mnt/boot/dietpi-wifi.txt\" 2>/dev/null || sed -i '' \"s|^aWIFI_SSID\[0\]=.*|aWIFI_SSID[0]='$ESC_SSID'|; s|^aWIFI_KEY\[0\]=.*|aWIFI_KEY[0]='$ESC_PSK'|\" \"$mnt/dietpi-wifi.txt\""
fi
[[ -f "${TARGET}/Automation_Custom_Script.sh" ]] && sudo cp -f "${TARGET}/Automation_Custom_Script.sh" "$mnt/boot/" 2>/dev/null || sudo cp -f "${TARGET}/Automation_Custom_Script.sh" "$mnt/" 2>/dev/null || true
[[ "$mnt" == /tmp/mnt_dietpi ]] && { sudo umount "$mnt" || true; anylinuxfs stop || true; }
diskutil eject "$sel" || true
fi
echo "✔ Done. Hostname: $HOSTNAME"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment