linux - 在没有原始文件系统权限的情况下获取磁盘扇区大小

标签 linux posix ioctl

我正在尝试获取扇区大小,特别是这样我就可以正确调整缓冲区大小以使用 O_DIRECT 进行读/写。

当我的应用以 root 身份运行时,以下代码有效:

int fd = open("/dev/xvda1", O_RDONLY|O_NONBLOCK);
size_t blockSize;
int rc = ioctl(fd, BLKSSZGET, &blockSize);

如何在不以 root 身份运行的情况下获取扇区大小?

最佳答案

根据 open() 的 Linux 联机帮助页:

In Linux alignment restrictions vary by file system and kernel version and might be absent entirely. However there is currently no file system-independent interface for an application to discover these restrictions for a given file or file system. Some file systems provide their own interfaces for doing so, for example the XFS_IOC_DIOINFO operation in xfsctl(3).

所以看起来您可以使用 xfsctl()... 获取此信息,如果您使用的是 xfs。

由于您的底层 block 设备是一个 Xen 虚拟 block 设备,并且在它下面可能有任意数量的层(LVM、dm-crypt、另一个文件系统等...)我不确定所有这些的意义有多大真的很适合你。

关于linux - 在没有原始文件系统权限的情况下获取磁盘扇区大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12939703/

相关文章:

linux - Linux 中的 Eclipse 不透明空白字符

linux - 为什么命名信号量可以被任何知道其名称的进程中的线程使用?

c++ - 如何反转存储在内存地址的位?

linux - 如何为 MXE 交叉编译器编译 Berkeley DB 5.3.28?

c++ - 关闭 AF_PACKET 与 AF_INET 的时间差?

python - 如何在 Python 中生成昨天和今天午夜的 POSIX 值?

c - select(2) 和 ioctl(2) 返回 0 而 stdin 有数据

linux - 除了 strace 之外,还有其他方法可以记录 ioctl 调用吗?

linux - Mono 使用 mscorlib.dll 2.0 而不是 4.0

c++ - 特定于线程的数据——为什么我不能只使用带有线程 ID 的静态映射?