Qemu/Choot Into an Img File
Directions for mounting an IMG file in Linux. Tested with Raspbian ARM
Mounting the image
Explanation of the task
IMG files found in arm images are complete disk dumps made to a file. The IMG
file will contain all of the partition information, just as you'd see it if
running dd if=/dev/sda bs=4K | cat
The mount command allows you to pass the exact start and end sectors you'd like to mount. Using this, we will mount the start and end of our partition inside the img file.
Getting Starting Sector
The output of this command will contain the start and end sectors of each partition. You can tell which partition is your IMG file's root partition by comparing the sizes. Larger typically means root.
fdisk -l file.img
[...]
Units: sectors of 1 * 512 = 512 bytes
[...]
Device Boot Start End Sectors Size Id Type
2021-05-07-raspios-buster-armhf-lite.img1 8192 532479 524288 256M c W95 FAT32 (LBA)
2021-05-07-raspios-buster-armhf-lite.img2 532480 3661823 3129344 1.5G 83 Linux
My root partition starts on sector 532480. This is a logical sector offset. It's a way to identify where this partition starts on a disk. What I need is the exact starting and ending byte instead. We have to convert the logical sector offset into a byte offset.
Mounting the IMG
Multiply the start sector (532480) by the sector size (512). I'm using bash syntax to accomplish this.
mount -o loop,offset=$((512*532480)),rw
~/2021-05-07-raspios-buster-armhf-lite.img /mnt
This will mount the drive. Since the partition partition, I don't have to worry about specifying the ending offset.