summaryrefslogtreecommitdiffstats
path: root/scripts/mkdisk.sh
diff options
context:
space:
mode:
Diffstat (limited to '')
-rwxr-xr-xscripts/mkdisk.sh85
1 files changed, 85 insertions, 0 deletions
diff --git a/scripts/mkdisk.sh b/scripts/mkdisk.sh
new file mode 100755
index 0000000..f9a8c09
--- /dev/null
+++ b/scripts/mkdisk.sh
@@ -0,0 +1,85 @@
1#!/usr/bin/env bash
2#
3# scripts/mkdisk.sh — create (or re-create) the FAT32 data disk that the
4# kernel mounts at /disk.
5#
6# Usage:
7# ./scripts/mkdisk.sh # create /tmp/disk.img (64M) if missing
8# ./scripts/mkdisk.sh --fresh # re-format (wipes the image!)
9# ./scripts/mkdisk.sh --size 128M # different size (default 64M)
10# ./scripts/mkdisk.sh /path/img # custom image path
11# ./scripts/mkdisk.sh --no-seed # skip the seed/ directory
12#
13# The image is created with `mkfs.fat -C`, which is the option that
14# actually creates the file — plain `mkfs.fat` only writes into an
15# existing one. Files from seed/ (if present) are copied in with mtools.
16#
17set -euo pipefail
18
19SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
20PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
21
22IMG="/tmp/disk.img"
23SIZE="64M"
24FRESH=0
25SEED_DIR="$PROJECT_DIR/seed"
26
27while [ $# -gt 0 ]; do
28 case "$1" in
29 --fresh) FRESH=1 ;;
30 --size)
31 shift
32 SIZE="$1"
33 ;;
34 --no-seed) SEED_DIR="" ;;
35 --help | -h)
36 head -20 "$0" | grep "^#" | sed 's/^# \?//'
37 exit 0
38 ;;
39 *)
40 IMG="$1"
41 ;;
42 esac
43 shift
44done
45
46if [ -f "$IMG" ] && [ "$FRESH" -eq 0 ]; then
47 echo "mkdisk: $IMG already exists (use --fresh to re-format)"
48 exit 0
49fi
50
51if ! command -v mkfs.fat >/dev/null 2>&1; then
52 echo "mkdisk: mkfs.fat not found — install dosfstools (sudo apt install dosfstools)" >&2
53 exit 1
54fi
55
56# mkfs.fat sizes are 1024-byte blocks; accept M/G/K suffixes too.
57case "$SIZE" in
58*G) SIZE_BLOCKS=$(( ${SIZE%G} * 1024 * 1024 )) ;;
59*M) SIZE_BLOCKS=$(( ${SIZE%M} * 1024 )) ;;
60*K) SIZE_BLOCKS=$(( ${SIZE%K} )) ;;
61*) SIZE_BLOCKS="$SIZE" ;;
62esac
63if [ -z "$SIZE_BLOCKS" ] || [ "$SIZE_BLOCKS" -le 0 ] 2>/dev/null; then
64 echo "mkdisk: bad size '$SIZE' (use e.g. 64M or 65536)" >&2
65 exit 1
66fi
67
68echo "mkdisk: creating $IMG ($SIZE FAT32, label RSDOS)"
69rm -f "$IMG"
70mkfs.fat -C -F 32 -n RSDOS "$IMG" "$SIZE_BLOCKS"
71
72if [ -n "$SEED_DIR" ] && [ -d "$SEED_DIR" ]; then
73 if ! command -v mcopy >/dev/null 2>&1; then
74 echo "mkdisk: warning: mcopy not found — skipping seed files (install mtools)" >&2
75 else
76 echo "mkdisk: seeding from $SEED_DIR/"
77 for f in "$SEED_DIR"/*; do
78 [ -f "$f" ] || continue
79 mcopy -i "$IMG" "$f" "::/$(basename "$f")"
80 echo " -> $(basename "$f")"
81 done
82 fi
83fi
84
85echo "mkdisk: done — boot with: make run (or: ./scripts/boot.sh)"