beta-tester
Posts: 1593
Joined: Fri Jan 04, 2013 1:57 pm
Location: de_DE

SOLVED: [stretch] mount: ... overlapping loop device exists

Sat Aug 05, 2017 5:30 am

hi,
i am not sure, if i can ask that question already, while stretch is not the official raspbian version, now.
i just upgraded & dist-upgraded from raspbian jessie to raspbian stretch successfully.

Code: Select all

Welcome to Raspbian GNU/Linux 9 (stretch)
but now i have an issue mounting the second partition of a disk image. the first is mounted well as usual.
(that issue was not present on raspbian jessie)

the mount option in /etc/fstab:

Code: Select all

/x/2017-07-05-raspbian-jessie-lite.img  /media/boot  auto  loop,ro,nofail,auto,offset=4194304  0  0
/x/2017-07-05-raspbian-jessie-lite.img  /media/root  auto  loop,ro,nofail,auto,offset=48234496  0  0
and the error message i get, for the second partition is:

Code: Select all

$ sudo mount -a
mount: /x/2017-07-05-raspbian-jessie-lite.img: overlapping loop device exists
in the man of mount, there is a note for loops, that if the mount file, the offset and sizelimit is the same, the same loop will be re-used.

Code: Select all

$ man mount
...
THE LOOP DEVICE
       ...
       Since  util-linux  v2.29 mount command re-uses the loop device rather than initialize a new device if the same backing file is already used for some loop device with the same
       offset and sizelimit. This is necessary to avoid a filesystem corruption.
that could explain the error message.
but in my case the offsets are different.
so i don't understand the error message and why it is not mounted.

only the first partition is mounted properly

Code: Select all

$ sudo mount
/x/2017-07-05-raspbian-jessie-lite.img on /media/boot type vfat (ro,relatime...)
...

$ sudo losetup -a
/dev/loop0: []: (/x/2017-07-05-raspbian-jessie-lite.img), offset 4194304
...
if i change the mount options in the /etc/fstab, to use a specific loop device i still run into that issue:

Code: Select all

/x/2017-07-05-raspbian-jessie-lite.img  /media/boot  auto  loop=/dev/loop1,ro,nofail,auto,offset=4194304  0  0
/x/2017-07-05-raspbian-jessie-lite.img  /media/root  auto  loop=/dev/loop2,ro,nofail,auto,offset=48234496  0  0
but when i do whe same step by step (attaching a loop and mounting the loop) be hand, it works:

Code: Select all

$ sudo losetup --offset 4194304 /dev/loop1 /x/2017-07-05-raspbian-jessie-lite.img
$ sudo mount /dev/loop1 /media/boot -o ro
$ sudo losetup --offset 48234496 /dev/loop2 /x/2017-07-05-raspbian-jessie-lite.img
$ sudo mount /dev/loop2 /media/root -o ro

$ sudo losetup -a
/dev/loop1: [45826]:260076 (/x/2017-07-05-raspbian-jessie-lite.img), offset 4194304
/dev/loop2: [45826]:260076 (/x/2017-07-05-raspbian-jessie-lite.img), offset 48234496
$ sudo mount
/dev/loop1 on /media/boot type vfat (ro,relatime...)
/dev/loop2 on /media/root type ext4 (ro,relatime...)
so the big question is, how can i mount the two partitions of the disk image with using /etc/fstab and mount -a?
Last edited by beta-tester on Thu Jun 18, 2020 5:41 am, edited 2 times in total.
{ I only give negative feedback }
RPi B (256MB), B (512MB), B+, ZeroW; 2B; 3B, 3B+; 4B (4GB)

beta-tester
Posts: 1593
Joined: Fri Jan 04, 2013 1:57 pm
Location: de_DE

SOLVED: [stretch] mount: ... overlapping loop device exists

Tue Aug 08, 2017 9:10 pm

i got the issue solved, by adding parameter sizelimit to the mount options as well.

e.g. from commandline:

Code: Select all

