I have a script that I use to mount diskimages that are LVM images. it does:
x=`losetup -f` #create a loop device echo $x > loopdev #echo that device to a file for later use losetup $x disk.img #mount the disk.img to the loop device kpartx -av $x #use kpartx to add the partition mappings vgscan && vgchange -ay #vgscan the new mappings and make active mount /dev/VolGroupRoot/LogVolRoot /mnt #mount the volgroup to /mnt mount -o loop,offset=$((2048 * 512)) disk.img /mnt/boot #mount the boot partition in the image to /mnt/boot
This works a charm when there are available loop devices.
Alternatively, to then umount the img I run:
umount /mnt/boot #umount boot umount /mnt #umount volgroup vgchange -an VolGroupRoot #make volgroup inactive x=`cat loopdev` #get my used loopdevice kpartx -d $x #delete the partition mappings losetup -d $x #detach the loop device /bin/rm loopdev #remove the loopdev file in prep for the next time I mount something using the script.
This runs without errors however, I find that it doesn't always remove the loop device for later use. losetup -a often shows those previously used loop devices still in it's table. dmsetup info doesn't show the loop devices but I do see them with lsof being held by kthreadd. As a result it appears there's no way to remove the /dev/loop devices without a reboot which seems like taking a sledge hammer to the problem. I can (and often do) end up building new loop devices to get around this but that's unsustainable and doesn't address the real issue. has anybody seen this and/or have any other things I can try (fwiw, this is happening on 3.18 and 4.1 kernel systems).