Replace Disk In LVM On Ubuntu

Overview

LVM makes adding and removing physical disk very easy as its an additional layer of abstraction. I will go through the steps need to replace an existing disk of same or larger size.

Warning! Always backup prior to doing anything

Setup

This method is doing online as in it is done whilst the system is up and running. I would recommend doing this during quiet or non busy time as data on the disks will be shifted about. To do this the system needs to have the old and new disks installed in the system and visible. In this example I will use /dev/sda as the old drive and /dev/sdc as the new drive.

There are few details we need to know about the volume itself in order to add and remove the disks.

sudo pvdisplay
The above will give you the physical disks in the volume pool. Use this to make sure the correct disk is chosen to be replaced. It will also tell you the VG Name which is the volume group name. If nothing comes up you may need to do a scan first:
sudo pvscan

sudo lvdisplay
Displays the logical volume details. Note down the LV Path.

Adding Disk To Group

Format the new drive to the same file format as the existing volume. I use Gparted for this.
Gparted Format

Create a physical LVM disk:
sudo pvcreate /dev/sdc1

Add the new disk to the volume group:
sudo vgextend myvg1 /dev/sdc1
where myvg1 should be replaced with the volume name.

If you’re just adding a new disk to for more storage space then skip to Resize Volume

Replacing Drive

Move the data from 1 disk to another:
sudo pvmove /dev/sda1 /dev/sdc1
This will take a while depending on the size and amount data data used.

Make the volume group smaller by preparing the volume to remove the disk from the group:
sudo vgreduce myvg1 /dev/sda1

Remove the disk from the group:
sudo pvremove /dev/sda1

Resize Volume

The new disk has been added to the group but the file system is still the same size. Run:
sudo vgdisplay
and make a note of the Free PE / Size. This is the amount of free physical disks that’s not been allocated to anything.

sudo lvextend -l +238465 /dev/myvg1/spare
where 238465 is the first value in Free PE / Size and /dev/myvg1/spare is the LV Path.

Run
sudo vgdisplay
again to check if there is any free space left.

Resize the partition to use the newly available space in the volume:
sudo resize2fs /dev/myvg1/spare

Summary

Logical Volume Management makes it very simple to add and remove disks and resizing them too. It does add overhead but another advantage is the ability to stripe the writes i.e splitting the writes over 2 disk simultaneously. That’s another topic for another time.

Replace one of the physical volumes in an LVM volume group

Adding Disks With LVM

About Danny

I.T software professional always studying and applying the knowledge gained and one way of doing this is to blog. Danny also has participates in a part time project called Energy@Home [http://code.google.com/p/energyathome/] for monitoring energy usage on a premise. Dedicated to I.T since studying pure Information Technology since the age of 16, Danny Tsang working in the field that he has aimed for since leaving school. View all posts by Danny → This entry was posted in Linux and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *.

All comments must go through an approval and anti-spam process before appearing on the website. Please be patience and do not re-submit your comment if it does not appear.

This site uses Akismet to reduce spam. Learn how your comment data is processed.