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?
A:
If you have no problem using external tools, exec this :
df -h | grep -i /dev/sda1
using popen, and parse the resulting line with strtok.
Geo
2009-09-28 14:32:53
I am trying to avoid doing something like that. The application is running on an embedded platform and would require too much resource to do the above.
Andrew
2009-09-28 14:37:19
`-h` is probably unnecessary here since the input will be read by program. `-i` is also redundant due to case-sensitivity. Other than that--a good LSB-conformant (i.e. portable) solution.
Pavel Shved
2009-09-28 14:37:49
+4
A:
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("/dev/sda1", &fsb) == 0)
printf("device has %ld blocks, each %ld bytes\n", fsb.f_blocks, fsb.f_bsize);
unwind
2009-09-28 14:35:27
+1
A:
Read and parse a number in device's sysfs entry. In your case,
- Full device (all partitions and partition table):
/sys/block/sda/size
- Logical partition on this device:
/sys/block/sda/sda1/size
The device does not have to be mounted yet.
Alex B
2009-09-28 14:36:02