View physical location of a file/directory on a hard disk

Is there any Linux program which offers the same (or some of the) functionality of Sysinternals DiskView, especially being able to view to physical location of a file on a hard disk?

DiskView URL: http://technet.microsoft.com/en-gb/sysinternals/bb896650

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

For some file systems like ext4 or btrfs on Linux, you can use filefrag to get the offsets of the data segments for the file on the block device the file system is on.

$ seq 1000 > a
$ filefrag -v a
Filesystem type is: ef53
File size of a is 3893 (1 block of 4096 bytes)
 ext:     logical_offset:        physical_offset: length:   expected: flags:
   0:        0..       0:   82784147..  82784147:      1:             eof
a: 1 extent found
$ sudo dd bs=4k skip=82784147 count=1 if=/dev/storage/home 2>&- | head
1
2
3
4
5
6
7
8
9
10

Here the block device is a LVM volume. That volume may have physical volumes on disks, on partitions, on RAID arrays, on files, on RAM, on network block devices… Going back to an actual disk or set of disk may prove difficult.

In my case, it’s relatively easy, as it’s just a logical volume on top of one GPT partition as one linear stretch.

$ sudo dmsetup table /dev/storage/home
0 1953120256 linear 8:98 384

So /dev/storage/home is 384 sectors within device 8:98, which happens to be /dev/sdg2 for me.

$ cat /sys/block/sdg/sdg2/start
489060352

So sdg2 is 489060352 sectors within /dev/sdg (the 7th disk on this system).

So I can obtain the offset within the single disk that file is on with:

$ sudo dd if=/dev/sdg skip="$((489060352+384+82784147*8))" count=1 2> /dev/null | head
1
2
3
4
5
6
7
8
9
10


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x