Files
SD-imaging-scripts/create-image-sdcard-resize-optional.sh
2025-12-13 21:20:06 -08:00

98 lines
2.3 KiB
Bash
Executable File

#!/bin/bash
set -e
RESIZE_SCRIPT="./auto-shrink-compress-img.sh"
echo "==== 🧠 SD Card Disk Imager + Auto Resize ===="
# === Step 1: Detect likely SD card devices ===
echo ""
echo "📦 Scanning for removable / USB storage devices..."
echo "📦 Scanning for removable / USB storage devices..."
mapfile -t DISKS < <(
lsblk -dpno NAME,RM,TRAN | awk '
{
name=$1
rm=$2
tran=$3
# Select:
# - anything marked removable (RM=1)
# - OR anything on USB transport
if (rm == "1" || tran == "usb") {
print name
}
}
'
)
if [ ${#DISKS[@]} -eq 0 ]; then
echo "❌ No likely SD/USB storage devices found!"
echo " (Debug: run `lsblk -dpno NAME,MODEL,SIZE,RM,ROTA,TRAN` manually to inspect.)"
exit 1
fi
echo ""
echo "📂 Found the following removable block devices:"
for i in "${!DISKS[@]}"; do
disk_info=$(lsblk -dno NAME,MODEL,SIZE "${DISKS[$i]}")
printf " [%d] %s\n" "$((i+1))" "$disk_info"
done
echo ""
read -p "👉 Enter the number of the disk to back up: " SELECTION
if ! [[ "$SELECTION" =~ ^[0-9]+$ ]] || [ "$SELECTION" -lt 1 ] || [ "$SELECTION" -gt "${#DISKS[@]}" ]; then
echo "❌ Invalid selection."
exit 1
fi
DISK="${DISKS[$((SELECTION-1))]}"
echo "✅ Selected disk: $DISK"
echo ""
# === Step 2: Ask for image filename ===
read -p "💾 Enter a name for the output image file (no extension): " IMG_NAME
IMG_FILE="${IMG_NAME}.img"
if [ -f "$IMG_FILE" ]; then
echo "⚠️ File '$IMG_FILE' already exists! Please choose a different name."
exit 1
fi
echo ""
echo "📸 Creating disk image from $DISK..."
sudo dd if="$DISK" of="$IMG_FILE" bs=4M status=progress conv=fsync
echo ""
echo "✅ Disk image created: $IMG_FILE"
# === Step 3: Ask if user wants to auto-shrink ===
echo ""
if [ ! -f "$RESIZE_SCRIPT" ]; then
echo "⚠️ Resize script '$RESIZE_SCRIPT' not found! Skipping auto-shrink."
exit 0
fi
echo "⏳ Would you like to resize & compress this image using:"
echo " $RESIZE_SCRIPT"
echo ""
echo "It will auto-run in 30 seconds if no answer is given."
read -t 30 -p "Type 'n' to skip or press [Enter] to run: " ANSWER
if [[ "$ANSWER" =~ ^[Nn]$ ]]; then
echo "🚫 Skipping resize/compression."
exit 0
fi
# === Step 4: Run the shrink script ===
echo ""
echo "🚀 Running auto-shrink script..."
bash "$RESIZE_SCRIPT"