Skip to content

Instantly share code, notes, and snippets.

@hhromic
Last active November 5, 2018 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hhromic/dfa64cd24f883f01088e41281ba8cb3d to your computer and use it in GitHub Desktop.
Save hhromic/dfa64cd24f883f01088e41281ba8cb3d to your computer and use it in GitHub Desktop.
Working with disk images in Linux

Working with Disk Images in Linux

Ref: https://www.linuxquestions.org/questions/linux-general-1/how-to-mount-img-file-882386/#post4365399

To list the partitions of a disk image:

$ fdisk -l mydisk.img
Disk mydisk.img: 15 GiB, 16088301568 bytes, 31422464 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
Disklabel type: dos
Disk identifier: 0xce5f7383

Device      Boot Start      End  Sectors  Size Id Type
mydisk.img1       8192    97921    89730 43.8M  c W95 FAT32 (LBA)
mydisk.img2      98304 31422463 31324160   15G 83 Linux

To mount a partition from a disk image:

  1. You must first figure out the block size in your image file. In the above example is 512 bytes:

    Units: sectors of 1 * 512 = 512 bytes
    
  2. Then find the starting block (Start) of the partition of interest. In the above example, the first partition starts at 8192 and the second at 98304:

    Device      Boot Start      End  Sectors  Size Id Type
    mydisk.img1       8192    97921    89730 43.8M  c W95 FAT32 (LBA)
    mydisk.img2      98304 31422463 31324160   15G 83 Linux
    
  3. Finally, multiply the start block by the block size to obtain the offset and use the following mount command:

    $ mount -o loop,offset=<OFFSET> mydisk.img /mnt
    $ mount -o loop,offset=4194304 mydisk.img /mnt   # in the example, the 1st partition (8192 * 512)
    $ mount -o loop,offset=50331648 mydisk.img /mnt  # in the example, the 2nd partition (98304 * 512)
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment