lima-vm/lima · error

failed to open ASIF device %#q: %w

Error message

failed to open ASIF device %#q: %w

What it means

After attaching, NewAttachedASIF opens the reported device path with O_RDWR to use it as a raw disk. This error wraps the os.OpenFile failure on that device. Attach reported success, but the device node cannot be opened read/write.

Source

Thrown at pkg/imgutil/nativeimgutil/asifutil/asif_darwin.go:53

}

// NewAttachedASIF creates a new ASIF image file at the specified path with the given size
// and attaches it, returning the attached device path and an open file handle.
// The caller is responsible for detaching the ASIF image device when done.
func NewAttachedASIF(path string, size int64) (string, *os.File, error) {
	if err := NewASIF(path, size); err != nil {
		return "", nil, err
	}
	attachArgs := []string{"image", "attach", "--noMount", path}
	out, err := exec.CommandContext(context.Background(), "diskutil", attachArgs...).Output()
	if err != nil {
		return "", nil, fmt.Errorf("failed to attach ASIF image %#q: %w", path, err)
	}
	devicePath := strings.TrimSpace(string(out))
	f, err := os.OpenFile(devicePath, os.O_RDWR, 0o644)
	if err != nil {
		_ = DetachASIF(devicePath)
		return "", nil, fmt.Errorf("failed to open ASIF device %#q: %w", devicePath, err)
	}
	return devicePath, f, err
}

// DetachASIF detaches the ASIF image device at the specified path.
func DetachASIF(devicePath string) error {
	if output, err := exec.CommandContext(context.Background(), "hdiutil", "detach", devicePath).CombinedOutput(); err != nil {
		return fmt.Errorf("failed to detach ASIF image %#q: %w: %s", devicePath, err, output)
	}
	return nil
}

// ResizeASIF resizes the ASIF image at the specified path to the given size.
func ResizeASIF(path string, size int64) error {
	resizeArgs := []string{"image", "resize", "--size", fmt.Sprintf("%d", size), path}
	if output, err := exec.CommandContext(context.Background(), "diskutil", resizeArgs...).CombinedOutput(); err != nil {
		return fmt.Errorf("failed to resize ASIF image %#q: %w: %s", path, err, output)
	}

View on GitHub (pinned to dd909d0973)

Solutions

  1. Grant the terminal/process Full Disk Access so it can open raw disk devices
  2. Inspect the parsed devicePath (log it) and verify it exists as a /dev node
  3. Re-run manually: `diskutil image attach --noMount <path>` and compare output format
  4. Run as a user with disk access or adjust sandbox/entitlements

Example fix

// before
devicePath, f, err := asifutil.NewAttachedASIF(path, size)
// after
devicePath, f, err := asifutil.NewAttachedASIF(path, size)
if err != nil && strings.Contains(err.Error(), "failed to open ASIF device") {
    log.Printf("check Full Disk Access; device path parsing may need update")
}
Defensive patterns

Strategy: try-catch

Validate before calling

// preflight: verify the process can open raw disk devices
if _, err := os.OpenFile("/dev/disk0", os.O_RDONLY, 0); err != nil { log.Printf("no raw disk access; grant Full Disk Access") }

Try / catch

device, f, err := asifutil.NewAttachedASIF(path, size)
if err != nil && strings.Contains(err.Error(), "failed to open ASIF device") {
    log.Printf("open failed for %q: check Full Disk Access / device path parsing", device)
    return err
}

Prevention

When it happens

Trigger: NewAttachedASIF gets a devicePath from diskutil output, but os.OpenFile fails: empty/incorrect device path parsed from output, permissions (process not permitted to open the disk device), or the device disappeared immediately after attach.

Common situations: Full Disk Access / permissions changes on newer macOS preventing access to /dev/disk*; output format change in diskutil causing a wrong trimmed path (e.g. with a newline suffix or BSD name instead of device node); sandboxed processes.

Related errors


AI-assisted analysis of lima-vm/lima@dd909d0973 (2026-09-01). Data as JSON: /api/errors/04b48e8a418d1f29. Report an issue: GitHub.