I drafted this blog post in 2016 (at least), but held off publishing it until I could have it fact checked. Well, 6 years have passed… I am 99% sure the information in this blog post is correct. But if you find an error with my explanation of the userspace-kernel-device dataflow then please send me an email so I can understand it better and update this post. Thank you!
I’ve been experimenting with creating functionality within bitmath for reading the size of storage devices. This would provide a function similar to Python’s
os.path.getsize, but for storage device capacity instead of file sizes.
Unfortunately, it turns out that there is no out of the box (and cross-platform) solution in Python for reading the capacity of system storage devices. This meant some research was going to be required. Luckily, possible solutions for how to do this are abundant across the internet. Well, for Linux anyway. Figuring out how to make this work on Mac OS X was more challenging.
And that’s where the story gets interesting.
In the rest of this blog post we’ll learn the basics of how programs can interact with storage devices via the
ioctl() system call. Then we’ll discuss the things we have to do and information we’ll need to have in order to implement an
ioctl() request in Python. Next we’ll see how to gather all the necessary information (request codes and expected result sizes). Finally we’ll put all of this together into a runnable Python program.
If you’re not familiar with that acronym, “ioctl” stands for “input/output control”.