#!/usr/bin/env bash # # scripts/mkdisk.sh — create (or re-create) the FAT32 data disk that the # kernel mounts at /disk. # # Usage: # ./scripts/mkdisk.sh # create /tmp/disk.img (64M) if missing # ./scripts/mkdisk.sh --fresh # re-format (wipes the image!) # ./scripts/mkdisk.sh --size 128M # different size (default 64M) # ./scripts/mkdisk.sh /path/img # custom image path # ./scripts/mkdisk.sh --no-seed # skip the seed/ directory # ./scripts/mkdisk.sh --label CRASHLOG # volume label (default RSDOS) # # The image is created with `mkfs.fat -C`, which is the option that # actually creates the file — plain `mkfs.fat` only writes into an # existing one. Files from seed/ (if present) are copied in with mtools. # set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" PROJECT_DIR="$(dirname "$SCRIPT_DIR")" IMG="/tmp/disk.img" SIZE="64M" FRESH=0 SEED_DIR="$PROJECT_DIR/seed" LABEL="RSDOS" while [ $# -gt 0 ]; do case "$1" in --fresh) FRESH=1 ;; --size) shift SIZE="$1" ;; --no-seed) SEED_DIR="" ;; --label) shift LABEL="$1" ;; --help | -h) head -20 "$0" | grep "^#" | sed 's/^# \?//' exit 0 ;; *) IMG="$1" ;; esac shift done if [ -f "$IMG" ] && [ "$FRESH" -eq 0 ]; then echo "mkdisk: $IMG already exists (use --fresh to re-format)" exit 0 fi if ! command -v mkfs.fat >/dev/null 2>&1; then echo "mkdisk: mkfs.fat not found — install dosfstools (sudo apt install dosfstools)" >&2 exit 1 fi # mkfs.fat sizes are 1024-byte blocks; accept M/G/K suffixes too. case "$SIZE" in *G) SIZE_BLOCKS=$((${SIZE%G} * 1024 * 1024)) ;; *M) SIZE_BLOCKS=$((${SIZE%M} * 1024)) ;; *K) SIZE_BLOCKS=$((${SIZE%K})) ;; *) SIZE_BLOCKS="$SIZE" ;; esac if [ -z "$SIZE_BLOCKS" ] || [ "$SIZE_BLOCKS" -le 0 ] 2>/dev/null; then echo "mkdisk: bad size '$SIZE' (use e.g. 64M or 65536)" >&2 exit 1 fi echo "mkdisk: creating $IMG ($SIZE FAT32, label $LABEL)" rm -f "$IMG" mkfs.fat -C -F 32 -n "$LABEL" "$IMG" "$SIZE_BLOCKS" if [ -n "$SEED_DIR" ] && [ -d "$SEED_DIR" ]; then if ! command -v mcopy >/dev/null 2>&1; then echo "mkdisk: warning: mcopy not found — skipping seed files (install mtools)" >&2 else echo "mkdisk: seeding from $SEED_DIR/" for f in "$SEED_DIR"/*; do [ -f "$f" ] || continue mcopy -i "$IMG" "$f" "::/$(basename "$f")" echo " -> $(basename "$f")" done fi fi echo "mkdisk: done — boot with: make run (or: ./scripts/boot.sh)"