Friday, March 2, 2018

Mounting disk image (.img) file in Linux

How to mount .img file in Linux which has got multiple partitions; 


1. First, check partition table of the image by using fdisk;

[root@Linux]# ls hive.img
hive.img


[root@Linux]# fdisk -l hive.img

Disk hive.img: 7549 MB, 7549747200 bytes, 14745600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x244b7fbe

   Device Boot      Start         End      Blocks   Id  System
hive.img1            2048       43007       20480    e  W95 FAT16 (LBA)
hive.img2   *       43008    14690303     7323648   83  Linux

2. Now we need to mount hive.img2 means the second partition of the disk image;

Now offset of hive.img2 is "Start Sector" X "Block Size"
mount -o loop,offset=$((43008*512)) hive.img /mnt

[root@Linux]# df -hP /mnt
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop0      6.8G  4.3G  2.1G  68% /mnt

Note: If offset is not specified, mount fails to identify the filesystem automatically as below;

[root@Linux]# mount -o loop hive.img /mnt
mount: /dev/loop0 is write-protected, mounting read-only
mount: unknown filesystem type '(null)'







No comments:

Post a Comment