storage

package
v0.0.0-...-d707387 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2024 License: Apache-2.0 Imports: 8 Imported by: 1

README

Glazier: Storage Package

The Glazier Storage package is a wrapper for Microsoft's Windows Storage Management API.

Example Usage

// Connect the API
svc, err := storage.Connect()
if err != nil {
  return err
}
defer svc.Close()

// Retrieve a disk
d, err := svc.GetDisks("WHERE Number=1")
if err != nil {
  return err
}
defer d.Close()

if len(d.Disks) < 1 {
  return errors.New("no disks found")
}
disk := d.Disks[0]

// Initialize the disk
if _, err := disk.Initialize(storage.MbrStyle); err != nil {
  return err
}

// Create a partition and assign drive letter D
part, _, err := disk.CreatePartition(0, true, 0, 0, "d", false, &storage.MbrTypes.IFS, nil, false, true)
if err != nil {
  return err
}
defer part.Close()

// Get the new volume
vset, err := svc.GetVolumes("WHERE DriveLetter='D'")
if err != nil {
  return err
}
defer vset.Close()

if len(vset.Volumes) < 1 {
  return errors.New("no volumes found")
}

// Format the volume with NTFS
fv, _, err := vset.Volumes[0].Format("NTFS", "", 0, false, true, false, false, false, false, false)
if err != nil {
  return err
}
defer fv.Close()

Documentation

Overview

Package storage provides storage management functionality.

Index

Constants

This section is empty.

Variables

View Source
var GptTypes = struct {
	// SystemPartition is the Windows system partition.
	SystemPartition GptType
	// MicrosoftReserved is the Microsoft Reserved partition.
	MicrosoftReserved GptType
	// BasicData is a basic data partition.
	BasicData GptType
	// LDMMetadata is a Logical Disk Manager (LDM) metadata partition on a dynamic disk.
	LDMMetadata GptType
	// LDMData is an LDM data partition on a dynamic disk.
	LDMData GptType
	// MicrosoftRecovery is the Windows recovery partition.
	MicrosoftRecovery GptType
}{
	SystemPartition:   "{c12a7328-f81f-11d2-ba4b-00a0c93ec93b}",
	MicrosoftReserved: "{e3c9e316-0b5c-4db8-817d-f92df00215ae}",
	BasicData:         "{ebd0a0a2-b9e5-4433-87c0-68b6b72699c7}",
	LDMMetadata:       "{5808c8aa-7e8f-42e0-85d2-e1e90434cfb3}",
	LDMData:           "{af9b60a0-1431-4f62-bc68-3311714a69ad}",
	MicrosoftRecovery: "{de94bba4-06d1-4d40-a16a-bfd50179d6ac}",
}

GptTypes holds the known GPT partition types.

View Source
var MbrTypes = struct {
	// FAT12 is a FAT12 file system partition.
	FAT12 MbrType
	// FAT16 is a FAT16 file system partition.
	FAT16 MbrType
	// Extended is an extended partition.
	Extended MbrType
	// Huge is a huge partition. Use this value when creating a logical volume.
	Huge MbrType
	// IFS is an NTFS or ExFAT partition.
	IFS MbrType
	// FAT32 is a FAT32 partition.
	FAT32 MbrType
}{
	FAT12:    1,
	FAT16:    4,
	Extended: 5,
	Huge:     6,
	IFS:      7,
	FAT32:    12,
}

MbrTypes holds the known MBR partition types.

Functions

This section is empty.

Types

type BusType

type BusType int

BusType describes a Bus Type

Ref: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/msft-disk

const (
	// Unknown is a type of Bus
	Unknown BusType = iota
	// SCSI is a type of Bus
	SCSI
	// ATAPI is a type of Bus
	ATAPI
	// ATA is a type of Bus
	ATA
	// Firewire is a type of Bus
	Firewire
	// SSA is a type of Bus
	SSA
	// FibreChannel is a type of Bus
	FibreChannel
	// USB is a type of Bus
	USB
	// RAID is a type of Bus
	RAID

	// SAS is a type of Bus
	SAS
	// SATA is a type of Bus
	SATA
	// SD is a type of Bus
	SD
	// MMC is a type of Bus
	MMC
	// Virtual is a type of Bus
	Virtual
	// FileBackedVirtual is a type of Bus
	FileBackedVirtual
	// StorageSpaces is a type of Bus
	StorageSpaces
	// NVMe is a type of Bus
	NVMe
)

func BusTypeValue

