These notes cover replacing a system disk’s layout with LVM.
This is a risky operation. Back up important data first, and don’t format the old disk until the new system has been verified.
Environment
CentOS 7.x.
The goal was to resize /, but the disk had been auto-partitioned into a messy layout with tangled symlinks, so I decided to move to LVM for management.
Preparation
Add a new disk to the node first. Split it into two partitions:
| Partition | Size | Purpose |
|---|---|---|
| /dev/vda1 | 200M | /boot, ext4 |
| /dev/vda2 | remaining space | LVM PV |
Partition with fdisk /dev/vda and mark /dev/vda1 as bootable.
Initialize LVM:
pvcreate /dev/vda2
vgcreate centos /dev/vda2
lvcreate --extents 100%FREE --name root centos
Initialize the filesystems
mkfs.ext4 /dev/vda1
mkfs.xfs /dev/mapper/centos-root
Mount the partitions
mkdir -pv /mnt/centos
mount /dev/mapper/centos-root /mnt/centos
mkdir -pv /mnt/centos/boot
mount /dev/vda1 /mnt/centos/boot
Sync the System to the New Partitions
You can use rsync to copy every file from the current system to the new partitions:
rsync -aAXv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} / /mnt/centos
Finishing Up
Look up the new partitions’ UUIDs with blkid and write them into /mnt/centos/etc/fstab:
blkid
UUID=682dff8d-3480-4f52-9f6a-1f9e832e5880 /boot ext4 defaults 0 2
UUID=0db29396-38bb-4a7b-94b0-344e7c122fd4 / xfs defaults 0 0
Replace the UUIDs with your own. Root is XFS, so its fsck pass is 0;
/bootis ext4, so it gets 2.
Enter the chroot
Mount the necessary filesystems:
mount --types proc /proc /mnt/centos/proc
mount --rbind /sys /mnt/centos/sys
mount --make-rslave /mnt/centos/sys
mount --rbind /dev /mnt/centos/dev
mount --make-rslave /mnt/centos/dev
Enter the chroot:
chroot /mnt/centos /bin/bash
source /etc/profile
export PS1="(chroot) $PS1"
Regenerate the Kernel and GRUB
rm -rf /boot/*
yum install kernel -y
grub2-install /dev/vda
grub2-mkconfig -o /boot/grub2/grub.cfg
exit
After exiting, unmount the mount points:
umount -R /mnt/centos
Other
Shut down, disconnect the old disk, then boot from the new one.
After it boots, check that services, networking and fstab are all fine. Once everything checks out, you can keep the old disk as a backup and format it later.
Afterword
This was a special case, and the conversion took effort. The new disk needs to be at least as large as the old one.
If you only need more capacity, there’s no need to go through all this: add the space, then use growpart with xfs_growfs/resize2fs.
Comments