lima-vm/lima · warning

failed to detach ASIF image %#q: %w: %s

Error message

failed to detach ASIF image %#q: %w: %s

What it means

DetachASIF detaches an attached ASIF image using `hdiutil detach <devicePath>`. This error wraps a non-zero exit and includes the combined output for diagnosis. It means the image device could not be detached.

Source

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

	}
	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)
	}
	return nil
}

type AttachedDisk struct {
	Disk      string // whole disk device, e.g. "disk4" (GUID_partition_scheme)
	Container string // APFS container partition, e.g. "disk4s2" (Apple_APFS)
	Data      string // data volume device, e.g. "disk7s5"
}

View on GitHub (pinned to dd909d0973)

Solutions

  1. Close all file handles on the device (including f from NewAttachedASIF) before detaching
  2. Retry the detach after a short delay (device busy is often transient)
  3. Check the included hdiutil output; if it suggests force, run `hdiutil detach -force <devicePath>` manually
  4. Ignore the error if it is 'not attached' during cleanup paths

Example fix

// before
f.Close()
_ = asifutil.DetachASIF(devicePath)
// after
f.Close()
time.Sleep(500 * time.Millisecond)
if err := asifutil.DetachASIF(devicePath); err != nil {
    log.Printf("detach failed (may need force): %v", err)
}
Defensive patterns

Strategy: retry

Validate before calling

// preflight: ensure no open handles remain before detaching
if f != nil { f.Close() }

Try / catch

if err := asifutil.DetachASIF(devicePath); err != nil {
    time.Sleep(time.Second)
    if err2 := asifutil.DetachASIF(devicePath); err2 != nil {
        log.Printf("detach failed, may need `hdiutil detach -force`: %v", err2) // non-fatal in cleanup
    }
}

Prevention

When it happens

Trigger: Calling DetachASIF (from NewAttachedASIF's cleanup path, convertTo, or other callers) when hdiutil detach fails: the device is busy (open file handles), already detached, or the devicePath is invalid.

Common situations: Processes still holding the device open during teardown; double-detach after a partial failure; forced detach needed because a mount or helper is stuck.

Related errors


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