func BusTypeValue(b string) (BusType, error)

BusTypeValue returns a BusType value from the human readable Bus Type name.

Ref: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/msft-disk

type Disk

type Disk struct {
	Path               string
	Location           string
	FriendlyName       string
	UniqueID           string
	UniqueIDFormat     int32
	Number             int32
	SerialNumber       string
	FirmwareVersion    string
	Manufacturer       string
	Model              string
	Size               uint64
	AllocatedSize      uint64
	LogicalSectorSize  int32
	PhysicalSectorSize int32
	LargestFreeExtent  uint64
	NumberOfPartitions int32
	ProvisioningType   int32
	OperationalStatus  int32
	HealthStatus       int32
	BusType            int32
	PartitionStyle     int32
	Signature          int32
	GUID               string
	IsOffline          bool
	OfflineReason      int32
	IsReadOnly         bool
	IsSystem           bool
	IsClustered        bool
	IsBoot             bool
	BootFromDisk       bool
	// contains filtered or unexported fields
}

Disk represents a MSFT_Disk object.

Ref: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/msft-disk

func (*Disk) Clear

func (d *Disk) Clear(removeData, removeOEM, zeroDisk bool) (ExtendedStatus, error)

Clear wipes a disk and all its contents.

Example:

d.Clear(true, true, true)

Ref: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/clear-msft-disk

func (*Disk) Close

func (d *Disk) Close()

Close releases the handle to the disk.

func (*Disk) ConvertStyle

func (d *Disk) ConvertStyle(style PartitionStyle) (ExtendedStatus, error)

ConvertStyle converts the partition style of an already initialized disk.

Ref: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/msft-disk-convertstyle

func (*Disk) CreatePartition

func (d *Disk) CreatePartition(size uint64, useMaximumSize bool, offset uint64, alignment int, driveLetter string, assignDriveLetter bool,
	mbrType *MbrType, gptType *GptType, hidden, active bool) (Partition, ExtendedStatus, error)

CreatePartition creates a partition on a disk.

If successful, the partition is returned as a new Partition object. The new Partition must be Closed().

Creating a GPT Basic Data partition, 100000000b size, drive letter "e:":

d.CreatePartition(100000000, false, 0, 0, "e", false, nil, &storage.GptTypes.BasicData, false, false)

Creating an MBR FAT32 partition, full available space, marked active, with auto-assigned drive letter:

CreatePartition(0, true, 0, 0, "", true, &storage.MbrTypes.FAT32, nil, false, true)

Ref: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/createpartition-msft-disk

func (*Disk) Initialize

func (d *Disk) Initialize(style PartitionStyle) (ExtendedStatus, error)

Initialize initializes a new disk.

Example:

d.Initialize(storage.GptStyle)

Ref: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/initialize-msft-disk

func (*Disk) Offline

func (d *Disk) Offline() (ExtendedStatus, error)

Offline takes the disk offline.

Example:

d.Offline()

Ref: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/msft-disk-offline

func (*Disk) Online

func (d *Disk) Online() (ExtendedStatus, error)

Online brings the disk online.

Example:

d.Online()

Ref: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/msft-disk-online

func (*Disk) Query

func (d *Disk) Query() error

Query reads and populates the disk state.

func (*Disk) Refresh

func (d *Disk) Refresh() (ExtendedStatus, error)

Refresh refreshes the cached disk layout information.

Example:

d.Refresh()

Ref: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/msft-disk-refresh

type DiskSet

type DiskSet struct {
	Disks []Disk
}

A DiskSet contains one or more Disks.

func (*DiskSet) Close

func (s *DiskSet) Close()

Close releases all Disk handles inside a DiskSet.

type DriveType

type DriveType int

DriveType describes a Drive Type

Ref: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/msft-volume

const (
	// UnknownDriveType is a type of Drive Type
	UnknownDriveType DriveType = iota
	// Invalid is a type of Drive Type
	Invalid
	// Removable is a type of Drive Type
	Removable
	// Fixed is a type of Drive Type
	Fixed
	// Remote is a type of Drive Type
	Remote
	// CDROM is a type of Drive Type
	CDROM
	// RAM is a type of Drive Type
	RAM
)

type ExtendedStatus

type ExtendedStatus struct{}

ExtendedStatus is a placeholder for MSFT_StorageExtendedStatus

Ref: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/msft-storageextendedstatus

type GptType

type GptType string

GptType describes a GPT partition type.

type MbrType

type MbrType int

MbrType describes an MBR partition type.

