简体   繁体   English

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

[英]Linux - How do i know the block map of the given file and/or the free space map of the partition

I am on Linux and need to know either of the two things:我在 Linux 上,需要知道以下两件事之一:

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? 1) 如果我在 Linux 下的分区上的某个文件系统上有一个常规文件,有没有办法从用户空间知道该文件在驱动器上占用的物理块集? 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? 2)有没有办法获得关于给定文件系统的整个可用空间的相同信息?

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.我也真的不想要一个必须对每个文件系统都了解很多的实现。

For at least your first question, it is possible in a filesystem-independent way. 至少对于第一个问题,可能以与文件系统无关的方式出现。 Copying from my answer to a serverfault question : 我的答案复制到serverfault问题

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. 一种获取块列表(无需像debugfs答案那样从分区中读取)的简单方法是使用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. 快速的Google搜索为我提供了FIBMAP用法的示例 ,它确实可以满足您的需求。 One advantage is that it will work on any filesystem which supports the bmap operation, not just ext3. 一个优点是它可以在支持bmap操作的任何文件系统上运行,而不仅仅是ext3。

A newer (and more efficient) alternative is the FIEMAP ioctl, which can also return detailed information about extents (useful for ext4). FIEMAP ioctl是更新(更有效)的替代方法, FIEMAP可以返回有关范围的详细信息(对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. 我只知道FITRIM ,它告诉基础块设备丢弃与文件系统的可用空间相对应的块。

A filefrag command line tool let's you look at allocation of files. Filefrag命令行工具让您查看文件的分配。 It uses the FIEMAP/FIBMAP ioctls mentioned in earlier answers. 它使用前面的答案中提到的F​​IEMAP / FIBMAP ioctl。 As mentioned before this is filesystem dependent. 如前所述,这取决于文件系统。 Not sure whether anything else than ext2/3/4 supports those ioctls. 不确定除ext2 / 3/4以外的其他功能是否支持这些ioctl。

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. dumpe2fs命令将报告可用块。 Again it is specific to ext2/3/4 filesystems. 同样,它特定于ext2 / 3/4文件系统。 Block numbers seem to be relative to the partition. 块号似乎与分区有关。

It isn't possible in a filesystem-independent way. 以文件系统无关的方式是不可能的。 Filesystems aren't necessarily even built on top of block devices (simple example: NFS). 文件系统甚至不一定建立在块设备之上(简单示例:NFS)。

For ext2 , ext3 and ext4 you can use the ext2fslib library, which is included in the e2fsprogs distribution. 对于ext2ext3ext4您可以使用ext2fslib库,该库包含在e2fsprogs发行版中。

Option --domain of partclone.[fstype] can output the block bitmap for many filesystem types.对于许多文件系统类型,选项--domain of partclone.[fstype]可以 output 块 bitmap。 + marks used block ranges, see https://github.com/Thomas-Tsai/partclone/issues/174 +标记使用的块范围,参见https://github.com/Thomas-Tsai/partclone/issues/174

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM