Linux - 我如何知道给定文件的块映射和/或分区的可用空间映射

发布于 2024-09-02 20:56:36 字数 273 浏览 3 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遗心遗梦遗幸福 2024-09-09 20:56:36

这是不可能以独立于文件系统的方式实现的。文件系统甚至不一定构建在块设备之上(简单的示例:NFS)。

对于 ext2ext3ext4,您可以使用 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 and ext4 you can use the ext2fslib library, which is included in the e2fsprogs distribution.

戏剧牡丹亭 2024-09-09 20:56:36

至少对于您的第一个问题,可以采用独立于文件系统的方式。复制我对服务器故障问题的回答

获取块列表的一种简单方法(无需像 debugfs 答案中那样从分区读取)是使用 FIBMAP ioctl。我不知道有什么命令可以做到这一点,但编写一个非常简单;快速的 Google 搜索为我提供了FIBMAP 使用示例,这正是你想要的。一个优点是它可以在任何支持 bmap 操作的文件系统上运行,而不仅仅是 ext3。

更新(且更高效)的替代方案是 FIEMAP ioctl,它还可以返回有关范围的详细信息(对于 ext4 很有用)。

不幸的是,我不知道免费空间有类似的东西。我只知道 FITRIM ,它告诉底层块设备丢弃与文件系统的可用空间相对应的块。

For at least your first question, it is possible in a filesystem-independent way. Copying from my answer to a serverfault question:

A simple way to get the list of blocks (without having to read from the partition like in the debugfs answers) is to use the FIBMAP ioctl. I do not know of any command to do so, but it is very simple to write one; a quick Google search gave me an example of FIBMAP use, which does exactly what you want. One advantage is that it will work on any filesystem which supports the bmap operation, not just ext3.

A newer (and more efficient) alternative is the FIEMAP ioctl, which can also return detailed information about extents (useful for ext4).

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.

浅笑轻吟梦一曲 2024-09-09 20:56:36

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.

弃爱 2024-09-09 20:56:36

partclone.[fstype] 的选项 --domain 可以输出多种文件系统类型的块位图。 + 标记使用的块范围,请参阅 https://github。 com/Thomas-Tsai/partclone/issues/174

Option --domain of partclone.[fstype] can output the block bitmap for many filesystem types. + marks used block ranges, see https://github.com/Thomas-Tsai/partclone/issues/174

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文