mount /x/2017-07-05-raspbian-jessie-lite.img /media/boot -o ro,loop,offset=$((8192*512)),sizelimit=$((85405*512))
mount /x/2017-07-05-raspbian-jessie-lite.img /media/root -o ro,loop,offset=$((94208*512)),sizelimit=$((3276162*512))
or in the /etc/fstab:

Code: Select all

/x/2017-07-05-raspbian-jessie-lite.img /media/boot auto ro,nofail,auto,loop,offset=4194304,sizelimit=43727360 0 0
/x/2017-07-05-raspbian-jessie-lite.img /media/root auto ro,nofail,auto,loop,offset=48234496,sizelimit=1677394944 0 0
{ I only give negative feedback }
RPi B (256MB), B (512MB), B+, ZeroW; 2B; 3B, 3B+; 4B (4GB)

adult-human-v0.9
Posts: 3
Joined: Sun May 31, 2015 10:18 pm

Re: SOLVED: [stretch] mount: ... overlapping loop device exists

Thu Aug 10, 2017 10:49 am

Thanks for posting this - very helpful. I hit this too. Let me add an explanation of where the sizelimit params come from in case anyone is wondering:

The numbers to use in the sizelimit= are the number of "Sectors" in the given partition, which can be found from looking at the partition table. (I refer to the above values of 85405 and 3276162).

Get them by examining the partition table with fdisk: The sizelimit parameters are picked from the "Sectors" column of output.

Code: Select all

$ fdisk -l 2017-07-05-raspbian-jessie-lite.img
Disk 2017-07-05-raspbian-jessie-lite.img: 1.6 GiB, 1725629440 bytes, 3370370 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: 0xa8790229

Device                               Boot Start     End Sectors  Size Id Type
2017-07-05-raspbian-jessie-lite.img1       8192   93596   85405 41.7M  c W95 FAT32 (LBA)
2017-07-05-raspbian-jessie-lite.img2      94208 3370369 3276162  1.6G 83 Linux

beta-tester
Posts: 1593
Joined: Fri Jan 04, 2013 1:57 pm
Location: de_DE

Re: SOLVED: [stretch] mount: ... overlapping loop device exists

Thu Aug 10, 2017 5:30 pm

or a bit more scriptfriendly (or userfriendly) view to get the offset and the size of partitions:

Code: Select all

pi@raspberry:~ $ sfdisk -d 2017-07-05-raspbian-jessie-lite.img

# partition table of 2017-07-05-raspbian-jessie-lite.img
unit: sectors

2017-07-05-raspbian-jessie-lite.img1 : start=     8192, size=    85405, Id= c
2017-07-05-raspbian-jessie-lite.img2 : start=    94208, size=  3276162, Id=83
but you still have to multiply these values by 512byte, because the unit are shown as sectors.
the mount (and losetup) command expects bytes as values for offset= and sizelimit=
{ I only give negative feedback }
RPi B (256MB), B (512MB), B+, ZeroW; 2B; 3B, 3B+; 4B (4GB)

jdpiguet
Posts: 1
Joined: Wed Dec 26, 2012 7:30 pm

Re: SOLVED: [stretch] mount: ... overlapping loop device exists

Sat Apr 13, 2019 8:43 am

Some more information:
  • You don't need to create a loop device, using the "loop" parameter in the mount command suffice.
  • You can get the size in bytes directly with "--bytes" for fdisk.
  • If you are scripting, you get get the necessary information with "fdisk --bytes -lo Id,Start,Size".
See below:

Code: Select all

root@amroth:~# fdisk --bytes -lo Id,Start,Size /srv/raspi/current/2019-04-08-raspbian-stretch-lite.img
Disk /srv/raspi/current/2019-04-08-raspbian-stretch-lite.img: 1.7 GiB, 1803550720 bytes, 3522560 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: 0xc1dc39e5

Id Start       Size
 c  8192   44979712
