无需在Linux中挂载即可阻止设备信息 [英] Block device information without mounting in Linux

查看:228
本文介绍了无需在Linux中挂载即可阻止设备信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在C ++中获取Linux中块设备的一些信息(特别是块大小).是否可以在不安装设备的情况下获得设备的块大小,也可以不查看动态文件(例如/sys中的文件),而仅使用系统调用来获得该块的大小.

I am trying to get some information (specifically block size) of block device in linux, in C++. Is it possible to get block size of a device without mounting it and possibly without looking into dynamic files (like the ones in /sys), but with a system call only.

我尝试使用stat,但是如果我询问有关/dev/sdb2的信息,它将返回有关/dev文件系统的数据.

I was trying with stat, but it returns data about /dev filesystem if I ask about /dev/sdb2.

如果无法通过系统调用,我应该在哪里查看动态文件(也无法找到它).

If it's impossible with system call, where should i look in dynamic files (haven't been able to locate it either.)

推荐答案

您要使用 ,尤其是BLKSSZGET.

引用linux/fs.h:

Quoting linux/fs.h:

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

未经测试的示例:

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

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

这篇关于无需在Linux中挂载即可阻止设备信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