Welcome to SpiderWiki,
There are currently 131 articles.

Moving from standard partitions to LVM with no reinstall

From SpiderWiki
Jump to: navigation, search

When I installed arch on my laptop. Since I was only testing it out I didn't bother creating the partitions on an LVM. I regretted that as soon as my / partition got full. I really like arch so I wanted to keep it but the initial install took a while so I didn't relish doing it all again. Luckily in my laptop I have 2 disk one of which wasn't being used.

I decided to have a go at moving to an LVM set up while avoiding having to install. To my surprise it worked! Here I describe how I did it.

Initial Setup


Both drives are 60GB

    /dev/sda1       /boot
    /dev/sda2       /
    /dev/sda3       /home
    /dev/sda4       swap

Preparing LVM

Create a partition using all the space on sdb and set the type as LVM (8e). Then:

pvcreate /dev/sdb1

vgcreate archvg /dev/sdb1

Now we start creating the logical volumes

lvcreate -L 20G -n rootlv  archvg

lvcreate -L 300M -n bootlv archvg

lvcreate -L 30G -n homelv archvg

Note: I had to make my homelv smaller that the current /home partition to give / more space

Now it's best if nothing is using the file systems so we're going to run level 1

init 1
umount /boot
umount /home

Now we copy the partitions over. I don't know if this is the best way but it's what worked for me.

dd if=/dev/sda1 of=/dev/archvg/bootlv
fsck -f /dev/arch/bootlv

dd if=/dev/sda2 of=/dev/archvg/rootlv
fsck -f /dev/archvg/rootlv

#since we made the rootlv larger we need to extend the file system
resize2fs /dev/archvg/rootlv

For home we can't copy the partition because the destination is smaller. Luckily the used space is less... at least it was after I deleted some stuff. We'll take care of that in a moment. Now we're going to mount our new file structure in a new directory.

mkdir /tmpmount
mount /dev/archvg/rootlv /tmpmount
mount /dev/archvg/bootlv /tmpmount/boot

#Make an fs on homelv
mkfs.ext4 /dev/archvg/homelv

mount /dev/archvg/homelv /tmpmount/home

#Now copy my home dir over to the new lv
cp -r /home/damonj /home/archvg/homelv

Next we're going to chroot into the new area but we will need /dev in there

mount -o bind /dev /tmpmount/dev
chroot /tmpmount

mount -t proc proc proc/
mount -t sysfs sys sys/
mount -t devpts pts dev/pts/

Update /etc/fstab

/dev/archvg/rootlv /     ext4 defaults 0 1
/dev/archvg/bootlv /boot ext4 defaults 0 1
/dev/archvg/homelv /home ext4 defaults 0 1

I needed to update the initcpio image so that my LVM could be used. Add the lvm2 hook if it's not there.

vim /etc/mkinitcpio.conf
HOOKS="... block lvm2 filesystems ..."

Then update your current image. I made a backup just in case.

mv /boot/initramfs-38-x86_64.img /boot/initramfs-38-x86_64.img.bak
mkinitcpio -p /boot/initramfs-38-x86_64.img

Now we need to update grub.

grub-mkconfig -o /boot/grub/grub.cfg
grub-install /dev/sda

If all went well then all we need is to exit out to our none chrooted envirinment and reboot.