Skip to contentSkip to conversation

rootmpfs

RFC Rob Landley 5 participants 8 messages Apr 3, 2013 – Apr 11, 2013

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Attached is my quick and dirty hack to make rootfs be tmpfs when CONFIG_TMPFS is enabled. It can't be this easy or somebody would have done it in the _eight_years_ since https://lkml.org/lkml/2006/7/31/145

Yes, it's got an #ifdef and out of place prototypes. Yes, it manually calls a module init function and compensates by making it reentrant. But it works, and when I "cat /dev/zero > filename" the filesystem fills _up_ instead of panicing the kernel.

So now that I've posted the error, would someone please tell me how I _should_ have done it?

Rob

P.S. If I actually change the filesystem type to a name other than "rootfs", it panics on the way up because various bits of the kernel are looking for that magic name. Sigh.

P.P.S. removing MS_NOUSER is actually intentional, there's a local cray patch that does the same thing because otherwise you can't --bind mount directories out of this filesystem, which is a thing they wanted to do.

#1 lore raw
Quoted 2 lines · Rob Landley

Attached is my quick and dirty hack to make rootfs be tmpfs when CONFIG_TMPFS is enabled.

For a somewhat quantum definition of "attached".

Rob

fs/ramfs/inode.c +8 -1
index c24f1e1..f875bf2 100644
@@ -244,10 +244,17 @@ struct dentry *ramfs_mount(struct file_system_type *fs_type,
 	return mount_nodev(fs_type, flags, data, ramfs_fill_super);
 }
 
