Ubuntu - mount image file with r/w permission

You need to make sure that your current user directory has read and write access to your operation

sudo mkdir -p /tmp/test && sudo mount -o loop,rw,sync image.img /tmp/test

Here's the full process, based on the answer here:

Your .img file is not an image of a partition, but of a whole disk. That means it starts with a bootloader and a partition table. You have to detect the offset of the partition and mount it specifically. Sadly I've never been able to find a Linux tool that automates that. So you have to do math, but it's easy. Here's the process:

fdisk -l raspberry_pi.img

Which gives the output below. Note the sector size in bytes (512 in this case; see line 2 below) and the Start sector of the partition (94208 for the Linux partition; see the last line below).

Disk raspberry_pi.img: 7.3 GiB, 7826571264 bytes, 15286272 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: 0xbeb1a7ff

Device          Boot Start      End  Sectors  Size Id Type
raspberry_pi.img1       8192    93813    85622 41.8M  c W95 FAT32 (LBA)
raspberry_pi.img2      94208 15069183 14974976  7.1G 83 Linux

Now, manually multiply the start sector * sector size to get the offset bytes that the mount command needs. In this case, 94208 * 512 = 48234496

sudo mkdir /media/sdcard
sudo mount -o loop,rw,sync,offset=48234496 printer_v5.img /media/sdcard

Now, the image's Linux partition is mounted at /media/sdcard and the root user can edit its files.

Finally, when you're finished:

sudo umount /media/sdcard

The problem is that there are multiple partitions in your image. A plain old mount looks for filesystem information at offset 0, which in your case apparently points to some bios boot information, but not to the desired ext4 fs. You should succeed by creating the loopback-device with an offset to the desired partition.

This link may help you out.

Tags:

Linux

Ubuntu