Skip to content

VirtualDevices

opencode-agent[bot] edited this page May 10, 2026 · 1 revision

VirtualDevices

Software-only devices not backed by hardware (RAM disk, loopback, etc.).

Overview

VirtualDevices are software-only device implementations in JNode that provide the same interface as hardware devices but without any physical hardware dependency. They extend the Device class but pass null for the bus parameter, indicating they are not connected to any physical bus like PCI or USB.

The virtual device framework enables several important capabilities in JNode:

  • RAM disks: In-memory block devices for fast temporary storage
  • Testing: Device simulation without requiring actual hardware
  • Software-only functionality: Loopback interfaces, virtual network devices

VirtualDevices integrate seamlessly with the DeviceManager and can be accessed through the same DeviceAPI pattern as hardware devices. They must implement the same lifecycle (register, start, stop) but don't require hardware resource allocation.

Key Components

Class / File Role
core/src/driver/org/jnode/driver/virtual/VirtualDevice.java Base class for virtual devices; extends Device with null bus
core/src/driver/org/jnode/driver/virtual/VirtualDeviceDriver.java Driver for virtual devices; handles device renaming on startup
core/src/driver/org/jnode/driver/virtual/VirtualDeviceFactory.java Factory for creating and registering virtual devices with DeviceManager
core/src/driver/org/jnode/driver/Device.java Base device class that VirtualDevice extends
core/src/driver/org/jnode/driver/DeviceManager.java Central registry where virtual devices are registered

How It Works

Device Creation and Registration

VirtualDevices are created through the VirtualDeviceFactory:

VirtualDevice device = VirtualDeviceFactory.createDevice("myVirtualDevice");

The factory performs these steps:

  1. Creates a new VirtualDevice instance with the given name
  2. Attaches a VirtualDeviceDriver to the device
  3. Retrieves the DeviceManager via DeviceUtils.getDeviceManager()
  4. Registers the device with the DeviceManager

VirtualDevice Class

The VirtualDevice class extends Device but passes null for the bus:

public class VirtualDevice extends Device {
    private final String initialName;

    public VirtualDevice(String id) {
        super(null, id);  // null bus indicates virtual device
        this.initialName = id;
    }
}

The null bus is the key indicator that this is a virtual device. The DeviceManager recognizes this and treats the device differently from hardware devices.

VirtualDeviceDriver Behavior

The VirtualDeviceDriver extends Driver and provides minimal startup behavior:

protected void startDevice() throws DriverException {
    VirtualDevice device = (VirtualDevice) getDevice();
    device.getManager().rename(device, device.getInitialName(), true);
}

On startup, the driver renames the device to its initial name (allowing the user-specified name to take effect after registration).

Integration with Device Framework

VirtualDevices participate fully in the device framework:

  • They implement DeviceAPI interfaces just like hardware devices
  • They can be discovered through the DeviceManager
  • Filesystems and other consumers access them through the same APIs
  • They support the full device lifecycle (register, start, stop, unregister)

Gotchas & Non-Obvious Behavior

  • No hardware resources: VirtualDevices don't require IRQ, DMA, or IO port allocation since they have no hardware dependency. This differs from physical devices which need ResourceManager allocation.

  • Null bus semantics: Passing null for the bus is a convention that signals to the DeviceManager that this is a software-only device. Other code may check for this condition.

  • Name persistence: The VirtualDeviceDriver renames the device on startup. If registration fails, the device may retain a system-assigned name rather than the requested name.

  • Testing considerations: When writing tests that involve virtual devices, remember they don't have the same failure modes as hardware (no timeout, no disconnected states). Consider adding simulated failure modes if testing error handling.

  • Resource cleanup: Since virtual devices don't hold hardware resources, cleanup is simpler, but the DeviceManager still needs to be properly notified on unregister to maintain a consistent device tree.

Related Pages

  • DeviceManager - Central device registry where virtual devices are registered
  • BlockDeviceLayer - Block device abstraction; virtual block devices like RAM disks use this
  • Filesystem-Layer - Filesystems access virtual block devices through the block layer
  • Driver-Framework - Overview of the device and driver architecture
  • DeviceAPI - Interface-based device API pattern used by virtual devices

Clone this wiki locally