strace for blockdevtells me you can use:
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
int main()
{
unsigned long long size;
int fd = open("/dev/sdx", O_RDONLY);
ioctl(fd, BLKGETSIZE64, &size);
std::cout << size << std::endl;
std::cout << (size>>20) << std::endl;
}
(replace sdx with the node name device)
Note prefers to use uint64_tif your compiler already supports it (include <cstdint>)
source
share