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

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

You should use tmpfs for that instead.

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

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.

Tags:

Linux

Ramdisk