Skip to the text
KDOS
Menu
kdos.dev KDOS from Scratch KDOS 0.2 · 4dd4a10 · 2026-11-30 Contents Index

399. Live persistence

A live KDOS session is the system running straight from the USB stick or disc it was written to, before anything is installed. Everything you change in it, from a file in your home directory to a package you install, goes into the upper, writable layer of an overlay on top of the read-only system image. That layer is held in RAM and is lost at power-off, unless the machine has a persistence store: a filesystem labelled KDOS_PERSIST (kdos persist create makes it ext4) that holds the layer instead, so the changes survive a reboot.

This page is for anyone who runs KDOS live and wants to keep what they change. It covers making a store, checking that the session is using it, booting past it, undoing what it holds and removing it. How the initramfs builds the overlay and what it requires of the store is explained in The live medium and persistence; this page gives the procedures.

Installation

Persistence is part of the live medium and needs nothing installed. Three pieces take part:

Piece From Does
kdos persist kdos-tools Reports the store, makes one with sfdisk, partx, losetup and blkid from util-linux and mkfs.ext4 from e2fsprogs, and resets one
The live path of the initramfs init script/phases/70_image/090_initramfs.sh Finds the store by its label at boot and uses it as the overlay’s upper layer
KDOS Live (clean session) script/phases/70_image/110_iso.sh A menu entry that passes nopersist and ignores the store for one boot

There is no service script. An installed machine has no overlay and never looks for a store: the initramfs reads the label only on the live path, when the command line names no root=UUID=.

Configuration

The store is configured by its label and nothing else. No file names its device, and nothing has to be edited for a store to be used: at boot the initramfs asks blkid for a filesystem labelled KDOS_PERSIST and uses whichever answers. The decision, from the generated init (\$ here is the generator’s escaping: the init is written by a heredoc, and the generated script has a plain $):

             # THE UPPER IS WHERE A LIVE SESSION'S WRITES LAND, and whether it
             # survives a power-off is the whole of what persistence means. A
             # filesystem labelled KDOS_PERSIST is used as the upper when one
             # is present; with no store the upper is a tmpfs and the session
             # is gone at reboot. \`kdos persist\` makes the store, and
             # find_persist waits for USB storage before it gives up on one.
             #
             # \`nopersist\` ON THE COMMAND LINE FORCES A CLEAN SESSION, and the
             # boot menu carries an entry that passes it. A store holding a
             # change that stops the desktop coming up would otherwise be
             # reachable only by taking the stick to another machine. From
             # that session \`kdos persist reset\` deletes what the store holds.
             #
             # THE STORE MUST CARRY XATTRS, HARDLINKS AND A d_type, so ntfs,
             # iso9660 and squashfs cannot hold one: overlayfs refuses such an
             # upper with EINVAL, which is the same answer it gives for every
             # other bad mount. Refusing them BY NAME here is what makes a
             # hand-made store say what is wrong with it. vfat and exfat stay
             # in the list though neither can carry the 12-character label.
             # \`kdos persist\` reports a store of these types as refused, from
             # its own copy of this list (refused_type in persist.c), which
             # must match this one.
             PERSIST=""
             case " \$(cat /proc/cmdline) " in
             *" nopersist "*)
                 echo "nopersist: this session will not be saved" ;;
             *)
                 find_persist ;;
             esac

             UPPER=""
             if [ -n "\$PERSIST" ]; then
                 PTYPE=\$(blkid -o value -s TYPE "\$PERSIST" 2>/dev/null)
                 case "\$PTYPE" in
                 vfat|exfat|ntfs|iso9660|squashfs)
                     echo "persistence store \$PERSIST is \$PTYPE, which cannot hold an overlay upper"
                     ;;
                 *)
                     mkdir -p /mnt/persist
                     if ! persist_dev "\$PERSIST" "\$MEDIUM"; then
                         echo "persistence store \$PERSIST: no loop device over \$MEDIUM"
                     elif mount "\$PERSIST_DEV" /mnt/persist; then
                         mkdir -p /mnt/persist/upper /mnt/persist/work
                         UPPER="upperdir=/mnt/persist/upper,workdir=/mnt/persist/work"
                         echo "Persistent session on \$PERSIST (\$PTYPE\${PERSIST_LOOP:+, through \$PERSIST_LOOP})"
                     else
                         echo "persistence store \$PERSIST would not mount"
                         [ -n "\$PERSIST_LOOP" ] && losetup -d "\$PERSIST_LOOP"
                     fi
                     ;;
                 esac
             fi

             # A STORE THAT DOES NOT WORK MUST NOT COST THE BOOT. Everything
             # above can fail on a medium somebody else wrote, and a stick that
             # drops to a shell because its persistence is broken is worse than
             # one that quietly forgets. Every failure lands here, and the
             # session comes up exactly as it would with no store at all.
             if [ -n "\$UPPER" ]; then
                 echo "Mounting OverlayFS (persistent)..."
                 mount -t overlay overlay -o lowerdir=/mnt/system,\$UPPER /newroot || UPPER=""
                 if [ -z "\$UPPER" ]; then
                     umount /mnt/persist 2>/dev/null
                     [ -n "\$PERSIST_LOOP" ] && losetup -d "\$PERSIST_LOOP"
                 fi
             fi
             if [ -z "\$UPPER" ]; then
                 echo "Mounting OverlayFS..."
                 mkdir -p /mnt/overlay
                 mount -t tmpfs tmpfs /mnt/overlay
                 mkdir -p /mnt/overlay/upper /mnt/overlay/work
                 mount -t overlay overlay -o lowerdir=/mnt/system,upperdir=/mnt/overlay/upper,workdir=/mnt/overlay/work /newroot
             fi

