Skip to contentSkip to conversation

fast creation and deletion of loop devices

DISCUSSION Simon Richter 1 participant 1 message Jul 8, 2021 Block layer

lore
Share & tools
lore
#0 lore raw

Hi,

I have just used a crude loop to search for a partition on a block device:

    for i in `seq 1 4096`
    do
        losetup -r -o ${i}k /dev/loop0 /dev/sda
        mount /dev/loop0 /mnt && break
        losetup -d /dev/loop0
    done

Creating the loop device fails sometimes, with

losetup: /dev/sda: failed to set up loop device: Device or resource busy

and the kernel log contains a few instances of

loop_set_status: loop0 () has still dirty pages (nrpages=5)

My machine is now in a state where I can call

until losetup -d /dev/loop0; do :; done

and the loop just keeps running.

I'm slightly confused why a read-only loop device would have dirty pages in the first place, and I suspect that there is a kernel bug here that allows me to enter an inconsistent state through a race condition if I just attach and detach loop devices fast enough.

Simon