spacer

Sysresccd-LVM-EN-Booting-linux-from-LVM-volumes

History


Contents

About

In this tutorial, we will explain the linux boot process in general, and how it works when Linux is installed on an LVM volume. It's recommended to read the previous chapter about LVM if you are not already familiar with LVM. This section will also give you a good understanding of the Linux boot process in general even if you don't use LVM, so it's worth reading. It's recommended that you read the previous chapters about LVM before reading this section.

Booting from an LVM Root filesystem

Many linux distributions such as Redhat/Fedora allow you to install the root filesystem on an LVM Logical Volume. This way you get the flexibility of LVM for this filesystem and not just for the data. The down side is that the boot procedure is a bit more complex, and you need to understand how its boots so that you are able to fix it in case of problems.

Booting using a root filesystem located on an LVM Logical Volume is more complicated because the kernel does not know anything about LVM. So it needs the LVM binary to mount the root filesystem. But the LVM binary is installed on the root filesystem that we want to mount... The solution to this egg and chicken problem is to boot with an initramdisk/initramfs. This is a compressed archive that contains all the kernel modules and programs that are required to mount the root filesystem at boot time. In general, it's located in /boot, and it's called initrd-x.y.z.gz. The other important file is the linux kernel image which is called vmlinuz-x.y.z.

Here are the important steps involved in booting from LVM:

  1. The BIOS executes the boot loader (grub/lilo) which is installed at the beginning of the hard disk
  2. The boot loader has its own code for reading partitions and filesystems. So it knows how to read files from the /boot partition which contains the linux kernel image (vmlinuz-x.y.z) and the initramfs (initrd-x.y.z.gz). It first loads these two files into memory. Then it executes the kernel image and it tells the kernel where the initramfs is located in memory. The boot command line is also passed to the kernel. This command line contains the important parameters for the kernel such as root=/dev/volgroup/lvroot.
  3. The linux kernel starts and executes its initialization code. Then it reads the initramfs from the memory. The contents is uncompressed into a new location in the memory.
  4. The contents of the initramfs is now available. The program/script called init is now executed. This script which is specific to each linux distribution is responsible for finding the root filesystem.
  5. If the LVM Physical Volumes are stored on the top of a RAID disk, the init program will first execute dmraid/mdadm to make this raid disk available.
  6. Then the init script will run programs such as pvscan/vgscan/lvscan to detect the LVM volumes located on the disks
  7. The LVM volumes are not usable yet. They have to be activated first. This is done by vgchange --available y or vgchange -ay.
  8. The init script reads the virtual file called /proc/cmdline to see what is the name of the root filesystem specified on the boot command line.
  9. The root filesystem is mounted in a temporary directory such as /rootfs and other things such as /rootfs/proc and /rootfs/dev may also be mounted.
  10. The initscript executes a chroot to /rootfs. This means that this directory becomes the new root for the processes which will be executed. When a process reads /bin/something it will read /rootfs/bin/something in reality.
  11. The secondary init program, the one which is stored on the root filesystem is now executed and it finishes the initialization with an access to the real root filesystem.

Distributions and rootfs on LVM

  • All the mainstream Linux distributions now support LVM2. But the installation program may not allow you to install the Linux rootfs onto an LVM Logical Volume.
    • Redhat + Fedora + Centos, anaconda (the installation program) installs the root filesystem on LVM by default, but you can change it.
    • Gentoo: since the default installation method is to install it by hand, you can directly install Gentoo on LVM if you know what you are doing
    • Ubuntu: as of version 09.04, the installation program does not support LVM, but you can probably migrate the root filesystem after installation.

There are good reasons for using LVM for the root filesystem:

  • The root filesystem will be more flexible: it will be easier to resize it if it's on an LVM Logical Volume
  • You can make snapshots of the root filesystem which is very useful if you want to make consistent backups of it

You may not want your root filesystem to be on an LVM Logical Volume if you don't want to boot with a ramdisk. If you compile your own kernel with all the important disk and filesystem drivers built in the linux image, it will be possible to boot with no initramfs as long as linux is installed on a standard disk partition that the kernel will be able to mount.

Extracting an initramfs

An initramdisk/initramfs contains the files necessary to obtain access to the root filesystem at boot time. It's possible to read its contents if you want to have a look at its contents. There are two sorts of such files: the old ones are called initramdisk and the new ones are called initramfs.

about initramdisks

Initramdisks existed first. It's a compressed image of a filesystem. In general the filesystem is an ext2fs but it can also be a minixfs.

Make an uncompressed copy of the initrd
cat /boot/initrd-x.y.z.gz | gzip -d > /var/tmp/initrd-x.y.z
Mount the copy of the original initrd
mkdir /var/tmp/mntramdisk ; mount -o ro,loop /var/tmp/initrd-x.y.z /var/tmp/mntramdisk
Copy the files to a new directory
cp -a /var/tmp/mntramdisk /var/tmp/ramdiskfiles
Unmount the image
cd / ; umount /var/tmp/mntramdisk

about initramfs

New Linux-2.6 based distributions provide initramfs instead. They are compressed cpio archives. Here is how to extract it

Create a temporary directory
mkdir /var/tmp/ramdiskfiles
Extract the compressed initramfs
cd /var/tmp/ramdiskfiles && zcat /boot/initrd-x.y.z.gz | cpio -id
Here is how to recreate an initramfs

The following command will create a new initramfs using files located in /var/tmp/ramdiskfiles-new:

find /var/tmp/ramdiskfiles-new | cpio -H newc -o | gzip -9 > boot/initrd-x.y.z-new.gz
spacer