How to change a physical partition system to LVM?

I have a server with Debian that have 3 physical partitions covering all the disk: boot, root y swap. Now I want to replace that partitions with LVM partitions. I know how install Debian with LVM at beginning, but in this case I can't install the system at beginning because the provider gets me a server with remote access and the system installed in this way.

How can I change that partitions using only an ssh connection and possibly other remote server where to put some temporal data?


You can't possibly replace them on a running server with only /, /boot and <swap> partitions. The only way to do it is to unmount the / partition, shrink it and create a new LVM partition at the end, but as you only have remote ssh access, you can't possibly shrink a live, mounted partition without data corruption.


A quick update from my side. Context: today I got online a dedicated server installed with physical partition scheme instead of LVM. There were 3 partitions:

/boot (ext4) - 512M / (ext4) - 730G swap - 8G

Due to the nature of not having console access the final goal was the convert the existing root partition to LVM.

Considering the ext4 is not shrinkable the only way was to reuse the swap partition as temporary root. I also decided to set up the temporary root with LVM to be sure the process can work in the right way.

First turned out the swap:

swapoff -a

Then resized the partition via parted (originally it was started from 742 to 750):

parted
resize 3 742 744

and created a partition for the LVM:

mkpart primary ext2 744 750
set 4 lvm on

PV/VG/LV/filesystem creation for the temp root:

pvcreate /dev/sda4
vgcreate VolGroup00 /dev/sda4
lvcreate -L 5.73G -n tmproot VolGroup00
mkfs.ext4 /dev/VolGroup00/tmproot

Next step was to copy the root to the temporary place:

mount /dev/VolGroup00/tmproot /media
rsync -ravzxq / /media/

Once everything was there then the entry for the root filesystem in /media/etc/fstab had to be changed as well:

/dev/VolGroup00/tmproot /                       ext4    defaults        1 1

Almost there, the last and let say the most unwanted part without console access was to modify the /boot/grub/grub.conf:

original entry:

title CentOS (2.6.32-279.22.1.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro root=UUID=e769af21-d9e1-455f-a6a7-7a9c84d8cbea rd_NO_LUKS LANG=en_US.UTF-8  KEYBOARDTYPE=pc KEYTABLE=hu rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_NO_LVM rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-279.22.1.el6.x86_64.img

modified entry:

title CentOS (2.6.32-279.22.1.el6.x86_64) LVM
insmod lvm
kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro LANG=en_US.UTF-8  KEYBOARDTYPE=pc KEYTABLE=hu SYSFONT=latarcyrheb-sun16 crashkernel=auto dolvm root=/dev/mapper/VolGroup00-tmproot panic=10 
#rd_NO_DM rd_NO_MD rd_NO_LUKS
initrd /initramfs-2.6.32-279.22.1.el6.x86_64.img

Just to be on the safe side: insmod lvm was added along with the dolvm parameter for the kernel and the root path was also changed to root=/dev/mapper/VolGroup00-tmproot. Important to use the /dev/mapper/ path at this time. As a safty deposit I also added the panic=10 parameter and didn't change the default boot entry in the header. Instead of that I went for a try with telling the grub to boot with the new setting only one time and in case of failure the original entry could work:

grub
savedefault --default=1 --once

And finally:

reboot

It was OK for me at the first time so I repeated the whole procedure with creating a new volume group on top of the original root partition and finally I got the root at the right place using LVM.

Hope this helps.