18

I am trying to mount root and boot partition of Raspbian image:

mount -v -o offset="70254592" -t ext4 /mnt/X/raspbian-jessie.img /tmp/raspbian
mount -v -o offset="4194304" -t vfat /mnt/X/raspbian-jessie.img /tmp/boot

mounting boot, when root is mounted results in:

mount: /mnt/X/raspbian-jessie.img: overlapping loop device exists

How to mount multiple partitions on one disk image at same time? (for disks it's obviously possible, why not for files?)

0

4 Answers 4

13

You need to specify the length of the partition(s) to avoid overlap. Option sizelimit, see man mount, man losetup.

9

losetup 2.21 -P option

losetup -P -f --show my.img

Creates one /dev/loopXpY per partition.

Advantage: executable pre-installed in many distros (util-linux package).

Disadvantage: quite recent option, not present in Ubuntu 14.04, before that just use kpartx: https://unix.stackexchange.com/a/405639/32558

losetup -P automation

Usage:

$ los my.img
/dev/loop0
/mnt/loop0p1
/mnt/loop0p2

$ ls /mnt/loop0p1
/whatever
/files
/youhave
/there

$ sudo losetup -l
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE                                                                                      DIO
/dev/loop1         0      0         0  0 /full/path/to/my.img

$ # Cleanup.
$ losd 0
$ ls /mnt/loop0p1
$ ls /dev | grep loop0
loop0

Source:

los() (
  img="$1"
  dev="$(sudo losetup --show -f -P "$img")"
  echo "$dev"
  for part in "$dev"?*; do
    if [ "$part" = "${dev}p*" ]; then
      part="${dev}"
    fi
    dst="/mnt/$(basename "$part")"
    echo "$dst"
    sudo mkdir -p "$dst"
    sudo mount "$part" "$dst"
  done
)
losd() (
  dev="/dev/loop$1"
  for part in "$dev"?*; do
    if [ "$part" = "${dev}p*" ]; then
      part="${dev}"
    fi
    dst="/mnt/$(basename "$part")"
    sudo umount "$dst"
  done
  sudo losetup -d "$dev"
)
6

Use kpartx tool. It will map image partitions using /dev/mapper which you can mount directly.

$ sudo kpartx -a disk.img
$ sudo mount -o loop /dev/mapper/loop0p2 /mnt

PS. Do not forget to remove mappings after you are done: sudo kpartx -d disk.img

1
  • 3
    kpartx is part of multipath-tools Oct 22, 2020 at 5:12
0

I guess the other answers here are all correct, but seems to me they are terse - or going a bit far afield. losetup is great, but it may be confusing to some who've only begun. So - here's my answer:

Since the OP didn't begin his question with fdisk -lu <file.img> I'll use my own RPi as an example. This because fdisk -lu <file.img> provides "the numbers" to complete the example.

On my Pi:

$ fdisk -lu ./back.img
Disk ./back.img: 2.89 GiB, 3099590656 bytes, 6053888 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: 0x00f24f4c

Device      Boot  Start     End Sectors  Size Id Type
./back.img1        2048  526335  524288  256M  c W95 FAT32 (LBA)
./back.img2      526336 6053887 5527552  2.6G 83 Linux

As we see there are two (2) partitions, a FAT32 (from 2048 to 526335), and a Linux/ext4 (from 526336 to 6053887) - or two partitions of 524,288 and 5,527,552 sectors (of 512 bytes/sector), respectively.

When I tried to mount the two partitions, I also got an "overlap" error:

$ sudo mount -o loop,offset=1048576 ./back.img /mnt/loopy/boot
$ sudo mount -o loop,offset=269484032 ./back.img /mnt/loopy/root
mount: /mnt/loopy/root: overlapping loop device exists for /home/pi/back.img.

As the answer above suggests, the solution is indeed the sizelimit option, but at least in my case, it was a bit more subtle:

$ sudo umount /mnt/loopy/boot /mnt/loopy/root 
$ sudo mount -o loop,offset=1048576,sizelimit=268435456 ./back.img /mnt/loopy/boot 

# NOTE: sizelimit = 512 bytes/sector * 524288 sectors = 268435456 bytes 

$ sudo mount -o loop,offset=269484032,sizelimit=2830106624 ./back.img /mnt/loopy/root 

# NOTE: sizelimit = 512 bytes/sector * 5527552 sectors = 2830106624 bytes 

Having successfully mounted the two partitions in back.img, I can now make the changes I need. I used my editor to modify three files in /mnt/loopy/root/etc : hostname, hosts and dhcpcd.conf. When I finished, I unmounted both & transferred the modified back.img file to my NAS:

$ sudo umount /mnt/loopy/root /mnt/loopy/boot 
$ rsync -avi ./back.img /mnt/SynologyNAS/rpi_share/raspberrypi3a 
sending incremental file list
>f.st...... back.img

sent 3,100,347,489 bytes  received 35 bytes  10,897,530.84 bytes/sec
total size is 3,099,590,656  speedup is 1.00 

# NOTE: don't use `cp` for this! 

And finally - as proof that all went well - I used Etcher to "burn" back.img to a new SD card, then booted my new RPi 3A+ with it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .