Please note that this guide may not work, as I wrote it after setting up my LVM system instead of during setup
I’ve had my Linux system running for a while, but today, when I was notified that I was running low on disk space, I decided to migrate to an LVM based system.
This conversion can be done in 4 steps:
Step 1: Back up Data With
First of all, you’re going to need somewhere to back up to. I used an external USB hard drive, but any other storage medium that is large enough will do. If you’re using a USB 3.0 device, you might want to plug it into a USB 3.0 port to take advantage of the extra speed.
Now, why don’t we use
We don’t use
cp because it doesn’t copy permissions, links, and other miscellaneous things. Instead, we copy the filesystem into an image on the backup drive.
The command to do this is:
$ dd if=/dev/sda* of=/mnt/image.ext4 bs=4k conv=notrunc,noerror,sync
if=/dev/sda*: This is the input file, which, in this case, is your filesystem. You’re going to want to replace /dev/sda* with whatever your device is, which you can find out with the
mount command. If you have multiple partitions, copy them into different images
of=/mnt/image.ext4: This can really be whatever you want. It’s the output file. I chose /mnt/image.ext4 just as an example. The one thing you want to watch out for is that the path is on your backup drive, not on any drive that you’re converting to LVM
bs=4k: This tells
dd to copy in blocks of 4 kilobytes, which is usually an efficient size to copy around
notrunc: People say you should have this when copying disks. I think it tells
dd not to make the output file smaller, but I don’t know
noerror: This tells
dd to keep copying even if it gets an I/O Error
sync: This tells
dd that when it finds an error, write 0s where it was so that the blocks in the output file still line up with the ones in the input file. This is important so that your filesystem doesn’t become any more corrupted if you have errors
Now’s the time to boot up a rescue disk. I used GParted live when I converted my system, but I’m sure that other rescue disks would work.
Open up a partiton editor and decide how you want your LVM volumes. One solution is to just take the entire disk an LVM volume, but I dual boot windows and had to make 3 LVM volumes on different partitons because of my bizzare partition layout.
GParted supports making partitons into LVM volumes. First, delete all the old partitions. Then create new partitons with the type “LVM Physical Volume” or something similar.
I had a /boot partition which I decided not to delete, and it made configuring GRUB a lot easier. If you have a /boot partition, you might want to consider not deleting it.
BEFORE YOU HIT ACCEPT, make sure your backups were made correctly! Try mounting the image files with
$ mount -t ext4 /mnt/image.ext4 /tst
and verify that all your files are there and intact. If you have done this, hit accept and wait for it to finish.
Next, open a terminal and type some commands in:
$ vgcreate <volume group> <volumes>
$ lvcreate -L <size> <volume group> -n <lvname>
Do this for all the virtual partitions you want. Replace <size> with how big you want your partiton in the units of your choice. I wanted my partiton to be the same size as the image, so I
ls -l‘d the image to find the size in bytes and put
lvcreate -L 161061273600b to indicate that I wanted 161061273600 bytes in my partiton. You can use the units of your choice, but make sure you know that KB/MB/GBs use powers of 1000 and KiB/MiB/GiBs use powers of 1024.
Replace <volume group> with the name of the volume group used above, and <lvname> with the name of the logical volume you want to create.
This step is very similar to Step 1, so I won’t go into as much detail here.
$ dd if=/mnt/image.ext4 of=/dev/mapper/<volume group>-<lvname> bs=4k conv=noerror,notrunc,sync
I expect you to use your brain to figure out that you should replace /mnt/image.ext4 with wherever you saved your disk image and such.
If you have multiple disk images to copy, just run the command multiple times with different logical volumes.
This will take a while to copy if you have a lot of data, so find something to do while it’s copying.
This is the trickiest step, where we have to inform Linux and GRUB and such about the new LVM layout. First, let’s mount the Linux system.
I used /mnt, but you can use any path you want.
$ mount /dev/mapper/<volume group>-<lvname> /mnt
Again, replace things with the applicable contents. If you have multiple partitions/logical volumes, mount them too.
chroot in, we need to mount some filesystems so that programs will work.
cd /mnt mount -t proc proc proc/ mount --rbind /sys sys/ mount --rbind /dev dev/
Once we do this, we can
chroot /mnt and configure the system.
/etc/fstab: Use your favorite text editor and open up /etc/fstab. There should be rules in there to mount your partitions. Replace each /dev/sd* with its corresponding LVM volume (/dev/mapper/<volume group>-<lvname>) and make sure the file is intact.
/etc/mkinitcpio.conf This needs to have the LVM module loaded in so that the kernel can load your rootfs. Open it up and look for this:
HOOKS="base udev ... block filesystems"
Now we need to rebuild the initcpio, so run
mkinitcpio -p linux
/boot/grub/grub.cfg Sorry, but I can’t help you if you don’t have an external /boot partition. But if you do, use grub-mkconfig to generate your GRUB configuration. Make sure that there’s an
insmod lvm2 in there and that it boots your kernel with
root=/dev/mapper/<vgname>-<lvname>. Next, re-install GRUB with
Next, boot up your system. Hopefully everything’s working properly! I might’ve forgotten a step somewhere, because I wrote this after I set up my system.
If anything (related to this guide) isn’t working right, leave a comment below