Create Test Files with xfs_mkfile Without Filling the Disk by Accident
You will finish with correctly sized test files, including a sparse holey file, and a quick way to check how much storage each one actually consumes. The examples use xfs_mkfile from xfsprogs 6.6.0, installed here as package version 6.6.0-1ubuntu2.1.
The route
Jump straight to the step you need, or tick off Done means at the end.
Allow about ten minutes. You need a shell and a directory where you may create temporary files. The normal commands do not need elevated privileges. This guide changes the filesystem by creating files, so use a disposable directory and do not point the examples at a service data directory.
1. Confirm the installed command
Check the binary and package before relying on option details. These are ordinary, read-only commands:
$ command -v xfs_mkfile
/usr/sbin/xfs_mkfile
$ xfs_mkfile -V
xfs_mkfile version 6.6.0
$ dpkg-query -W -f='${Package} ${Version}\n' xfsprogs
xfsprogs 6.6.0-1ubuntu2.1
The installed manual describes -v, -n, -p and -V. It does not describe a separate option for choosing permissions, ownership or a destination directory. Those come from the normal filesystem and process rules around the command.
Checkpoint
If command -v finds nothing, stop here. Install or repair the package through your normal system administration process rather than copying a different binary into place.
2. Make a disposable workspace
Create a private temporary directory. mktemp prints the directory name, so save it and inspect the value before using it:
$ WORKDIR=$(mktemp -d /tmp/xfs-mkfile-demo.XXXXXX)
$ printf 'workspace: %s\n' "$WORKDIR"
workspace: /tmp/xfs-mkfile-demo.A1b2C3
$ ls -ld "$WORKDIR"
drwx------ ... /tmp/xfs-mkfile-demo.A1b2C3
The suffix is deliberately variable. Do not copy the displayed example directory name as though it were a fixed path. Every file in the rest of this guide is created inside WORKDIR.
Safety boundary
xfs_mkfile creates real files and can request large sizes. A mistaken value such as 100g can consume significant space. Start with kilobytes or megabytes, and check the destination before using a larger value.
3. Create a zero-filled file
Give the command one size, followed by one or more filenames. The default unit is bytes. The suffixes are k for kilobytes, b for blocks, m for megabytes and g for gigabytes:
$ xfs_mkfile 64k "$WORKDIR/sample.bin"
$ stat -c 'size=%s bytes, allocated=%b blocks' "$WORKDIR/sample.bin"
size=65536 bytes, allocated=128 blocks
Without -n, the program pads the file with zeroes. The exact allocated-block count depends on the filesystem block size, but the byte size should be 65,536 for 64k on this version. Use stat as the verification command that matters for your host.
The size belongs before all filenames. For example, this creates two 64-kilobyte files:
$ xfs_mkfile 64k "$WORKDIR/one.bin" "$WORKDIR/two.bin"
$ stat -c '%n %s bytes' "$WORKDIR/one.bin" "$WORKDIR/two.bin"
/tmp/xfs-mkfile-demo.A1b2C3/one.bin 65536 bytes
/tmp/xfs-mkfile-demo.A1b2C3/two.bin 65536 bytes
Do not write xfs_mkfile 64k 128k file when you mean two sizes. The command interprets every word after the size as a filename, so it would try to create a file literally named 128k.
4. Create a sparse holey file
Use -n when you need the apparent length without writing the data area:
$ xfs_mkfile -n 1m "$WORKDIR/holey.bin"
$ stat -c 'size=%s bytes, allocated=%b blocks' "$WORKDIR/holey.bin"
size=1048576 bytes, allocated=0 blocks
This is a holey file. The command seeks to the requested end and writes a block, rather than filling the whole range with zeroes. The apparent size is still one mebibyte in this example, while the allocated-block result is commonly much smaller. Treat allocation as filesystem-dependent and verify it with stat or du:
$ du -B1 "$WORKDIR/holey.bin"
0 /tmp/xfs-mkfile-demo.A1b2C3/holey.bin
A sparse file can grow into real storage when a process writes into its holes. It is useful for testing large-file handling, but it is not a reliable way to reserve that space.
5. Preallocate, then verify the result
-p takes a different route: it preallocates the file, overwrites it with zeroes, then truncates it to the requested size. This can be useful when you want the command to exercise preallocation while still ending with a zero-filled file:
$ xfs_mkfile -p 128k "$WORKDIR/preallocated.bin"
$ stat -c 'size=%s bytes, allocated=%b blocks' "$WORKDIR/preallocated.bin"
size=131072 bytes, allocated=256 blocks
Preallocation is not a promise that a future application write can never fail. Free space, quotas, filesystem limits and later activity still matter. It also does not make the file sparse. Compare the du result with the holey example if the distinction matters to your test.
6. Add names and sizes to the output
Use -v when a script or a manual test needs a report of each created file:
$ xfs_mkfile -v 4k "$WORKDIR/a" "$WORKDIR/b"
/tmp/xfs-mkfile-demo.A1b2C3/a 4k bytes
/tmp/xfs-mkfile-demo.A1b2C3/b 4k bytes
The output is for people, not a stable data format. If another command needs an exact byte count, inspect the resulting path with stat. A successful command normally returns status 0:
$ printf 'exit status: %s\n' "$?"
exit status: 0
Run that check immediately. A later command would replace the status you are trying to inspect.
7. Remove only the files you created
These examples are state-changing, so clean up deliberately. First list the workspace, then remove only the known paths and the workspace directory:
$ find "$WORKDIR" -maxdepth 1 -type f -print
$ rm -f -- "$WORKDIR/sample.bin" "$WORKDIR/one.bin" "$WORKDIR/two.bin" \
"$WORKDIR/holey.bin" "$WORKDIR/preallocated.bin" "$WORKDIR/a" "$WORKDIR/b"
$ rmdir -- "$WORKDIR"
$ test ! -e "$WORKDIR" && echo 'workspace removed'
workspace removed
Do not substitute a broad path for WORKDIR. If the variable is empty or points somewhere unexpected, stop and inspect it. The commands above remove the files made by this guide; they do not undo files created by a separate test.
Done means
- You checked the installed
xfs_mkfileversion and package. - You placed test files in a disposable directory.
- You used one size before one or more filenames.
- You verified apparent size with
statand storage use withstatordu. - You understand that
-nmakes a holey file, while-ppreallocates and zero-fills. - You removed only the files and directory created for the test.