Linux LVM – How to Convert a Linux Box to LVM After Installation

linuxlvm

My Linux server uses LVM because I replace the machine a year ago and installed from scratch. LVM has been a life saver and I love its flexibility. My Linux laptop, however, was last fully installed three years ago, and thus uses straight partitions.

Is it possible — without a reinstall — to convert an existing system to LVM? If so, what tools can do this job?

I expect that any filesystems to be shared with Windows on a dual-boot system cannot be LVM. If I am wrong, please let me know, but I would be surprised.

Best Answer

Yes, this can be done.

Yes, you can use it for a dual-boot windows system, provided you make Windows the first partition, and it's a primary partition (not an extended inside a logical). My instructions here do not cover this situation, which requires you have some experience dealing with dual booting. If you're after a dual-boot windows/linux setup, don't follow any advice below this point.


I'll go one better: with only a few exceptions, you should be able to migrate your entire laptop live, provided you use an external drive with enough capacity.

Warning! This proceedure is EXTREMELY DANGEROUS! It assumes you have intimate knowledge of the partitioning process, you have a separate /boot partition, and you know a botched attempt can leave you with a brain-damaged installation THAT WON'T BOOT! YOU HAVE BEEN WARNED!

You'll need:

  • /boot located on a separate partition. If you have a combined root filesystem (everyting is stuffed into / ) then this won't work.
  • The ability to boot from a USB hard drive.
  • an external drive large enough to hold all of the data and programs on your existing system. Be sure to run a "du /" and wait for the end result, to get a real feel for the kind of space you'll need.

General Procedure (forgive me for lack of a few commands, I'm writing this very quickly):

  1. Attach the external drive and locate its device name. Let's pretend that you see it as /dev/sdb.
  2. Create a two partitions on your external drive. In our example, this will show up as /dev/sdb1 and /dev/sdb2.

    parted /dev/sdb

  3. Format the first partition as ext2 or ext3, and labeled "boot". Format the second partition on the external drive as type "Linux LVM".

  4. Get the newly minted LVM volume recognized by the system using

    pvscan ; vgscan

  5. Carve off a large-enough chunk of the external LVM volume to hold the entire laptop's hard drive. If in doubt, just use all of the space available.

  6. Format your LVM-based volume as ext3 or whatever filesystem tickles your fancy.

    mke3fs /dev/ExternalUSBVolume/ExternalRoot

  7. For each non-special file system mount, create a directory on your external disk and copy (with attributes) all of your files in each mount to that disk. Ignore copies of mountpoints like /proc, etc. as these are "special".

  8. create the special mountpoints by hand, so that they exist on your external drive. Do not populate them, just make sure the names and permissions match.
  9. (Very carefully) use your bootloader to install a boot image on your external drive, into the /boot partition. This process is a bit complex so I'll leave it to you to find the info elsewhere. It's also dangerous if you botch it - you could end up trying to overwrite your bootloader on your laptop drive. Be careful.
  10. Reboot your laptop. When it is going through the POST, be sure to specify the external drive as your boot drive. At this point in time, you should have a (more-or-less) near-replica of your laptop's drive. (to the other admins out there: yes, I know, the log files and others are not in a 100% copied state, but that's not the emphasis here...it's on getting the data and tools moved over...)
  11. Once you can boot from the external drive, you can then use it to wipe your laptop drive. At this point, you're "living out of" your external drive. Keep this in mind.
  12. Then use a partition editor - gparted or whatever - to delete the original laptop partitions (bye-bye data...), then mark your newly-minted area as "LVM". Go through the process of getting your LVM volume recognized, partitioned, formatted, etc.
  13. Using LVM's mirroring process, mirror your live system (which is currently on the external drive) back to the laptop drive. Take a lunch break (or two) for this as it will take time.
  14. Remount the live filesystem, moving the / filesystem from the external drive to your laptop LVM volume.
  15. Re-run your bootloader installation process. This is critical as the current ramdisk will not have the right settings and will not know you've moved things around. Failing to do this will result in an unbootable system and a visit to a rescue disk. If all else fails, you can boot from your external drive again (nothing lost...)
  16. Reboot the laptop without the external drive attached, and specifying the laptop's internal drive instead. It should boot fine. Note anything that feels or looks hurt or broken (there shouldn't be anything), and fix it.
  17. Sit back and enjoy a beverage for a job well done.
Related Topic