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.

相关推荐
.小墨迹2 小时前
开源的自动驾驶框架
c++·人工智能·学习·算法·ubuntu·开源·自动驾驶
hhy_smile13 小时前
Ubuntu24.04 环境配置自动脚本
linux·ubuntu·自动化·bash
一叶龙洲20 小时前
解决Vmware Ubuntu共享文件夹有时无法识别
ubuntu
会飞的土拨鼠呀21 小时前
Ubuntu系统缺少 iptables 工具
linux·运维·ubuntu
花间相见1 天前
【AI开发】—— Ubuntu系统使用nvm管理Node.js多版本,版本切换一键搞定(实操完整版)
linux·ubuntu·node.js
.小墨迹1 天前
apollo中车辆的减速绕行,和加速超车实现
c++·学习·算法·ubuntu·机器学习
小Pawn爷1 天前
13.virtualbox安装ubuntu
linux·运维·ubuntu
VekiSon1 天前
Linux内核驱动——Ubuntu 网络启动环境配置与操作
linux·arm开发·嵌入式硬件·ubuntu
YYYing.1 天前
【Linux/C++进阶篇(二) 】超详解自动化构建 —— 日常开发中的“脚本” :Makefile/CMake
linux·c++·经验分享·ubuntu