summaryrefslogtreecommitdiffstats
path: root/scripts/mkdisk.sh
blob: 2c62f767d680501c0922ab2422daccae3611c007 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/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)"