Skip to content

Instantly share code, notes, and snippets.

@islander
Created June 21, 2018 04:34
Show Gist options
  • Save islander/cfb1aa99f032842742ea7a698ca44c88 to your computer and use it in GitHub Desktop.
Save islander/cfb1aa99f032842742ea7a698ca44c88 to your computer and use it in GitHub Desktop.
Rename LVM Volume Group
#!/bin/bash
# Rename LVM Volume Group
# (c)https://askubuntu.com/a/900532
# 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
read -p "Enter new hostname: " newhostname
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 -u -k all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment