Tag - Linux

Linux LVM-based Multiple Disks System/Data Migration

What I Have

  1. 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
    
  2. New Disk /dev/nvme1n1:
    • Empty, unformatted disk.
    • To be partitioned and used to replace /dev/nvme0n1 and /dev/nvme2n1.

What I Want

  1. 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 /.
  2. 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

  1. Launch fdisk to partition /dev/nvme1n1:
    sudo fdisk /dev/nvme1n1
    
  2. 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).
  3. Save and exit fdisk (w).
  4. Format the partitions:
    sudo mkfs.vfat -F32 /dev/nvme1n1p1
    sudo mkfs.ext4 /dev/nvme1n1p2
    sudo pvcreate /dev/nvme1n1p3
    
  5. Add /dev/nvme1n1p3 to the volume group:
    sudo vgextend ubuntu-vg /dev/nvme1n1p3
    

Step 2: Migrate LVM Data

  1. Move LVM data from old disks to the new disk:
    sudo pvmove /dev/nvme0n1p3 /dev/nvme1n1p3
    sudo pvmove /dev/nvme2n1 /dev/nvme1n1p3
    
  2. 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

  1. 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
    
  2. 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

  1. Mount the new root Logical Volume:
    sudo mount /dev/mapper/ubuntu--vg-root /mnt/target_root
    
  2. 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
    
  3. Edit /mnt/target_root/etc/fstab:
    • Update UUIDs for /boot/efi, /boot, and / using blkid:
      sudo blkid
      
      Example /etc/fstab:
      UUID=<new-efi-uuid> /boot/efi vfat defaults 0 1
      UUID=<new-boot-uuid> /boot ext4 defaults 0 2
      

Step 5: Install GRUB

  1. 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
    
  2. Install GRUB:
    grub-install
    update-grub
    
  3. Exit chroot:
    exit
    

Step 6: Verify and Test

  1. Set /dev/nvme1n1 as the boot device in your BIOS/UEFI settings.
  2. Remove /dev/nvme0n1 and /dev/nvme2n1 from the machine.
  3. Boot into the system and verify:
    lsblk
    cat /etc/fstab
    df -h
    

Step 7: Resize the New Volume

  1. Extend the LV:
    sudo lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
    
  2. Resize the Filesystem
    sudo resize2fs /dev/ubuntu-vg/ubuntu-lv
    

- DONE -