LVM2

Dec 16, 2013
3 min read
May 27, 2023 09:13 EEST

To move an existing installation to a lvm2 the following steps a necessary:

Build initramfs using dracut

Enable it:

echo "sys-fs/lvm2 static" >>/etc/portage/package.use
echo "sys-fs/mdadm static" >>/etc/portage/package.use
echo "sys-apps/busybox static" >>/etc/portage/package.use
echo "media-libs/freetype static-libs" >>/etc/portage/package.use
echo "sys-kernel/dracut" >> /etc/portage/package.keywords
echo "media-libs/freetype" >> /etc/portage/package.keywords
echo "media-gfx/splashutils" >> /etc/portage/package.keywords
echo "sys-kernel/dracut device-mapper" >> /etc/portage/package.use
echo 'DRACUT_MODULES="gensplash lvm mdraid"' >> /etc/make.conf
emerge -av --autounmask-write sys-kernel/dracut
dispatch-conf
emerge -av sys-kernel/dracut

Generate it with:

dracut "" `uname -r`
ln -s /boot/initramfs-`uname -r`.img /boot/initramfs.img

Modify your grub config:

title Gentoo Linux
root (hd0,0)
kernel /vmlinuz root=/dev/vg/root-new
initrd /initramfs.img

Move root to LVM2

At first we have to backup the complete root partition to a backup partition. You can do this by execute:

find / -depth -xdev -print0 |cpio --null --sparse -pVd  /newroot/

This will copy all files from / to /newroot excluding all external mount points.

Adapt the /etc/fstab to point to the correct root partition, in this case:

/dev/vg/root-new /   ext4  defaults 0  1

Create the real root

After we moved our current root to a lvm2 and booted it from there we can completely destroy the old boot harddisk and can recreate it from scratch. I increased the /boot partition to 100MB and the rest is used for LVM2.

Use fdisk to create a 100MB partition and make it bootable, create the lvm partition as a primary partition, it should look like this:

   Geraet  boot.     Anfang        Ende     Bloecke   Id  System
/dev/sda1   *        2048      206847      102400   83  Linux
/dev/sda2          206848   312581807   156187480   8e  Linux LVM

As next mount /dev/sda1 to /boot and install grub:

mke2fs /dev/sda1
mount /dev/sda1 /boot
grub
root (hd0,0)
setup (hd0)
exit

At first we have to create a PV on the harddisk:

pvcreate /dev/sda2

New we can create a new volume group.

vgcreate boot /dev/sda2

Now we create a logical volume for the swap partition:

lvcreate -L2G -nswap boot
mkswap /dev/boot/swap
swapon /dev/boot/swap

Next is create to new partition for the gentoo system and use the rest for VDR:

lvcreate -L30G -nroot boot
mkfs.ext4 /dev/boot/root
lvcreate -l100%FREE -nvideo1 boot
mkfs.ext4 /dev/boot/video1

Now we copy the backup back to the new root partition:

mount /dev/boot/root /newroot/
find / -depth -xdev -print0 |cpio --null --sparse -pVd  /newroot/

Adapt the /etc/fstab:

/dev/sda1               /boot           ext2    defaults        1 2
/dev/boot/swap          none            swap    sw              0 0
/dev/boot/root          /               ext4    defaults        0 1
/dev/boot/video1        /video1         ext4    defaults        0 0
none                    /proc           proc    defaults        0 0
none                    /dev/shm        tmpfs   defaults        0 0

/dev/vg/video0          /video0         ext4    defaults        1 1

And modify the /boot/grub/grub.conf:

title=Gentoo Linux
root (hd0,0)
kernel /vmlinuz root=/dev/boot/root
initrd /initramfs.img

Reboot your system and enjoy your new structure.

Initramfs with genkernel

genkernel --lvm --mdadm --dmraid --iscsi --disklabel initramfs

Related Posts