4

I need to determine the sector range(s) occupied by a file on a disk device (for Linux). Let's assume it's a simple disk (no RAID) with MBR or GPT partition table.

I learned that I can get the position of a file within the file system using FIBMAP ioctl. How do I translate the data returned by FIBMAP into LBA sectors of the disk? Thank you

Praveen Vinny
  • 2,372
  • 6
  • 32
  • 40
Isso
  • 1,285
  • 11
  • 23
  • 1
    I suspect this may be filesystem-dependent. Which filesystem(s) do you need to support? – NPE Dec 04 '12 at 20:49
  • ext3, ext4 as a minimum. But I would really appreciate a file system independent solution – Isso Dec 04 '12 at 20:52
  • Wouldn't it be easier to start at the beginning inode and iterate from there? And as the file may be fragmented to each and every block, is it worth the hassle? – ott-- Dec 04 '12 at 23:02
  • @ott-- sorry I'm not sure what do you mean. Why should I iterate the inodes if I only need to know the position of a single file? – Isso Dec 04 '12 at 23:16
  • You wrote `position of a file` in the header and then `the sector range(s) occupied by a file`. I supposed the latter. – ott-- Dec 04 '12 at 23:20
  • hmm, may be I'm missing something, but whats the difference? I mean file may occupy one or more sector ranges, I need to determine them – Isso Dec 04 '12 at 23:23
  • Look at the source code of the hdparm program and look at a bug report here: http://sourceforge.net/p/hdparm/bugs/58/ – Windows programmer Sep 06 '13 at 04:05

1 Answers1

0

I don't have an exact answer for you but if you look at the source code of hdparm you should be able to figure it out. If you use hdparm --fibmap /dir1/dir2/foo it will output the chunks of the file in rows of file offset, start LBA, stop LBA, and sector length. I hope that helps.