type OperationalStatus

type OperationalStatus uint16

OperationalStatus describes an operational status.

Ref: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/msft-partition

const (
	// UnknownOperationalStatus is a type of Operational Status
	UnknownOperationalStatus OperationalStatus = 0
	// OnlineOperationalStatus is a type of Operational Status
	OnlineOperationalStatus OperationalStatus = 1
	// NoMediaOperationalStatus is a type of Operational Status
	NoMediaOperationalStatus OperationalStatus = 3
	// FailedOperationalStatus is a type of Operational Status
	FailedOperationalStatus OperationalStatus = 5
	// OfflineOperationalStatus is a type of Operational Status
	OfflineOperationalStatus OperationalStatus = 4
)

type Partition

type Partition struct {
	DiskNumber           int32
	PartitionNumber      int32
	DriveLetter          string
	AccessPaths          []string
	OperationalStatus    int32
	TransitionState      int32
	Size                 uint64
	MbrType              int32
	GptType              string
	GUID                 string
	IsReadOnly           bool
	IsOffline            bool
	IsSystem             bool
	IsBoot               bool
	IsActive             bool
	IsHidden             bool
	IsShadowCopy         bool
	NoDefaultDriveLetter bool
	// contains filtered or unexported fields
}

Partition represents a MSFT_Partition object.

Ref: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/msft-partition

func (*Partition) AddAccessPath

func (p *Partition) AddAccessPath(accessPath string, autoAssign bool) (ExtendedStatus, error)

AddAccessPath adds a mount path or drive letter assignment to the partition.

Example: assign a Drive letter with D:

p.AddAccessPath("D:", false)

Example: Automatically assign the next available Drive Letter:

p.AddAccessPath("", true)

Note: You cannot specify both a valid drive letter and auto assignment as true together.

Ref: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/addaccesspath-msft-partition

func (*Partition) Close

func (p *Partition) Close()

Close releases the handle to the partition.

func (*Partition) GetSupportedSize

func (p *Partition) GetSupportedSize() (PartitionSupportedSize, ExtendedStatus, error)

GetSupportedSize retrieves the minimum and maximum sizes that the partition can be resized to

Ref: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/msft-partition-getsupportedsizes

func (*Partition) Offline

func (p *Partition) Offline() (ExtendedStatus, error)

Offline takes the partition offline.

Example:

p.Offline()

Ref: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/msft-partition-offline

func (*Partition) Online

func (p *Partition) Online() (ExtendedStatus, error)

Online brings the partition online by mounting the associated volume (if one exists).

Example:

p.Online()

Ref: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/msft-partition-online

func (*Partition) Query

func (p *Partition) Query() error

Query reads and populates the partition state.

func (*Partition) RemoveAccessPath

func (p *Partition) RemoveAccessPath(accessPath string) (ExtendedStatus, error)

RemoveAccessPath removes the access path from the partition.

Example: Remove the driveLetter of D: from a partition

p.RemoveAccessPath("D:")

Ref: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/removeaccesspath-msft-partition

func (*Partition) Resize

func (p *Partition) Resize(size uint64) (ExtendedStatus, error)

Resize attempts to resize a partition.

Ref: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/msft-partition-resize

type PartitionSet

type PartitionSet struct {
	Partitions []Partition
}

A PartitionSet contains one or more Partitions.

func (*PartitionSet) Close

func (s *PartitionSet) Close()

Close releases all Partition handles inside a PartitionSet.

type PartitionStyle

type PartitionStyle int32

PartitionStyle represents the partition scheme to be used for a disk.

const (
	// MbrStyle represents the MBR partition style for a disk.
	MbrStyle PartitionStyle = 1
	// GptStyle represents the GPT partition style for a disk.
	GptStyle PartitionStyle = 2
	// UnknownStyle represents an unknown partition style.
	UnknownStyle PartitionStyle = 0
)

type PartitionSupportedSize

type PartitionSupportedSize struct {
	SizeMin uint64
	SizeMax uint64
}

PartitionSupportedSize represents the minimum and maximum sizes a partition can be resized to.

Ref: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/msft-partition-getsupportedsizes

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service represents a connection to the host Storage service (in WMI).

func Connect

func Connect() (Service, error)

Connect connects to the WMI provider for managing storage objects. You must call Close() to release the provider when finished.

Example: storage.Connect()

func (*Service) Close

func (svc *Service) Close()

Close frees all resources associated with a volume.

func (Service) GetDisks

func (svc Service) GetDisks(filter string) (DiskSet, error)

