[PATCH 2/2] init: allow mounting arbitrary non-blockdevice filesystems as root

From: Christoph Hellwig
Date: Thu Jun 17 2021 - 11:37:55 EST


Currently the only non-blockdevice filesystems that can be used as the
initial root filesystem are NFS and CIFS, which use the magic
"root=/dev/nfs" and "root=/dev/cifs" syntax that requires the root
device file system details to come from filesystem specific kernel
command line options.

Add a little bit of new code that allows to just pass arbitrary
string mount options to any non-blockdevice filesystems so that it can
be mounted as the root file system.

For example a virtiofs root file system can be mounted using the
following syntax:

"root=myfs rootfstype=virtiofs rw"

Based on an earlier patch from Vivek Goyal <vgoyal@xxxxxxxxxx>.

Signed-off-by: Christoph Hellwig <hch@xxxxxx>
---
init/do_mounts.c | 40 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 40 insertions(+)

diff --git a/init/do_mounts.c b/init/do_mounts.c
index ec32de3ad52b..64c60cb72ecb 100644
--- a/init/do_mounts.c
+++ b/init/do_mounts.c
@@ -534,6 +534,44 @@ static int __init mount_cifs_root(void)
}
#endif

+static int __init try_mount_nodev(char *fstype)
+{
+ struct file_system_type *fs = get_fs_type(fstype);
+ int err = -EINVAL;
+
+ if (!fs)
+ return -EINVAL;
+ if (!(fs->fs_flags & (FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA)))
+ err = do_mount_root(root_device_name, fstype, root_mountflags,
+ root_mount_data);
+ put_filesystem(fs);
+
+ if (err != -EACCES && err != -EINVAL)
+ panic("VFS: Unable to mount root \"%s\" (%s), err=%d\n",
+ root_device_name, fstype, err);
+ return err;
+}
+
+static int __init mount_nodev_root(void)
+{
+ char *fs_names, *p;
+ int err = -EINVAL;
+
+ fs_names = (void *)__get_free_page(GFP_KERNEL);
+ if (!fs_names)
+ return -EINVAL;
+ split_fs_names(fs_names, root_fs_names);
+
+ for (p = fs_names; *p; p += strlen(p) + 1) {
+ err = try_mount_nodev(p);
+ if (!err)
+ break;
+ }
+
+ free_page((unsigned long)fs_names);
+ return err;
+}
+
void __init mount_root(void)
{
#ifdef CONFIG_ROOT_NFS
@@ -550,6 +588,8 @@ void __init mount_root(void)
return;
}
#endif
+ if (ROOT_DEV == 0 && mount_nodev_root() == 0)
+ return;
#ifdef CONFIG_BLOCK
{
int err = create_dev("/dev/root", ROOT_DEV);
--
2.30.2