Documentation
¶
Overview ¶
Package xfs provides read/write access to XFS filesystem images without requiring root privileges or external tools. It targets XFS v5 (CRC-enabled superblock, v3 inodes) as used by Rocky Linux 9, AlmaLinux 9, and other RHEL-family distributions. Version 4 filesystems are also supported for reading.
Partition tables (MBR/GPT) are detected automatically; pass partIndex = -1 to auto-select the first Linux data partition.
Supported operations:
- ReadFile, ListDir, Stat — complete for all directory formats.
- WriteFile — creates or overwrites regular files. For existing files the data is rewritten in-place when the new content fits the current block allocation; larger rewrites allocate additional blocks. New files are created by allocating an inode and data blocks via the AG free-space B-trees and inserting an entry in the parent directory.
- DeleteFile — removes a file and frees its inode and data blocks.
Index ¶
Constants ¶
const DefaultMaxReadSize int64 = 1 << 30
DefaultMaxReadSize is the largest file ReadFile will materialise in memory unless SetMaxReadSize says otherwise: 1 GiB.
di_size, di_nblocks and the superblock's sb_dblocks all come from the image, so none of them can bound an allocation by itself: an inode forged to nblocks = 2^40 "owns" enough blocks for any size, and a superblock may declare a filesystem as large as it likes. Only the caller knows how much memory it is willing to spend, so there is a documented ceiling.
const MaxLabelLen = 12
MaxLabelLen is the on-disk size of the XFS volume label (sb_fname, at superblock offset 108).
Variables ¶
var ErrBlockOutOfRange = fmt.Errorf("xfs: block offset out of range")
ErrBlockOutOfRange is returned when a block number's byte offset would overflow int64 (a forged extent/btree pointer).
var ErrNotFound = errors.New("not found")
ErrNotFound is returned when a path component is not found.
var ErrSymlinkLoop = errors.New("xfs: too many levels of symbolic links")
ErrSymlinkLoop is returned when one path resolution follows more than safeio.MaxSymlinks symbolic links in total (the Linux MAXSYMLINKS limit). The budget is TOTAL, not depth: a link that resolves two further links, 40 levels deep, would otherwise cost 2^38 lookups from a tiny image.
var ErrTooLarge = safeio.ErrTooLarge
ErrTooLarge is returned, wrapped, when an on-disk size or count would make the driver allocate or walk more than the image can legitimately hold. It is safeio.ErrTooLarge, so errors.Is works against either name.
Functions ¶
This section is empty.
Types ¶
type BlockBackend ¶
type BlockBackend = blockBackend
BlockBackend is the exported alias of blockBackend, so external packages can implement it and feed instances to OpenFromDevice. Same shape as ext4.BlockDevice and zfs.BlockBackend — keeping the interface uniform across go-filesystems lets a single adapter (e.g. luksAsBlock) serve every FS.
type FS ¶
type FS interface {
filesystem.Filesystem
// Grow is the canonical "extend to newSizeBytes" entry point. It
// requires newSizeBytes to be at least the current filesystem size
// and to align to a whole number of allocation groups (each AG =
// sb_agblocks × blockSize). Equal sizes are a no-op.
Grow(newSizeBytes int64) error
// Resize routes the uniform filesystem.Resizer call to Grow when
// expanding; shrink is intentionally not supported in XFS (matches
// the kernel + xfsprogs semantics) and returns
// filesystem.ErrShrinkUnsupported when newSizeBytes < current.
Resize(newSizeBytes int64) error
// GrowTo is the historical name kept for callers that target the
// filesystem.Grower interface. It is a thin alias of Grow.
GrowTo(newSizeBytes int64) error
ExtendedStat(path string) (*InodeStat, error)
Chown(path string, uid, gid uint32) error
Chmod(path string, perm os.FileMode) error
Chtimes(path string, atime, mtime time.Time) error
Symlink(target, linkPath string) error
Truncate(path string, newSize int64) error
Link(oldPath, newPath string) error
// Reflink clones srcPath into a new file dstPath that shares srcPath's
// data extents copy-on-write (the equivalent of `cp --reflink`). It
// requires the filesystem to have been formatted with reflink support
// (FormatConfig.Reflink); dstPath must not already exist.
Reflink(srcPath, dstPath string) error
// HasReflink reports whether the filesystem carries the reflink feature.
HasReflink() bool
Label() string
SetLabel(label string) error
}
FS is the public interface returned by Open. Extends filesystem.Filesystem with the XFS-specific operations — image resize (Grow/Resize/GrowTo) plus inode metadata setters and the rich ExtendedStat reader.
func Format ¶
func Format(path string, sizeBytes int64, cfg FormatConfig) (FS, error)
Format creates a new XFS v5 filesystem in the file at path. sizeBytes must be a multiple of 4096 and at least 64 MiB (one AG). On success the newly formatted filesystem is opened and returned.
func Open ¶
Open opens an XFS filesystem image, auto-detecting the partition table and selecting the first Linux data partition. Pass partIndex ≥ 0 to pick a specific (0-based) partition entry; pass -1 for auto-detection.
func OpenFromDevice ¶
func OpenFromDevice(dev BlockBackend, partIndex int) (FS, error)
OpenFromDevice opens an XFS filesystem backed by an arbitrary blockBackend. Layered callers (LUKS / qcow2 / in-memory) feed the FS without an *os.File-backed image.
type FormatConfig ¶
type FormatConfig struct {
// UUID is the filesystem UUID. A random UUID is generated when all bytes are zero.
UUID [16]byte
// Label is the volume label (up to 12 bytes; silently truncated).
Label string
// Reflink enables shared-extent (copy-on-write) support: the
// XFS_SB_FEAT_RO_COMPAT_REFLINK feature bit plus an empty per-AG refcount
// B-tree, matching mkfs.xfs -m reflink=1,rmapbt=0. Files can then be cloned
// with FS.Reflink so they share physical blocks.
Reflink bool
// Quota, when non-zero, enables on-disk quota accounting: the requested
// quota inodes (user/group/project) are created and sb_qflags is set.
// See QuotaConfig. The zero value leaves quotas disabled.
Quota QuotaConfig
}
FormatConfig holds optional parameters for Format.
type InodeStat ¶
type InodeStat struct {
Inode uint64
Mode uint16
Size uint64
NBlocks uint64 // di_nblocks: count of filesystem blocks allocated
NLink uint32
UID uint32
GID uint32
Generation uint32
ATime time.Time
CTime time.Time
MTime time.Time
CRTime time.Time // v3 only — birth time; zero on v1/v2 inodes
}
InodeStat is the rich metadata view of an XFS inode. The common filesystem.Stat only exposes mode/size/inode; callers that want uid/gid, timestamps, link count or the inode generation number use ExtendedStat.
All timestamps are returned in UTC.
type QuotaConfig ¶
type QuotaConfig struct {
User bool // user quota accounting (sb_uquotino)
Group bool // group quota accounting (sb_gquotino)
Project bool // project quota accounting (sb_pquotino)
Enforce bool // also enforce limits (set *_ENFD flags)
}
QuotaConfig selects which quota types a Format enables. The zero value leaves quotas off. Enforce additionally sets the *_ENFD (limit enforcement) flags; without it only accounting (*_ACCT) plus the *_CHKD "counts verified" flags are set, which is the state of a freshly quota-checked filesystem.
