Linux 块设备的大小为奇数(非偶数)
是否可以创建奇数大小的 Linux (2.6) 块设备(例如环回设备)?我没能做到。 losetup
似乎向下舍入到 512 字节边界。用户模式 Linux ubd
设备的 ubd
设备似乎四舍五入为 512 字节边界。在struct request
中,我们有sector_t __sector
作为读/写操作的块偏移量。
我问这个问题只是为了教育目的。我可以处理 512 字节边界,但我仍然对是否可以绕过它感兴趣。在这个问题中,我对其他抽象层(例如使用常规文件或字符设备)不感兴趣。
Is it possible to create a Linux (2.6) block device (such as a loopback device) with an odd size? I couldn't make it happen. losetup
seems to round down to 512 byte boundary. The ubd
devices of User-mode Linux ubd
devices seem to round up to 512 byte boundary. In struct request
, we have sector_t __sector
for the block offset for read/write operations.
I'm asking this question just for educational purposes. I can cope with the 512 byte boundary, but I'm still interested if it would be possible to bypass it. In this question I'm not interested in other layers of abstraction (such as using regular files or character devices).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不可以。Linux 2.6 块层不理解任何小于 512 字节的内容。任何更小的值(尤其是不是 2 的幂)都需要对大量代码进行重大重写。
No. The Linux 2.6 block layer doesn't comprehend anything smaller than 512 bytes. Anything smaller (especially not a power of 2) would require a major rewrite of an awful lot of code.
这就是块设备而不是字符设备的原因:块粒度。这种二分法的存在是因为将一次只处理一个块的真实硬件建模为也处理块的抽象要高效得多。否则的话,每一次操作都会变成成本更高的计算。
正如您提到的,绕过它的方法是使用面向字符的设备或抽象。这是 Unix 设备模型的核心:一切都是一系列八位位组,除了那些只能虚拟化为一个八位位组的东西。
This is what makes a block device instead of a character device: the block granularity. The dichotomy exists because it is vastly more efficient to model real hardware that works a block at a time as an abstraction that also deals in blocks. To do otherwise would turn every operation into a much more costly computation.
The way to bypass it is, as you mention, to use a character oriented device or abstraction. This is central to the Unix device model: everything is a series of octets, except for the things that can only be virtualized as one.