Block device information without mounting in Linux

末鹿安然 提交于 2019-11-29 11:04:36
themel

You want to use ioctl, in particular BLKSSZGET.

Quoting linux/fs.h:

#define BLKSSZGET  _IO(0x12,104)/* get block device sector size */

Untested example:

#include <sys/ioctl.h>
#include <linux/fs.h>

int fd = open("/dev/sda");
size_t blockSize;
int rc = ioctl(fd, BLKSSZGET, &blockSize);
Bub

I think the ioctl value should rather be unsigned long than size_t (the latest is more memory related), I would also initialize it to 0 (just in case BLKSSZGET returns unsigned int instead).

#include <sys/ioctl.h>
#include <linux/fs.h>

int fd = open("/dev/sda");
unsigned long blockSize = 0;
int rc = ioctl(fd, BLKSSZGET, &blockSize);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!