hyperledger/fabric · error

error while creating final dir for snapshot:%s

Error message

error while creating final dir for snapshot:%s

What it means

After staging the snapshot in a temp dir, generateSnapshot creates the ledger's final snapshots directory via os.MkdirAll before renaming the temp dir into it. This error wraps an os.MkdirAll failure on the per-ledger snapshots directory.

Source

Thrown at core/ledger/kvledger/snapshot.go:144

	if err != nil {
		return err
	}
	logger.Debugw("Exported public state and private state hashes", "channelID", l.ledgerID)

	if err := l.generateSnapshotMetadataFiles(
		snapshotTempDir, txIDsExportSummary,
		configsHistoryExportSummary, stateDBExportSummary,
	); err != nil {
		return err
	}
	logger.Debugw("Generated metadata files", "channelID", l.ledgerID)

	if err := fileutil.SyncDir(snapshotTempDir); err != nil {
		return err
	}
	slgr := SnapshotsDirForLedger(snapshotsRootDir, l.ledgerID)
	if err := os.MkdirAll(slgr, 0o755); err != nil {
		return errors.Wrapf(err, "error while creating final dir for snapshot:%s", slgr)
	}
	if err := fileutil.SyncParentDir(slgr); err != nil {
		return err
	}
	slgrht := SnapshotDirForLedgerBlockNum(snapshotsRootDir, l.ledgerID, lastBlockNum)
	if err := os.Rename(snapshotTempDir, slgrht); err != nil {
		return errors.Wrapf(err, "error while renaming dir [%s] to [%s]:", snapshotTempDir, slgrht)
	}
	return fileutil.SyncParentDir(slgrht)
}

func (l *kvLedger) generateSnapshotMetadataFiles(
	dir string,
	txIDsExportSummary,
	configsHistoryExportSummary,
	stateDBExportSummary map[string][]byte,
) error {
	// generate metadata file

View on GitHub (pinned to 2736b63f8f)

Solutions

  1. Inspect the path <snapshotsRootDir>/<ledgerID>/snapshots — remove any conflicting file and fix permissions
  2. Check dmesg/mount for the filesystem going read-only and remount read-write
  3. Ensure consistent ownership of the snapshots root directory by the peer user
  4. Delete partially created snapshot dirs and re-run GenerateSnapshot()

Example fix

// diagnosis
ls -la /var/hyperledger/production/snapshots/<ledgerID>
// if 'snapshots' is a file
rm /var/hyperledger/production/snapshots/<ledgerID>/snapshots
// then re-run GenerateSnapshot()
Defensive patterns

Strategy: validation

Validate before calling

slgr := filepath.Join(snapshotsRootDir, ledgerID, "snapshots")
if fi, err := os.Stat(slgr); err == nil && !fi.IsDir() {
    return fmt.Errorf("%s exists as a file; remove it before snapshotting", slgr)
}

Try / catch

err := ledger.GenerateSnapshot()
var pe *fs.PathError
if err != nil && errors.As(err, &pe) && errors.Is(pe.Err, syscall.EROFS) {
    log.Error("snapshots filesystem went read-only; remount and retry")
}

Prevention

When it happens

Trigger: GenerateSnapshot() reaching the finalize step while the target per-ledger snapshots dir cannot be created: path permission issues, a file existing where the directory should be, or disk errors.

Common situations: A regular file named like the ledger snapshots dir blocks creation (e.g. leftover from a failed run or bad copy); read-only volume became read-only mid-operation (disk error remount).

Understand the failure class

Background: mkdir permission denied (EACCES): failed to create directory errors explained — this error's family across 32 libraries.

Related errors


AI-assisted analysis of hyperledger/fabric@2736b63f8f (2026-09-04). Data as JSON: /api/errors/2ed820e640c01d59. Report an issue: GitHub.