Skip to contentSkip to conversation

iotests/108: probe loop device setup

PATCH In review Ilya Leoshkevich 3 participants 5 messages Oct 13 – Oct 30

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iotests/108: probe loop device setup

Running iotests/108 in a Docker container, that has passwordless sudo configured, fails with:

losetup: /home/user/src/qemu/build/scratch/qcow2-file-108/t.qcow2: failed to set up loop device: No such file or directory

because it incorrectly assumes losetup is allowed to do loop device setup, when in reality it can only list devices.

Fix by probing loop device setup, using the test script as a backing file. With this, the test is correctly skipped.

---
 tests/qemu-iotests/108 | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tests/qemu-iotests/108 +2 -1
index 1de718188aa..adbaaf87095 100755
@@ -53,7 +53,8 @@ _supported_os Linux
 _unsupported_imgopts 'refcount_bits=\([^1]\|.\([^6]\|$\)\)' data_file
 
 # This test either needs sudo -n losetup or FUSE exports to work
-if sudo -n losetup &>/dev/null; then
+if probe_dev=$(sudo -n losetup -r --show -f "$0" 2>/dev/null); then
+    sudo -n losetup -d "$probe_dev"
     loopdev=true
 else
     loopdev=false
Signature · 1 line
2.56.0
#1 lore raw
Quoted 21 lines · Ilya Leoshkevich

Running iotests/108 in a Docker container, that has passwordless sudo configured, fails with:

losetup: /home/user/src/qemu/build/scratch/qcow2-file-108/t.qcow2: failed to set up loop device: No such file or directory

because it incorrectly assumes losetup is allowed to do loop device setup, when in reality it can only list devices.

Fix by probing loop device setup, using the test script as a backing file. With this, the test is correctly skipped.

---
 tests/qemu-iotests/108 | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tests/qemu-iotests/108 +0 -0
index 1de718188aa..adbaaf87095 100755

tests/qemu-iotests/108 _supported_os Linux · line 57

Quoted 6 lines
tests/qemu-iotests/108 +2 -1
@@ -53,7 +53,8 @@ _supported_os Linux
 _unsupported_imgopts 'refcount_bits=\([^1]\|.\([^6]\|$\)\)' data_file
 
 # This test either needs sudo -n losetup or FUSE exports to work
-if sudo -n losetup &>/dev/null; then
+if probe_dev=$(sudo -n losetup -r --show -f "$0" 2>/dev/null); then
+    sudo -n losetup -d "$probe_dev"

Is 'sudo -n losetup -f' a simpler test? It prints out the next /dev/loopN value available only if a mount would succeed, which means that it is a stronger test than the existing 'sudo -n losetup', but also does not require a subsequent cleanup.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org
#2 lore raw
Quoted 24 lines · Eric Blake
Quoted 23 lines · Ilya Leoshkevich

Running iotests/108 in a Docker container, that has passwordless sudo configured, fails with:

losetup: /home/user/src/qemu/build/scratch/qcow2-file-108/t.qcow2: failed to set up loop device: No such file or directory

because it incorrectly assumes losetup is allowed to do loop device setup, when in reality it can only list devices.

Fix by probing loop device setup, using the test script as a backing file. With this, the test is correctly skipped.

---
  tests/qemu-iotests/108 | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
tests/qemu-iotests/108 +0 -0
index 1de718188aa..adbaaf87095 100755
@@ -53,7 +53,8 @@ _supported_os Linux
  _unsupported_imgopts 'refcount_bits=\([^1]\|.\([^6]\|$\)\)' data_file
 
Quoted 9 lines

tests/qemu-iotests/108 _supported_os Linux · line 57

Quoted 4 lines
tests/qemu-iotests/108 +2 -1
  # This test either needs sudo -n losetup or FUSE exports to work
-if sudo -n losetup &>/dev/null; then
+if probe_dev=$(sudo -n losetup -r --show -f "$0" 2>/dev/null); then
+    sudo -n losetup -d "$probe_dev"

Is 'sudo -n losetup -f' a simpler test? It prints out the next /dev/loopN value available only if a mount would succeed, which means that it is a stronger test than the existing 'sudo -n losetup', but also does not require a subsequent cleanup.

I don't think 'losetup -f' attempts a mount, since it works fine without privileges:

$ id -u
1000
$ losetup -f
/dev/loop0
$ losetup -r -f /bin/true
losetup: /bin/true: failed to set up loop device: Permission denied

A quick glance at the source code [1] seems to confirm this: the iterator is immediately discarded after finding a free device name. The loop-control logic above looks similar.

[1] https://github.com/util-linux/util-linux/blob/v2.42.4/lib/loopdev.c#L1760

Reviewed-by
#3 lore raw
Quoted 39 lines · Ilya Leoshkevich
Quoted 17 lines · Eric Blake
Quoted 16 lines · Ilya Leoshkevich

Running iotests/108 in a Docker container, that has passwordless sudo configured, fails with:

losetup: /home/user/src/qemu/build/scratch/qcow2-file-108/t.qcow2: failed to set up loop device: No such file or directory

because it incorrectly assumes losetup is allowed to do loop device setup, when in reality it can only list devices.

Fix by probing loop device setup, using the test script as a backing file. With this, the test is correctly skipped.

---
  tests/qemu-iotests/108 | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 15 lines

tests/qemu-iotests/108 _supported_os Linux · line 56

Quoted 10 lines
tests/qemu-iotests/108 +2 -1
index 1de718188aa..adbaaf87095 100755
@@ -53,7 +53,8 @@ _supported_os Linux
  _unsupported_imgopts 'refcount_bits=\([^1]\|.\([^6]\|$\)\)' data_file
  # This test either needs sudo -n losetup or FUSE exports to work
-if sudo -n losetup &>/dev/null; then
+if probe_dev=$(sudo -n losetup -r --show -f "$0" 2>/dev/null); then
+    sudo -n losetup -d "$probe_dev"

Is 'sudo -n losetup -f' a simpler test? It prints out the next /dev/loopN value available only if a mount would succeed, which means that it is a stronger test than the existing 'sudo -n losetup', but also does not require a subsequent cleanup.

I don't think 'losetup -f' attempts a mount, since it works fine without privileges:

Quoted 6 lines
$ id -u
1000
$ losetup -f
/dev/loop0
$ losetup -r -f /bin/true
losetup: /bin/true: failed to set up loop device: Permission denied

Thanks for testing. In that case, I think your longer version is reasonable.

Quoted 8 lines

A quick glance at the source code [1] seems to confirm this: the iterator is immediately discarded after finding a free device name. The loop-control logic above looks similar.

[1]
https://github.com/util-linux/util-linux/blob/v2.42.4/lib/loopdev.c#L1760

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org
#4 lore raw
In reply to Ilya Leoshkevich

Am 13.10.2026 um 20:28 hat Ilya Leoshkevich geschrieben:

Quoted 5 lines

Running iotests/108 in a Docker container, that has passwordless sudo configured, fails with:

losetup: /home/user/src/qemu/build/scratch/qcow2-file-108/t.qcow2: failed to set up loop device: No such file or directory

Quoted 7 lines

because it incorrectly assumes losetup is allowed to do loop device setup, when in reality it can only list devices.

Fix by probing loop device setup, using the test script as a backing file. With this, the test is correctly skipped.

Won't loop-create-file and fuse-truncate need the same fix?

Maybe we could have a _require_losetup() helper function in common.rc for the shell-based tests? (Only two at the moment, though I also have a patch on the list that would add a third one.)

Kevin