How do I format a partition into ext4 with a specific amount of total filesystem space, which is in the second column of df -B1? Can I do that with another filesystem type?

I need that only for test purposes. The tested software has rules and policies which are based on total space of the mounted filesystems.

  • 1
    @KamilMaciorowski I misused the word "available". Replaced it with "total space". I hope it became clearer. – George Sovetov Dec 3 at 0:35

A couple of things about the second column of df. First, regardless of what units you specify via the -B option, for all file systems, the amount is going to be some multiple of the file system blocksize, which in turn is almost always a power of two. So if you want to format a file system where the second number of "df -B1" is not a multiple of the file system blocksize, say, 102233 (a prime number) ---- it's not happening. You're not going to be able to do this with any file system using linux.

Secondly, from SuSv3, defining that second column:

"The total size of the file system in 512-byte units. The exact meaning of this figure is implementation-defined, but should include <space used>, <space free>, plus any space reserved by the system not normally available to a user."

(In Linux, df violates the Posix/SUSv3 specification by being BSD compatible --- that is, by default it displays units in 1k blocks, instead of the historic AT&T 512 byte sectors.)

The exact calculation of that figure is not fixed by POSIX or the Single Unix Specification, because even at the time that the standards committee was drafting POSIX standard, there were already differences between different Unix versions and file systems implementations of what might be included in that figure. For example, for ext2/3/4, that first figure is calculated using the total number of blocks in the file systems, subtracting out the fixed metadata (e.g., the superblocks, block group descriptors, inode allocation bitmaps, block allocation bitmaps, and inode tables). Other file systems may do things different in terms of what blocks are included and excluded in that first number.

So if you want to format a file system so it has some exact number, that might or might not be possible, and doing so is going to require a bit of trial and error. You can control the number of inodes in the file system, and in general mess with the file system tuning parameters, but I would suggest that it's really not worth it.

Instead, I'd suggest using a test mock[1] so you can simulate the output of df -B1. This might be as simple as simply creating a shell script named "df" which is placed in a directory which is in the PATH ahead of the "real" df program.

[1] https://en.wikipedia.org/wiki/Mock_object

  • I'm happy to see such a detailed answer! Unfortunately, I cannot rely on how the tested software gets the filesystem space, but it seems to be equal to what df outputs, that's why I mentioned that. We ended up with estimating how much space is allocated for metadata and adjusting the size of the formatted space to get more or less predictable total space after formatting. – George Sovetov 2 days ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.