Files
MontaukOS/scripts/mkramdisk.sh
T

43 lines
1.7 KiB
Bash
Executable File

#!/bin/bash
# mkramdisk.sh - Create a USTAR tar archive for the MontaukOS ramdisk
# Usage: ./scripts/mkramdisk.sh [input_dir] [output_path]
set -e
INPUT_DIR="${1:-programs/bin}"
OUTPUT_PATH="${2:-ramdisk.tar}"
if [ ! -d "$INPUT_DIR" ]; then
echo "mkramdisk: input directory '$INPUT_DIR' does not exist, creating empty ramdisk"
mkdir -p "$INPUT_DIR"
# Create a placeholder file so the tar isn't completely empty
echo "MontaukOS ramdisk" > "$INPUT_DIR/readme.txt"
fi
# Create USTAR tar archive.
#
# -h (dereference) is NOT optional: Fs/Ramdisk.cpp only recognises typeflag
# '5' (directory), so a symlink entry unpacks as a zero-length regular file --
# silently, with no error anywhere. A staged tree containing symlinks would
# ship empty files. Copying the target in is the only faithful representation.
tar --format=ustar -h -cf "$OUTPUT_PATH" -C "$INPUT_DIR" .
if tar -tvf "$OUTPUT_PATH" | grep -q '^l'; then
echo "mkramdisk: ERROR: symlink survived into $OUTPUT_PATH (would unpack as an empty file)" >&2
tar -tvf "$OUTPUT_PATH" | grep '^l' >&2
exit 1
fi
# The kernel's ramdisk file table is fixed-size (MaxFiles in
# kernel/src/Fs/Ramdisk.hpp). Entries past it are dropped, and an image that
# only just fits leaves no slots for files created at runtime (users.toml!).
MAX_FILES=$(sed -n 's/.*constexpr int MaxFiles = \([0-9]*\);.*/\1/p' \
"$(dirname "$0")/../kernel/src/Fs/Ramdisk.hpp")
ENTRIES=$(tar -tf "$OUTPUT_PATH" | wc -l)
if [ -n "$MAX_FILES" ] && [ "$ENTRIES" -gt $((MAX_FILES - 512)) ]; then
echo "mkramdisk: ERROR: $ENTRIES entries leaves under 512 free of the kernel's $MAX_FILES ramdisk slots; raise MaxFiles" >&2
exit 1
fi
echo "mkramdisk: created $OUTPUT_PATH from $INPUT_DIR ($(wc -c < "$OUTPUT_PATH") bytes)"