Ubuntu - mount image file with r/w permission

Host: Ubuntu 15.04

With Disk Image Mounter, I can mount the test.img and three directories popped up.

  1. system-a
  2. system-b
  3. writable

As I've searched around, I can not edit the directories since those are read-only.

The possible solution I found so far is to copy these directories to another new place and generate a new image file.

However, since the image file contains these three directories, how can I do it?

=============post update=============

mount: wrong fs type, bad option, bad superblock on /dev/loop1,
       missing codepage or helper program, or other error

       In some cases useful info is found in syslog - try
       dmesg | tail or so.

=====================================

Device                Start     End Sectors  Size Type
test_custom2.img1    8192   16383    8192    4M BIOS boot
test_custom2.img2   16384  147455  131072   64M EFI System
test_custom2.img3  147456 2244607 2097152    1G Linux filesystem
test_custom2.img4 2244608 4341759 2097152    1G Linux filesystem
test_custom2.img5 4341760 7614463 3272704  1.6G Linux filesystem

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.


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