To create a RAM-based block device in Ubuntu, use the
brd (RAM block device) kernel module to create a fast, memory-backed device (/dev/ram* ), or use tmpfs for simpler, file-based RAM storage. The brd method offers higher IOPS and mimics a real physical block device, ideal for performance testing or secure, temporary storage.
Method 1: Using brd (Kernel RAM Block Device)
This creates an actual block device in RAM, allowing for partitioning and file systems.
-
Load the
brdmodule and create a 1GB RAM disk (size is in KiB):bash
sudo modprobe brd rd_nr=1 rd_size=1048576 max_part=1-
rd_nr=1: Creates one device (/dev/ram0). -
rd_size=1048576: Size in KiB (
1024×1024=1GB1024 cross 1024 equals 1 GB
1024×1024=1GB ).
-
-
Format the RAM disk (e.g., ext4):
bash
sudo mkfs.ext4 /dev/ram0 -
Mount the device :
bash
sudo mkdir /mnt/ramdisk sudo mount /dev/ram0 /mnt/ramdisk -
Clean up (unmount and remove module):
bash
sudo umount /mnt/ramdisk sudo rmmod brd
Method 2: Using tmpfs (Temporary File System)
tmpfs is generally easier, acts like a ramdisk, and can swap to disk if RAM runs out.
-
Create mount point :
bash
sudo mkdir /mnt/ramdisk -
Mount as
tmpfs(e.g., 1GB):bash
sudo mount -t tmpfs -o size=1G tmpfs /mnt/ramdisk
Summary Table
| Feature | brd (Method 1) |
tmpfs (Method 2) |
|---|---|---|
| Type | Actual Block Device | Filesystem in RAM |
| Performance | Highest IOPS | High |
| Swap | No | Yes (may swap to disk) |
| Setup | modprobe + mkfs |
mount -t tmpfs |
Note: Any data on a RAM disk is lost upon reboot or when the module is removed.