1

First, I have create the directory that I will want to mount to.

mkdir /mnt/ramdisk

Now, I could easily turn this into a ramdisk using ramfs or tmpfs via

mount -t tmpfs -o size=512m tmpfs /mnt/ramdisk

I've found a tutorial on how to create a ramdisk which breaks this syntax down as:

mount -t [TYPE] -o size=[SIZE] [FSTYPE] [MOUNTPOINT]

The tutorial indicates that I can replace [FSTYPE] with ext4 to change the FS to ext4. However, I am not convinced this method is correct and that the author has misjudged what changing the [FSTYPE] argument actually does.

UPDATE: For those interested, G-Man and Johan Myréen have weighed in on my speculations about [FSTYPE]. Essentially, the [FSTYPE] argument acts as a necessary (but ignored) placeholder used by mount. See this post's comments for more details.

I would like to know the proper way to create an ext4 ramdisk. That is, I want a temporary directory in memory that uses the ext4 file system. How can this be achieved?

  • 1
    The RAM disk block device might be more appropriate... – Stephen Kitt Oct 29 '17 at 22:59
  • 1
    Good point!  It might not be clear (since James Coyle made no effort to explain it) that the command mount -t tmpfs -o size=512m tmpfs /mnt/ramdisk consists of five pieces: mount, -t tmpfs, -o size=512m, tmpfs and /mnt/ramdisk — the second tmpfs doesn’t go with the -o.  And the basic mount syntax is mount device  dir,  … (Cont’d) – G-Man Oct 29 '17 at 23:30
  • 1
    (Cont’d) …  so you’re right — the penultimate argument isn’t really [FSTYPE], but rather device (i.e., the analog of /dev/sda1).  And, according to How to correctly mount a tmpfs?, when you have -t tmpfs, the device is just a placeholder, and not interpreted (but, apparently, it is stored, and subsequently reported by df).  (I suspect the same is true for -t ramfs.)  So I agree with you that James Coyle’s tutorial is misleading. … (Cont’d) – G-Man Oct 29 '17 at 23:33
  • 1
    (Cont’d) …  Which makes me all the more confident that it’s impossible to specify a filesystem type for a RAM disk. … … … … … … … … … … … … … … … … … …  Additional interesting note: mount(8) says that -o size=512m is valid only with -t tmpfs, and is ignored for ramfs). – G-Man Oct 29 '17 at 23:35
  • 1
    The FSTYPE field must be present to get enough command parameters so that the mount program interprets we are doing a "manual" mount, i.e. it should not look up /mnt/ramdisk in /etc/fstab. It can be anything, and the value is ignored for tmpfs. – Johan Myréen Oct 30 '17 at 0:35
5
mkdir /mnt/ramdisk
mount -t ramfs ramfs /mnt/ramdisk
dd if=/dev/zero of=/mnt/ramdisk/ext4.image bs=1M count=512
mkfs.ext4 /mnt/ramdisk/ext4.image
mkdir /mnt/ext4ramdisk
mount -o loop /mnt/ramdisk/ext4.image /mnt/ext4ramdisk

But remember, ext4 was not designed to use in ram! tmpfs and ramfs are always better choice in ram than any disk-based filesystem.

  • I am aware that tmpfs and ramfs are better choices to be used in ram. Could you please clarify exactly what is happening the the last three lines of your answer? Also, if I were to use your solution, how can I verify that /mnt/ext4ramdisk is indeed using ext4? – buratino Oct 29 '17 at 21:46
  • 1
    The last lines create 1. a big file filled with zeroes on the ramfs, 2. creates an Ext4 filesystem using the file as storage, 3. creates the directory /mnt/ext4ramdisk outside the ramfs, 4. loop mounts the image file on /mnt/ext4ramdisk. – Johan Myréen Oct 30 '17 at 2:44
3

I have combined an idea given to me by Ipor Sircer's answer with Stephen Kitt's suggestion of using a RAM disk block device.

First, I compiled CONFIG_BLK_DEV_RAM into my kernel. I changed the default number of RAM disks from 16 to 8 (BLK_DEV_RAM_COUNT), though that is based on preference and not necessity.

Next, I created the folder I want to mount to.

mkdir /mnt/ext4ramdisk

Finally, I formatted my RAM disk block device with ext4 and mounted it.

mkfs.ext4 /dev/ram0
mount -t ext4 /dev/ram0 /mnt/ext4ramdisk

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.