-static struct dentry *rootfs_mount(struct file_system_type *fs_type,
+int shmem_init(void);
+int shmem_fill_super(struct super_block *sb, void *data, int silent);
+static __init struct dentry *rootfs_mount(struct file_system_type *fs_type,
 	int flags, const char *dev_name, void *data)
 {
+#ifdef CONFIG_TMPFS
+	if (shmem_init()) return NULL;
+	return mount_nodev(fs_type, flags, data, shmem_fill_super);
+#else
 	return mount_nodev(fs_type, flags|MS_NOUSER, data, ramfs_fill_super);
+#endif
 }
 
 static void ramfs_kill_sb(struct super_block *sb)
mm/shmem.c +2 -0
index 1c44af7..d2cb93e 100644
@@ -2785,6 +2785,8 @@ int __init shmem_init(void)
 {
 	int error;
 
+	if (shmem_inode_cachep) return 0;
+
 	error = bdi_init(&shmem_backing_dev_info);
 	if (error)
 		goto out4;
#2 lore raw
In reply to Rob Landley

Rob,

FWIW I have a patch to do something like this. It even gives you a rdsize=xxx tunable kernel parameter that lets you specify the size of the tmpfs, which acts like the -osize= mount flag (so phrases like 100M or 20% works). So doing things like 'cat /dev/zero > filename' will not run you out of all available memory. (Note: If you don't specify rdsize= on the kernel command line, it will not convert rootfs to tmpfs).

See attached.

-Byron

Quoted 12 lines · Rob Landley

Attached is my quick and dirty hack to make rootfs be tmpfs when CONFIG_TMPFS is enabled. It can't be this easy or somebody would have done it in the _eight_years_ since https://lkml.org/lkml/2006/7/31/145

Yes, it's got an #ifdef and out of place prototypes. Yes, it manually calls a module init function and compensates by making it reentrant. But it works, and when I "cat /dev/zero > filename" the filesystem fills _up_ instead of panicing the kernel.

So now that I've posted the error, would someone please tell me how I _should_ have done it?

Rob

Quoted 8 lines

P.S. If I actually change the filesystem type to a name other than "rootfs", it panics on the way up because various bits of the kernel are looking for that magic name. Sigh.

P.P.S. removing MS_NOUSER is actually intentional, there's a local cray patch that does the same thing because otherwise you can't --bind mount directories out of this filesystem, which is a thing they wanted to do.-- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

init/initramfs.c renamed from init/initramfs.c.orig +43 -1
@@ -569,14 +569,56 @@
 }
 #endif
 
+static char * __initdata ramdisk_size;
+
+static int __init rdsize_setup(char *str)
+{
+	ramdisk_size = str;
+	return 1;
+}
+__setup("rdsize=", rdsize_setup);
+
+static int __init change_root_to_tmpfs(void)
+{
+	char size[38], *s;
+
+	sprintf(size, "size=%.20s,nr_inodes=0", ramdisk_size);
+	if ((s = strchr(size, ',')))
+		*s = '\0';
+
+	if (!sys_mkdir("/root", 0700) &&
+	    !sys_mount("/dev/root", "/root", "tmpfs", 0, size) &&
+	    !sys_chdir("/root") &&
+	    !sys_mount(".", "/", NULL, MS_MOVE, NULL) &&
+	    !sys_chroot("."))
+	        return 0;
+
+	panic("Failed to mount tmpfs as root filesystem");
+}
+
+
 static int __init populate_rootfs(void)
 {
-	char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size);
+	char *err;
+
+	if (ramdisk_size)
+		change_root_to_tmpfs();
+
+	err = unpack_to_rootfs(__initramfs_start, __initramfs_size);
 	if (err)
 		panic(err);	/* Failed to decompress INTERNAL initramfs */
 	if (initrd_start) {
 #ifdef CONFIG_BLK_DEV_RAM
 		int fd;
+		if (ramdisk_size) {
+			printk(KERN_INFO "Unpacking initramfs...\n");
+			err = unpack_to_rootfs((char *)initrd_start,
+				initrd_end - initrd_start);
+			if (err)
+				panic(err);
+			free_initrd();
+			return 0;
+		}
 		printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
 		err = unpack_to_rootfs((char *)initrd_start,
 			initrd_end - initrd_start);
#3 lore raw
Quoted 8 lines · Byron Stanoszek

Rob,

FWIW I have a patch to do something like this. It even gives you a rdsize=xxx tunable kernel parameter that lets you specify the size of the tmpfs, which acts like the -osize= mount flag (so phrases like 100M or 20% works). So doing things like 'cat /dev/zero > filename' will not run you out of all available memory. (Note: If you don't specify rdsize= on the kernel command line, it will not convert rootfs to tmpfs).

In init/do_mounts.c the boot infrastructure already has kernel command line options "rootflags=" and "rootfstype=", so the logical thing to do is probably to hook those up to rootfs. (That way instead of special casing a new option we use the existing tmpfs option parsing.)

The default tmpfs size is 50%, which solves the "trivial to exhaust memory and panic a kernel running under rootfs" problem. Having one tmpfs also fixes the case that multiple tmpfs mounts (for /home and /var, for example,) have separate memory limits that don't coordinate with each other, so if /home can use 30% and /var can use 30%, that's 60% plus whatever rootfs is already using, so you can easily squeeze the kernel against the wall without meaning to. (Yes, you can make one tmpfs mount and --bind mount from there to elsewhere, I've seen that done. Having rootfs just _be_ tmpfs makes this much easier to track.)

Quoted 1 line

See attached.

You're not actually changing the type of rootfs, you're overmounting it with a second filesystem instance. (Mine hasn't got a "change", it just mounts it correctly the first time, and there's just one rootfs instance.)

What _is_ wrong with my version is that if you select tmpfs as a module bad things happen; it tries to use code that's not there. I dunno of an #ifdef that distinguishes between module and builtin, so I think I have to add another kconfig symbol...

I'll poke at it.

Rob

#4 lore raw
Quoted 12 lines · Rob Landley
Quoted 8 lines · Byron Stanoszek

Rob,

FWIW I have a patch to do something like this. It even gives you a rdsize=xxx tunable kernel parameter that lets you specify the size of the tmpfs, which acts like the -osize= mount flag (so phrases like 100M or 20% works). So doing things like 'cat /dev/zero > filename' will not run you out of all available memory. (Note: If you don't specify rdsize= on the kernel command line, it will not convert rootfs to tmpfs).

In init/do_mounts.c the boot infrastructure already has kernel command line options "rootflags=" and "rootfstype=", so the logical thing to do is probably to hook those up to rootfs. (That way instead of special casing a new option we use the existing tmpfs option parsing.)

Quoted 7 lines

The default tmpfs size is 50%, which solves the "trivial to exhaust memory and panic a kernel running under rootfs" problem. Having one tmpfs also fixes the case that multiple tmpfs mounts (for /home and /var, for example,) have separate memory limits that don't coordinate with each other, so if /home can use 30% and /var can use 30%, that's 60% plus whatever rootfs is already using, so you can easily squeeze the kernel against the wall without meaning to. (Yes, you can make one tmpfs mount and --bind mount from there to elsewhere, I've seen that done. Having rootfs just _be_ tmpfs makes this much easier to track.)

Quoted 1 line

See attached.

You're not actually changing the type of rootfs, you're overmounting it with a second filesystem instance. (Mine hasn't got a "change", it just mounts it correctly the first time, and there's just one rootfs instance.)

What _is_ wrong with my version is that if you select tmpfs as a module bad things happen; it tries to use code that's not there. I dunno of an #ifdef that distinguishes between module and builtin, so I think I have to add another kconfig symbol...

See include/linux/kconfig.h: IS_MODULE() and IS_BUILTIN().

Quoted 2 lines

I'll poke at it.

Signature · 1 line
~Randy
#6 lore raw
In reply to Rob Landley
Quoted 3 lines · Rob Landley

Attached is my quick and dirty hack to make rootfs be tmpfs when CONFIG_TMPFS is
enabled. It can't be this easy or somebody would have done it in the

I don't see anything attached, but...

I have been running using tmpfs as my root for years. I tweaked the debian initrd scripts to mount a tmpfs filesystem, cpio recover a compressed cpio image from the disk drive and then pivotroot. Did not require any kernel tweaks.

Thanks,
Robin

Quoted 27 lines

_eight_years_
since https://lkml.org/lkml/2006/7/31/145

Yes, it's got an #ifdef and out of place prototypes. Yes, it manually calls a module
init function and compensates by making it reentrant. But it works, and when I
"cat /dev/zero > filename" the filesystem fills _up_ instead of panicing the kernel.

So now that I've posted the error, would someone please tell me how I _should_ have done it?

Rob

P.S. If I actually change the filesystem type to a name other than "rootfs", it panics on the way up because various bits of the kernel are looking for that magic name. Sigh.

P.P.S. removing MS_NOUSER is actually intentional, there's a local cray patch that does the same thing because otherwise you can't --bind mount directories out of this filesystem, which is a thing they wanted to do.--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#5 lore raw
In reply to Randy Dunlap
Quoted 24 lines · Randy Dunlap
Quoted 4 lines · Rob Landley
Quoted 3 lines · Byron Stanoszek

Rob,

FWIW I have a patch to do something like this. It even gives you a

rdsize=xxx

Quoted 1 line
Quoted 1 line

tunable kernel parameter that lets you specify the size of the

tmpfs, which

Quoted 1 line
Quoted 1 line

acts like the -osize= mount flag (so phrases like 100M or 20%

works). So doing

Quoted 1 line
Quoted 1 line

things like 'cat /dev/zero > filename' will not run you out of all

available

Quoted 1 line
Quoted 1 line

memory. (Note: If you don't specify rdsize= on the kernel command

line, it will

Quoted 3 lines
Quoted 1 line

not convert rootfs to tmpfs).

In init/do_mounts.c the boot infrastructure already has kernel

command line options "rootflags=" and "rootfstype=", so the logical thing to do is probably to hook those up to rootfs. (That way instead of special casing a new option we use the existing tmpfs option parsing.)

Quoted 2 lines

The default tmpfs size is 50%, which solves the "trivial to exhaust

memory and panic a kernel running under rootfs" problem. Having one tmpfs also fixes the case that multiple tmpfs mounts (for /home and /var, for example,) have separate memory limits that don't coordinate with each other, so if /home can use 30% and /var can use 30%, that's 60% plus whatever rootfs is already using, so you can easily squeeze the kernel against the wall without meaning to. (Yes, you can make one tmpfs mount and --bind mount from there to elsewhere, I've seen that done. Having rootfs just _be_ tmpfs makes this much easier to track.)

Quoted 3 lines
Quoted 2 lines

See attached.

Quoted 7 lines
Quoted 1 line

You're not actually changing the type of rootfs, you're

overmounting it with a second filesystem instance. (Mine hasn't got a "change", it just mounts it correctly the first time, and there's just one rootfs instance.)

Quoted 2 lines

What _is_ wrong with my version is that if you select tmpfs as a

module bad things happen; it tries to use code that's not there. I dunno of an #ifdef that distinguishes between module and builtin, so I think I have to add another kconfig symbol...

See include/linux/kconfig.h: IS_MODULE() and IS_BUILTIN().

Good to know, thanks.

(It turns out I was looking at a distro kernel directory and vanilla only lets TMPFS be static anyway, but I should still use that in case it changes, and I think I still need to wire up a rootfsflags argument.)

Rob

#7 lore raw
In reply to Rob Landley
Quoted 5 lines · Rob Landley <rob <at> landley.net> writes:

Attached is my quick and dirty hack to make rootfs be tmpfs when CONFIG_TMPFS is
enabled. It can't be this easy or somebody would have done it in the _eight_years_
since https://lkml.org/lkml/2006/7/31/145

Hi Rob,

We've been using a variation of the 2006 patch in TinyCore for years. It's a fairly essential piece for us, as using it can save several seconds of boot time for us.

Since we run in the initramfs, to get size limits and swappability, we used to tar the files and switch_root. Both unnecessary steps when the kernel could directly unpack to tmpfs.

So consider this a +1 "we use this".

- Lauri