Want to learn how to move LVM volume group to another machine in Linux? This tutorial covers everything you need to know.
Linux LVM (Logical Volume Manager) Group is a way of managing disk space on Linux systems. It allows you to combine multiple physical disks and create logical volumes (LVs). These LVs can span across multiple physical volumes (PVs).
It is possible to resize logical volumes. You can also move and mirror them without interrupting background apps or data.
In this guide, we will use the vgimport
and vgexport
commands to move these disks. But before that, you should go through some useful processes to move easier. These include:
- Backing up all your important data
- Set up the destination machine
- Scan and list all the necessary commands. One small mistake can do a lot of damage to your disk
- Install the required packages in the current machine
You can use LVM snapshots for backing up data.
How to Move LVM Volume Group to Another Machine in Linux
If you’ve done all the previously mentioned steps, then let’s proceed to this guide so you can start moving your LVM group to another device.
We will use Oracle VirtualBox to demonstrate the process. Also, we will be using Debian 11. And we would like to mention that some steps may differ in your case. Especially if you’re using physical disks—so remember that.
Get Logical Volume Information
First, you need to install the lvm2 package. To do that, use this command:
$ sudo apt-get install lvm2
Output:
Now you need to check the status of your physical volume, volume group, and logical volume. For that, you use the below command:
$ sudo pvscan
Output:
PV /dev/sda2 VG centos lvm2 [<10.00 GiB / 0 free]
Total: 1 [<10.00 GiB] / in use: 1 [<10.00 GiB] / in no VG: 0 [0 ]
This will list all your physical volumes. You get to see all the initialized physical volumes, volume groups, and sizes of each group. If you would like to know more details about the volume groups, try running this command:
$ sudo vgs
Output:
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <10.00g 0
Each column will have a value when you run the command.
If you’d like to know about the logical volume under your volume group, run this command:
$ sudo lvs
Output:
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <9.50g
swap centos -wi-ao---- 512.00m
To find out the mount point of your logical volume, use this command:
$ lsblk
Output:
Another way to check is to use the df
command and add the volume group directory as an argument, like this:
$ df -h /path/to/volume/group
Output:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 9.5G 2.6G 6.9G 28% /
In the last column, you will see the mount point.
Once you know the mount point, you can check the logical volume in more detail to see what’s in it. To do so, run this command:
$ ls -l /logical/group/mount/point
Export LVM Volume Group
Now that you’ve checked everything about your LVs, it’s time to export them. To start, you need to unmount your file system. Use this command to unmount:
$ umount [mount point]
Next, you need to make sure that the part you’re unmounting is currently not busy (or not in use). Or else, a “target is busy” error will appear on your screen.
To see your mount status, use this command:
$ df -h
You can also run this command to achieve the same results as the previous one:
$ mount
After that, use this command:
$ sudo lvchange -an [LV path]
The previous command will deactivate the logical volumes on the specified directory. Now you do the same thing for the volume group as well. Run this command to do so:
$ sudo vgchange -an [LV path]
Output:
0 logical volume(s) in volume group "vg_name" now active
The last you need to do is to export the volume group. you can export it by executing this command:
$ sudo vgexport [vg_name]
Output:
Volume group "vg_name" successfully exported
Now if you run pvscan
, you should see an output like this:
PV /path/to/pv is in exported VG vg_name [Full size / Available size]
Once you’re done, you may shut down your system so that you can unplug the disk(s) that contain the logical volume group. After that, unplug the correct disks.
Import LVM Volume Group
Before anything else, plug in the unplugged disks to your destination machine. Now run this command in your new system:
$ sudo pvscan
Output:
pvscan -- reading all physical volumes (this may take a while...)
pvscan -- inactive PV "/path/to/pv" is in EXPORTED VG "vg_name" [Full size / Available size]
…
You can try to run this command to check the attributes of your physical volumes:
$ sudo pvs
Output:
PV VG Fmt Attr PSize PFree
/path/to/pv vg_name lvm2 ax- <total size <available size
The ax
attribute indicates that this is an exported volume group.
Now you use the vgimport command to import the volume group. Run the command like this:
$ sudo vgimport [VG name]
Output:
Volume group "vg_name" successfully imported
To double-check that it worked, use this command:
$ sudo vgs
Output:
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <10.00g 0
You can also use this alternative command to confirm if it worked:
$ sudo vgscan
Output:
Found volume group "vg_name" using metadata type lvm2
The next step is to activate this volume group. To do so, run the command below:
$ sudo vgchange -ay [VG name]
Output:
1 logical volume(s) in volume group "vg_name" now active
Now mount this volume group to the new file system using these commands:
$ sudo mkdir /new/directory/for/lvm
$ sudo mount /path/to/pv /new/directory/for/lvm
Once again, if you’d like to check out the contents in the volume, run this command:
$ ls -l /new/directory/for/lvm
This lets you see if you imported all the existing files to the new system.
Final Thoughts
And that marks the end of this article. Here, we ran over the process of moving the LVM volume group to another machine in Linux. We’ve covered how to export it from the current system, and how to import it to the new system using the vgexport and vgimport commands.
It may feel a bit complicated at first but if you followed everything we mentioned and ran all the commands properly, you should get this done in no time.
If this guide helped you, please share it.