GetDisks queries for local disks.

Close() must be called on the resulting DiskSet to ensure all disks are released.

Get all disks:

svc.GetDisks("")

To get specific disks, provide a valid WMI query filter string, for example:

svc.GetDisks("WHERE Number=1")
svc.GetDisks("WHERE IsSystem=True")

func (*Service) GetPartitions

func (svc *Service) GetPartitions(filter string) (PartitionSet, error)

GetPartitions queries for local partitions.

Close() must be called on the resulting PartitionSet to ensure all disks are released.

Get all partitions:

svc.GetPartitions("")

To get specific partitions, provide a valid WMI query filter string, for example:

svc.GetPartitions("WHERE DiskNumber=1")

func (Service) GetVolumes

func (svc Service) GetVolumes(filter string) (VolumeSet, error)

GetVolumes queries for local volumes.

Close() must be called on the resulting VolumeSet to ensure all volumes are released.

Get all volumes:

svc.GetVolumes("")

To get specific volumes, provide a valid WMI query filter string, for example:

svc.GetVolumes("WHERE DriveLetter=D")

type Volume

type Volume struct {
	DriveLetter     string
	Path            string
	HealthStatus    int32
	FileSystem      string
	FileSystemLabel string
	FileSystemType  int32
	Size            uint64
	SizeRemaining   uint64
	DriveType       int32
	DedupMode       int32
	// contains filtered or unexported fields
}

Volume represents a MSFT_Volume object.

Ref: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/msft-volume

func (*Volume) Close

func (v *Volume) Close()

Close releases the handle to the volume.

func (*Volume) Flush

func (v *Volume) Flush() error

Flush flushes the cached data in the volume's file system to disk.

Ref: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/msft-volume-flush

func (*Volume) Format

func (v *Volume) Format(
	fs string,
	fsLabel string,
	allocationUnitSize int32,
	full bool,
	force bool,
	compress interface{},
	shortFileNameSupport interface{},
	setIntegrityStreams interface{},
	useLargeFRS interface{},
	disableHeatGathering interface{}) (Volume, ExtendedStatus, error)

Format formats a volume.

You may want to use one of the filesystem-specific helpers instead of calling this directly.

fs can be one of "ExFAT", "FAT", "FAT32", "NTFS", "ReFS". Set allocationUnitSize to 0 for default.

Note: The Windows API requires any parameters not supported by a given filesystem to be nil (NOT zero value). To enable this here, any non-universal parameters are implemented as interfaces and must be passed as either the correct type for the underlying API field or nil. Attempting to pass a field to a filesystem that doesn't support it will result in a vague and unhelpful code 1 (unsupported) from the API.

If successful, the formatted volume is returned as a new Volume object. Close() must be called on the new Volume.

Ref: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/format-msft-volume

func (*Volume) FormatFAT32

func (v *Volume) FormatFAT32(label string, allocationUnitSize int32, full, force bool) (Volume, ExtendedStatus, error)

FormatFAT32 is a helper for calling Format using only the options supported by FAT32.

func (*Volume) FormatNTFS

func (v *Volume) FormatNTFS(label string, allocationUnitSize int32, full, force, compress, shortFileNameSupport, useLargeFRS, disableHeatGathering bool) (Volume, ExtendedStatus, error)

FormatNTFS is a helper for calling Format using only the options supported by NTFS.

func (*Volume) FormatReFS

func (v *Volume) FormatReFS(label string, allocationUnitSize int32, full, force, setIntegrityStreams, disableHeatGathering bool) (Volume, ExtendedStatus, error)

FormatReFS is a helper for calling Format using only the options supported by ReFS.

func (*Volume) Optimize

func (v *Volume) Optimize(reTrim, analyze, defrag, slabConslidate, tierOptimize bool) (ExtendedStatus, error)

Optimize optimizes the volume.

Ref: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/optimize-msft-volume

func (*Volume) Query

func (v *Volume) Query() error

Query reads and populates the volume state.

func (*Volume) SetFileSystemLabel

func (v *Volume) SetFileSystemLabel(fileSystemLabel string) (ExtendedStatus, error)

SetFileSystemLabel Sets the file system label for the volume.

Ref: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/msft-volume-setfilesystemlabel

type VolumeSet

type VolumeSet struct {
	Volumes []Volume
}

A VolumeSet contains one or more Volumes.

func (*VolumeSet) Close

func (s *VolumeSet) Close()

Close releases all Volume handles inside a VolumeSet.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL