I regularly mount the partitions inside an image file as loop devices:

sudo kpartx -a test.img

That creates loop devices such as


When I'm done, I get rid of them with

sudo kpartx -d test.img

and usually the loop devices go away. But sometimes, they don't, and I don't know why. If I then do a:

sudo losetup -d /dev/mapper/loop0p1

losetup seems happy enough, except that the device stays. How do I get rid of them without rebooting?

  • When it does happen, are you sure test.img is still fully accessible and that no program had used it without passing by the loopback device? – user2284570 Feb 23 '15 at 21:49
  • The image file is still there. But I don't understand what you mean with "no program had used it". Is there a rule that it can't be? For example, I might install grub to the image file, while the partitions inside are still mounted via loop devices. Is that a no-no? – Johannes Ernst Feb 23 '15 at 22:52
  • It would mean that the content of the file is different from what the kernel try to handle trough the loop device. – user2284570 Feb 23 '15 at 22:55
  • I would understand potentially undefined results. But not that I'm prevented from removing the loop device. (And I'm not certain that this is the right explanation; there seem to be cases where I didn't touch the image) – Johannes Ernst Feb 23 '15 at 23:26
  • No, because it is likely to turn the kthread responsible for the loop device into an hung task. As long as the pid exist the loop can't get deleted. – user2284570 Feb 23 '15 at 23:28

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.