hashicorp/packer · error

Failed to copy %s data to archive: %s

Error message

Failed to copy %s data to archive: %s

What it means

After successfully writing the tar header, createTarArchive streams the file body into the archive with io.Copy(archive, file). If copying bytes to the tar (and its compression) writer fails, the post-processor returns this error naming the file path and cause. The archive is incomplete and the build fails.

Source

Thrown at post-processor/compress/post-processor.go:406

		fi, err := file.Stat()
		if err != nil {
			return fmt.Errorf("Unable to get fileinfo for %s: %s", path, err)
		}

		header, err := tar.FileInfoHeader(fi, path)
		if err != nil {
			return fmt.Errorf("Failed to create tar header for %s: %s", path, err)
		}

		// workaround for archive format on go >=1.10
		setHeaderFormat(header)

		if err := archive.WriteHeader(header); err != nil {
			return fmt.Errorf("Failed to write tar header for %s: %s", path, err)
		}

		if _, err := io.Copy(archive, file); err != nil {
			return fmt.Errorf("Failed to copy %s data to archive: %s", path, err)
		}
	}
	return nil
}

func createZipArchive(files []string, output io.WriteCloser) error {
	archive := zip.NewWriter(output)
	defer archive.Close()

	for _, path := range files {
		path = filepath.ToSlash(path)

		source, err := os.Open(path)
		if err != nil {
			return fmt.Errorf("Unable to read file %s: %s", path, err)
		}
		defer source.Close()

View on GitHub (pinned to eb36e3c3e4)

Solutions

  1. Free disk space on the output path and verify the storage target is writable and reachable.
  2. Re-run the build once the underlying storage issue is fixed.
  3. Read the wrapped cause in the message to identify whether it's a read or write failure.
  4. Keep the compress post-processor's output on local/reliable storage rather than flaky network mounts.

Example fix

// before: output to a small tmpfs
{"type": "compress", "output": "/tmp/artifact.tar.gz"} // tmpfs too small for 10GB image
// after
{"type": "compress", "output": "/var/artifacts/artifact.tar.gz"}
Defensive patterns

Strategy: validation

Validate before calling

// preflight disk space before compressing
const stat = fs.statfsSync(outDir)
if (stat.bavail * stat.bsize) < requiredBytes throw new Error('insufficient space for archive')

Prevention

When it happens

Trigger: io.Copy from the opened source file to archive.Writer returns an error — usually the output stream broke (disk full, pipe closed, compression writer error) or a read error occurred on the source file.

Common situations: Output volume fills while writing a large archive; NFS/network storage drops mid-write; source file truncated or unreadable after os.Open succeeded (e.g. file changed on disk); gzip/zstd writer hit an internal error that surfaces here.

Related errors


AI-assisted analysis of hashicorp/packer@eb36e3c3e4 (2026-09-05). Data as JSON: /api/errors/0c896a1b4440481e. Report an issue: GitHub.