Device Detection

I need to know if there is a way to detect what block devices are currently available to be mounted, what block devices are currently mounted, and the type of block device (aka: is it a loop device? is it a phisical device (like a HDD/flash drive, etc...?)

I've searched for API documentation, but I could not find any on this particular topic.

I have delved into /sys/dev/block, but I feel like I shouldn't experiment if the API provides a way to gather this information.
So, I think I may have fiured it out. I was digging around, and found this:

/proc/partitions : has all the block partitions that can be mounted.
/proc/mounts : has all mounted devices, as well as their filesystem (if applicable) and permissions.

Both are updated in real-time.

I can get the size of any device from /sys/dev/block (following major:minor as given in /proc/partitions), but I really need to aqcuire the amoutn of free space available. Is there any better way to do this than calculating the size of used space?
Last edited on
Maybe statvfs http://linux.die.net/man/2/statvfs
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <unistd.h>
#include <sys/statvfs.h>
 
int main()
{
        struct statvfs fs;
        statvfs("/", &fs);
 
        std::cout << (fs.f_bavail * 4096) / 1024 / 1024  
        << " MB free" << std::endl;
}
Thanks naraku9333!
Topic archived. No new replies allowed.