9pfs library implements a client for the 9P network protocol, enabling Unikraft applications to access host filesystems or remote file servers through a virtual filesystem device.
Overview
9P (Plan 9 Filesystem Protocol) provides a lightweight method for accessing files between a guest operating system and a host. In Unikraft, 9pfs creates virtual filesystem devices (uk_9pdev) that allow direct, pass-through access to host directories.
Key Features
- Host filesystem access: Share directories between host and Unikraft guest
- Protocol support: 9P2000.u (Unix) and 9P2000.L (Linux) variants
- VirtIO transport: Efficient virtualized I/O through VirtIO-9P
- Zero-copy operations: Minimal overhead for file operations
- POSIX compatibility: Standard file operations work seamlessly
Architecture
Data Structures
Mount Data
The mount structure contains connection and protocol information:Protocol Versions
- 9P2000.u: Basic protocol with Unix-specific extensions
- 9P2000.L: Enhanced protocol with Linux-specific features (symbolic links, permissions)
File Data
File-specific state for directory operations:Node Data
Per-node state tracking:Configuration
Enable 9pfs
LIBVFSCORE: VFS core interfaceLIBUK9P: 9P protocol implementation
Platform Requirements
For QEMU/KVM platforms, enable VirtIO support:- Platform Configuration → KVM guest
- KVM guest options → Virtio → Virtio PCI device support
Usage
Basic Example
Mounting at Boot
Using vfscore Auto-mount
Configure automatic mounting:- Library Configuration → vfscore: VFS Core Interface
- vfscore: Configuration → Automatically mount a root filesystem (/)
- Default root filesystem → 9pfs
- Default root device: Set to
rootfs(must match QEMU mount_tag)
Using posix-vfs mount()
Mount Options
Supported options (comma-separated):| Option | Values | Description | Default |
|---|---|---|---|
version | 9P2000.u or 9P2000.L | Protocol version (case sensitive) | 9P2000.L |
uname | string | Username for mount | "" |
aname | string | File tree to access | "" |
Running with QEMU
Basic Setup
Create a directory to share:QEMU Parameters Explained
-
-fsdev local,id=myid,path=./rootfs/,security_model=nonelocal: Use local filesystem backendid=myid: Identifier for this fsdevpath=./rootfs/: Host directory to sharesecurity_model=none: No security mapping (simplest, development only)
-
-device virtio-9p-pci,fsdev=myid,mount_tag=rootfsvirtio-9p-pci: VirtIO 9P PCI devicefsdev=myid: Links to the fsdev defined abovemount_tag=rootfs: Tag used by guest to identify this share (must match configuration)
Security Models
For production use, consider security models:Advanced Configuration
Multiple Mount Points
Share different directories at different mount points:Read-Only Mounts
Use mount flags for read-only access:Using posix-vfs-fstab
Configure automatic mounting via kernel parameters:Use Cases
Development Workflow
Share source code and build outputs:Configuration Management
Share configuration files without embedding them in the image:Log Collection
Write logs to host filesystem for easy access:Data Processing
Process large datasets from host without copying:Testing and CI/CD
Share test files and collect results:Performance Considerations
Optimization Tips
- Use 9P2000.L: The Linux variant offers better performance and features
- Batch operations: Minimize round-trips by batching file operations
- Cache strategies: Use appropriate caching for your workload
- Buffer sizes: Larger read/write buffers reduce overhead
When to Use 9pfs
Good for:- Development and debugging (easy file sharing)
- Configuration file access
- Log file writing
- Small to medium file I/O
- Shared data between host and guest
- High-performance I/O workloads
- Database files (use block devices)
- Very large files with random access
- Latency-sensitive operations
Alternatives
- ramfs: For temporary, high-speed storage (in-memory)
- Block devices: For high-performance persistent storage
- initrd: For read-only embedded filesystems
Troubleshooting
Mount Tag Mismatch
Error: “No such device” when mounting Solution: Ensure mount_tag in QEMU matches the device name in mount():Permission Denied
Issue: Cannot access files on host filesystem Solutions:- Check host directory permissions
- Use appropriate security_model
- Try
security_model=nonefor testing
Protocol Version Errors
Error: “Protocol version not supported” Solution: Explicitly specify version:VirtIO Device Not Found
Issue: 9P device not available Solutions:- Enable VirtIO PCI support in platform configuration
- Verify QEMU command includes both
-fsdevand-deviceoptions - Check
LIBUK9Pis enabled in configuration
Related Libraries
References
- 9P Protocol Specification
- Source:
lib/9pfs/in Unikraft repository