apache/hadoop · error · IOException

Cannot create directory {rootPath}

Error message

Cannot create directory {rootPath}

What it means

In Storage.analyzeStorage, when the storage root does not exist and startOpt is FORMAT or HOTSWAP, the code creates it with root.mkdirs(); failure throws IOException('Cannot create directory <rootPath>'). This is the top-level storage root (e.g. /dfs/nn), created before any locking or layout work, so failures are purely filesystem-level.

Source

Thrown at hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/common/Storage.java:681

          location.getStorageType() == StorageType.PROVIDED) {
        // currently we assume that PROVIDED storages are always NORMAL
        return StorageState.NORMAL;
      }

      assert root != null : "root is null";
      boolean hadMkdirs = false;
      String rootPath = root.getCanonicalPath();
      try { // check that storage exists
        if (!root.exists()) {
          // storage directory does not exist
          if (startOpt != StartupOption.FORMAT &&
              startOpt != StartupOption.HOTSWAP) {
            LOG.warn("Storage directory {} does not exist", rootPath);
            return StorageState.NON_EXISTENT;
          }
          LOG.info("{} does not exist. Creating ...", rootPath);
          if (!root.mkdirs()) {
            throw new IOException("Cannot create directory " + rootPath);
          }
          hadMkdirs = true;
        }
        // or is inaccessible
        if (!root.isDirectory()) {
          LOG.warn("{} is not a directory", rootPath);
          return StorageState.NON_EXISTENT;
        }
        if (!FileUtil.canWrite(root)) {
          LOG.warn("Cannot access storage directory {}", rootPath);
          return StorageState.NON_EXISTENT;
        }
      } catch(SecurityException ex) {
        LOG.warn("Cannot access storage directory {}", rootPath, ex);
        return StorageState.NON_EXISTENT;
      }

      this.lock(); // lock storage if it exists

View on GitHub (pinned to 2add963021)

Solutions

  1. Pre-create the parent path with correct ownership: mkdir -p <parent> && chown <hdfs-user> <parent>
  2. Fix or correct the configured path (dfs.namenode.name.dir / dfs.datanode.data.dir) if it collides with a file
  3. Verify the mount is read-write and has space and inodes (df -h, df -i), then retry

Example fix

# before
sudo -u hdfs hdfs namenode -format   # /dfs/nn parent root-owned

# after
sudo mkdir -p /dfs && sudo chown hdfs:hdfs /dfs
sudo -u hdfs hdfs namenode -format
Defensive patterns

Strategy: validation

Validate before calling

Path root = Paths.get(conf.get(DFSConfigKeys.DFS_NAMENODE_NAME_DIR_KEY));
Path parent = root.getParent();
if (Files.exists(root) && !Files.isDirectory(root)) throw new IOException(root + " is a file");
if (!Files.isWritable(Files.exists(parent) ? parent : parent.getParent())) {
  throw new IOException("No write permission to create " + root);
}
// then run format

Try / catch

try {
  sd.analyzeStorage(startOpt, storage, false);
} catch (IOException e) {
  if (e.getMessage().startsWith("Cannot create directory")) {
    throw new IOException("Create parent of " + rootPath + " with correct ownership first", e);
  } else throw e;
}

Prevention

When it happens

Trigger: Formatting into a new path whose parent is not writable by the daemon user, where a component of the path exists as a regular file, on a read-only filesystem, or with no free space/inodes.

Common situations: dfs.namenode.name.dir pointing at a fresh mount whose parent is root-owned; typo'd path colliding with an existing file; read-only mount; exhausted inodes.

Related errors


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/8c0df27eb0623f6d. Report an issue: GitHub.