script/phases/70_image/090_initramfs.sh, lines 1435–1507

find_persist is the lookup. It asks for the label again every tenth of a second while USB storage is still arriving, so a store on a stick that is enumerated after the medium is found is still used:

#
# THE PERSISTENCE STORE IS LOOKED FOR UNTIL USB STORAGE HAS SETTLED. The
# medium is found the moment its own device answers, and a store on another
# USB stick is still being enumerated then: behind a hub it gets an address a
# few hundred milliseconds later, and usb-storage waits a further delay_use
# (one second) before it scans the disk. A single lookup at that point finds
# nothing and the session runs in RAM with the store attached.
#
# USB IS BUSY while a hub port holds a device that is not yet configured, or
# a mass-storage interface has no block device beneath it. Neither state is
# visible for the moment between a hub being configured and its first port
# reporting a connection (power-good plus debounce, well under a second), so
# while a hub other than a root hub is present the lookup also waits until
# the set of USB devices has stood still for one second. With nothing busy
# and no such hub, a boot with no store pays one blkid and no wait. The bound
# is 10 seconds, the bound the medium and the root device get: a device that
# never finishes costs that much and no more, and the boot goes on in RAM.
#
# A NEW DISK GETS ONE MORE LOOKUP. Its block device appears in sysfs before
# the kernel has read its partition table, so a blkid that starts then can
# list the disk without the partition holding the store. blkid opens every
# disk it lists, and the open waits for that read, so the lookup after it
# sees the partitions: the wait ends only after a lookup whose scan counted
# the same USB disks as the scan before the previous one. That second lookup
# runs at once, with no sleep.
: "\${USB_DEVICES:=/sys/bus/usb/devices}"
uptime_cs() {
    local u
    read -r u _ < /proc/uptime
    NOW=\$(( 10#\${u%.*}\${u#*.} ))
}
usb_scan() {
    local p s d found
    USB_BUSY=""
    USB_SEEN=""
    USB_DISKS=""
    USB_HUB=0
    for p in "\$USB_DEVICES"/*:*/*-port*/state; do
        [ -r "\$p" ] || continue
        read -r s < "\$p"
        case "\$s" in
        "not attached"|configured|suspended) ;;
        *) USB_BUSY="\$USB_BUSY \${p%/state}" ;;
        esac
    done
    for d in "\$USB_DEVICES"/*; do
        [ -e "\$d" ] || continue
        case "\${d##*/}" in
        *:*)
            [ -r "\$d/bInterfaceClass" ] || continue
            read -r s < "\$d/bInterfaceClass"
            [ "\$s" == "08" ] || continue
            found=0
            for p in "\$d"/host*/target*/*/block/*; do
                [ -e "\$p" ] || continue
                found=1
                USB_DISKS="\$USB_DISKS \${p##*/}"
            done
            [ "\$found" == "1" ] || USB_BUSY="\$USB_BUSY \$d"
            ;;
        usb*)
            USB_SEEN="\$USB_SEEN \${d##*/}"
            ;;
        *)
            USB_SEEN="\$USB_SEEN \${d##*/}"
            [ -r "\$d/bDeviceClass" ] || continue
            read -r s < "\$d/bDeviceClass"
            [ "\$s" == "09" ] && USB_HUB=1
            ;;
        esac
    done
}
find_persist() {
    local deadline quiet last=x disks="" said=0
    PERSIST=""
    uptime_cs
    deadline=\$(( NOW + 1000 ))
    while :; do
        usb_scan
        PERSIST=\$(blkid -L KDOS_PERSIST 2>/dev/null)
        [ -n "\$PERSIST" ] && return 0
        uptime_cs
        if [ "\$USB_SEEN" != "\$last" ]; then
            last="\$USB_SEEN"
            quiet=\$NOW
            [ "\$USB_HUB" == "1" ] && quiet=\$(( NOW + 100 ))
        fi
        if [ -z "\$USB_BUSY" ] && [ "\$NOW" -ge "\$quiet" ]; then
            [ "\$USB_DISKS" == "\$disks" ] && return 1
            disks="\$USB_DISKS"
            continue
        fi
        disks="\$USB_DISKS"
        if [ "\$NOW" -ge "\$deadline" ]; then
            echo "USB storage still settling after 10s; no persistence store found"
            return 1
        fi
        if [ "\$said" == "0" ]; then
            echo "Waiting for USB devices to settle before looking for a persistence store..."
            said=1
        fi
        sleep 0.1
    done
}

