Introduction to lvrename command
lvrename command renames an existing logical volume in the volume group. It is available in the lvm2
package in the Linux system.
Are you new to LVM and still learning how it works?
We have written detailed articles covering different areas of managing logical volumes, which you can follow using the below links:
Manage Logical Volume in Linux - One STOP Solution
Understand LVM Architecture
Create LVM during installation RHEL/CentOS 7/8
How to use LVM Snapshot for Backup and Restore
Create Mirrored Logical Volume
Create Striped Logical Volume
Lab Environment
I have a Virtual Box with me installed with CentOS 8 which I will use for demonstrating the steps but the same steps should work on any other distribution such as Ubuntu, Debian, Rocky Linux, Alma Linux, Fedora, SuSE etc
I have following Physical Volumes (PVs)
[root@centos8-2 ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 rhel lvm2 a-- <9.52g 0 /dev/sdb rhel lvm2 a-- <10.00g <9.00g
With single Volume Group (VG)
[root@centos8-2 ~]# vgs VG #PV #LV #SN Attr VSize VFree rhel 2 3 0 wz--n- <19.52g <9.00g
and 3 Logical Volumes
[root@centos8-2 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert data rhel -wi-ao---- 1.00g root rhel -wi-ao---- 9.04g swap rhel -wi-a----- 488.00m
We will be renaming root and data logical volume with all the dos and donts to make sure you do not end up with a broken system.
This is the lvm2 rpm version installed on my server:
[root@centos8-2 ~]# rpm -q lvm2 lvm2-2.03.11-5.el8.x86_64
Some FAQs before we start
Do I need to go to single user or rescue mode to rename root logical volume or Volume Group?
NO, you can rename your root logical volume or volume group without having to worry about unmounting a live file system. So you can perform all the steps on your existing run level without going to single user or rescue mode.
Do I need to unmount or reboot the server to rename a logical volume?
The logical volume rename happens on the fly. Although the partition will still show as mounted with old logical volume name unless it is re-mounted. Now it is possible to re-mount a non-root logical volume runtime but for root logical volume we will have to perform a reboot.
Will there be any data loss?
If you follow the steps as I am going to explain then you don't have to worry about any data loss. But it is still strictly recommended to backup your file system before performing any such operation.
Can I rename logical volume and volume group at the same time?
Yes both logical volume and volume group can be renamed at the same time, after which you can reboot your server to activate the changes. Although it would be recommended to do step by step to minimise data loss risk.
Steps to rename non-root logical volume
First we will learn the steps to rename a non-root logical volume.
Step-1: Get UUID of logical volumes
First let us get the UUID of all the partitions and logical volumes available on our system:
[root@centos8-2 ~]# blkid /dev/sda1: UUID="41e11fe0-1fe5-4f1b-aeb4-f0036fb8a1f9" BLOCK_SIZE="1024" TYPE="ext4" PARTUUID="7430d82b-01" /dev/sda2: UUID="0OK9XM-YwkG-4OMK-H8v0-5fj8-McJV-t0bT3O" TYPE="LVM2_member" PARTUUID="7430d82b-02" /dev/sdb: UUID="l82r8n-moqe-eODW-DCCk-nNKg-ibjg-NGhD4a" TYPE="LVM2_member" /dev/mapper/rhel-root: UUID="b9c1bb38-fabb-464b-ac02-92e382549244" BLOCK_SIZE="4096" TYPE="ext4" /dev/mapper/rhel-swap: UUID="d9d43573-6471-4132-b936-03e9a06254a4" TYPE="swap" /dev/mapper/rhel-data: UUID="a0494213-5791-4bb9-b9e9-4d1f69ca4424" BLOCK_SIZE="4096" TYPE="ext4"
Step-2: Rename non-root logical volume
Here you can check the UUID of rhel-data
logical volume. Next, just go ahead and rename your chosen LV using lvrename
command:
[root@centos8-2 ~]# lvrename /dev/rhel/data /dev/rhel/new-data
Renamed "data" to "new-data" in volume group "rhel"
Verify the lvs
command output:
[root@centos8-2 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
new-data rhel -wi-ao---- 1.00g
root rhel -wi-ao---- 9.04g
swap rhel -wi-a----- 488.00m
Step-3: Update /etc/fstab
If you are using absolute name of your logical volume in /etc/fstab
then you will have to update the same to use new name of the volume. But since the name is not static and can be changed frequently so it is a better idea to use UUID of the logical volume to mount the partition.
So, let's get the UUID of our renamed logical volume:
[root@centos8-2 ~]# blkid /dev/mapper/rhel-new--data
/dev/mapper/rhel-new--data: UUID="a0494213-5791-4bb9-b9e9-4d1f69ca4424" BLOCK_SIZE="4096" TYPE="ext4"
As you can see, the UUID of the logical volume has not changed even after rename operation. So we will go ahead and add the same in our /etc/fstab file:
UUID=a0494213-5791-4bb9-b9e9-4d1f69ca4424 /data ext4 defaults 1 1
and I have removed below entry from the old logical volume:
/dev/mapper/rhel-data /data ext4 defaults 1 1
Step-4: Refresh LVM attribute
It is important to update the attribute of the renamed LV:
[root@centos8-2 ~]# lvchange /dev/rhel/new-data --refresh
We are all done. If required you can perform a reboot and verify if your server is rebooting properly.
Post reboot, I can see my new-data logical volume is successfully mounted:
[root@centos8-2 ~]# df -h /data/
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-new--data 976M 2.6M 907M 1% /data
Steps to rename root logical volume
This can be tricky so please follow all the steps properly.
Step-1: Get the UUID of root logical volume
First let us get the UUID of our root logical volume which we can use it later in our fstab
file:
[root@centos8-2 ~]# blkid /dev/mapper/rhel-root
/dev/mapper/rhel-root: UUID="b9c1bb38-fabb-464b-ac02-92e382549244" BLOCK_SIZE="4096" TYPE="ext4"
Step-2: Rename root logical volume
Next go ahead and rename the root logical volume using lvrename
command:
[root@centos8-2 ~]# lvrename /dev/rhel/root /dev/rhel/new-root
Renamed "root" to "new-root" in volume group "rhel"
Verify the available logical volumes to get the LV name for root.
As you can see, the logical volume has been successfully renamed:
[root@centos8-2 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
new-data rhel -wi-ao---- 1.00g
new-root rhel -wi-ao---- 9.04g
swap rhel -wi-a----- 488.00m
Although the mounted partition still uses old LV name:
[root@centos8-2 ~]# df -h /
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 8.9G 6.7G 1.7G 80% /
Step-3: Update /etc/fstab
As explained previously, it is better to have UUID instead of logical volume name so that even if the name of the volume changes, it will not impact the fstab
.
Verify the UUID of root logical volume to make sure it has not changed:
[root@centos8-2 ~]# blkid /dev/mapper/rhel-new--root
/dev/mapper/rhel-new--root: UUID="b9c1bb38-fabb-464b-ac02-92e382549244" BLOCK_SIZE="4096" TYPE="ext4"
Now I will go ahead and replace the root logical volume name with the UUID in /etc/fstab
:
Change
/dev/mapper/rhel-root / ext4 defaults 1 1
To
UUID=b9c1bb38-fabb-464b-ac02-92e382549244 / ext4 defaults 1 1
Alternatively you can also specify the new logical volume name i.e. /dev/mapper/rhel-new--root
instead of /dev/mapper/rhel-root
.
Step-4: Refresh LVM attribute
Lets reload the metadata of our root logical volume:
[root@centos8-2 ~]# lvchange /dev/rhel/new-root --refresh
Step-5: Re-generate initramfs
We must re-generate the initramfs image. Take backup of existing initramfs file:
[root@centos8-2 ~]# cp /boot/initramfs-$(uname -r).img /boot/initramfs-$(uname -r).img.$(date +%m-%d-%H%M%S).bak
[root@centos8-2 ~]# ls -l /boot/initramfs-*
-rw-------. 1 root root 65694077 Sep 18 10:13 /boot/initramfs-0-rescue-a9af8aaa2cdc4396bd1b74f5466e3094.img
-rw-------. 1 root root 28001356 Sep 18 10:53 /boot/initramfs-4.18.0-305.19.1.el8_4.x86_64.img
-rw------- 1 root root 28001356 Feb 11 22:44 /boot/initramfs-4.18.0-305.19.1.el8_4.x86_64.img.02-11-224404.bak
-rw-------. 1 root root 27874144 Sep 18 10:53 /boot/initramfs-4.18.0-80.el8.x86_64.img
Rebuild the initramfs:
[root@centos8-2 ~]# dracut -v -f /boot/initramfs-$(uname -r).img $(uname -r)
Step-6: Update GRUB2 using grubby
We will use grubby tool to update the GRUB2 configuration as with grub2-mkconfig I was getting below error while updating the GRUB:
[root@centos8-2 ~]# grub2-mkconfig -o /boot/grub2/grub.cfg
/usr/sbin/grub2-probe: error: failed to get canonical path of `/dev/mapper/rhel-root'.
Now to fix this, I had to switch to rescue mode and then rebuild the GRUB. So, I chose to use grubby instead, to update the configuration runtime. But if you are OK to go to rescue mode then you can update GRUB_CMDLINE_LINUX
inside /etc/default/grub
with the new root logical volume name and execute grub2-mkconfig command to rebuild the GRUB2.
HINT: For legacy BIOS use grub2-mkconfig -o /boot/grub2/grub.cfg
and for UEFI BIOS use grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg
List the existing kernel options loaded on the server:
[root@centos8-2 ~]# grub2-editenv - list | grep kernelopts kernelopts=root=/dev/mapper/rhel-root ro resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet
Now copy the kernelopts
content from above and update the root logical volume name where ever required and set the new kernelopts
as shown below:
[root@centos8-2 ~]# grub2-editenv - set "kernelopts=root=/dev/mapper/rhel-new--root ro resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/new-root rd.lvm.lv=rhel/swap rhgb quiet"
Verify the updated entries:
[root@centos8-2 ~]# grub2-editenv - list | grep kernelopts kernelopts=root=/dev/mapper/rhel-new--root ro resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/new-root rd.lvm.lv=rhel/swap rhgb quiet
Step-7: Update Kernel Arguments
List the default kernel and it's arguments:
[root@centos8-2 ~]# grubby --info DEFAULT index=0 kernel="/boot/vmlinuz-4.18.0-305.19.1.el8_4.x86_64" args="ro resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/new-root rd.lvm.lv=rhel/swap rhgb quiet $tuned_params" root="/dev/mapper/rhel-new--root" initrd="/boot/initramfs-4.18.0-305.19.1.el8_4.x86_64.img $tuned_initrd" title="CentOS Linux (4.18.0-305.19.1.el8_4.x86_64) 8" id="a9af8aaa2cdc4396bd1b74f5466e3094-4.18.0-305.19.1.el8_4.x86_64"
Here copy the args
section from above output and apply to all the available kernel:
[root@centos8-2 ~]# grubby --args "ro resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/new-root rd.lvm.lv=rhel/swap rhgb quiet $tuned_params" --update-kernel ALL
Verify the updated root logical volume using:
# grubby --info ALL
Step-8: Verify the changes
Go ahead and reboot your server with fingers crossed. If all is well, then your server should boot up successfully. Post reboot, as you can see my root partition is now mounted with new root logical volume name:
[root@centos8-2 ~]# df -h /
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-new--root 8.9G 6.7G 1.7G 80% /
Bonus Tip: Rename Volume Group
Now this article is not originally about renaming volume group but the steps are not very much different.
You have to follow the steps of renaming root logical volume from this tutorial. The brief steps to be followed are:
- Instead of lvrename, you have to use
vgrename oldvg newvg
- Update fstab for all the logical volumes using the respective VG or use UUID for all logical volumes
- Activate VG using
vchange -ay
- Take backup and rebuild initramfs
- Update and regenerate grub2
- Reboot
Summary
In this tutorial we covered step by step instructions to rename logical volume (root and non-root). We also covered the steps to be performed post the lvrename operation to make sure the system doesn't break. I have observed that in non-rhel servers, the grub2-mkconfig command works flawlessly while with RHEL and CentOS servers I was getting error. So I have used grubby command to update the kernel.
This goes without saying, please take a backup before performing any such activity. Let me know how it went for you or if you have any questions or feedback using the comment section below.
What's Next
10+ lvchange command examples in Linux [Cheat Sheet]
Further Reading
The information you provide has been great in helping me reconfigure my systems lvm configuration. I have one question though,
can I perform all three procedures rename non-root lv, root lv, and the lv group and then reboot once or do they each have to be performed separately?
This is something you should test once in lab before attempting in production.