Migrating Physical Partitions to LVM on Remote Debian Server Without Reinstallation


6 views

When dealing with a remote production server where physical partitions need conversion to LVM, we face several technical constraints:

  • No physical console access (SSH-only)
  • No ability to boot from external media
  • Requirement to maintain system uptime during conversion
  • Limited temporary storage options
# 1. Prepare temporary storage on remote server
ssh backup-server "mkdir -p /backup/$(hostname)"

Create LVM physical volumes on existing partitions (excluding /boot):

# 2. Initialize PVs on existing partitions
pvcreate /dev/sda2
pvcreate /dev/sda3

# 3. Create volume group
vgcreate vg0 /dev/sda2 /dev/sda3

For the root partition migration while keeping the system operational:

# 4. Create logical volumes
lvcreate -L 20G -n root vg0
lvcreate -L 4G -n swap vg0

# 5. Format new volumes
mkfs.ext4 /dev/vg0/root
mkswap /dev/vg0/swap

The actual data migration requires careful handling:

# 6. Mount and copy data
mkdir /mnt/newroot
mount /dev/vg0/root /mnt/newroot
rsync -axHAX --delete / /mnt/newroot/

# 7. Preserve special directories
mkdir /mnt/newroot/{proc,sys,dev,run,tmp}

Essential post-migration adjustments:

# 8. Update fstab
cat > /mnt/newroot/etc/fstab << EOF
/dev/sda1 /boot ext4 defaults 0 2
/dev/vg0/root / ext4 defaults 0 1
/dev/vg0/swap none swap sw 0 0
EOF

# 9. Reinstall GRUB
chroot /mnt/newroot /bin/bash
grub-install /dev/sda
update-grub
exit

The riskiest part requiring minimal downtime:

# 10. Reboot and verify
reboot

# After reboot check:
lsblk
vgs
lvs

Essential precautions before starting:

  1. Complete disk image backup to remote server
  2. Document original partition table
  3. Prepare emergency boot script on remote server

Converting physical partitions to LVM on a live system presents unique challenges, especially when working remotely via SSH. The primary constraints are:

  • Cannot unmount the root filesystem during operation
  • Limited temporary storage space for data migration
  • Need to maintain bootability throughout the process

Before starting, ensure you have:

  • Another server with SSH access for temporary storage
  • At least 20% free space on your root partition
  • Backups of critical data
  • Root access to the Debian server

1. Prepare Temporary Storage

On your secondary server, create a directory for temporary storage:

ssh backup-server "mkdir -p /tmp/lvm-migration"

2. Install Required Packages

On your Debian server:

apt-get update
apt-get install -y lvm2 dmsetup mdadm

3. Create Physical Volumes

Assuming your disk is /dev/sda and you have free space:

pvcreate /dev/sda4

4. Set Up Volume Group

vgcreate vg0 /dev/sda4

5. Migrate Root Partition

This is the most critical step. We'll use rsync to copy data to a temporary logical volume:

lvcreate -L 10G -n root-temp vg0
mkfs.ext4 /dev/vg0/root-temp
mkdir /mnt/newroot
mount /dev/vg0/root-temp /mnt/newroot
rsync -aAXv / --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} /mnt/newroot/

6. Update Boot Configuration

Edit /etc/fstab on the new root:

nano /mnt/newroot/etc/fstab

Replace entries with LVM paths:

/dev/vg0/root    /    ext4    defaults    0    1

7. Rebuild Initramfs

chroot /mnt/newroot update-initramfs -u
chroot /mnt/newroot update-grub

8. Final Switch

After verifying everything works from the temporary root, you can:

  1. Rename the temporary LV to root
  2. Remove old physical partitions
  3. Extend the volume group to include freed space
lvrename vg0 root-temp root
  • If boot fails, use a rescue ISO to chroot and fix configurations
  • Monitor disk space during rsync operations
  • Consider using screen or tmux for long-running SSH sessions

For repeatable migrations, consider this bash script snippet:

#!/bin/bash
# Validate free space
FREE_SPACE=$(df -h / | awk 'NR==2 {print $4}')
if [[ ${FREE_SPACE%G} -lt 5 ]]; then
    echo "Insufficient free space"
    exit 1
fi

# Create LVM structures
pvcreate /dev/sda4 >/dev/null 2>&1 || { echo "PV creation failed"; exit 1; }
vgcreate vg0 /dev/sda4 >/dev/null 2>&1 || { echo "VG creation failed"; exit 1; }
lvcreate -L 10G -n root-temp vg0 >/dev/null 2>&1 || { echo "LV creation failed"; exit 1; }

# Filesystem and copy
mkfs.ext4 /dev/vg0/root-temp >/dev/null 2>&1
mkdir -p /mnt/newroot
mount /dev/vg0/root-temp /mnt/newroot
rsync -aAXv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} / /mnt/newroot/