How do you rename the volume group that contains the root volume in LVM?
I want to rename the volume group that my root volume is on. How can I do this?
28 Answers
NOTE: Your distro may discourage editing /boot/grub/grub.cfg. If that is the case, this script may be a bad idea. Alternately, you may just be able to run grub-mkconfig to fix that. I haven't tested on those distros, so check your situation.
First, You need to know the volume group name may have a dash in it. If it does, than any use of the /dev/mapper/ reference will need to have two dashes. In 16.04, it defaults to having a "-vg" appended to the name so this should be assumed.
Second, you should know that messing this up can cause your system to be unbootable and result in having to boot from a rescue disk and fix stuff causing downtime. (aka: Don't do this in production.)
To do the actual rename use lvrename oldname newname.
After renaming you must edit both /etc/fstab and /boot/grub/grub.cfg to update the use of the name for any reference to your root and probably also your swap locations.
Additionally, you need to run this command to update the initramfs for all the kernels.
update-initramfs -c -k allI use the following script to handle this when deploying a new template. Again, don't do this in production unless you have a high tolerance for downtime.
#!/bin/bash
# Must be run with root permissions
# sudo will be sufficient
if [ "$(id -u)" != "0" ]; then echo "This script must be run as root" 1>&2 exit 1
fi
#Ask for new hostname $newhost
read -p "Enter new hostname: "
newhostname=$REPLY
oldhostname=$(cat /etc/hostname)
echo "Changing LVM names"
# ${var//-} syntax removes all dashes from the name simplifying the
# requirement to use a double-dash in some places to escape the dash
newvg=${newhostname//-}
# Find the volume group that root is in
vg=`lvdisplay -C|awk '$1=="root" {print $2}'`
if [[ ${vg} == *"-"* ]]; then #has dashes in current name vgrename ${vg} ${newhostname//-} vg=`echo $vg|sed "s/-/--/g"` sed -i "s/${vg}/${newvg}/g" /etc/fstab sed -i "s/${vg}/${newvg}/g" /boot/grub/grub.cfg else #no dashes in current name vgrename ${vg} ${newvg} sed -i "s/${vg}/${newvg}/g" /etc/fstab sed -i "s/${vg}/${newvg}/g" /boot/grub/grub.cfg
fi
update-initramfs -c -k allIf you have any improvements of this script, please share. I'm always looking for ways to improve and account for various edge cases.
8Here is a revised version, fix on string replacement of vg and also print updated files.
#!/bin/bash
# Must be run with root permissions
# sudo will be sufficient
if [ "$(id -u)" -ne 0 ]; then echo "This script must be run as root" 1>&2 exit 1
fi
# Ask for new hostname $newhost
read newhostname -p "Enter new hostname: "
#oldhostname=$(cat /etc/hostname)
echo "Changing LVM names"
# ${var//-} syntax removes all dashes from the name simplifying the
# requirement to use a double-dash in some places to escape the dash
newvg=${newhostname//-}
# Find the volume group that root is in
vg=$(lvdisplay -C | awk '$1=="root" {print $2}')
echo "old vg name: " $vg
echo "new vg name: " $newvg
if [[ ${vg} == *"-"* ]]; then # has dashes in current name vgrename ${vg} ${newhostname//-} vg=${vg//-/--} sed -i "s/${vg}/${newvg}/g" /etc/fstab sed -i "s/${vg}/${newvg}/g" /boot/grub/grub.cfg sed -i "s/${vg}/${newvg}/g" /etc/initramfs-tools/conf.d/resume
else # no dashes in current name vgrename ${vg} ${newvg} sed -i "s/${vg}/${newvg}/g" /etc/fstab sed -i "s/${vg}/${newvg}/g" /boot/grub/grub.cfg sed -i "s/${vg}/${newvg}/g" /etc/initramfs-tools/conf.d/resume
fi
#check files
echo fstab update:
grep ${newvg} /etc/fstab
echo grub.cfg update:
grep ${newvg} /boot/grub/grub.cfg
echo resume update:
grep ${newvg} /etc/initramfs-tools/conf.d/resume
update-initramfs -c -k allWhen using this script, the machine may not shutdown correctly as it tries to "stop" the mappings. Depending on your config this may even seem as the machine is in a boot state while in reality it isn't even shutdown first.
It is helpful to remove "quiet splash" from GRUB_CMDLINE_LINUX_DEFAULT as you then see the messages.
I did a little modification on the script to also change hostname.
#!/bin/bash
# Must be run with root permissions
# sudo will be sufficient
if [ "$(id -u)" != "0" ]; then echo "This script must be run as root" 1>&2 exit 1
fi
#Ask for new hostname $newhost
read -p "Enter new hostname: "
newhostname=$REPLY
oldhostname=`cat /etc/hostname`
# ${var//-} syntax removes all dashes from the name simplifying the
# requirement to use a double-dash in some places to escape the dash
newvg=${newhostname//-}
# Find the volume group that root is in
vg=`lvdisplay -C|awk '$1=="root" {print $2}'`
echo
echo "old hostname : " $oldhostname
echo "old vg name : " $vg
echo "new hostname / vg name: " $newvg
echo
echo "Changing LVM names..."
vgrename ${vg} ${newvg}
if [[ ${vg} == *"-"* ]]; then #has dashes in current name vg=`echo $vg|sed "s/-/--/g"`
fi
sed -i "s/${vg}/${newvg}/g" /etc/fstab
sed -i "s/${vg}/${newvg}/g" /boot/grub/grub.cfg
sed -i "s/${vg}/${newvg}/g" /etc/initramfs-tools/conf.d/resume
echo
echo "Changing Hostname..."
sed -i "s/${oldhostname}/${newvg}/g" /etc/hostname
sed -i "s/${oldhostname}/${newvg}/g" /etc/hosts
#check files
echo
echo fstab update:
grep ${newvg} /etc/fstab
echo grub.cfg update:
grep ${newvg} /boot/grub/grub.cfg
echo resume update:
grep ${newvg} /etc/initramfs-tools/conf.d/resume
echo hostname update:
grep ${newvg} /etc/hostname
echo hosts update:
grep ${newvg} /etc/hosts
update-initramfs -c -k all File /boot/grub/grub.cfg shouldn't be edited manually.
There is file header below:
" DO NOT EDIT THIS FILE It is automatically generated by grub-mkconfig using templates from /etc/grub.d and settings from /etc/default/grub
BEGIN /etc/grub.d/00_header
" 1 There is an alternative approach that does not require worrying about changing (and messing up) the boot process.
When renaming a volume group, the UUID of logical volumes does not change.
If you first specify all mounts in /etc/fstab with their UUIDs (like /dev/disk/by-uuid/UUID), you can safely rename a volume group.
To get the UUIDs for disks you can inspect the output of lsblk -o name,uuid,mountpoint,size.
After renaming you must edit both /etc/fstab and /boot/grub/grub.cfg to update the use of the name for any reference to your root and probably also your swap locations. /etc/initramfs-tools/conf.d/resume is needed, too.
So, add this code:
sed -i "s/${vg}/${newvg}/g" /etc/initramfs-tools/conf.d/resume 1 The boot menu also needed editing on Ubuntu 18 (and probably others). So - simplified for only changing a vg name and preserving the use of a dash in the name:
#!/bin/bash
oldvg="ubu16svr-vg"
oldvgdash="ubu16svr--vg"
newvg="ubusvr-vg"
newvgdash="ubusvr--vg"
if [ "$(id -u)" != "0" ]; then echo "This script must be run as root" 1>&2 exit 1
fi
vgrename ${oldvg} ${newvg}
sed -i "s/${oldvg}/${newvg}/g" /etc/fstab
sed -i "s/${oldvgdash}/${newvgdash}/g" /etc/fstab
sed -i "s/${oldvg}/${newvg}/g" /boot/grub/grub.cfg
sed -i "s/${oldvgdash}/${newvgdash}/g" /boot/grub/grub.cfg
sed -i "s/${oldvg}/${newvg}/g" /boot/grub/menu.lst
sed -i "s/${oldvgdash}/${newvgdash}/g" /boot/grub/menu.lst
sed -i "s/${oldvg}/${newvg}/g" /etc/initramfs-tools/conf.d/resume
sed -i "s/${oldvgdash}/${newvgdash}/g" /etc/initramfs-tools/conf.d/resume
update-initramfs -c -k all I wanted to do exactly this and after googling around and reading many blogs etc, I came to the conclusion there must be a better way than editing files that should be managed by maintainer scripts, (grub.cfg). Certainly this is the case when considering a single desktop machine, rather than a whole bunch of VMs. Therefore I studied the matter and found that the process is potentially painless. However having said this I think care should always be taken to avoid any boot-time screws ups. What surprised me was the references constantly made suggesting that it is necessary to update the initramfs. I found this step is not necessary. So read fully and proceed with caution:
Assuming the system is already booted you should see that in grub.cfg there are 2 important entries.
set root='lvmid/BKJJuy-Zs3w-O9Hu-Onbc-k4Zf-uxzZ-X9cdws/b7S0Z8-R6KC-ukz5-J9Ov-Jc2n-THMZ-tgFwBM'
linux /boot/vmlinuz-5.4.0-96-generic root=/dev/mapper/<old-VG-name>-root roThe 'set root' line tells GRUB2 where it can find the root device. This is critical as otherwise Grub can not load the initramfs needed to actually activate the LVM VG on which '/' resides. In this case the root device is identified using UUID and consists of the VG-UUID + LV-UUID. You may well ask, well if the LVM module is not loaded how can Grub read from this device to find the initramfs; rather 'chicken and egg'. Actually the answer is quite simple; GRUB2 contains its own code base that can understand the LVM metadata found on the raw disk. Therefore it can use the LVM UUIDs to locate the root filesystem and therefore find, load and decompress /boot/initrd.img. For a better understanding of this bootstrap process see:
Once the initramfs is mounted the init script from Ubuntu, (which is embedded in the initramfs), uses the information found on the virtual filesystem /proc/cmdline which was already generated by grub by parsing the 'linux' line from grub.cfg. This is used by initramfs 'init' procedure to map and activate the correct LVM logical volume used as '/'. Therefore AFAIK there are no references to the LVM VG name embedded into the initramfs and an update of this is not necessary.
Therefore to make the change to a LV mounted as root, first modify the name of the VG:
vgchange -v <old-VG-name> <new-VG-name>Then modify /etc/fstab to reflect this change in the mount definition of the root device:
/dev/mapper/<new-VG-name>-root / ext4 errors=remount-ro 0 1If you have other entries in fstab which also reference the old VG name, such as swap etc, these too must be modified to ensure systemd generator creates the correct .mount units at boot time.
At this stage I would simply reboot, having made a careful note of the new VG name you have used. You may find that the system hangs on the way down, due I believe to the old systemd generator unit which still contain the old VG name. It gets a bit confused. Still don't worry its a one time issue.
Upon reboot enter the GRUB2 menu, if you have your system configured to hide the menu, you will need to ensure you have requested the menu option, as I remember by holding 'shift' at boot time. However first check this works before you make any changes as some BIOS issues can make accessing the grub menu problematic and its critical you can get to the menu. Assuming that you have use 'e' to enter edit mode, then modify the entry on the 'linux' line which defines 'root=' and change this to match the new VG name. ctrl-x after the changes and boot the system.
This should bring up the system fully and normally.
Finally we must now update the menu entries in grub.cfg. This can be done using:
update-grubHope that works for others. As for myself, I prefer this to manually editing the grub.cfg file.