Linux LVM-based Multiple Disks System/Data Migration
What I Have
Current Setup
- Disk /dev/nvme0n1:
- Partition 1 (/dev/nvme0n1p1): EFI System, mounted as /boot/efi.
- Partition 2 (/dev/nvme0n1p2): Linux FileSystem, mounted as /boot.
- Partition 3 (/dev/nvme0n1p3): LVM, part of ubuntu-vg Volume Group, mounted as /.
- Disk /dev/nvme2n1:
- Entire disk is an LVM Physical Volume, also part of ubuntu-vg.
Output of
sudo pvs
:PV VG Fmt Attr PSize PFree /dev/nvme0n1p3 ubuntu-vg lvm2 a-- <473.89g 0 /dev/nvme2n1 ubuntu-vg lvm2 a-- <476.94g 0
- Disk /dev/nvme0n1:
- New Disk /dev/nvme1n1:
- Empty, unformatted disk.
- To be partitioned and used to replace /dev/nvme0n1 and /dev/nvme2n1.
What I Want
- Disk /dev/nvme1n1 Setup:
- Partition 1 (/dev/nvme1n1p1): EFI System, 1 GiB, formatted as vfat, mounted as /boot/efi.
- Partition 2 (/dev/nvme1n1p2): Linux FileSystem, 2 GiB, formatted as ext4, mounted as /boot.
- Partition 3 (/dev/nvme1n1p3): LVM, occupies the remaining space, part of ubuntu-vg, mounted as /.
- Complete Migration:
- All data (including /boot, /boot/efi, and /) migrated to /dev/nvme1n1.
- GRUB configured to boot from /dev/nvme1n1.
- /dev/nvme0n1 and /dev/nvme2n1 removed after migration.
What I Do
Step 1: Partition the New Disk
- Launch
fdisk
to partition /dev/nvme1n1:sudo fdisk /dev/nvme1n1
- Create partitions:
- Partition 1:
- Size: 1 GiB.
- Type: EFI System (t, code 1).
- Partition 2:
- Size: 2 GiB.
- Type: Linux FileSystem.
- Partition 3:
- Size: Remaining space.
- Type: Linux LVM (t, code 8e).
- Partition 1:
- Save and exit fdisk (w).
- Format the partitions:
sudo mkfs.vfat -F32 /dev/nvme1n1p1 sudo mkfs.ext4 /dev/nvme1n1p2 sudo pvcreate /dev/nvme1n1p3
- Add /dev/nvme1n1p3 to the volume group:
sudo vgextend ubuntu-vg /dev/nvme1n1p3
Step 2: Migrate LVM Data
- Move LVM data from old disks to the new disk:
sudo pvmove /dev/nvme0n1p3 /dev/nvme1n1p3 sudo pvmove /dev/nvme2n1 /dev/nvme1n1p3
- Remove old Physical Volumes:
sudo vgreduce ubuntu-vg /dev/nvme0n1p3 sudo vgreduce ubuntu-vg /dev/nvme2n1 sudo pvremove /dev/nvme0n1p3 sudo pvremove /dev/nvme2n1
Step 3: Migrate Boot Data
- Mount new partitions
sudo mount /dev/nvme1n1p2 /mnt/target_p2 sudo mkdir -p /mnt/target_p2/boot/efi sudo mount /dev/nvme1n1p1 /mnt/target_p2/boot/efi
- Copy data from old /boot and /boot/efi:
sudo rsync -avh /boot/ /mnt/target_p2/ sudo rsync -avh /boot/efi/ /mnt/target_p2/boot/efi/
Step 4: Update /etc/fstab
- Mount the new root Logical Volume:
sudo mount /dev/mapper/ubuntu--vg-root /mnt/target_root
- Bind /boot and /boot/efi to the new root:
sudo mount --bind /mnt/target_p2 /mnt/target_root/boot sudo mount --bind /mnt/target_p2/boot/efi /mnt/target_root/boot/efi
- Edit /mnt/target_root/etc/fstab:
- Update UUIDs for /boot/efi, /boot, and / using blkid:
Example /etc/fstab:sudo blkid
UUID=<new-efi-uuid> /boot/efi vfat defaults 0 1 UUID=<new-boot-uuid> /boot ext4 defaults 0 2
- Update UUIDs for /boot/efi, /boot, and / using blkid:
Step 5: Install GRUB
- Enter a chroot environment:
sudo mount --bind /dev /mnt/target_root/dev sudo mount --bind /proc /mnt/target_root/proc sudo mount --bind /sys /mnt/target_root/sys sudo mount --bind /sys/firmware/efi/efivars /mnt/target_root/sys/firmware/efi/efivars sudo chroot /mnt/target_root
- Install GRUB:
grub-install update-grub
- Exit chroot:
exit
Step 6: Verify and Test
- Set /dev/nvme1n1 as the boot device in your BIOS/UEFI settings.
- Remove /dev/nvme0n1 and /dev/nvme2n1 from the machine.
- Boot into the system and verify:
lsblk cat /etc/fstab df -h
Step 7: Resize the New Volume
- Extend the LV:
sudo lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
- Resize the Filesystem
sudo resize2fs /dev/ubuntu-vg/ubuntu-lv
0 Comments
No comments yet.