Pure Go ext4 filesystem implementation for creating disk images without external dependencies.
A pure Go library for creating ext4 filesystem images programmatically. Designed for building disk images for virtual machines and embedded systems without requiring root privileges or external tools like mke2fs.
Primary use case: Creating new ext4 images from scratch. Limited support exists for modifying images previously created by this library (e.g., updating
/initbinary). Standard ext4 filesystems created bymke2fscannot be opened due to unsupported features (journaling, 64-bit, flex_bg).
- Pure Go: No external dependencies or system calls
- Extent-based: Modern ext4 extent trees for efficient block mapping
- Extended attributes: Full xattr support for security labels and metadata
- Symlinks: Both fast (inline) and slow (block-based) symlinks
- Simple API: Easy to use for creating filesystem images programmatically
- Docker integration: End-to-end testing with real Linux kernel verification
go get github.com/pilat/go-ext4fspackage main
import "github.com/pilat/go-ext4fs"
func main() {
// Create 64MB ext4 image
img, err := ext4fs.New(
ext4fs.WithImagePath("disk.img"),
ext4fs.WithSizeInMB(64),
)
if err != nil {
panic(err)
}
defer img.Close()
// Create directories and files
etcDir, _ := img.CreateDirectory(ext4fs.RootInode, "etc", 0755, 0, 0)
img.CreateFile(etcDir, "hostname", []byte("myhost\n"), 0644, 0, 0)
img.CreateSymlink(etcDir, "hosts", "/etc/hostname", 0, 0)
img.SetXattr(etcDir, "user.comment", []byte("System configuration"))
// Finalize and save
if err := img.Save(); err != nil {
panic(err)
}
}package main
import "github.com/pilat/go-ext4fs"
func main() {
// Open existing image (must have been created by this library)
img, err := ext4fs.Open(ext4fs.WithExistingImagePath("disk.img"))
if err != nil {
panic(err)
}
defer img.Close()
// Modify: delete old file, add new one
newInit := []byte("#!/bin/sh\nexec /bin/sh\n")
img.Delete(ext4fs.RootInode, "old-init")
img.CreateFile(ext4fs.RootInode, "init", newInit, 0755, 0, 0)
if err := img.Save(); err != nil {
panic(err)
}
}This library is optimized for creating simple disk images for small VMs. The following ext4 features are not implemented:
| Feature | Impact |
|---|---|
| Journaling | No crash recovery (not needed for image creation) |
| 64-bit block addresses | Maximum filesystem size ~16 TB |
| Extent tree depth > 1 | Maximum ~1,360 extents per file; sufficient for contiguous files up to ~170 TB |
| HTree directory indexing | Linear directory scan; fine for small directories |
| Inline data | Small files use regular blocks |
| Encryption | No at-rest encryption support |
| Quotas | No user/group quota tracking |
For most VM use cases (boot disks, configuration filesystems, small data volumes), these limitations have no practical impact.
Created images can be verified using standard Linux tools:
# Check filesystem integrity
e2fsck -n -f disk.img
# Mount and inspect (requires root)
sudo mount -o loop disk.img /mnt
# View filesystem info
dumpe2fs disk.imgMIT License - see LICENSE file for details.