Remove an LVM Disk

By | 2021-11-21

There may be occasions where you need to remove an LVM disk. Maybe the host is a VM and you need space on the hypervisor? Perhaps the device is a fiber channel SAN target that you no longer need? No matter the reason, it isn’t terribly difficult to remove an LVM disk.

There are three major steps:

  1. Clear enough space in the volume group.
  2. Remove the device from the volume group
  3. Remove the device from LVM.

Everything in this guide should be done as root or with a privilege escalation tool such as sudo. If you are in a hurry and know your way around a UNIX or Linux system, click here for a quick checklist.

Example Scenario

Suppose I have a host I am using to test WordPress changes for Tyler’s Guides. Let’s say the underlying database is PostgreSQL. It has two instances on separate logical volumes. I decide it is time to update the OS to a new major release. I don’t want to be without a test system if things go awry, so I opt to migrate one of them to a new host. To save disk space in my storage environment, I decide to remove one of three physical volumes.

# lsblk 
NAME             MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                8:0    0   20G  0 disk 
├─sda1             8:1    0 19.1G  0 part /
├─sda2             8:2    0    1K  0 part 
└─sda5             8:5    0  880M  0 part [SWAP]
sdb                8:16   0    1G  0 disk 
└─pgdata-wptest1 254:0    0  1.5G  0 lvm  /wptest1
sdc                8:32   0    1G  0 disk 
├─pgdata-wptest1 254:0    0  1.5G  0 lvm  /wptest1
└─pgdata-wptest2 254:1    0  1.5G  0 lvm  /wptest2
sdd                8:48   0    1G  0 disk 
└─pgdata-wptest2 254:1    0  1.5G  0 lvm  /wptest2
sr0               11:0    1 1024M  0 rom  

In the example system, LVM is managing a single volume group, pgdata. It is composed of three physical volumes. The volume group has two logical volumes, wptest1 and wptest2.

Clear Enough Space

You can do one of two things, remove logical volumes or shrink them. Due to the potential of data loss, I prefer to remove them. If you opt to shrink it, make absolutely sure you have a backup. Also ensure you reduce the size of the filesystem or whatever it is that is using that device. Be certain you don't reduce the LV more than the reduction of what is on the LV. For the example, I am going to just remove a volume.

Let's assume I have already moved the wptest2 instance to a new host. It is safe to remove. First, I need to ensure the filesystem on it isn't in use.

I unmount the filesystem and remove it from /etc/fstab:

# umount /wptest2
# grep wptest2 /etc/fstab 
/dev/mapper/pgdata-wptest2  /wptest2 ext4 defaults 1 2
# sed -i 's|/dev/mapper/pgdata-wptest2|#/dev/mapper/pgdata-wptest2|g' /etc/fstab
# grep wptest2 /etc/fstab 
#/dev/mapper/pgdata-wptest2  /wptest2 ext4 defaults 1 2

Now, I remove the logical volume:

# lvremove /dev/mapper/pgdata-wptest2
Do you really want to remove active logical volume pgdata/wptest2? [y/n]: y
  Logical volume "wptest2" successfully removed

Let's take a look at our storage configuration again:

# lsblk 
NAME             MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                8:0    0   20G  0 disk 
├─sda1             8:1    0 19.1G  0 part /
├─sda2             8:2    0    1K  0 part 
└─sda5             8:5    0  880M  0 part [SWAP]
sdb                8:16   0    1G  0 disk 
└─pgdata-wptest1 254:0    0  1.5G  0 lvm  /wptest1
sdc                8:32   0    1G  0 disk 
└─pgdata-wptest1 254:0    0  1.5G  0 lvm  /wptest1
sdd                8:48   0    1G  0 disk 
sr0               11:0    1 1024M  0 rom  

Removing sdd would be the easiest thing to do, but it would rob you of a valuable example. I will remove sdb instead. If I try to remove either, I will get an error:

# pvremove /dev/sdd
  PV /dev/sdd is used by VG pgdata so please use vgreduce first.
  (If you are certain you need pvremove, then confirm by using --force twice.)
  /dev/sdd: physical volume label not removed.
# pvremove /dev/sdb
  PV /dev/sdb is used by VG pgdata so please use vgreduce first.
  (If you are certain you need pvremove, then confirm by using --force twice.)
  /dev/sdb: physical volume label not removed.

I need to remove the device from the volume group first:

# vgreduce pgdata /dev/sdb
  Physical volume "/dev/sdb" still in us

Remember how the output of lsblk showed wptest1 is using sdb? I must migrate the extents off of the disk before I can remove it.

# pvmove /dev/sdb
  /dev/sdb: Moved: 3.92%
  /dev/sdb: Moved: 100.00%

Let's take another look at the storage devices:

# lsblk 
NAME             MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                8:0    0   20G  0 disk 
├─sda1             8:1    0 19.1G  0 part /
├─sda2             8:2    0    1K  0 part 
└─sda5             8:5    0  880M  0 part [SWAP]
sdb                8:16   0    1G  0 disk 
sdc                8:32   0    1G  0 disk 
└─pgdata-wptest1 254:0    0  1.5G  0 lvm  /wptest1
sdd                8:48   0    1G  0 disk 
└─pgdata-wptest1 254:0    0  1.5G  0 lvm  /wptest1
sr0               11:0    1 1024M  0 rom  

Now I can finally remove sdb.

Remove The Volume

Now that I have cleared enough space from the volume and moved everything off of the device I want to remove, I can remove it. First remove it from the volume group:

vgreduce pgdata /dev/sdb
  Removed "/dev/sdb" from volume group "pgdata"

Now remove it from LVM:

# pvremove /dev/sdb
  Labels on physical volume "/dev/sdb" successfully wiped.

Now you can safely remove the underlying storage device from your system.

Quick Steps

  1. Migrate or delete applicable data. Unmount filesystems, edit /etc/fstab, etc
  2. Use lvremove of lvreduce to remove or shrink LVs as requred.
  3. Move PV exents off the device. pvmove device_file
  4. Remove the PV from the VG. vgreduce VG_name device_file
  5. Remove the PV from LVM. pvremove device_file

References

See Also

Discuss