[PATCH v2] Add /dev/fd and /dev/std*

Demi Marie Obenour
2025-09-18T19:45:57Z

This is nearly same as 14483e1a690c (img/app: add /dev/fd and /dev/std*),
but for the host and for vm/sys/net. The only difference is that the
symlinks are created in init, rather than in rc.init. This ensures that
nothing in the main system can run before they the links are created.

While only Spectrum-provided code should run in these VMs, third-party
dependencies are likely to only be tested in an environment where these
links are present. Since the BSDs also have these links, some code
might even use them on purpose.

Not having these links could cause severe bugs. For instance, if
/dev/stdout or /dev/stderr is missing, writing to them will create a
regular file instead. This could cause other code to misbehave.
Furthermore, /dev is a devtmpfs, so this could consume a lot of memory.

[1]: https://spectrum-os.org/lists/archives/spectrum-devel/87h5xdjo9p.fsf@alyssa.is/

Signed-off-by: Demi Marie Obenour <demiobenour@gmail.com>
---
Changes in v2:
- Do not switch to util-linux switch_root as Busybox switch_root is
good enough.
- Leave /proc and /sys in /etc/fstab as they will not be remounted if
already mounted.
- Link to v1: https://spectrum-os.org/lists/archives/spectrum-devel/20250914-util-linux-switch-root-v1-0-9cf9349cbca3@gmail.com
---
host/rootfs/image/etc/s6-linux-init/scripts/rc.init | 5 +++++
vm/sys/net/image/etc/s6-linux-init/scripts/rc.init | 5 +++++
2 files changed, 10 insertions(+)

diff --git a/host/rootfs/image/etc/s6-linux-init/scripts/rc.init b/host/rootfs/image/etc/s6-linux-init/scripts/rc.init
index 674fd38cc76837c7be25a5ef060f0f4d4b786394..46e5cd6d1b99c12fab84a2797b37d9f4eccaa56a 100755
--- a/host/rootfs/image/etc/s6-linux-init/scripts/rc.init
+++ b/host/rootfs/image/etc/s6-linux-init/scripts/rc.init
@@ -4,6 +4,11 @@

if { s6-rc-init -c /etc/s6-rc /run/service }

+if { ln -s /proc/self/fd /dev }
+if { ln -s /proc/self/fd/0 /dev/stdin }
+if { ln -s /proc/self/fd/1 /dev/stdout }
+if { ln -s /proc/self/fd/2 /dev/stderr }
+
if { mount --make-shared /run }
if { mount -a --mkdir }

diff --git a/vm/sys/net/image/etc/s6-linux-init/scripts/rc.init b/vm/sys/net/image/etc/s6-linux-init/scripts/rc.init
index 1016d0c62bc6103bc9e865a389f5d482ef6c2b76..4378400b0b34f12bfa6a994029546f9c38058d10 100755
--- a/vm/sys/net/image/etc/s6-linux-init/scripts/rc.init
+++ b/vm/sys/net/image/etc/s6-linux-init/scripts/rc.init
@@ -4,6 +4,11 @@

if { s6-rc-init -c /etc/s6-rc /run/service }

+if { ln -s /proc/self/fd /dev }
+if { ln -s /proc/self/fd/0 /dev/stdin }
+if { ln -s /proc/self/fd/1 /dev/stdout }
+if { ln -s /proc/self/fd/2 /dev/stderr }
+
if { mkdir -p /dev/pts /dev/shm }
if { mount -a }

---
base-commit: edd53bb5dd9b682fec744d6df7921b64e0f56565
change-id: 20250914-util-linux-switch-root-10034b149e45
--
Sincerely,
Demi Marie Obenour (she/her/hers)