hashicorp/nomad · error

could not copy file: %v

Error message

could not copy file: %v

What it means

After creating the destination file, CopyDir streams the source contents with io.Copy. If reading the source or writing to the destination fails, the walk aborts with 'could not copy file: %v' wrapping the io error. This surfaces disk-full, permission, and mid-copy I/O failures.

Source

Thrown at helper/escapingfs/copydir.go:54

		r, err := srcFs.Open(oldPath)
		if err != nil {
			return fmt.Errorf("could not open existing file: %v", err)
		}
		defer r.Close()
		info, err := r.Stat()
		if err != nil {
			return fmt.Errorf("could not stat file: %v", err)
		}

		w, err := os.OpenFile(newPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, info.Mode())
		if err != nil {
			return err
		}

		if _, err := io.Copy(w, r); err != nil {
			w.Close()
			return fmt.Errorf("could not copy file: %v", err)
		}
		return w.Close()
	})
}

View on GitHub (pinned to 482b49bf1a)

Solutions

  1. Check free space on the destination filesystem (`df -h`) and free space or move the copy target to a volume with room
  2. Fix destination permissions/ownership so the process can write the file contents
  3. Check source media health and remount/retry if a network filesystem dropped
  4. Run `dmesg`/storage diagnostics if errors persist; check quotas

Example fix

// before
$ df -h /var/lib/nomad
/dev/sda1  10G  10G  0  100% /var/lib/nomad
// after
$ rm -rf /var/lib/nomad/tmp/*   # or expand the volume
$ df -h /var/lib/nomad
/dev/sda1  10G  4G  6G  40% /var/lib/nomad
Defensive patterns

Strategy: validation

Validate before calling

var st syscall.Statfs_t
if err := syscall.Statfs(dstRoot, &st); err != nil { return err }
avail := int64(st.Bavail) * int64(st.Bsize)
needed := dirSize(src) // sum of source file sizes
if avail < needed*2 { return fmt.Errorf("insufficient space: need %d, have %d", needed*2, avail) }

Try / catch

if err := escapingfs.CopyDir(src, dst); err != nil {
    if strings.HasPrefix(err.Error(), "could not copy file") {
        // typically ENOSPC or write failure: free space / check dest volume, then retry
        log.Error("copy failed", "err", err)
        return fmt.Errorf("copy failed, check dest disk space: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: CopyDir where io.Copy fails: destination filesystem is full (ENOSPC), destination path permissions deny writing after open, source file read error mid-stream (NFS drop, disk error), or process resource limits.

Common situations: Host disk full when copying client task directories; quota-limited volumes; copy interrupted by storage failure; source on a flaky network mount; large files hitting disk quotas.

Related errors


AI-assisted analysis of hashicorp/nomad@482b49bf1a (2026-09-04). Data as JSON: /api/errors/4256bcecba487723. Report an issue: GitHub.