0

I'm shrinking a Linux SDCard partition using AOakley's awesome guide. I'm trying to automate it, but there appears to be no command-line tool for determining the number of bytes in a partition that are used.

For example, gparted tells me the used space is 1.88GB out of 6.99GB (note, I'm using /dev/loop0 which is mounted to the second partition of /dev/sdb):

df is wrong:

% df /dev/loop0
Filesystem  ... Use%
udev        ... 0%

...and fdisk and parted don't print used space.

What can I use from the command-line to determine the used space in a partition the way gparted does?

ANSWER: Summary from below: The partition is ext4 which allows me to use dumpe2fs and extract: free blocks, block count (total), and block size which can be combined to exactly match gparted. Turns out that resize2fs handles fragmentation during the shrink. See the manpage for ext4 for more info (although the manpage editor is a tad bitter about GiB vs. GB! :)

1

df is almost the right tool but you should query the mountpoint (or any file below) that belongs to the filesystem you want to examine, not the device node. The node you used, /dev/loop0, is a special file that belongs to the udev virtual filesystem; hence you got udev.

Mount /dev/loop0 (or /dev/sdb2, I'm not sure why the need for /dev/loop0) somewhere (it can be ro mount) and invoke df /the/mountpoint. Then df will tell you what the filesystem reports about itself.

I said "df is almost the right tool" because there is no guarantee you can shrink the filesystem (and later the partition) to the size reported by the tool. What df prints can be your first (or rather zeroth) estimate.

You need different tools to resize different fielsystems. In a perfect world such tool (or some accompanying tool) is able to tell you the minimum possible size, or at least the minimum size for which resizing will succeed for sure. In reality you can encounter this:

Known Bugs
The minimum size of the filesystem as estimated by resize2fs may be incorrect, especially for filesystems with 1k and 2k blocksizes.

(source)

gparted uses filesystem-specific tools. I did run it with strace and saw no evidence it uses df. I saw btrfs filesystem show …, dumpe2fs …, …

Summary:

  • You used df in the wrong way.
  • It's better to use a tool specific to the filesystem anyway; gparted does. I cannot tell you what tool you should use because you haven't specified the filesystem in question.
  • Ah, good point: I see the correct usage when I mount the second partition's /dev/loop0 to a mount-point. It's an 'ext4' partition. I'm not sure how fragementation works on ext4 filesystems, so I'm suspicious that df is reporting the boundary around all used space, holes included. I forgot all about strace, I'll look into that and see what gparted is calling for this partition. Not sure what the family of ext4 tools are called (googling now!). Thanks for the push in the right direction! – PeterT Feb 7 '20 at 4:17
  • Looks like dumpe2fs has inode/fragment/block info, just need to learn about it unless you know an easy way to parse that file? – PeterT Feb 7 '20 at 4:27

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.