Skip to content

RamDisk Driver

opencode-agent[bot] edited this page May 10, 2026 · 2 revisions

RamDisk Driver

Virtual block device using RAM as backing store for testing and initrd.

Overview

The RamDisk Driver provides a software-only block device that uses system RAM as its backing store. It implements the FSBlockDeviceAPI interface, making it appear as a regular block device to the filesystem layer. This enables filesystem testing without physical disk hardware and can serve as a RAM disk for boot-time initialization.

Key Components

File Purpose
RamDiskDriver.java Main driver implementation, extends Driver and implements FSBlockDeviceAPI
RamDiskDevice.java Device class extending Device with size parameter
RamDiskCommand.java Shell command for creating/managing RAM disks at runtime

How It Works

Driver Architecture

RamDiskDriver extends Driver
    └── implements FSBlockDeviceAPI
            ├── read(long devOffset, ByteBuffer dest)
            ├── write(long devOffset, ByteBuffer src)
            ├── getLength()
            ├── getSectorSize() -> 512
            ├── flush()
            └── getPartitionTableEntry() -> null

Device Initialization

public RamDiskDriver(String deviceName) {
    this.devName = deviceName;
}

protected void startDevice() throws DriverException {
    this.device = (RamDiskDevice) getDevice();
    // Register with DeviceManager
    if (this.data == null) {
        this.data = new byte[device.getSize()];
        this.device.registerAPI(BlockDeviceAPI.class, this);
    } else {
        this.device.registerAPI(FSBlockDeviceAPI.class, this);
    }
}

Data Storage

The driver uses a simple byte[] array to store all data:

private byte[] data;

public void read(long devOffset, ByteBuffer dest) {
    dest.put(this.data, (int) devOffset, dest.remaining());
}

public void write(long devOffset, ByteBuffer src) {
    src.get(this.data, (int) devOffset, src.remaining());
}

This provides fast read/write performance since all operations are in-memory.

Usage Patterns

Testing Filesystems

The RamDisk Driver is extensively used in the test framework (fs/src/test/) to test filesystem implementations without requiring real disk hardware:

RamDiskDevice device = new RamDiskDevice(bus, name, size);
RamDiskDriver driver = new RamDiskDriver(name);
init(null, driver, device);

Runtime Creation

The RamDiskCommand allows shell-based RAM disk creation:

RamDiskDevice dev = new RamDiskDevice(null, "dummy", size);
dev.setDriver(new RamDiskDriver(null));

Gotchas

  • No Persistence: Data is lost on system reboot or power cycle
  • Memory Consumption: RAM disks consume heap memory proportional to their size
  • No Partition Support: getPartitionTableEntry() returns null
  • Fixed Sector Size: Always returns 512 bytes regardless of actual device size
  • No Flush Implementation: flush() is a no-op since RAM requires no syncing

Related Pages

Clone this wiki locally