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.
Some examples:
1. Create a disk image of size 10MB with a FAT32 filesystem spanning the entire disk.
import diskfs "github.com/diskfs/go-diskfs" size := 10*1024*1024 // 10 MB diskImg := "/tmp/disk.img" disk := diskfs.Create(diskImg, size, diskfs.Raw, diskfs.SectorSizeDefault) fs, err := disk.CreateFilesystem(0, diskfs.TypeFat32) 2. 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. import diskfs "github.com/diskfs/go-diskfs" diskSize := 10*1024*1024 // 10 MB diskImg := "/tmp/disk.img" disk := diskfs.Create(diskImg, size, diskfs.Raw, diskfs.SectorSizeDefault) table := &mbr.Table{ LogicalSectorSize: 512, PhysicalSectorSize: 512, Partitions: []*mbr.Partition{ { Bootable: false, Type: Linux, Start: 2048, Size: 20480, }, }, } fs, err := disk.CreateFilesystem(1, diskfs.TypeFat32) 3. 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" import diskfs "github.com/diskfs/go-diskfs" diskSize := 10*1024*1024 // 10 MB diskImg := "/tmp/disk.img" disk := diskfs.Create(diskImg, size, diskfs.Raw, diskfs.SectorSizeDefault) table := &gpt.Table{ LogicalSectorSize: 512, PhysicalSectorSize: 512, Partitions: []*gpt.Partition{ { LogicalSectorSize: 512, PhysicalSectorSize: 512, ProtectiveMBR: true, }, }, } f, err := os.Open("/root/contents.dat") written, err := disk.WritePartitionContents(1, f) 4. 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. import diskfs "github.com/diskfs/go-diskfs" diskSize := 10*1024*1024 // 10 MB diskImg := "/tmp/disk.img" disk := diskfs.Create(diskImg, size, diskfs.Raw, diskfs.SectorSizeDefault) table := &mbr.Table{ LogicalSectorSize: 512, PhysicalSectorSize: 512, Partitions: []*mbr.Partition{ { Bootable: false, Type: Linux, Start: 2048, Size: 20480, }, }, } fs, err := disk.CreateFilesystem(1, diskfs.TypeFat32) err := fs.Mkdir("/FOO/BAR") rw, err := fs.OpenFile("/FOO/BAR/AFILE.EXE", os.O_CREATE|os.O_RDRWR) b := make([]byte, 1024, 1024) rand.Read(b) err := rw.Write(b)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Create ¶
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()
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 ¶
type OpenOpt func(o *openOpts) error
OpenOpt func that process Open options
func WithOpenMode ¶
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 ¶
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 ¶
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 |
Package util common utilities or other elements shared across github.com/diskfs/go-diskfs packages
|
Package util common utilities or other elements shared across github.com/diskfs/go-diskfs packages |