abiosoft/colima · error

error backing up kubeconfig: %w

Error message

error backing up kubeconfig: %w

What it means

Emitted during kubeconfig persistence: an existing kubeconfig file was detected at kubeconfFile (the first entry of $KUBECONFIG, else ~/.kube/config) and the host-level backup copy ('cp <kubeconfFile> <dir>/config-bak-<unix-time>') failed. Colima backs up your existing kubeconfig before overwriting it with the merged k3s one; a failed backup aborts the chain so your existing config is never clobbered without a safety copy.

Source

Thrown at environment/container/kubernetes/kubeconfig.go:91

		host := c.host.WithEnv(envVar)

		// get merged config
		kubeconfig, err := host.RunOutput("kubectl", "config", "view", "--raw")
		if err != nil {
			return err
		}

		// save
		return host.Write(tmpkubeconfFile, []byte(kubeconfig))
	})

	// backup current settings and save new config
	a.Add(func() error {
		// backup existing file if exists
		if stat, err := c.host.Stat(kubeconfFile); err == nil && !stat.IsDir() {
			backup := filepath.Join(filepath.Dir(tmpkubeconfFile), fmt.Sprintf("config-bak-%d", time.Now().Unix()))
			if err := c.host.Run("cp", kubeconfFile, backup); err != nil {
				return fmt.Errorf("error backing up kubeconfig: %w", err)
			}
		}
		// save new config
		if err := c.host.Run("cp", tmpkubeconfFile, kubeconfFile); err != nil {
			return fmt.Errorf("error updating kubeconfig: %w", err)
		}

		return nil
	})

	// set new context
	conf, _ := ctx.Value(config.CtxKey()).(config.Config)
	if conf.AutoActivate() {
		a.Add(func() error {
			out, err := c.host.RunOutput("kubectl", "config", "use-context", profile)
			if err != nil {
				return err
			}

View on GitHub (pinned to c3a5f9184d)

Solutions

  1. Check ownership and permissions: 'ls -la ~/.kube ~/.kube/.colima*' and fix with 'sudo chown -R $(id -u):$(id -g) ~/.kube' if a previous sudo run owned it.
  2. Ensure the directory of $KUBECONFIG (if set) is writable: 'touch "$(dirname "${KUBECONFIG%%:*}")/.wtest" && rm ...'.
  3. Free disk space ('df -h ~') if the volume is full.
  4. Unset or correct a bogus KUBECONFIG ('echo $KUBECONFIG') and re-run 'colima start --kubernetes' — on success the previous config-bak-* files in ~/.kube/.<profile>/ hold any prior backups.

Example fix

# before
$ sudo colima start --kubernetes   # later cp fails: ~/.kube/.colima owned by root
# after
$ sudo chown -R "$(id -u):$(id -g)" ~/.kube
$ colima start --kubernetes
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure the backup directory is writable before starting kubernetes
info, err := os.Stat(filepath.Join(hostHome, ".kube"))
if err == nil && info.Mode().Perm()&0200 == 0 {
    return fmt.Errorf("~/.kube is not writable; fix permissions first")
}

Try / catch

if err := c.host.Run("cp", kubeconfFile, backup); err != nil {
    // do not abort the whole start for a backup failure; proceed without backup but warn
    log.Warnln(fmt.Errorf("error backing up kubeconfig: %w", err))
}

Prevention

When it happens

Trigger: ~/.kube (or the dir containing the temp profile dir, i.e. ~/.kube/.<profile>/) is read-only or owned by another user (running colima under sudo writes earlier steps as root, leaving root-owned files); KUBECONFIG points to a path in a directory you cannot write to; disk full; the resolved directory was removed between the mkdir step and the cp; macOS SIP/managed corporate machines protecting the path.

Common situations: Previously ran colima (or kubectl) with sudo so ~/.kube or ~/.kube/.colima is root-owned; KUBECONFIG set to a file inside a mounted/read-only location; home directory quota exhausted; multiple profiles where an earlier partial run left a stale, unwritable temp dir.

Related errors


AI-assisted analysis of abiosoft/colima@c3a5f9184d (2026-08-15). Data as JSON: /api/errors/f126ac4b2f3ea5a9. Report an issue: GitHub.