30

Using Linux, when I boot I automatically have 16 16MB ramdisks, however, I would like to create one really large ramdisk to test some software.

I found that I can adjust the size of the ramdisks already on the system with the kernel boot parameter ramdisk_size however, this makes all 16 ramdisks (/dev/ram0 - /dev/ram15) the size that is specified. So if I want to create a 1GB ramdisk, I would need 16GB of memory.

Basically, I want to create one 10GB ramdisk which would be /dev/ram0. How would I go about doing that? I assume there is a kernel boot parameter, but I just haven't found it.

7 Answers 7

21

Kernel compile time

There are two kernel configuration options that you can set in your .config file:

CONFIG_BLK_DEV_RAM_COUNT=1
CONFIG_BLK_DEV_RAM_SIZE=10485760

This configured my kernel to create one ramdisk that is 10G at boot time.

Notes:

  • CONFIG_BLK_DEV_RAM_SIZE is in KB.
  • Don't specify more memory than you actually have RAM in your computer.
  • In menuconfig look under Device Drivers->Block Devices.

Boot time

You can specify the size of the ram disks you create via the kernel boot parameter ramdisk_size. For example:

kernel /vmlinuz-2.6.32.24 ro root=LABEL=/ rhgb quiet ramdisk_size=10485760

Now I can boot my machine and make a file system on it, mount it and use it exactly like a block device.

# mkfs.xfs /dev/ram0
# mount /dev/ram0 /mnt/ramdisk

Sources:

  1. http://www.vanemery.com/Linux/Ramdisk/ramdisk.html [dead]
  2. https://www.kernel.org/doc/Documentation/blockdev/ramdisk.txt
16

You should use tmpfs for that instead.

mount -t tmpfs -o size=10g none /mnt/point
5
  • Can I make a filesystem on a tmpfs? I need a block device that resides in memory that I can make a filesystem on. Can I do this on a tmpfs?
    – Kevin S.
    Mar 9, 2011 at 13:31
  • tmpfs is a filesystem. It just happens to reside in memory. Mar 9, 2011 at 17:07
  • 3
    I appreciate the answer and normally a tmpfs would do the trick, but I need to create an xfs filesystem on the part of memory I am writing to.
    – Kevin S.
    Mar 10, 2011 at 22:12
  • The problem with tmpfs is that it is backed by swap. If you start using swap, then you are losing the benefits of being in pure RAM. Also, some people prefer to have an area that isn't backed for crypto applications.
    – drudru
    Aug 11, 2013 at 22:27
  • Related answers and details are here: unix.stackexchange.com/questions/66329/…
    – jocull
    Oct 1, 2016 at 15:04
8

To make a large ram disk after boot without messing around with kernel parameters. Use tmpfs, make a file, mount it via loop, and mount that via a filesystem:

mount -t tmpfs -o size=200M tmpfs temp/
cd temp/
dd if=/dev/zero of=disk.img bs=1M count=199
losetup /dev/loop0 disk.img
mkfs.ext4 /dev/loop0
cd ..
mount /dev/loop0 temp2/

Probably a bit of performance penalty going through multiple different layers... but at least it works.

6

Another option is to use the loop devices (as opposed to the loobpack feature of mount as previously mentioned):

dd if=dev/zero of=myfs.img bs=1M count=1024
losetup /dev/loop0 myfs.img
mkfs.xfs /dev/loop0

Now /dev/loop is a legitimate block device which your app would act upon like a physical device or ramdisk, except that it is file backed. You can mount is somewhere or have you app act upon the device node, which implements the standard block ioctls. Saves your system ram and useful to keep around for testcases, etc.

(You can even fdisk myfs.img, create partitions on it and use --offset and --sizelimit with losetup to point each /dev/loopX to specific partitions in the image, so loop0, loop1 become just like sdc1, sdc2, etc)

1
  • +1 as this does not need recompiling the kernel
    – m-ric
    Jun 18, 2014 at 15:30
4

The object of a ramdrive is speed. Tmpfs is not a drive. Loop devices are not drives, but you can put a drive image on a loop device. Ram disks "are" drives, and very fast drives. Try running:

hdparm -t /dev/sda 

and then:

hdparm -t /dev/ram0

You'll see what I mean! But hdparm sometimes destroys the ram drive. So, you have to make it again.

Sometimes there are no /dev/ram devices. To make one:

mknod -m 0777 /dev/ram0 b 1 0 

but it won't have any size. To give it size:

dd if=/dev/zero of=/dev/ram0

and it will stop when the drive is full. The maximum size is determined by the kernel config parm: CONFIG_BLK_DEV_RAM_SIZE.

Then, just format it, i.e.

mke2fs /dev/ram0

And mount it:

mount /dev/ram0 /mnt/ramdrive

A favorite of mine is debugging code stored on a ramdrive. The compiles are at least 10x speed of code stored on a hard drive. Database tables stored on ramdrive also fly, but you must have a script that writes to hard disk periodically. Most admins don't have the guts to put data on ram disks. And some tables are too large.

1
  • Thanks! It was the mknod -m 0777 /dev/ram0 b 1 0 that I needed on Devuan 3, after which /dev/ram0 to /dev/ram15 just appeared. Jun 30, 2022 at 6:52
3

You could use a loop file instead. Just create a loop file the size you want it (if you wish to put it into a tmpfs ramdisk, fine), and then format the loop file and mount it.

dd if=/dev/zero of=myfile bs=1G count=10
mkfs.xfs -d file myfile
mount -t xfs -o loop myfile mymntpoint
2
  • I actually tried this before I posted my question. It is a great solution, but the software I am testing acts differently when it is dealing with a file and not a block device, that is why I was looking at the ramdisk option. Thanks.
    – Kevin S.
    Mar 11, 2011 at 13:55
  • 2
    Loopback devices are block devices, so I'm not sure how that is possible?
    – pjc50
    Jul 10, 2012 at 14:11
1

On lighthouse 64 (puupy linux based on slackware) I did this;

mke2fs /dev/ram1 4096000 # nearly 4G ramdisk, choose the size of ramdisk less than actual ram!
mount /dev/ram1 /mnt/dvd # dvd on my laptop is unused choose your device from /mnt/

There you have a ramdisk mounted as a directory /mnt/dvd where you can copy paste save do everything as you would in a hd directory except in ram. remember to save your files to hd before shutdown.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .