lima-vm/lima · error

failed to attach ASIF image %#q: %w

Error message

failed to attach ASIF image %#q: %w

What it means

NewAttachedASIF attaches an ASIF image with `diskutil image attach --noMount` and captures the device path from stdout. This error wraps a non-zero exit from that attach command. The image exists but could not be attached as a block device.

Source

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

			if err3 := os.Rename(path+".asif", path); err3 != nil {
				return fmt.Errorf("failed to rename ASIF image from %#q to %#q: %w", path+".asif", path, err3)
			}
		}
	}
	return nil
}

// 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
}

View on GitHub (pinned to dd909d0973)

Solutions

  1. Run `diskutil image list` / `hdiutil info` and detach stale attached images, then retry
  2. Run the attach command manually to see diskutil's stderr
  3. Reboot or kill stale `diskimages-helper` processes if attach persistently fails
  4. Verify the image is a valid ASIF (created successfully by NewASIF) and macOS supports image attach

Example fix

// cleanup stale attachments before retry
out, _ := exec.Command("diskutil", "image", "list", "-plist").Output()
// detach matching image ids, then:
device, f, err := asifutil.NewAttachedASIF(path, size)
Defensive patterns

Strategy: retry

Validate before calling

// preflight: check for already-attached images
out, _ := exec.Command("diskutil", "image", "list").Output()
if strings.Contains(string(out), filepath.Base(path)) { return errors.New("image already attached") }

Try / catch

device, f, err := asifutil.NewAttachedASIF(path, size)
if err != nil {
    if strings.Contains(err.Error(), "failed to attach") {
        time.Sleep(2 * time.Second) // transient attach failures: retry once after cleanup
    }
    return err
}

Prevention

When it happens

Trigger: Calling NewAttachedASIF (via convertTo) when diskutil attach fails: another image already attached with the same identity, diskimages-helper stuck, image corrupt or not a valid ASIF, or the macOS build lacks `diskutil image attach`.

Common situations: A previous run crashed without detaching and the system holds a stale attach; running inside a VM/container where diskutil cannot attach images; macOS version mismatch where the subcommand syntax differs.

Related errors


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