Documentation
¶
Overview ¶
Package diskimages provides zero-cgo Darwin wrappers for inspecting attached disk images via DiskImages2 and unmounting or detaching them via DiskArbitration.
ListAttached enumerates currently attached disk image devices using DiskImages2's DIAttachedDeviceInfo and enriches each entry with read-only state from DiskArbitration when available. DeviceByBSDName looks up a single attached disk image by BSD device name (such as "disk4" or "/dev/disk4").
Unmount, Eject, and Detach perform DiskArbitration unmount and eject operations without shelling out to hdiutil or diskutil. Detach unmounts all volumes on the whole disk (treating already-unmounted disks as ready) and then ejects the whole disk.
On non-Darwin platforms, Available returns false and all operations return ErrUnsupported.
Basic usage:
devices, err := diskimages.ListAttached()
if err != nil {
log.Fatal(err)
}
for _, d := range devices {
fmt.Printf("%s -> %s (%d bytes, readonly=%v)\n",
d.BSDName, d.ImagePath, d.MediaSize, d.ReadOnly)
}
if err := diskimages.Detach(ctx, "disk4", diskimages.DetachOptions{Force: true}); err != nil {
log.Fatal(err)
}
Example ¶
package main
import (
"fmt"
"log"
"github.com/tmc/apple/x/diskimages"
)
func main() {
if !diskimages.Available() {
fmt.Println("unavailable")
return
}
devices, err := diskimages.ListAttached()
if err != nil {
log.Fatal(err)
}
fmt.Printf("found %d attached disk image(s)\n", len(devices))
}
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrUnsupported is returned on non-Darwin platforms or when the required // Apple framework is not available. ErrUnsupported = errors.New("diskimages: unsupported on this platform") // ErrNotFound is returned when the requested BSD disk device cannot be found. ErrNotFound = errors.New("diskimages: device not found") // ErrInvalidBSDName is returned when a BSD device name is empty or malformed. ErrInvalidBSDName = errors.New("diskimages: invalid BSD device name") )
Functions ¶
func Available ¶
func Available() bool
Available reports whether DiskImages2.framework is loaded and usable.
func Detach ¶
func Detach(_ context.Context, _ string, _ DetachOptions) error
Detach unmounts all volumes on the whole disk identified by bsdName and then ejects it using DiskArbitration.
Example ¶
package main
import (
"context"
"log"
"time"
"github.com/tmc/apple/x/diskimages"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
if err := diskimages.Detach(ctx, "/dev/disk4", diskimages.DetachOptions{Force: true}); err != nil {
log.Fatal(err)
}
}
Output:
Types ¶
type AttachedDevice ¶
type AttachedDevice struct {
// BSDName is the whole-disk BSD device name (for example, "disk4").
BSDName string
// ImagePath is the local filesystem path of the backing disk image, if any.
ImagePath string
// ImageURL is the URL string of the backing disk image.
ImageURL string
// ShadowPath is the local filesystem path of the shadow file, if any.
ShadowPath string
// CachePath is the local filesystem path of the cache file, if any.
CachePath string
// MediaSize is the media size in bytes, if reported.
MediaSize int64
// BlockSize is the logical block size in bytes, if reported.
BlockSize int64
// PID is the process ID of the backing diskimages daemon, if reported.
PID int
// FrameworkNum identifies the DiskImages framework version (1 or 2).
FrameworkNum int64
// InstanceID is the framework instance identifier for the attached image.
InstanceID string
// ReadOnly reports whether the attached media is read-only when
// determinable from DiskArbitration.
ReadOnly bool
}
AttachedDevice describes a currently attached disk image device.
func DeviceByBSDName ¶
func DeviceByBSDName(_ string) (*AttachedDevice, error)
DeviceByBSDName looks up a single currently attached disk image by its BSD device name.
func ListAttached ¶
func ListAttached() ([]AttachedDevice, error)
ListAttached returns information about all currently attached disk images.
type DetachOptions ¶
type DetachOptions struct {
// Force forces unmounting of busy volumes before ejecting the whole disk.
Force bool
}
DetachOptions configures Detach.
type DissenterError ¶
type DissenterError struct {
// Operation is the DiskArbitration operation that was rejected
// ("unmount" or "eject").
Operation string
// BSDName is the normalized BSD device name targeted by the operation.
BSDName string
// Status is the raw DAReturn status code from the dissenter.
Status int32
// StatusName is the human-readable DAReturn constant name, if known.
StatusName string
// Message is the optional status string supplied by the dissenter.
Message string
}
DissenterError reports a rejection from DiskArbitration during an unmount or eject operation.
func (*DissenterError) Error ¶
func (e *DissenterError) Error() string
type UnmountOptions ¶
type UnmountOptions struct {
// Force unmounts even if files are still open on the volume.
Force bool
// Whole unmounts all volumes tied to the whole disk object.
Whole bool
}
UnmountOptions configures Unmount.