Learn › Linux Systems Programming with Python › Filesystems and capacity
Find the filesystem behind a path and use Python to report total, free and available bytes alongside inode counts.
You want to save another file in your home directory. Which pool of space will it use, and how much of that space is available to an ordinary program? You’ll ask Linux about two paths, then fix a Python report so it distinguishes free space from space available to ordinary users.
You should be able to run Python, work with variables and strings, and navigate a Linux terminal. In Chapter 1, you opened a file by name and Linux gave your Python process a file descriptor. Here you’ll ask about the filesystem the path reaches. Open WebIDE and Terminal in the practice machine. The starter capacity_report.py is in /home/admin. Work from that directory.
A file has contents and information about those contents. An inode records information such as the file’s type, size, owner and permissions. A directory contains entries that connect names to inode numbers. In /home/admin/capacity_report.py, Linux follows the directory names to admin, then finds the entry for capacity_report.py. An inode number identifies a file within its own filesystem, which organizes files, directories and the space they share.
A mount point is the place where Linux attaches a filesystem tree to the path hierarchy. A path can start under / and cross into a different filesystem as Linux resolves it. Two different files on the same filesystem use the same pool of data space and inodes. Changing the path you ask about can take you to another pool, even on the same machine.
os.statvfs(path) gives Python named fields from the filesystem that contains an existing path. Use the path to choose the filesystem. Then read the fields to understand its capacity.
| Field | What it counts |
|---|---|
f_frsize | Bytes in one unit of the three block counts below |
f_blocks | Total blocks |
f_bfree | Free blocks, including any kept outside ordinary allocation |
f_bavail | Blocks reported available to unprivileged users |
f_files | Total inodes reported by this filesystem |
f_ffree | Free inodes reported by this filesystem |
Suppose a filesystem returns the following example values. They are chosen to make the arithmetic easy to check.
f_bsize = 8192 f_frsize = 4096
f_blocks = 20 f_bfree = 7 f_bavail = 5
f_files = 10 f_ffree = 2
Multiply each block count by 4096 to get bytes. f_bsize is a separate size field. These counts use f_frsize.
| Report field | Calculation | Result |
|---|---|---|
| Total bytes | 20 × 4096 | 81,920 |
| Free bytes | 7 × 4096 | 28,672 |
| Available bytes | 5 × 4096 | 20,480 |
| Total inodes | f_files | 10 |
| Free inodes | f_ffree | 2 |
An ordinary program should read 20,480 available bytes as a snapshot of what the filesystem reports. The difference from free space is 2 × 4096 bytes, which can reflect filesystem reservation or allocation policy. The numbers alone don’t tell you the policy that caused the gap. Some filesystems report the same free and available value. Permissions, quotas and activity between reads can still affect a later write.
The inode counts stay as counts. A filesystem can have space for file contents and still lack a free inode for a new file. Think of the bytes and free inode counts as two different questions to check.
Now work the same calculation on your Linux machine. python3 -c runs the Python code inside the shell’s single quotes. This program takes one capacity reading of your home directory and prints each result with a label.
python3 -c 'import os
stats = os.statvfs("/home/admin")
block_bytes = stats.f_frsize
print("Total bytes:", stats.f_blocks * block_bytes)
print("Free bytes:", stats.f_bfree * block_bytes)
print("Available bytes:", stats.f_bavail * block_bytes)
print("Total inodes:", stats.f_files)
print("Free inodes:", stats.f_ffree)'
stats holds the result of that one call. The dot in stats.f_blocks selects its f_blocks field. The three byte lines multiply different block counts by the same unit. The two inode lines use counts directly. Your output is live and will differ from the example table. Compare the two byte labels for free and available. If their values match here, keep both labels because another filesystem can report a gap.
Suppose f_frsize is 1024, there are 18 total blocks, 6 free and 4 available. The filesystem reports 12 total inodes and 0 free. Work through the five report values with the table above. Start by multiplying 18 × 1024.
The total is 18,432 bytes, free is 6,144 bytes and available is 4,096 bytes. The inode counts are 12 total and 0 free. The available byte count alone looks promising, yet this example filesystem has no free inode for a new file. Replay the example above if you want to compare the calculations.
Now use the real Terminal to find where two paths lead. ls -id displays the inode number for each path itself. findmnt -T shows the mount containing a path. Here -n omits the header and -o TARGET,FSTYPE shows the mount point and filesystem type.
cd /home/admin
ls -id /home/admin /home/admin/capacity_report.py
findmnt -n -o TARGET,FSTYPE -T /home/admin
findmnt -n -o TARGET,FSTYPE -T /home/admin/capacity_report.py
Look for different inode numbers and the same mount point and type. The directory and script have their own inodes, but draw from the same filesystem pool. Separate calls can see different free counts if filesystem activity occurs between them. If a path is missing, confirm the starter is open in WebIDE and the Terminal is on this lesson’s machine.
/dev/shm is commonly a mount point for tmpfs, a filesystem backed by virtual memory with its own limit. Before you run the next commands, write down whether home and /dev/shm will show the same mount point and why. Also write down what you’ll check before deciding which byte count is useful to an ordinary program. Keep your prediction and reason in your own notes, such as capacity-notes.txt in your workspace. You can write unsure and continue, or return to the examples.
findmnt -n -o TARGET,FSTYPE -T /home/admin
findmnt -n -o TARGET,FSTYPE -T /dev/shm
df -B1 --output=target,fstype,size,used,avail /home/admin /dev/shm
df -i /home/admin /dev/shm
Read the mount point and type first. If /dev/shm shows its own tmpfs mount, its capacity numbers describe that mount. Read the home row for the filesystem that contains your home. In df -B1, the size, used and available columns are in bytes. Subtract used from size to get free bytes. In df -i, Inodes and IFree are the total and free inode counts. Use the values returned by your machine. Small changes between separate reads are normal. If the mount is missing or the observed types don’t match your prediction, record what you saw and ask for help verifying the lab environment.
Python can print the raw fields before your report calculates anything. python3 -c runs the short Python program inside the quotes. Look for f_frsize, f_bfree and f_bavail in your result.
python3 -c 'import os; print(os.statvfs("/home/admin"))'
Now run the starter for both paths and look at the labels and numbers. The report prints a JSON object, which pairs a label such as free_bytes with a value. A few starter lines use the wrong source fields. The live machine can return equal values for some fields, so the wrong line may look right until the inputs change.
python3 capacity_report.py /home/admin
python3 capacity_report.py /dev/shm
Open capacity_report.py in WebIDE. main() gets the path from the command line, calls os.statvfs(path) and passes that one reading to summarize. The line def summarize(stats): defines a function and names the reading stats inside it. return sends its result back to main(). The result is a dictionary, a group of label: value pairs. For example, "total_bytes": stats.f_blocks * block_bytes gives the result of that multiplication the label total_bytes. main() prints the dictionary with the path included.
Edit summarize(stats) so each of its five labels matches the worked example. In your notes, connect each changed line to the relevant field or unit. Save the script. Keep the main() function and both filesystems as they are.
Run the two Python commands again. Match each reported path to the corresponding mount row from findmnt. Check total_bytes and available_bytes against the corresponding df -B1 columns, and compare free_bytes with size minus used. Compare total_inodes and free_inodes with df -i. Rerun df near your Python readings if the free counts differ. Use the report and command outputs to explain any large mismatch before trusting the numbers.
Use Check chapter for formative feedback. It checks byte conversion, inode counts, and the report on two snapshots taken from this machine. Its examples vary values that can happen to be equal in a single live reading. It gives feedback on what the report returns, without awarding XP.
In a later practice session, suppose a filesystem reports 200 free blocks and zero free inodes, with a block-count unit of 2,048 bytes. The free and available block counts are equal. Explain which reported numbers matter for creating a thousand tiny new files. Then check how your report handles those counters and explain whether another directory on the same mount would change the answer. Verify your interpretation using the byte and inode fields without creating any files to fill a filesystem.
Practice How much space is available on this filesystem? in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.