How can I change the amount and size of Linux ramdisks (/dev/ram0 - /dev/ram15)?

Solution 1:

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

Solution 2:

You should use tmpfs for that instead.

mount -t tmpfs -o size=10g none /mnt/point

Solution 3:

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.

Solution 4:

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)