Linux - 我如何知道给定文件的块映射和/或分区的可用空间映射
我在 Linux 上,需要知道两件事之一:
1)如果我在 Linux 下的分区上的某个文件系统上有一个常规文件,有没有办法知道该文件在驱动器上占用的物理块集来自用户空间?或者至少是文件系统集群的集合?
2)有没有办法获得有关给定文件系统的整个可用空间的相同信息?
在这两种情况下,我都明白,如果有任何可能的方法来提取这些信息,它可能是完全不安全和活泼的(在我看到它们并以某种方式对它们采取行动之间,这些块集可能发生任何事情)。我也真的不想要一个必须了解每个文件系统很多信息的实现。
I am on Linux and need to know either of the two things:
1) If i have a regular file on some file system on a partition under Linux is there a way to know the set of the physical blocks that this file occupies on the drive from user space? Or at least the set of the file system's clusters?
2) Is there a way to get the same information about the whole free space of the given file system?
In both cases i understand that if there is any possible way to extract this info it will probably be totally unsafe and racy (anything could happen to these set of blocks between the time i see them and act on them somehow). I also really don't want an implementation that will have to know a lot about every filesystem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是不可能以独立于文件系统的方式实现的。文件系统甚至不一定构建在块设备之上(简单的示例:NFS)。
对于
ext2
、ext3
和ext4
,您可以使用ext2fslib
库,该库包含在e2fsprogs 中分布。
It isn't possible in a filesystem-independent way. Filesystems aren't necessarily even built on top of block devices (simple example: NFS).
For
ext2
,ext3
andext4
you can use theext2fslib
library, which is included in thee2fsprogs
distribution.至少对于您的第一个问题,可以采用独立于文件系统的方式。复制我对服务器故障问题的回答:
不幸的是,我不知道免费空间有类似的东西。我只知道
FITRIM
,它告诉底层块设备丢弃与文件系统的可用空间相对应的块。For at least your first question, it is possible in a filesystem-independent way. Copying from my answer to a serverfault question:
Unfortunately, I do not know of anything similar for the free space. I only know about
FITRIM
, which tells the underlying block device to discard the blocks corresponding to the free space of the filesystem.filefrag 命令行工具可让您查看文件的分配。它使用前面答案中提到的 FIEMAP/FIBMAP ioctl。如前所述,这是依赖于文件系统的。不确定除了 ext2/3/4 之外的其他是否支持这些 ioctl。
我不确定报告的物理值是指磁盘的启动还是块设备(分区或逻辑卷)的启动。我有一种感觉,可能是后者。对位于同一磁盘但不同分区的一堆文件调用它就会知道。
dumpe2fs 命令将报告空闲块。同样,它特定于 ext2/3/4 文件系统。块号似乎与分区相关。
A filefrag command line tool let's you look at allocation of files. It uses the FIEMAP/FIBMAP ioctls mentioned in earlier answers. As mentioned before this is filesystem dependent. Not sure whether anything else than ext2/3/4 supports those ioctls.
I'm not sure whether the pysical values reported refer to the start of the disk or to the start of the block device (partition or logical volume). I have the feeling it might be the latter one. Calling it on a bunch of files located on the same disk but in different partitions will tell.
The dumpe2fs command will report free blocks. Again it is specific to ext2/3/4 filesystems. Block numbers seem to be relative to the partition.
partclone.[fstype]
的选项--domain
可以输出多种文件系统类型的块位图。+
标记使用的块范围,请参阅 https://github。 com/Thomas-Tsai/partclone/issues/174Option
--domain
ofpartclone.[fstype]
can output the block bitmap for many filesystem types.+
marks used block ranges, see https://github.com/Thomas-Tsai/partclone/issues/174