The Problem
On CentOS/RHEL 6.x with Logical Volume Manger, all the LVM commands return locking error immediately after boot up as shown below:
# lvdisplay Failed to create directory /var/lock/lvm File-based locking initilisation failed.
The Solution
Above message is getting logged due to a check added in lvm code to verify if the path mentioned for locking_dir option in /etc/lvm/lvm.conf file is available or not and then print an error if the path used for locking_dir option is not available. This error is typically observed when /var is created as a separate filesystem. In most cases this happens if the /var/ filesystem is mounted as read-only.
$ grep -i "locking_dir" etc/lvm/lvm.conf # Configuration option global/locking_dir. locking_dir = "/var/lock/lvm"
Workaround:
1. Configure the locking_dir option in /etc/lvm/lvm.conf to a path in root (/) filesystem itself, as shown below:
# vi /etc/lvm/lvm.conf locking_dir = "/run/lock"
2. Make sure that above directory is present on server, or you could create the same manually using following command:
# mkdir -p /run/lock
3. Run the lvm commands by defining temporary locking directory
# lvdisplay --config 'global { locking_dir = "/run/lock" } ' # pvdisplay --config 'global { locking_dir = "/run/lock" } ' # vgdisplay --config 'global { locking_dir = "/run/lock" } '
Permanent Solution:
Mount the /var filesystem with rw option or if the filesystem is read-only due to filesystem corruption, run fsck against the unmounted /var partition to fix it but ensure you have a valid backup before running fsck.