83 98304 1753219072
root@amroth:~# mount -o loop,offset=$((98304*512)),sizelimit=1753219072 /srv/raspi/current/2019-04-08-raspbian-stretch-lite.img /mnt
root@amroth:~# df -h /mnt
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop0      1.6G  877M  638M  58% /mnt
root@amroth:~# ls -Alh /mnt
total 88K
drwxr-xr-x  2 root root 4.0K avr  8 11:51 bin
drwxr-xr-x  2 root root 4.0K avr 13 10:11 boot
drwxr-xr-x  4 root root 4.0K avr  8 11:39 dev
drwxr-xr-x 84 root root 4.0K avr 13 10:11 etc
drwxr-xr-x  3 root root 4.0K avr 13 10:11 home
drwxr-xr-x 16 root root 4.0K avr  8 11:58 lib
drwx------  2 root root  16K avr  8 12:24 lost+found
drwxr-xr-x  2 root root 4.0K avr  8 11:37 media
drwxr-xr-x  2 root root 4.0K avr  8 11:37 mnt
drwxr-xr-x  3 root root 4.0K avr  8 11:48 opt
drwxr-xr-x  2 root root 4.0K fév 17 16:05 proc
drwx------  3 root root 4.0K avr 13 10:11 root
drwxr-xr-x  5 root root 4.0K avr  8 11:49 run
drwxr-xr-x  2 root root 4.0K avr  8 11:59 sbin
drwxr-xr-x  2 root root 4.0K avr  8 11:37 srv
drwxr-xr-x  2 root root 4.0K fév 17 16:05 sys
drwxrwxrwt  2 root root 4.0K avr  8 12:25 tmp
drwxr-xr-x 10 root root 4.0K avr  8 11:37 usr
drwxr-xr-x 11 root root 4.0K avr  8 11:37 var
root@amroth:~# 

konsumer
Posts: 3
Joined: Sun Sep 23, 2012 8:57 am

Re: SOLVED: [stretch] mount: ... overlapping loop device exists

Thu Jun 18, 2020 2:03 am

This was helpful for me to make a universal chroot script (run things in context of pi image) which in turn, might be helpful to others:

Code: Select all

#!/bin/bash

if (( $# < 3 )); then
  echo "Usage: chroot.sh IMAGE_FILE MOUNT_POINT ...CHROOT_ARGS"
  exit 1
fi

# grab first 2 arguments, and leave rest of arguments
IMAGE="${1}"
shift
ROOT="${1}"
shift
CHROOT_ARGS=$@

if [ ! -f "${IMAGE}" ]; then
  echo "Image file ${IMAGE} does not exist"
  exit 1
fi

# unmount loopback on exit
set -e
function cleanup {
  umount -f "${ROOT}/boot/"
  umount -f "${ROOT}"
}
trap cleanup EXIT

mkdir -p "${ROOT}"

INFO=($(fdisk --bytes -lo Start,Size "${IMAGE}" | tail -n 2))

BOOT_START=$((${INFO[0]} * 512))
BOOT_SIZE=${INFO[1]}
ROOT_START=$((${INFO[2]} * 512))
ROOT_SIZE=${INFO[3]}

mount -o loop,offset=${ROOT_START},sizelimit=${ROOT_SIZE} -t ext4 "${IMAGE}" "${ROOT}"
mount -o loop,offset=${BOOT_START},sizelimit=${BOOT_SIZE} -t vfat "${IMAGE}" "${ROOT}/boot"

chroot "${ROOT}" $CHROOT_ARGS

This will automatically unmount things when the chroot is done.

Usage example:

Code: Select all

mkdir work
chroot.sh 2020-05-27-raspios-buster-lite-armhf.img ./work bash
If you have binfmt_misc setup to run qemu for arm binaries, this will run the chroot very efficiently on any host machine. I am using it to pre-configure a pi image with all the stuff I want installed. I can do this, if my setup script is in work/ before the chroot:

Code: Select all

chroot.sh 2020-05-27-raspios-buster-lite-armhf.img ./work bash /MYSETUPSCRIPT
one caveat is that it helps to setup DNS before chroot:

Code: Select all

echo 'nameserver 8.8.4.4' > work/etc/resolv.conf

Return to “Raspberry Pi OS”