pvcreate fails with "Can't open <device> exclusively. Mounted filesystem?"

Solution Verified - Updated -

Environment

  • Red Hat Enterprise Linux (RHEL) 4, 5, 6, 7
  • LVM2
    • Creating a physical volume with pvcreate

Issue

  • pvcreate throws an error:

    # pvcreate /dev/mapper/mpath1
    Can't open /dev/mapper/mpath1 exclusively.  Mounted  filesystem?
    
  • For vgcreate also below error message is displayed:

    # vgcreate vg1 /dev/mapper/mpath1
    Can't open /dev/mapper/mpath1 exclusively.  Mounted filesystem?
    Unable to add physical volume '/dev/mapper/mpath1' to volume group 'vg1'.
    

Resolution

  • There are various causes of this error, so you may need to determine which is causing the error for you so you can rectify it and pvcreate will succeed. Some possible causes include:

    • Ensure that the device is not mounted.
    • If running pvcreate on the whole disk, remove any partitions on that disk or create LVM PV on a partition instead.
    • Make sure lvm filter is accepting the device.
    • If the device is a device-mapper-multipath device, ensure there are no stale entries in dmsetup table output. Remove any stale entries with dmsetup remove dm_map_name.
    • If the device was previously an LVM Physical Volume (PV), remove any stale device-mapper table entries with dmsetup remove <map name of partition>:

      # pvcreate /dev/sdx1
      Can't open /dev/sdx1 exclusively.  Mounted filesystem?
      # dmsetup table 
      rootvg-rootlv: 0 67108864 linear 8:3 384
      appsvg-apps: 0 35233792 linear 65:112 384    <---
      $ grep "65   112" /proc/partitions 
      65   112   17677440 sdx
      # dmsetup remove appsvg-apps
      
    • NOTE: If the device is in use (ie. on a Virtual machine or mounted), services using the device must be stopped and devices unmounted prior to running this command.

Root Cause

  • LVM will only create a physical volume label on a device if it is not currently held open by anything else.
  • There are various causes of this issue. Some causes include:
    • The target device for pvcreate has a mounted filesystem.
    • The target device for pvcreate has a partition, but you are running pvcreate on the whole disk.
    • The target device for pvcreate is a multipath map and no longer has on-disk partitions but may still have partitions mapped in device-mapper tables.
    • The target device for pvcreate was previously an LVM Physical Volume (PV), and there are still entries in the device-mapper table for this device.

Diagnostic Steps

The following symptoms may be present if this issue applies to your system:

  • When trying to create a physical volume, the following error is encountered:

    # pvcreate /dev/mapper/mpath1
    Can't open /dev/mapper/mpath1 exclusively.  Mounted  filesystem?
    

Various different symptoms may be present, and with them comes different resolutions:

1. Device may have a mounted filesystem:

  • Check if the device is listed in the output of mount. If so, unmount it, and then it can be used as a physical volume (but note that pvcreate will overwrite part any file system on the device).

2. Device may have a partition, but pvcreate is run on the whole disk:

  • Check if the device has any partitions, using fdisk -l or parted.

3. Device is a multipath device without partitions, but there are still multipath devices in device-mapper table with partitions.

  • If the device is a multipath map, it may no longer have on-disk partitions but may still have partitions mapped in device-mapper. Check the output of dmsetup table to look for any references to the device's major:minor. If any exist, they can be unmapped with dmsetup remove <map name of partition>.

4. Device was previously an LVM Physical Volume (PV), but there are still device-mapper table entries for the old PV device.

  • If the device was formerly a physical volume in a volume group that was active when metadata was wiped out from the disks, there may still be logical volumes mapped in device-mapper. Check the output of dmsetup table for any references to the device's major:minor. If any exist, they can be unmapped with dmsetup remove <map name of volume>. However, if the logical volume maps are still in use (such as by a Virtual Machine or mounted file system), it must be closed (such as by stopping the VM or unmounting the file system) before it can be removed.

Helpful dmsetup commands that can help in diagnosing the problem:

dmsetup ls
dmsetup info
dmsetup info -c
dmsetup ls -tree
cat /etc/lvm/lvm.conf | grep -i filter

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments