Package: refind
Version: 0.14.2-2.1
Severity: normal
Tags: patch upstream

Hi Rod,

FindMountedESP() in refind-install starts by running "mount /boot",
"mount /boot/efi" and "mount /efi" without checking if they are mounted
already. mount does not refuse a second mount of an fstab entry, so each
run of refind-install mounts /boot/efi (and /boot, if it is a separate
partition) again, on top of the first. In a VM, mounting an fstab entry
a second time gives two mounts; only "mount -a" skips what is mounted.

On a running system that mostly goes unnoticed. It breaks installers
that run refind-install in a chroot of the target, as the Calamares
bootloader module does. With a separate /boot partition, the second
/boot hides the first /boot/efi, and the install fails at the last step
with "Could not unmount target system": the second "umount /boot/efi"
finds nothing mounted there.

I hit it in a Calamares install of a Debian sid based system with rEFInd
and a separate /boot, and reproduced it with refind-install alone in a
VM. The attached refind-chroot-mounts-repro.sh (VM only: it runs
refind-install, which tries to write a boot entry) sets up a target the
way an installer does (a root, an ext4 /boot with the ESP under it,
/proc /sys /dev /run bound in, /boot and /boot/efi in its fstab) and
runs refind-install in a chroot of it. After that, findmnt lists /boot
and /boot/efi twice each.

The attached patch makes FindMountedESP() mount each directory only if
mountpoint says it is not mounted yet. With exactly this patch, the same
test mounts each file system once, and unmounting them in the order
Calamares uses works.

The code is the same in upstream master, so the patch applies there too.

Thanks, Kel


-- System Information:
Debian Release: forky/sid
  APT prefers unstable
  APT policy: (500, 'unstable')
Architecture: amd64 (x86_64)

Kernel: Linux 7.3.0-6.slh.3-aptosid-amd64 (SMP w/20 CPU threads; PREEMPT)
Locale: LANG=en_AU.UTF-8, LC_CTYPE=en_AU.UTF-8 (charmap=UTF-8), LANGUAGE not set
Shell: /bin/sh linked to /usr/bin/dash
Init: systemd (via /run/systemd/system)
LSM: AppArmor: enabled
--- a/refind-install    2026-12-01 21:57:13.535104520 +1000
+++ b/refind-install    2026-12-01 21:57:13.546651468 +1000
@@ -1242,9 +1242,15 @@
 # any of these locations.
 # Sets InstallDir to the ESP mount point.
 FindMountedESP() {
-   mount /boot &> /dev/null
-   mount /boot/efi &> /dev/null
-   mount /efi &> /dev/null
+   local Dir
+
+   # Mount each only if it isn't mounted yet: mount itself would stack a
+   # second mount on top of the first.
+   for Dir in /boot /boot/efi /efi ; do
+      if ! mountpoint -q "$Dir" ; then
+         mount "$Dir" &> /dev/null
+      fi
+   done
    EspLine=$(df "$RootDir/efi" 2> /dev/null | grep efi)
    if [[ -z "$EspLine" ]] ; then
       EspLine=$(df "$RootDir"/boot/efi 2> /dev/null | grep boot/efi)
#!/bin/bash
# VM ONLY: run as root in a throwaway live VM, never on a real machine. It
# runs the real refind-install, which writes an EFI boot entry, and makes
# loop devices.
# refind-install mounts /boot and /boot/efi again when they are mounted
# already; an installer runs it in a chroot of its target, where that
# stacked /boot breaks the installer's final unmount.
# usage: refind-chroot-mounts-repro.sh <refind .deb> [<patched refind-install>]
# A fake target the way Calamares lays it out: root, a plain /boot and the
# ESP mounted beneath it, /proc /sys /dev /run bound in, fstab naming both.
set -u
deb=$1
patched=${2:-}
T=/tmp/calamares-root-repro
work=/tmp/refind-repro

setup() {
        mkdir -p "$work" "$T"
        truncate -s 300M "$work/esp.img"
        truncate -s 1G "$work/boot.img"
        truncate -s 2G "$work/root.img"
        esp=$(losetup -f --show "$work/esp.img")
        boot=$(losetup -f --show "$work/boot.img")
        root=$(losetup -f --show "$work/root.img")
        mkfs.vfat -F32 -n EFI "$esp" > /dev/null
        mkfs.ext4 -q -L boot "$boot"
        mkfs.ext4 -q -L rootfs "$root"
        mount "$root" "$T"
        # the live system's userland stands in for the installed one
        mkdir -p "$T"/{usr,etc,proc,sys,dev,run,tmp,boot,efi}
        mount --bind /usr "$T/usr"
        for l in bin sbin lib lib64; do [ -L "/$l" ] && cp -a "/$l" "$T/"; done
        cp -a /etc/. "$T/etc/"
        mount "$boot" "$T/boot"
        mkdir -p "$T/boot/efi"
        mount "$esp" "$T/boot/efi"
        for d in proc sys dev run; do mount --bind "/$d" "$T/$d"; done
        {
                echo "UUID=$(blkid -s UUID -o value "$root") / ext4 defaults 0 
1"
                echo "UUID=$(blkid -s UUID -o value "$boot") /boot ext4 
defaults 0 2"
                echo "UUID=$(blkid -s UUID -o value "$esp") /boot/efi vfat 
umask=0077 0 2"
        } > "$T/etc/fstab"
        mkdir -p "$T/tmp/refind"
        dpkg-deb -x "$deb" "$T/tmp/refind"
}

mounts() {
        echo "-- mounts under $T:"
        findmnt -rn -o TARGET,SOURCE | grep "^$T/boot" | sed 's/^/   /'
}

# what Calamares' UmountJob does: every mount under the target, sorted by
# path descending, each `umount -lv`; the first failure fails the install
calamares_umount() {
        echo "-- Calamares-style unmount (path order):"
        local rc=0
        for m in $(findmnt -rn -o TARGET | grep "^$T/" | sort -r); do
                if ! umount -l "$m" 2> "$work/err"; then
                        echo "   umount $m FAILED: $(cat "$work/err")"
                        rc=1
                        break
                fi
                echo "   umount $m ok"
        done
        return $rc
}

teardown() {
        umount -R -l "$T" 2> /dev/null
        losetup -d "$esp" "$boot" "$root"
        rm -rf "$work"
}

run_case() {
        echo "== $1"
        setup
        mounts
        chroot "$T" /tmp/refind/usr/share/refind/refind-install --yes > 
"$work/ri.log" 2>&1
        echo "-- refind-install exit $?; $(grep -m1 -i -e 'installing rEFInd' 
"$work/ri.log")"
        grep -i -m3 -e "error" -e "abort" "$work/ri.log" | sed 's/^/   /'
        mounts
        calamares_umount && echo "-- install would have completed"
        teardown
        echo
}

run_case "refind-install 0.14.2 as shipped"
[ -n "${patched}" ] || exit 0
mkdir -p /tmp/deb-patched && dpkg-deb -R "$deb" /tmp/deb-patched
cp "${patched}" /tmp/deb-patched/usr/share/refind/refind-install
echo "== the change:"
diff -u <(dpkg-deb --fsys-tarfile "$deb" | tar -xO 
./usr/share/refind/refind-install) \
        /tmp/deb-patched/usr/share/refind/refind-install | sed -n '3,$p'
dpkg-deb -b /tmp/deb-patched /tmp/refind-patched.deb > /dev/null
deb=/tmp/refind-patched.deb
run_case "refind-install 0.14.2 with the change"
rm -rf /tmp/deb-patched /tmp/refind-patched.deb

Reply via email to