Skip to content

Commit 649cb20

Browse files
committed
docs: mention nullfs
Add a section about nullfs and how it enables pivot_root() to work. Link: https://patch.msgid.link/20260112-work-immutable-rootfs-v2-4-88dd1c34a204@kernel.org Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent 576ee5d commit 649cb20

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

Documentation/filesystems/ramfs-rootfs-initramfs.rst

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,15 @@ What is rootfs?
7676
---------------
7777

7878
Rootfs is a special instance of ramfs (or tmpfs, if that's enabled), which is
79-
always present in 2.6 systems. You can't unmount rootfs for approximately the
80-
same reason you can't kill the init process; rather than having special code
81-
to check for and handle an empty list, it's smaller and simpler for the kernel
82-
to just make sure certain lists can't become empty.
79+
always present in 2.6 systems. Traditionally, you can't unmount rootfs for
80+
approximately the same reason you can't kill the init process; rather than
81+
having special code to check for and handle an empty list, it's smaller and
82+
simpler for the kernel to just make sure certain lists can't become empty.
83+
84+
However, if the kernel is booted with "nullfs_rootfs", an immutable empty
85+
filesystem called nullfs is used as the true root, with the mutable rootfs
86+
(tmpfs/ramfs) mounted on top of it. This allows pivot_root() and unmounting
87+
of the initramfs to work normally.
8388

8489
Most systems just mount another filesystem over rootfs and ignore it. The
8590
amount of space an empty instance of ramfs takes up is tiny.
@@ -121,17 +126,26 @@ All this differs from the old initrd in several ways:
121126
program. See the switch_root utility, below.)
122127

123128
- When switching another root device, initrd would pivot_root and then
124-
umount the ramdisk. But initramfs is rootfs: you can neither pivot_root
125-
rootfs, nor unmount it. Instead delete everything out of rootfs to
126-
free up the space (find -xdev / -exec rm '{}' ';'), overmount rootfs
127-
with the new root (cd /newmount; mount --move . /; chroot .), attach
128-
stdin/stdout/stderr to the new /dev/console, and exec the new init.
129+
umount the ramdisk. Traditionally, initramfs is rootfs: you can neither
130+
pivot_root rootfs, nor unmount it. Instead delete everything out of
131+
rootfs to free up the space (find -xdev / -exec rm '{}' ';'), overmount
132+
rootfs with the new root (cd /newmount; mount --move . /; chroot .),
133+
attach stdin/stdout/stderr to the new /dev/console, and exec the new init.
129134

130135
Since this is a remarkably persnickety process (and involves deleting
131136
commands before you can run them), the klibc package introduced a helper
132137
program (utils/run_init.c) to do all this for you. Most other packages
133138
(such as busybox) have named this command "switch_root".
134139

140+
However, if the kernel is booted with "nullfs_rootfs", pivot_root() works
141+
normally from the initramfs. Userspace can simply do::
142+
143+
chdir(new_root);
144+
pivot_root(".", ".");
145+
umount2(".", MNT_DETACH);
146+
147+
This is the preferred method when nullfs_rootfs is enabled.
148+
135149
Populating initramfs:
136150
---------------------
137151

0 commit comments

Comments
 (0)