ext4-lwext4
A safe Rust wrapper for ext2/3/4 filesystem operations based on lwext4.
Features
- Create ext2/3/4 filesystems with
mkfs - Mount and unmount filesystems
- File operations: create, read, write, truncate, seek
- Directory operations: create, remove, iterate
- Symbolic and hard links
- File metadata and permissions
- Block device abstraction for various backing stores
- Zero runtime dependencies (lwext4 is statically compiled)
Installation
Add to your Cargo.toml:
[]
= "0.1"
Quick Start
use ;
use ;
// Create a 100MB disk image
let device = create?;
// Format as ext4
mkfs?;
// Reopen and mount
let device = open?;
let fs = mount?;
// Create a directory
fs.mkdir?;
// Write a file
// Read it back
// List directory
for entry in fs.open_dir?
// Unmount
fs.umount?;
Block Devices
The crate provides several block device implementations:
| Type | Description |
|---|---|
FileBlockDevice |
Backed by a file (disk image) |
MemoryBlockDevice |
In-memory storage (for testing/embedded) |
You can implement the BlockDevice trait for custom storage backends:
use BlockDevice;
Filesystem Types
use MkfsOptions;
// ext2 (no journaling)
let opts = ext2;
// ext3 (with journaling)
let opts = ext3;
// ext4 (with extents and journaling) - default
let opts = ext4;
// Custom options
let opts = MkfsOptions ;
API Overview
Filesystem Operations
let fs = mount?;
// Directory operations
fs.mkdir?;
fs.rmdir?;
fs.rename?;
// File operations
let file = fs.open?;
fs.remove?;
// Links
fs.symlink?;
fs.link?;
fs.readlink?;
// Metadata
let meta = fs.metadata?;
fs.chmod?;
fs.chown?;
// Filesystem info
let stats = fs.stats?;
println!;
fs.umount?;
File Operations
use ;
let mut file = fs.open?;
// Write
file.write_all?;
// Seek
file.seek?;
// Read
let mut buf = ;
file.read_exact?;
// Truncate
file.truncate?;
// Get size
let size = file.size?;
Directory Iteration
let dir = fs.open_dir?;
for entry in dir
License
This project uses a dual licensing scheme:
Default: MIT OR Apache-2.0
By default (without GPL features), this project is licensed under either:
at your option.
With GPL Features: GPL-2.0
When compiled with gpl-extents or gpl-xattr features, the resulting binary is licensed under GPL-2.0 due to the inclusion of GPL-licensed source files from lwext4.
| Feature | Description | License Impact |
|---|---|---|
| (default) | Base ext4 support | MIT OR Apache-2.0 |
gpl-extents |
Advanced extents implementation | GPL-2.0 |
gpl-xattr |
Extended attributes support | GPL-2.0 |
gpl |
All GPL features | GPL-2.0 |
lwext4 Licensing
The underlying lwext4 library has mixed licensing:
- Most source files: BSD-3-Clause
ext4_extent.c,ext4_xattr.c: GPL-2.0
This crate excludes GPL-licensed files by default to maintain permissive licensing compatibility.
Building from Source
# Clone with submodules
# Build (default, no GPL)
# Build with GPL features
# Run tests
Requirements
- Rust 1.90+ (2024 edition)
- C compiler (for building lwext4)
Contributing
Contributions are welcome! Please feel free to submit issues and pull requests.