it4i logoDocumentation
Storage

Working With a Lot of Small Files

Problem

Shared parallel filesystems such as Lustre, GPFS or BeeGFS are designed for high-bandwidth access to large files, not for handling millions of tiny ones. Every file operation (open, stat, close, create, unlink, directory listing) is served by a small number of metadata servers that are shared by all users of the cluster. A workload that touches hundreds of thousands of small files generates a flood of metadata requests, which makes the job itself slow (it spends more time waiting for metadata than reading data) and degrades filesystem responsiveness for everyone else, so even a simple ls can hang for other users. Small files also waste storage, because each one occupies at least one full block and one inode, and many sites enforce inode (file count) quotas for exactly this reason.

The best solution is to not create many small files in the first place. Store your data in a small number of large, structured files using a parallel I/O library:

TechnologyDescription
MPI-IOPart of the MPI standard; lets many processes read and write different parts of a single shared file efficiently.
HDF5Hierarchical, self-describing container format. Groups and datasets replace directories and files. Supports parallel access via MPI-IO.
NetCDF-4Array-oriented format built on top of HDF5, widely used in climate and earth sciences.
ADIOS2High-performance I/O framework for simulations, with pluggable backends.

A typical example: instead of writing one file per timestep or per sample, write each timestep as a dataset within a single HDF5 file.

import h5py
import numpy as np

with h5py.File("results.h5", "w") as f:
    for step in range(10000):
        f.create_dataset(f"step_{step:05d}", data=np.random.rand(100))

Python users can also use h5py with mpi4py for parallel writes, and many machine learning frameworks support container formats (TFRecord, WebDataset tar shards, LMDB) that serve the same purpose.

Alternative: Pack Files Into a Single Image and Mount via FUSE

If you cannot change the application (e.g., third-party software that expects a directory tree, or an existing dataset of millions of files), pack the files into a single file on the shared filesystem and mount it with FUSE (Filesystem in Userspace). FUSE runs without root privileges. The shared filesystem then sees only one large file, while your application still sees an ordinary directory tree. All metadata operations happen locally on the compute node.

Two approaches are described below.

Option 1: ext4 Filesystem in a File (fuse2fs)

This approach gives you a full read-write filesystem and is best suited for datasets that are accessed many times or that you need to modify.

Create the Image From an Existing Directory

mkfs.ext4 can create the filesystem and populate it from a directory in one step with the -d option, without needing root:

# Estimate the required size and add some headroom (~20 %)
du -sh my_dataset/
find my_dataset/ | wc -l      # number of files and directories

# Create an empty image file (sparse, allocates no space up front)
truncate -s 50G my_dataset.img

# Create an ext4 filesystem and copy the contents of my_dataset/ into it.
# -N sets the number of inodes; it must be larger than the number of files.
# -m 0 disables reserved blocks, which are not needed here.
mkfs.ext4 -F -m 0 -N 2000000 -d my_dataset/ my_dataset.img

If mkfs.ext4 is not in your PATH, try /sbin/mkfs.ext4 or /usr/sbin/mkfs.ext4.

To create an empty image that you fill later, omit the -d option.

Mount the Image

mkdir -p $HOME/mnt/my_dataset

# Read-only mount (safe for concurrent use from many nodes)
fuse2fs -o ro,fakeroot my_dataset.img $HOME/mnt/my_dataset

# Read-write mount (only ONE node/process at a time!)
fuse2fs -o rw,fakeroot my_dataset.img $HOME/mnt/my_dataset

The fakeroot option lets you access all files regardless of their stored ownership.

Unmount the Image

fusermount -u $HOME/mnt/my_dataset

Always unmount a read-write image cleanly before using it elsewhere; otherwise the image can be corrupted. If needed, check it with e2fsck -f my_dataset.img.

Never mount the same image read-write on more than one node at the same time, and never mix a read-write mount with read-only mounts. ext4 is not a cluster filesystem and the image will be corrupted. Read-only mounts on many nodes simultaneously are safe.

Option 2: Mount a tar Archive (archivemount)

This approach is the simplest when your data is already distributed as a tar archive, or when you only need occasional read access.

Create the Archive

tar -cf my_dataset.tar my_dataset/

Prefer an uncompressed tar. Compressed archives (.tar.gz, .tar.xz) work, but random access into them is very slow because the archive must be decompressed sequentially.

Mount the Archive

mkdir -p $HOME/mnt/my_dataset

archivemount -o readonly my_dataset.tar $HOME/mnt/my_dataset

Unmount the Archive

fusermount -u $HOME/mnt/my_dataset

At mount time, archivemount reads through the whole archive to build an index, so mounting very large archives can take a while. Mounting read-write is possible, but changes are written back by rewriting the entire archive at unmount, which is slow and risky for large archives. Use the readonly option unless you really need to write.

Using a FUSE Mount in a Batch Job

Mount at the start of the job and make sure the image is unmounted even if the job fails:

#!/bin/bash
#SBATCH --job-name=small-files
#SBATCH --nodes=1
#SBATCH --time=02:00:00

MNT=/tmp/$USER/dataset_$SLURM_JOB_ID
mkdir -p "$MNT"

fuse2fs -o ro,fakeroot $HOME/my_dataset.img "$MNT"
# or: archivemount -o readonly $HOME/my_dataset.tar "$MNT"

trap 'fusermount -u "$MNT"; rmdir "$MNT"' EXIT

./my_application --input "$MNT"

For multi-node jobs, mount the image read-only on each node (e.g.: with srun --ntasks-per-node=1) before starting the application.

Summary

ApproachWritableBest for
HDF5 / MPI-IO / NetCDF / ADIOS2YesNew or modifiable codes; recommended
ext4 image + fuse2fsYes (single node)Unmodifiable applications, frequently reused or changing datasets
tar + archivemountRead-only (practically)Existing tar archives, occasional read access

If you are unsure which approach fits your workflow, contact user support.

© 2026 IT4Innovations – All rights reserved.

On this page