-
Notifications
You must be signed in to change notification settings - Fork 0
VirtualDevices
Software-only devices not backed by hardware (RAM disk, loopback, etc.).
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.
| 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 |
VirtualDevices are created through the VirtualDeviceFactory:
VirtualDevice device = VirtualDeviceFactory.createDevice("myVirtualDevice");The factory performs these steps:
- Creates a new
VirtualDeviceinstance with the given name - Attaches a
VirtualDeviceDriverto the device - Retrieves the DeviceManager via
DeviceUtils.getDeviceManager() - Registers the device with the DeviceManager
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.
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).
VirtualDevices participate fully in the device framework:
- They implement
DeviceAPIinterfaces 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)
-
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
nullfor 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.
- 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