Browse .img without mounting

Is there anyway to browse a .img file without mounting it? I have an .img file which I have generated from a clonezilla partition snapshot. I somehow have problems mounting the file with the command mount -o loop sda5.img /mnt/.

dmesg | tail shows me the following error:

[ 1325.395286] EXT4-fs (loop0): bad geometry: block count 11221248 exceeds size of device (11218432 blocks)

Anyone got an idea how to browse without mounting or what might be causing this error?


Solution 1:

If this is an image of a whole hard drive or as in your case this might be an image of an extended partition, you will need to know the partition layout and the offsets of each partition in the file to be able to mount them.

To do this you will need the parted tool, you can install it with the following command:

apt-get install parted

After installing parted, start it with the path to your image file. A parted prompt will be shown:

$ parted /path/to/image/file/sda5.img
GNU Parted 2.3
.....
(parted)

In this prompt (prefixed with (parted)) type the following commands:

(parted) unit B
(parted) print
.....
Number  Start          End            Size           Type     File system  Flags
 1      1048576B       1573912575B    1572864000B    primary  ntfs         boot
 2      1573912576B    156774694911B  155200782336B  primary  ntfs
 3      156774694912B  171454758911B  14680064000B   primary  ntfs
 4      171454758912B  180044693503B  8589934592B    primary

(parted) q

The table in the output will tell you the offsets of each partition in the file and the filesystem type in each partition. For example to mount the first partition in the exemplary output above you will need to enter following command:

sudo mount -o loop,ro,offset=1048576 /path/to/image/file/sda5.img /mnt/partition

Make sure that /mnt/partition exists first.

Solution 2:

I got this same issue with an ext4 image generated from a Clonezilla snapshot.

What actually worked for me was to extend the file to the size the partition table reports, using truncate. The error dmesg reported in my case was:

[15687.653868] EXT4-fs (loop0): bad geometry: block count 18482176 exceeds size
  of device (18352672 blocks)

So I adjusted the length of the file with:

truncate -o -s 18482176 /path/to/partition.img

(^ note that's the first, and larger of the two numbers in the log)

After that mount was happy and I could access my data.