5

Can I find free space of unmounted partition using system files like sys or proc?

I know how to find total space but have no idea about free space. Please suggest using system files only. For total space of unmounted partition I am using /proc/partitions file.

  • bro it gives total space of the partition. – Sarthak_Bhutani Oct 6 '17 at 4:55
4

This is dependent on the filesystem type, but you could try to use fsck to find out how much free space there is left. Finding the free space requires a tool that understands the structure of the filesystem, and there usually aren't many of those in addition to fsck.

2

procfs should be used specifically for process related info. (as it is not stictly followed but still to be on safer side don't depend on it as there is chance of it getting deprecated).

so sysfs will give us system info. this will give info of full disk size.

cat /sys/block/sda/size

replace sda with your partition name.

ls /sys/block/sda/

will give info about partitions available check for directory naming sda1, sda2 ...

same thing as sda can be used to check size of these partitions.

cat /sys/block/sda/sda1/size

Same note as above watch partition names. change sda to sdb,sdc, etc according to your requirement.

Hope this answers your question.

  • 1
    without mounting the partition I doubt we will be able to read free blocks. there is draw back doing so because this info will be written multiple time to update which may be harmful for disk life. so after disk is mounted and scanned this info is available to you. – Devidas Oct 3 '17 at 6:29
  • 1
    It shows the number of all blocks on the partition, not the free ones. – user259412 Oct 4 '17 at 2:27
  • Yeah you are right peterh. So I have added comment above stating "without mounting the partition I doubt we will be able to read free blocks". I meant in sysfs or procfs. there are other utilities as stated, but he asked for procfs or sysfs so I mentioned it. what I meant while answering was there is a place where info about all partition is stored in kernel but free scpace is not one of them. – Devidas Oct 4 '17 at 6:56
0

There is no general way for that, but there is a way which works in most cases.

The reason, why there is no general way for that, that also the kernel doesn't know it. To know it, first it would need to analyze the partition.

Without mounting the partition, the kernel knows only its existence, but no more. Essentially, it is a line of blocks with random data. The kernel knows, where is it and how to access, but doesn't know, what to do with it.

However, there are also user-space tools which can analyze the content of a filesystem, without mounting them. As all the filesystems have very different data structures, to handle them you need different tools.

In the case of ext2/3/4, it is

# dumpe2fs /dev/your_root|grep '^Free blocks'
dumpe2fs 1.42.13 (17-May-2015)
Free blocks:              5721580
0

I don't know if it can be done, as you say, "using system files only," but you can use resize2fs to get an estimate without mounting. From the man page:

       -M     Shrink the file system to minimize its size as much as possible,
              given the files stored in the file system.

       -P     Print an estimate of the number of file  system  blocks  in  the
              file system if it is shrunk using resize2fs's -M option and then
              exit.

So, resize2fs -P /dev/PARTITION will give you an estimate of how many blocks the filesystem on that partition is taking up. Total blocks on the partition can be found, as you said, from /proc/partitions, or with blockdev --getsz /dev/PARTITION, then do a little bit of arithmetic. I don't know how close the estimated minimum size is to the actual disk usage, but I'm guessing the minimum size is larger than the actual disk usage, to give some leeway. We can check by mounting it and running du.

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.