ramdisk: the live root grows into half of memory

The live root was a fixed 96 MB image in the kernel: 60 MB of zeros on
the ISO, twice, and room for 60 MB of packages whatever the machine had.

Now the image is only what the root holds, plus slack, measured by
building it once large. Past its end the ramdisk goes on to half of the
memory free at mount, as pages taken the first time each block is
written; an unwritten block reads as zeros and costs nothing. SchFS
keeps no free-space map on disk, so growing a volume is a larger total
before open builds the map: grow_to, set for the ramdisk root.

8 GB machine: a 4 GB root, Xorg installed into it in 77 s with 7.6 GB
still free. The ISO is 172 MB instead of 344.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01S1giNvrHTrPMPw7V6x1kQ4
This commit is contained in:
Nexoniarz 2026-10-24 17:20:43 +02:00
commit d0941780f5
7 changed files with 120 additions and 13 deletions

View file

@ -461,12 +461,14 @@ CFLAGS_SYS = $(CFLAGS) -I./kernel/sys
# loads that by PIO one sector at a time -- a kernel with a userland inside it
# takes visibly longer to boot than a 350 KB one.
#
# 24576 blocks of 4 KB is 96 MB: the apk root's base system is 35 MB, and the rest is
# room for `apk add` on the live system. The ramdisk is part of the kernel file
# and so of the ISO, empty space included; a system that installs a desktop
# wants a disk.
# Sized to fit, not fixed. The image is what the root uses plus RAMDISK_SLACK
# blocks and an eighth, measured by building it once large; the kernel then
# grows the volume into half of the free memory at boot (kernel/dev/ramdisk.c),
# which is where `apk add` on the live system writes. It was a fixed 96 MB,
# 60 of it zeros carried in the kernel and twice over on the ISO, and room for
# 60 MB of packages whatever the machine had.
# -----------------------------------------------------------------------
RAMDISK_BLOCKS ?= 24576
RAMDISK_SLACK ?= 1024
# RAMDISK_NINODES used to live here, sizing mkfs's preallocated inode table.
# The ramdisk is a SchFS image now (see $(BUILD)/fs-ram.img) and SchFS allocates
@ -825,7 +827,7 @@ $(BUILD)/kernel/kernel: $(BUILD)/kernel/kernel.pass1 $(BUILD)/kernel/ksym.o
$(OBJDUMP) -S $@ > $(BUILD)/kernel/kernel.asm
# The ramdisk root image, and the object wrapping it for the linker. Built from
# the same staged sysroot as the main image, just capped at RAMDISK_BLOCKS --
# the same staged sysroot as the main image, sized to fit (see RAMDISK_SLACK) --
# mkfs's content dedup means the whole userland still fits in a few MB.
# Built with mkschfs, not mkfs.
#
@ -837,7 +839,12 @@ $(BUILD)/kernel/kernel: $(BUILD)/kernel/kernel.pass1 $(BUILD)/kernel/ksym.o
$(BUILD)/fs-ram.img: $(MKSCHFS) $(ROOT_STAMP)
@mkdir -p $(BUILD)
@rm -f $@
$(MKSCHFS) $@ --size $(RAMDISK_BLOCKS) --label schos \
@used=$$($(MKSCHFS) $@ --size 262144 --label schos \
$(RAMDISK_EXCLUDE_ARGS) --sysroot $(ROOT_TREE) | \
sed -n 's/^ *\([0-9][0-9]*\) of [0-9][0-9]* blocks used.*/\1/p'); \
test -n "$$used" || { echo "fs-ram.img: mkschfs did not say how much it used" >&2; exit 1; }; \
rm -f $@; \
$(MKSCHFS) $@ --size $$((used + used / 8 + $(RAMDISK_SLACK))) --label schos \
$(RAMDISK_EXCLUDE_ARGS) --sysroot $(ROOT_TREE)
$(BUILD)/kernel/ramdisk_obj: $(BUILD)/fs-ram.img