JRehkemper.de

Move Logical Volume to different Drive with LVM

LVM provides you with two options to move logical volumes to different physical volumes or disks.
But before you move any data around, you should always make sure, that you have a functional backup.

Move to a bigger Disk

In the first example I want to move a logical volume to a larger disk. The Logical Volume is called vg_data-lv_data. The old disk is /dev/sdc and the new disk dev/sdd.

First you need to create a new physical volume on the new disk.

$ sudo pvcreate /dev/sdd

After that we need to add sdd to the Volume Group vg_data.

$ sudo vgextend vg_data /dev/sdd

Now comes the scary part: Moving the data from one disk in the volume group to another one.

$ sudo pvmove /dev/sdc /dev/sdd

Next we can remove the old disk. This will remove it from the Volume Group and also remove the marking as a Physical Volume.

$ sudo pvremove /dev/sdc

Now our logical volume is moved to the new and larger disk. But to make use of the additional space, we need to extend the logical volume.

$ sudo lvextend -l +100%FREE /dev/mapp/vg_data-lv_data

This will use the whole disk for that logical volume. If you want to extend it by a fixed amount use -L instead of -l.

Finally we can remove the old disk.

Move Swap Partition

In this example we want move the swap partitions from /dev/sdc to /dev/sdd. This is somewhat different from normal partitions, because swap is a special filesystem.

First we need to create a physical volume on our new disk.

$ sudo pvcreate /dev/sdd

Next we add it to the same volume group.

$ sudo vgextend vg_swap /dev/sdd

Now we need to disable the swap. To do that we need to first move all data from the swap back into the RAM. This might take a while depending on your disk and swap-usage. Make sure there is enough memory available.

$ sudo swapoff -a

To prevent this swap partition to be used again on the next boot, we remove it from the /etc/fstab.

#/dev/mapper/vg_swap-lv_swap none swap default 0 0
$ sudo mount -a

Use the free command to check, that there is now swap registered.

$ free -h
             total       used        free      shared  buff/cache   available
Mem:         46Gi        32Gi        2.4Gi     197Mi   13Gi         14Gi
Swap:        0           0           0

Normally you could shrink the logical volume like so.

$ sudo lvreduce -L 15G /dev/vg_swap/lv_swap --resizefs

The --resizefs flag would shrink the filesystem as well. This is quite dangerous and may cause dataloss depending on the filesystem.

But this does not work for swap. The only option for swap is to delete the logical volume and recreate it. That is not a problem, since swap has no persistent data and we moved all the data back into RAM anyway.

So lets delete the logical volume.

$ sudo lvremove /dev/vg_swap/lv_swap

Now we will remove the old disk from the volume group. That way we can recreate the new logical volume on all the free space.

$ sudo vgreduce vg_swap /dev/sdc

Now we create the new logical volume in the size of the new disk /dev/sdd.

$ sudo lvcreate -l +100%FREE -n lv_swap vg_swap

To use our logical volume we have to create a swap-filesystem on it.

$ sudo mkswap /dev/vg_swap/lv_swap

To use our swap we have to add it back into the /etc/fstab

/dev/mapper/vg_swap-lv_swap none swap default 0 0

Then mount it

$ sudo mount -a

and activate the swap.

$ sudo swapon -a

If we now check with free -h, we will see our swap used.

$ free -h
             total        used        free      shared  buff/cache   available
Mem:         46Gi         32Gi        2.4Gi     197Mi   13Gi         14Gi
Swap:        16G          1G          15G
profile picture of the author

Jannik Rehkemper

I'm an professional Linux Administrator and Hobby Programmer. My training as an IT-Professional started in 2019 and ended in 2022. Since 2023 I'm working as an Linux Administrator.