CSE Roadmap

Common tier · about 3 hours · 7 parts

Part 7 of 7 · 3 min read

Exercises

Easy

Easy 1: Blocks of a deleted file (20 minutes)

A program holds an unlinked file of 4 MiB open. By how many KiB does df used rise, and by how many does du rise?

Done when: 4,096 KiB and 0 KiB.

Hint

the blocks stay allocated, but no directory entry leads to them.

Easy 2: Sparse numbers (20 minutes)

A file made with truncate -s 100M has one 4 KiB block written. How many 4 KiB blocks does the apparent size cover, and how many are real?

Done when: 25,600 blocks of apparent size and 1 real block.

Hint

100 MiB / 4 KiB.

Easy 3: Reserved blocks (20 minutes)

A file system has 1,000,000 blocks of 4 KiB and reserves 5%. How many blocks and how many GiB are reserved?

Done when: 50,000 blocks, about 0.19 GiB.

Hint

1,000,000 x 0.05, then x 4,096 bytes and divide by 2^30.

Medium

Medium 1: Reproduce it (45 minutes)

Make a small ext4 image and mount it. Compile the program of section 2 and run it in the background. While it sleeps, read df, du and ls -l /proc/PID/fd. Report the three readings and the one after it ends.

Done when: a table like the one in section 2 with your numbers.

Hint

use ls -l /proc/$!/fd after starting the program with &.

Medium 2: Free it without a restart (45 minutes)

Change the program so that it sleeps for 60 seconds. While it sleeps, truncate the file through /proc/PID/fd/3 with : > /proc/PID/fd/3. Read df before and after.

Done when: df used falls by about 4,096 KiB while the program is still running, and a note on why this can hurt a real program.

Hint

the descriptor number is 3 in this program.

Medium 3: A hidden mount (45 minutes)

Put a file of 1 MiB in a directory, mount a tmpfs on it, and show that du of the directory sees nothing while df of the parent file system still counts the file. Then bind-mount the parent to see the file again.

Done when: the three readings, and the file found through the bind mount.

Hint

mount -t tmpfs tmpfs dir, then mount --bind parent bindpoint.

Hard

Hard 1: Find deleted files (90 minutes)

Write a program that scans /proc/*/fd, reads each link with readlink, and prints the process ID, the descriptor, the path and the size (fstat through the descriptor) for every link that ends in (deleted).

Done when: the program finds the file of the program of section 2 while it sleeps, with a size of 4,194,304 bytes.

Hint

use opendir("/proc") and numbers only, then opendir of fd in each.

Hard 2: truncate against fallocate (90 minutes)

Make two files of 20 MiB on the image: one with truncate -s 20M and one with fallocate -l 20M. Compare ls -l, du and df for each.

Done when: the same apparent size for both, a real use of 0 for the first and 20,480 KiB for the second, and a sentence on why fallocate can make a later write fail less often.

Hint

use du -k on each file and df -k before and after.

Hard 3: The log rotation incident (90 minutes)

A service writes 2 MB/s to a log. A script deletes the log each night without signalling the service. The disk has 20 GiB free. How long until it is full? What number does du show then, and what does df show? Write the fix in three lines.

Done when: 10,240 s (2.8 hours), du near zero for the log and df at 100%, and a fix such as "signal the service to reopen the log, use the service's own rotation, and alert at 80%".

Hint

free space divided by the write rate.