| Crates.io | vramblk |
| lib.rs | vramblk |
| version | 0.1.0 |
| created_at | 2025-04-18 08:46:38.442191+00 |
| updated_at | 2025-04-18 08:46:38.442191+00 |
| description | A block device that exposes GPU memory to userspace via a NBD server. |
| homepage | |
| repository | https://github.com/theblazehen/vramblk |
| max_upload_size | |
| id | 1639026 |
| size | 46,484 |
A Rust application that uses OpenCL to allocate GPU memory and exposes it as a block device using a NBD server implementation.
git clone https://github.com/theblazehen/vramblk.git
cd vramblk
cargo build --release
The executable will be at target/release/vramblk.
cargo install vramblk
nbd-client utility (for connecting the kernel NBD module to the server)sudo apt update
sudo apt install ocl-icd-opencl-dev opencl-headers nbd-client build-essential
sudo dnf install ocl-icd-devel opencl-headers nbd-client gcc make
The application does not strictly require root privileges. However, if you intend to use VRAM as swap, running as root (or with the appropriate capabilities) is strongly recommended to allow locking memory with mlockall(2), and for nbd-client operations. If you do not run as root, you can grant the necessary capability with:
sudo setcap cap_ipc_lock=eip ./target/release/vramblk
This allows the process to lock memory without full root privileges.
./target/release/vramblk --list-devices
sudo ./target/release/vramblk [OPTIONS]
The server will attempt to lock its memory using mlockall and then run in the foreground, listening on the specified address. Locking memory with mlockall ensures the server process is never swapped out, which is critical for swap usage. Check the log output for success or failure of mlockall.
You need the nbd-client utility for this step.
# Example connecting /dev/nbd0 to the server running on localhost:10809
sudo nbd-client localhost 10809 /dev/nbd0 -N vram
Replace localhost:10809 with the listen address if you changed it, /dev/nbd0 with the desired device, and vram with the export name if changed.
Start the server as root, or grant CAP_IPC_LOCK capability:
# Option 1: Run as root
sudo ./target/release/vramblk --size 2G
# Option 2: Grant CAP_IPC_LOCK and run as regular user
sudo setcap cap_ipc_lock=eip ./target/release/vramblk
./target/release/vramblk --size 2G
Connect the NBD device for swap:
sudo nbd-client localhost 10809 /dev/nbd0 -N vram -swap -C 1
-swap tells nbd-client to optimize for swap usage.-C 1 sets the number of connections to 1 (recommended for swap).Mark the device as swap and enable:
sudo mkswap /dev/nbd0
sudo swapon /dev/nbd0
Check swap status:
swapon --show
To disable swap and disconnect:
sudo swapoff /dev/nbd0
sudo nbd-client -d /dev/nbd0
Important:
mlockall fails, swap usage is unsafe. mlockall locks the server's memory into RAM, preventing it from being swapped out, which is essential for swap reliability.nbd-client process itself should also be protected from swapping (consider running it as a systemd service with MemoryDenyWriteExecute=no and LimitMEMLOCK=infinity).-s, --size <SIZE>: Size of the block device (accepts suffixes: e.g., 512M, 2G, default: 2048M)-d, --device <DEVICE>: GPU device index to use (default: 0)-p, --platform <PLATFORM>: OpenCL platform index (default: 0)-l, --listen-addr <LISTEN_ADDR>: Listen address for the NBD server (default: "127.0.0.1:10809")-e, --export-name <EXPORT_NAME>: Export name advertised over NBD (default: "vram")-v, --verbose: Enable verbose logging--list-devices: List available OpenCL platforms and devices and exit-h, --help: Print help information-V, --version: Print version information# Start server for 4GB device on GPU 0, listening on default address
sudo ./target/release/vramblk --size 4G --device 0
# In another terminal: Connect nbd-client
sudo nbd-client localhost 10809 /dev/nbd0 -N vram
vramblk executable parses arguments and initializes logging.mlockall(MCL_CURRENT | MCL_FUTURE) to lock its current and future memory pages into RAM, preventing swap-out.VRamBuffer).nbd::server::handshake.VramSeeker struct wrapping the VRamBuffer which implements std::io::{Read, Write, Seek}.nbd::server::transmission, which calls the VramSeeker methods for I/O.Ctrl+C is received.nbd-client to be installed separately.mlockall, OpenCL) and nbd-client.mlockall might fail if limits (ulimit -l) are too low or user lacks privileges.nbd-client from swapping is not handled by this application.MIT
Contributions are welcome! Please feel free to submit a Pull Request.