multiple paritioning of loopback device leads to error

I’m in the development of a partitioning tool, for testing purposes I have to run partitioning and LVM configurations repeatedly on a loopback device

  • Partition /dev/loop0 with 3 partitions /dev/loop0p1 /dev/loop0p2 /dev/loop0p3
  • Create LVMs
pvcreate /dev/loop0p2 /dev/loop0p3
vgcreate test_vg /dev/loop0p2 /dev/loop0p3
lvcreate -L 1GB test_vg -n vol1 --wipesignature y --zero y
lvcreate -L 1GB test_vg -n vol2 --wipesignature y --zero y
partprobe /dev/loop0

After such a run I’d like to return everything again on the same device, however, when running the partitioning I’m getting the error

Partition(s) 2, 3 on /dev/loop0 have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use.  As a result, the old partition(s) will remain in use.  You should reboot now before making further changes.

Now I’m aware that a solution is to restart, but it’s not reasonable to do that continuously during development. Is there a way fix the issue without having to reboot all the time?

Asked By: wasp256


This error appears to be caused when you try to modify or create partitions /dev/loopxp2 and /dev/loopxp3 despite them still being in use as physical volumes for LVM.

LVM is not like partitioning in that sense. Even if no filesystem is mounted the physical volumes are still in use by LVM itself.

In your question you don’t mention creating. A volume group. The principle here is that you need to operate on the whole volume group together (all of it available or none of it).

To detach it and tear down the loopback devices you need to vgexport the volume group; this will stop LVM using the physical volumes. Then you will be able to manipulate the partition table without error.

Answered By: Philip Couling
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.