script/phases/70_image/090_initramfs.sh, lines 1125–1228

A store on the disk the medium was found on is mounted through a loop device at the partition’s offset, because the medium’s whole-disk mount keeps the kernel from opening any partition on that disk; persist_dev decides:

# THE STORE ON THE BOOT DISK IS MOUNTED THROUGH A LOOP. The medium is mounted
# as the whole disk (ISO9660 starts at its first sector, so no partition
# carries it), and a mounted disk is claimed exclusively: the kernel then
# refuses to open any partition on it for a mount ("Can't open blockdev"). A
# loop over the disk at the partition's offset and length opens the disk with
# no such claim. Direct I/O keeps the store's writes out of a second page
# cache, so the shutdown's sync and read-only remount reach the stick. A store
# on any other disk is mounted as it is. Sets PERSIST_DEV to the device to
# mount and PERSIST_LOOP to the loop when it made one; sysfs counts start and
# size in 512-byte sectors whatever the disk's block size.
persist_dev() {   # <store partition> <medium device>
    local part=\${1##*/} disk off len
    PERSIST_DEV="\$1"
    PERSIST_LOOP=""
    disk=\$(cd -P "/sys/class/block/\$part/.." 2>/dev/null && pwd -P)
    disk=\${disk##*/}
    [ -e "/sys/class/block/\$part/partition" ] || return 0
    [ "/dev/\$disk" == "\$2" ] || return 0
    off=\$(( \$(cat "/sys/class/block/\$part/start") * 512 ))
    len=\$(( \$(cat "/sys/class/block/\$part/size") * 512 ))
    PERSIST_LOOP=\$(losetup --find --show --direct-io=on -o "\$off" --sizelimit "\$len" "\$2") || return 1
    PERSIST_DEV="\$PERSIST_LOOP"
}

script/phases/70_image/090_initramfs.sh, lines 1230–1252

The store’s mount is moved into the new root as /mnt/persist, so the running session can see it and the shutdown can remount it read-only by that name before it reboots:

            # THE PERSISTENCE STORE MOVES FOR THE SAME REASON AND ONE MORE.
            # Left in the initramfs namespace it is unreachable by name, so
            # \`kdos persist\` could not report how full the store is that the
            # session is writing to. It also has to be a mount the SHUTDOWN can
            # name: /etc/inittab remounts /mnt/persist read-only, and a store
            # not in the new root's table would stay writable at reboot.
            if [ -n "\$UPPER" ]; then
                mkdir -p /newroot/mnt/persist
                mount --move /mnt/persist /newroot/mnt/persist
            fi

script/phases/70_image/090_initramfs.sh, lines 1574–1583

On the store itself, the overlay uses two directories, upper/ (the session’s files) and work/ (overlayfs’s own), which the initramfs creates when they are missing.

kdos persist create makes the filesystem with no reserved blocks and the label, through a loop device when the store is on the boot disk:

	printf("making ext4 on %s%s%s...\n", part, loop ? " through " : "",
	       loop ? node : "");
	KbArgv mk = { 0 };
	kb_argv_add(&mk, "mkfs.ext4");
	kb_argv_add(&mk, "-q");
	/* -F: the partition was made a moment ago, so whatever an older
	 * signature in its sectors claims is not somebody's data, and with no
	 * terminal mke2fs would read its question's answer as no. */
	kb_argv_add(&mk, "-F");
	/* No reserved blocks: this is not a root filesystem and five per cent
	 * of a stick is a lot to hold back from the only thing using it. */
	kb_argv_add(&mk, "-m");
	kb_argv_add(&mk, "0");
	kb_argv_add(&mk, "-L");
	kb_argv_add(&mk, PERSIST_LABEL);
	kb_argv_add(&mk, node);
	kb_argv_end(&mk);
	int mkrc = kb_run(&mk);
	sync();
	close_node(node, loop);
	if (mkrc != 0) {
		fprintf(stderr, "kdos persist: mkfs.ext4 failed on %s\n", part);
		return 1;
	}

src/system/kdos-tools/persist.c, lines 969–992

The menu entry that skips the store:

/KDOS Live (clean session)
    comment: Ignore the persistence store and start fresh
    protocol: linux
    path: boot():/boot/vmlinuz
    module_path: boot():/boot/initrd.img
    cmdline: root=/dev/ram0 rw console=tty0 console=ttyS0,115200n8 quiet loglevel=3 nopersist kdos.medium=$MEDIUM_ID

script/phases/70_image/110_iso.sh, lines 399–404

Usage

Making a store on the boot stick

  1. Boot KDOS Live.

  2. See what is there: kdos persist. With no store it prints no persistence store, followed by a reminder that the session’s writes are in RAM and that kdos persist create makes a store.

  3. Make the store in the free space after the image:

    sudo kdos persist create

    It names the disk, how many partitions it has and the filesystem, and asks you to type yes:

      disk       /dev/sda  (24G)
      partitions 3 now; the store is added after the last
      filesystem ext4, labelled KDOS_PERSIST
    
      Nothing already on /dev/sda is moved or rewritten: the store is
      made from the free space AFTER the last partition.
    
    Type yes to go ahead:

    Any other answer ends it with kdos persist: not confirmed and exit status 1; --yes (or -y) skips the question. It appends one partition with sfdisk --append and never rewrites the existing entries, registers it with partx -a, makes ext4 on it through a loop device (making ext4 on /dev/sda4 through /dev/loop1...), and ends with store ready on /dev/sda4 (5.0G). and It becomes the session's upper layer at the next boot.

  4. Run kdos persist. Until the next boot it reports state present, NOT in use by this session.

  5. Reboot. The store is used from the next boot, not the one that made it.

  6. Run kdos persist again. It reports state in use by this session and the free space, as free 4.8G of 4.8G. findmnt /mnt/persist shows the loop device the store is mounted through (kdos persist names the partition behind it), and / is an overlay whose upperdir is /mnt/persist/upper. Files written by root and by kdos in the session are there after the next reboot.

With no device named, create picks the disk the live system booted from. A machine that booted the image from an optical drive, as a virtual machine with the ISO attached does, has /dev/sr0 there: kdos persist says the boot medium is a disc and cannot hold a store, and create refuses it: kdos persist: /dev/sr0 is read-only (a disc) — name a USB stick or disk. A Ventoy stick runs the image as a file inside its own partition, through /dev/mapper/ventoy; booted under UEFI the medium is that mapping, and under BIOS it is the stick’s second-partition node, which Ventoy points at the same mapping. Under both firmwares the report says a Ventoy stick cannot take a KDOS store, and create refuses the medium, and the physical stick under it, with the same words. Name a writable disk instead, as below. The Welcome tour’s page offers each writable USB stick that has nothing mounted and is not the Ventoy stick.

Making a store on another device

The store can be on a second USB stick or on an internal disk; the label is what the boot looks for, wherever it is.

  1. Make it with kdos persist, naming the disk: sudo kdos persist create /dev/sdb. The new partition goes in the free space after that disk’s last partition; a disk with no partition table is given a GPT holding only the store (partitions none; a GPT is written holding the store). A disk formatted whole, with no partition table, is refused with its type named.
  2. Or make one by hand on a partition you have already made: sudo mkfs.ext4 -L KDOS_PERSIST /dev/sdb1. Any Linux filesystem with extended attributes, hard links and file types in directory entries will do; vfat, exfat, ntfs, iso9660 and squashfs are refused at boot. A FAT or exFAT label holds at most 11 characters, so mkfs.vfat and mkfs.exfat both fail on KDOS_PERSIST before such a store can exist, mkfs.vfat with an error about the label’s length; an NTFS volume takes the label, and the boot refuses it.
  3. Reboot with that device attached.

The initramfs asks for the label as soon as it has mounted the system image from the medium, and keeps asking while USB storage is still arriving: while a hub port holds a device that is not yet configured, or a USB mass-storage interface has no disk beneath it. A new stick has no disk for about a second after it is enumerated, because usb-storage waits that long before it scans it. While a hub other than a root hub is attached, the initramfs also waits until the set of USB devices has stood still for one second, since a hub shows nothing in the moment before it reports what is plugged into it. The console shows Waiting for USB devices to settle before looking for a persistence store... while it waits. The wait ends as soon as the store answers, and after 10 seconds at most, with USB storage still settling after 10s; no persistence store found, after which the session runs in RAM. A boot with no USB storage pending and no external hub does not wait. It asks once, and asks again straight away whenever the set of USB disks has changed since the ask before, because the kernel creates a disk before it has read the disk’s partitions.

Keep one store attached. With two filesystems labelled KDOS_PERSIST, blkid -L answers with one of them, and which one is not settled; kdos persist lists every one it finds after also.

Booting without the store

  1. Stop the boot menu’s countdown.
  2. Choose KDOS Live (clean session).

The command line gains nopersist, the console shows nopersist: this session will not be saved and Mounting OverlayFS..., and the session runs in RAM, its upper layer on a tmpfs, as with no store. The store is left exactly as it was, and kdos persist reports it as present, NOT in use by this session. This is the way past a change saved in the store that stops the desktop from starting.

Undoing what the store holds

  1. Boot KDOS Live (clean session). kdos persist reset refuses while the session is writing to the store (… is this session's upper layer and cannot be changed under it).

  2. Delete the saved changes, all of them or one part:

    sudo kdos persist reset            # everything in upper/
    sudo kdos persist reset --home     # upper/home only
    sudo kdos persist reset --system   # everything in upper/ but home/

    It names the store and what it removes, and asks you to type yes; --yes skips the question. It mounts the store on a private directory under /run (through a loop device when the store is on the boot stick), empties what was asked and the overlay’s work/, unmounts it and syncs, and ends with reset /dev/sda4. The next persistent boot starts from it.

  3. Reboot normally. Deleting a whiteout from upper/ brings back the image’s file it hid, so a reset system part is the system as the image shipped it.

Removing the store

  1. Boot KDOS Live (clean session), so that the session is not writing to the store.
  2. Find the store’s device with sudo blkid -L KDOS_PERSIST, for example /dev/sdb2.
  3. Take the label off: sudo e2label /dev/sdb2 "" for an ext store. kdos persist then prints no persistence store, and the next boot shows Mounting OverlayFS... with no (persistent) and runs in RAM.
  4. To free the space as well, delete the partition: sudo sfdisk --delete /dev/sdb 2, naming the disk and the partition’s number.

Tips

Troubleshooting

kdos persist says refused: ntfs cannot hold an overlay upper. The store’s filesystem is one the boot refuses by name. Remake it as ext4 (sudo kdos persist create after taking the label off it, or mkfs.ext4 -L KDOS_PERSIST on the partition).

kdos persist says present, NOT in use by this session. The store was made during this boot, the boot was a clean session (the report says so), or the store is on a device that took more than 10 seconds to appear, which the console reports as USB storage still settling after 10s; no persistence store found (see Making a store on another device). Reboot with KDOS Live; for the last case, put the store on the boot stick, on an internal disk, or on a port that is not behind a slow hub.

The store is there, but every boot starts clean. The initramfs prints why on the console. The KDOS Live entries send it to the serial line, so to read it on the screen boot KDOS Live (verbose). The messages are persistence store /dev/vda1 is ntfs, which cannot hold an overlay upper, persistence store … would not mount, or nopersist: this session will not be saved. Check the type with sudo blkid -L KDOS_PERSIST and sudo blkid <device>, and remake a store of the wrong type as ext4. When the overlay itself refuses the store, the session falls back to RAM, and the console shows Mounting OverlayFS (persistent)... followed by Mounting OverlayFS....

kdos persist create says sfdisk would not add a partition. The lines after it are sfdisk’s own reason; the usual one is no free space after the last partition on that disk, or an MBR whose four primary entries are taken. Name another device, or make a partition by hand and label it.

kdos persist create says the new partition did not appear. The partition was added to the table, but the kernel has no device node for it yet, and no filesystem was made. The message names the disk and tells you to reboot and then format the new partition as kdos persist create would have:

  1. Reboot.
  2. Find the new partition, the last one on that disk: lsblk /dev/sdX.
  3. Format it: sudo mkfs.ext4 -m 0 -L KDOS_PERSIST /dev/sdX<n>.

Do not run kdos persist create again instead: it would append a second partition, because the first has no label for it to find.

The desktop does not start after a change. Boot KDOS Live (clean session), then run sudo kdos persist reset --home (a change to your settings) or --system (a change to the system), as in Undoing what the store holds.

Writes fail with “No space left on device”. The store is full. kdos persist shows the free space; reset it or make a larger one on another device.

Known gaps

See also