Mounting a file as loop devices is simple enough operation! But what if the file just disappears because of the network storage got unreachable?
In our case the shared network storage had got unavailable and the ext4 file system of the loop device got read-only! Unfortunately after the reset of the shared network storage and mounting it in the same place, the loop device (/dev/loop0) still maintained the old file descriptor to the unavailable file. And the losetup could not detach the device, at all.
root@srv ~# losetup -d /dev/loop0 root@srv ~# losetup -l NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE /dev/loop0 0 0 0 0 /mnt/storage4/servers/test_raw_image.img [root@lsrv1 ~]# kpartx -df /dev/loop0 read error, sector 0 read error, sector 1 read error, sector 29
The above command could not detach the device. Unfortunately, the losetup does not have the force detach, so the server ended with blocked loop0 device pointing to unavailable file. kpartx does not work, neither.
The solution is to use dmsetup!
root@srv ~# dmsetup remove /dev/mapper/loop0p1 root@srv ~# losetup -l root@srv ~#
And there is no loop device any more. The wrong pointing loop device has been removed successfully! Now the user can use the loop0 for another device and in many cases, this helps to umount the filesystem!
This is super useful ! Thank you !