0

I found this simple example of an initramfs from the Gentoo Wiki:

#!/bin/busybox sh

# Mount the /proc and /sys filesystems.
mount -t proc none /proc
mount -t sysfs none /sys

# Do your stuff here.
echo "This script just mounts and boots the rootfs, nothing else!"

# Mount the root filesystem.
mount -o ro /dev/sda1 /mnt/root

# Clean up.
umount /proc
umount /sys

# Boot the real thing.
exec switch_root /mnt/root /sbin/init

It mounts proc to /proc and sysfs to /sys. At the end, before switch_root, it unmounts them. However, in man switch_root I read:

switch_root moves already mounted /proc, /dev, /sys and /run to newroot and makes newroot the new root filesystem and starts init process

  • Why does this initramfs example unmount proc and sys if switch_root is supposed to move them?
  • sysfs is mounted to /sys; where did sysfs come from and why is it named different?
  • I have seen an example with /dev, so how do I know which directories to mount?
0

Why does this initramfs example unmount proc and sys if switch_root is supposed to move them?

The man page you read documents switch_root as provided by util-linux. As you said, this command moves some virtual filesystems to the new root. However, the script you provided has the shebang #!/bin/busybox sh, which means it uses BusyBox. BusyBox provides its own switch_root, which doesn't move these filesystems. The purpose of BusyBox is to provide many packages in one, so the script most likely uses the BusyBox version of switch_root.

sysfs is mounted to /sys; where did sysfs come from and why is it named different?

The mount commands in this script use mount -t [type] [device] [dir]:

  • cat /proc/filesystems lists [type].
  • If cat /proc/filesystems lists a filesystem with nodev, then it is a virtual filesystem, and [device] is arbitrary (customarily none).
  • [dir] is the mountpoint.

To see a filesystem's current mountpoint, run mount and use grep to find the filesystem. On my system, mount | grep sysfs shows sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime).

I have seen an example with /dev, so how do I know which directories to mount?

I don't know how to tell which virtual filesystems to mount, but I trust the Gentoo example. If you have more stuff to do in your initramfs (you probably do), then I recommend the following:

mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -t devtmpfs devtmpfs /dev
mkdir /dev/pts
mount -t devpts devpts /dev/pts
1
  • I'm pretty confident, but I'm answering my own question. Please share anything you know, especially if I'm wrong. – Not me Oct 20 '20 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.