I have a flash drive device (/dev/sda1) mounted to /mnt on an embedded linux system (kernel 2.6.23). Using C how do I work out the size of the drive?
Step 1. Check that Windows Properties shows that the drive has the size stated. From Explorer, navigate to the USB drive and right-click properties and check the Capacity shown. This should (approximately) match the stated drive capacity, which is usually printed on the outside of the drive, and / or on the box.
The first USB flash drive came on the market in 2000 with a storage capacity of 8 megabytes (MB). Drives now come in capacities ranging between 8 gigabytes (GB) and 1 terabyte (TB), depending on manufacturer, and future capacity levels are expected to reach 2 TB.
On Linux, if you're not worried about portability (C doesn't know about drives, so any such specific code will be unportable), use statfs()
:
struct statfs fsb;
if(statfs("/mnt", &fsb) == 0)
printf("device has %ld blocks, each %ld bytes\n", fsb.f_blocks, fsb.f_bsize);
Read and parse a number in device's sysfs entry. In your case,
/sys/block/sda/size
/sys/block/sda/sda1/size
The device does not have to be mounted yet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With