My backup disks use lvm.

They are both inserted into the same cage, so only one of them can be present in the system at any one time. When I remove a disk, the entry in /dev/mapper usually doesn't go away, misleading to be interpreted as if the disk was present.

I want to run a command, that updates /dev/mapper entirely, to be sure that available disks show up and unavailable disk do not.

How can I do that?


Before removing the disk you need to deactive the LVM Volume Group first with vgchange -an <vg_name>. This will also deactivate all the logical volumes and remove the device mapper mappings and thus removing the entries in /dev/mapper.

  • Also, after deactivating the VG but before unplugging the disk, one should signal the kernel that the disk is about to be unplugged. The fundamental way to do this would be echo 1 > /sys/block/sdX/device/delete (replacing sdX with the actual disk device), but some (not all) versions of the eject command will also be able to do the right thing.
    – telcoM
    Nov 12 '21 at 12:24
  • This might lead towards a solution for me, but really looks like a workaround for the problem. When I reboot, the /dev/mappers directory reflects the volumes that are present, depending on whether I have inserted one or the other disk. Hotplug let's me swap the drives without a reboot on a hardware level. Somewhere in between things are not happening automatically, so I am asking for a call that basically does what's otherwise happening at boot time. If I deactivate the a volume, it will still be deactivated, when inserting next time or booting.
    – Ingo
    Nov 13 '21 at 16:05
  • So, how would that help software to identify, which drive is inserted or not?
    – Ingo
    Nov 13 '21 at 16:08
  • @telcoM and how would a script know, what sdX is? I mean, how would it identify, which block device represents the inserted drive. Under /dev/mapper, I have the names of the volume group and logical volume, which I can define. sdX gets assigned automatically to anything 'random'.
    – Ingo
    Nov 13 '21 at 16:11
  • Activation during boot is done by systemd (dracut for root filesystem LVs), check lvm2-activation-generator. Everything is torn down during reboot simply because device mapper tables are in memory so they are removed even if not removed correctly. You need to deactivate the LVs (are remove the mappings directly with dmsetup), not doing that is equivalent of removing the disk without unmounting the filesystem first. Nov 13 '21 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.