Documentation
¶
Overview ¶
Package diskfs implements methods for creating and manipulating disks and filesystems
methods for creating and manipulating disks and filesystems, whether block devices in /dev or direct disk images. This does **not** mount any disks or filesystems, neither directly locally nor via a VM. Instead, it manipulates the bytes directly.
This is not intended as a replacement for operating system filesystem and disk drivers. Instead, it is intended to make it easy to work with partitions, partition tables and filesystems directly without requiring operating system mounts.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Create ¶
Might be deprecated in future: use <backend>.CreateFromPath + diskfs.OpenBackend Create a Disk from a path to a device Should pass a path to a block device e.g. /dev/sda or a path to a file /tmp/foo.img The provided device must not exist at the time you call Create()
Example (Fat32) ¶
Create a disk image of size 10MB with a FAT32 filesystem spanning the entire disk.
package main import ( "log" "os" diskfs "github.com/diskfs/go-diskfs" "github.com/diskfs/go-diskfs/disk" "github.com/diskfs/go-diskfs/filesystem" ) func check(err error) { if err != nil { log.Fatal(err) } } func unused(_ ...any) { } func main() { var size int64 = 10 * 1024 * 1024 // 10 MB diskImg := "/tmp/disk.img" defer os.Remove(diskImg) theDisk, _ := diskfs.Create(diskImg, size, diskfs.SectorSizeDefault) fs, err := theDisk.CreateFilesystem(disk.FilesystemSpec{ Partition: 0, FSType: filesystem.TypeFat32, }) check(err) unused(fs) }
Output:
Example (Fat32WithDirsAndFiles) ¶
Create a disk of size 20MB with an MBR partition table, a single partition beginning at block 2048 (1MB), of size 10MB filled with a FAT32 filesystem, and create some directories and files in that filesystem.
package main import ( "crypto/rand" "log" "os" diskfs "github.com/diskfs/go-diskfs" "github.com/diskfs/go-diskfs/disk" "github.com/diskfs/go-diskfs/filesystem" "github.com/diskfs/go-diskfs/partition/mbr" ) func check(err error) { if err != nil { log.Fatal(err) } } func unused(_ ...any) { } func main() { var size int64 = 20 * 1024 * 1024 // 20 MB diskImg := "/tmp/disk.img" defer os.Remove(diskImg) theDisk, _ := diskfs.Create(diskImg, size, diskfs.SectorSizeDefault) table := &mbr.Table{ LogicalSectorSize: 512, PhysicalSectorSize: 512, Partitions: []*mbr.Partition{ { Bootable: false, Type: mbr.Linux, Start: 2048, Size: 20480, }, }, } check(theDisk.Partition(table)) fs, err := theDisk.CreateFilesystem(disk.FilesystemSpec{ Partition: 1, FSType: filesystem.TypeFat32, }) check(err) err = fs.Mkdir("/FOO/BAR") check(err) rw, err := fs.OpenFile("/FOO/BAR/AFILE.EXE", os.O_CREATE|os.O_RDWR) check(err) b := make([]byte, 1024) _, err = rand.Read(b) check(err) written, err := rw.Write(b) check(err) unused(written) }
Output:
Example (Gpt) ¶
Create a disk of size 20MB with a GPT partition table, a single partition beginning at block 2048 (1MB), of size 10MB, and fill with the contents from the 10MB file "/root/contents.dat"
package main import ( "log" "os" diskfs "github.com/diskfs/go-diskfs" "github.com/diskfs/go-diskfs/partition/gpt" ) func check(err error) { if err != nil { log.Fatal(err) } } func unused(_ ...any) { } func main() { var size int64 = 20 * 1024 * 1024 // 20 MB diskImg := "/tmp/disk.img" defer os.Remove(diskImg) theDisk, _ := diskfs.Create(diskImg, size, diskfs.SectorSizeDefault) table := &gpt.Table{ LogicalSectorSize: 512, PhysicalSectorSize: 512, ProtectiveMBR: true, Partitions: []*gpt.Partition{ { Start: 1 * 1024 * 1024 / 512, Size: 10 * 1024 * 1024, }, }, } check(theDisk.Partition(table)) f, err := os.Open("/root/contents.dat") check(err) written, err := theDisk.WritePartitionContents(1, f) check(err) unused(written) }
Output:
Example (Mbr) ¶
Create a disk of size 20MB with an MBR partition table, a single partition beginning at block 2048 (1MB), of size 10MB filled with a FAT32 filesystem.
package main import ( "log" "os" diskfs "github.com/diskfs/go-diskfs" "github.com/diskfs/go-diskfs/disk" "github.com/diskfs/go-diskfs/filesystem" "github.com/diskfs/go-diskfs/partition/mbr" ) func check(err error) { if err != nil { log.Fatal(err) } } func unused(_ ...any) { } func main() { var size int64 = 20 * 1024 * 1024 // 20 MB diskImg := "/tmp/disk.img" defer os.Remove(diskImg) theDisk, _ := diskfs.Create(diskImg, size, diskfs.SectorSizeDefault) table := &mbr.Table{ LogicalSectorSize: 512, PhysicalSectorSize: 512, Partitions: []*mbr.Partition{ { Bootable: false, Type: mbr.Linux, Start: 2048, Size: 20480, }, }, } check(theDisk.Partition(table)) fs, err := theDisk.CreateFilesystem(disk.FilesystemSpec{ Partition: 1, FSType: filesystem.TypeFat32, }) check(err) unused(fs) }
Output:
func Open ¶
Might be deprecated in future: use <backend>.New + diskfs.OpenBackend Open a Disk from a path to a device in read-write exclusive mode Should pass a path to a block device e.g. /dev/sda or a path to a file /tmp/foo.img The provided device must exist at the time you call Open(). Use OpenOpt to control options, such as sector size or open mode.
func OpenBackend ¶ added in v1.5.0
Open a Disk using provided fs.File to a device in read-only mode Use OpenOpt to control options, such as sector size or open mode.
Example (Backend) ¶
Instantiate the backend manually for later use in diskfs.OpenBackend
package main import ( "log" "os" diskfs "github.com/diskfs/go-diskfs" "github.com/diskfs/go-diskfs/backend/file" ) func check(err error) { if err != nil { log.Fatal(err) } } func unused(_ ...any) { } func main() { // some exemplary fs.FS dirfs := os.DirFS("/tmp/images") // the fs.File we'll use f, err := dirfs.Open("theImage.raw") check(err) theBackend := file.New(f, false) // As opposed to File example, theBackend will be used as is (i.e. not wrapped). // This usage scenario is for upcoming support for other backends (qcow2 and such). theDisk, err := diskfs.OpenBackend(theBackend) check(err) unused(theDisk) }
Output:
Example (Create) ¶
Create a disk of size 20MB at provided pathname using backend abstraction. This is a proposed way of usage after factoring out underlying file operations to the backend. diskfs.Create() is slowly deprecating now.
package main import ( "log" diskfs "github.com/diskfs/go-diskfs" "github.com/diskfs/go-diskfs/backend/file" ) func check(err error) { if err != nil { log.Fatal(err) } } func unused(_ ...any) { } func main() { theBackend, err := file.CreateFromPath("/tmp/my.img", 20*1024*1024) check(err) theDisk, err := diskfs.OpenBackend(theBackend) check(err) unused(theDisk) }
Output:
Example (Open) ¶
Open existing image using backend abstraction This is a proposed way of usage after factoring out underlying file operations to the backend. diskfs.Open() is slowly deprecating now.
package main import ( "log" "os" diskfs "github.com/diskfs/go-diskfs" "github.com/diskfs/go-diskfs/backend/file" ) func check(err error) { if err != nil { log.Fatal(err) } } func unused(_ ...any) { } func main() { readOnly := true theBackend, err := file.OpenFromPath("/tmp/my.img", readOnly) check(err) defer os.Remove("/tmp/my.img") theDisk, err := diskfs.OpenBackend(theBackend) check(err) unused(theDisk) }
Output:
Types ¶
type OpenModeOption ¶
type OpenModeOption int
OpenModeOption represents file open modes
const ( // ReadOnly open file in read only mode ReadOnly OpenModeOption = iota // ReadWriteExclusive open file in read-write exclusive mode ReadWriteExclusive // ReadWrite open file in read-write mode ReadWrite )
type OpenOpt ¶ added in v1.3.0
type OpenOpt func(o *openOpts) error
OpenOpt func that process Open options
func WithOpenMode ¶ added in v1.3.0
func WithOpenMode(mode OpenModeOption) OpenOpt
WithOpenMode sets the opening mode to the requested mode of type OpenModeOption. Default is ReadWriteExclusive, i.e. os.O_RDWR | os.O_EXCL
func WithSectorSize ¶ added in v1.3.0
func WithSectorSize(sectorSize SectorSize) OpenOpt
WithSectorSize opens the disk file or block device with the provided sector size. Defaults to the physical block size.
type SectorSize ¶ added in v1.3.0
type SectorSize int
SectorSize represents the sector size to use
const ( // SectorSizeDefault default behavior, defaulting to defaultBlocksize SectorSizeDefault SectorSize = 0 // SectorSize512 override sector size to 512 SectorSize512 SectorSize = 512 // SectorSize4k override sector size to 4k SectorSize4k SectorSize = 4096 )
Directories
¶
Path | Synopsis |
---|---|
Package disk provides utilities for working directly with a disk
|
Package disk provides utilities for working directly with a disk |
Package filesystem provides interfaces and constants required for filesystem implementations.
|
Package filesystem provides interfaces and constants required for filesystem implementations. |
fat32
Package fat32 provides utilities to interact with, manipulate and create a FAT32 filesystem on a block device or a disk image.
|
Package fat32 provides utilities to interact with, manipulate and create a FAT32 filesystem on a block device or a disk image. |
iso9660
Package iso9660 provides utilities to interact with, manipulate and create an iso9660 filesystem on a block device or a disk image.
|
Package iso9660 provides utilities to interact with, manipulate and create an iso9660 filesystem on a block device or a disk image. |
squashfs
Package squashfs provides support for reading and creating squashfs filesystems references:
|
Package squashfs provides support for reading and creating squashfs filesystems references: |
Package partition provides ability to work with individual partitions.
|
Package partition provides ability to work with individual partitions. |
gpt
Package gpt provides an interface to GUID Partition Table (GPT) partitioned disks.
|
Package gpt provides an interface to GUID Partition Table (GPT) partitioned disks. |
mbr
Package mbr provides an interface to Master Boot Record (MBR) partitioned disks.
|
Package mbr provides an interface to Master Boot Record (MBR) partitioned disks. |
Package testhelper provides some useful helpers for testing in github.com/diskfs/go-diskfs subpackages
|
Package testhelper provides some useful helpers for testing in github.com/diskfs/go-diskfs subpackages |