To create a RAM-based block device in Ubuntu.

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.

  1. Load the brd module 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 ).

  2. Format the RAM disk (e.g., ext4):

    bash

    复制代码
    sudo mkfs.ext4 /dev/ram0
  3. Mount the device :

    bash

    复制代码
    sudo mkdir /mnt/ramdisk
    sudo mount /dev/ram0 /mnt/ramdisk
  4. 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.

  1. Create mount point :

    bash

    复制代码
    sudo mkdir /mnt/ramdisk
  2. 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.

相关推荐
苏三福3 小时前
ubuntu 在文件管理系统中用sftp打开远程文件空白的问题解决
linux·运维·ubuntu
Anesthesia丶4 小时前
llama-server编译部署+opencode验证记录(Ubuntu, GPU)
ubuntu·opencode·llama-server·qwen3.8-9b
Sunqk56654 小时前
将开发板串口连接到Ubuntu后未找到对应的设备文件?
linux·运维·ubuntu
YHHLAI4 小时前
TypeScript 面试题:type 与 interface 的区别与相同点
java·ubuntu·typescript
玖石书2 天前
Ubuntu 更换 USTC 镜像源完整指南(AMD64 / AARCH64)
linux·运维·ubuntu
牢姐与蒯2 天前
Linux进程(五).之环境变量
linux·运维·服务器·ubuntu
黑臂麒麟2 天前
Harmony鸿蒙实战应用4:随手账本——编辑与删除账单
ubuntu·华为·鸿蒙
玖石书2 天前
WSL2 手动安装 Ubuntu 24.04 到指定磁盘位置
linux·运维·ubuntu·wsl
冰雪青松3 天前
Ubuntu 运维命令大全:从入门到精通的实战参考手册
运维·服务器·ubuntu
feilieren3 天前
ubuntu 安装 docker
linux·ubuntu·docker