LVM

From Bibliotheca Anonoma
Revision as of 19:53, 31 January 2018 by Antonizoon (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

LVM is greatly extensible and can be set up into Volume Groups.

<x404102> You make an LVM drive array on the drives. Then you partition it in two partitions, 2TB each, for both drives that makes VG volume groups. so you have 4 volume ground A0 A1 B0 B1. You put a RAID 1 on A0 and B0 and a RAID 0 on A1 and B1.
<x404102> It's not really partition it's just volume groups.
<x404102> You format your two RAIDs, A is 2TB and B is 4TB.
<antonizoon> Nice
<x404102> If you are booting from them you have to lose space from the boot partition but it can come from the 4TB VGs.
<antonizoon> I use a small ssd for boot so no problem
<x404102> cool
<x404102> You can do it in the debian installer if you are installing fresh.
<antonizoon> I'll note this down
<x404102> Yeah LVM is recommended it's extensible. You can grow a RAID https://raid.wiki.kernel.org/index.php/Growing

From now on, now that I understand the value and power of LVM partitioning, I will set it up for every drive I see.

LVM (Graphical)

system-config-lvm or kvpm

Graphical partitioning systems are the best way to create logical volumes.

LVM (Command Line)

Create MBR or GPT Partitions

First, you should format your drive with a partition table. While LVM can be installed to the entire disk without a partition table, there are severe downsides, especially if you are using a non-UEFI BIOS, and no speed advantages.

While GPT is a superior partition table format, not all BIOSes support it, especially those made before 2012 (when Windows 8 and UEFI was released for PCs). Thus, I chose to use MBR with one partition.

Use GParted to create an MBR Partition table and one Linux LVM partition. Or use fdisk and create one partition with type 8e (Linux LVM).

http://askubuntu.com/questions/153761/can-i-install-to-a-raw-logical-volume-or-must-i-partition-the-space-first

Create Physical Volume

In this example, the SSD partition is at /dev/sda1. This may not always be the case, so be very careful.

# pvcreate /dev/sda1

Create Volume Group

Now, you can create a volume group for your drive. It might be a good idea to follow a decent naming convention: one where you use your hostname.

# vgcreate vg_stavatron_00 /dev/sda

You can extend it to other devices as well using vgextend.

# vgextend vg_stavatron_00 /dev/sdc

Create Logical Volume

Now we need to create some logical volumes in the volume group. The following partitions are usually prudent choices:

Note: Reserve about 20% of free space above the minimum to handle increased sizes.

  • /boot (100-200MB) - Contains the kernel and other boot information.
  • / (15-20GB) - The root partition, containing most of the system.
  • /var (10-12GB) - Contains many small files. The choice of file system type should consider this fact if a separate partition is used.
  • Because a lot of small read/writes are made to /var/, which can reduce it's working lifetime, you should consider storing it on a hard drive.
  • /tmp (tmpfs) - To significantly enhance system performance, the data in /tmp can be stored in RAM instead. This also reduces wear on the SSD. You can set up tmpfs after installation.
  • swap - A swap partition is necessary when RAM is completely used up, or the system needs to store data for hibernation (on a laptop).
  • Alternatively, you could use a variable-size on-demand swapfile on an ext4 partition. NEVER create a swapfile on a brtfs partition.
  • You should reduce the swappiness to a very low level, such as 1.
# lvcreate -L <size> <volume_group> -n <logical_volume>
# lvcreate -L 100M vg_stavatron_00 -n lv_boot
# lvcreate -L 20G vg_stavatron_00 -n lv_root
# lvcreate -L 15G vg_stavatron_00 -n lv_var
# lvcreate -l +100%FREE vg_stavatron_00 -n lv_home

Filesystem Formats

There are various filesystem formats you can choose from, and each of them bring their own benefits and disadvantages. They also have certain settings that optimize performance on SSDs.

  • ext4 - Rock solid, well tested, known to be reliable. /boot also requires you to use an ext filesystem, so this is the best choice.
  • /boot /home, /
  • brtfs - Relatively new, known for being bleeding edge, but offers higher performance. Make sure you use the latest kernels possible if you plan to use it.
  • However, even though stability is much higher these days, brtfs is not stable enough in comparison to ext4 for situations where corruption caused by the filesystem is unacceptable, in the case of the /home partition.
  • The problem with btrfs is that when it fails, it fails HARD. It also doesn't deal with random power outages very well (which can be common on a laptop) and corrupts files to the point that it can't be deleted without a "btrfs check".
  • In addition, with LVM you cannot resize a brtfs partition. On the flip side, you can make subvolumes under brtfs.
  • /, /var/

Format the logical volumes you made:

# mkfs.ext4 /dev/mapper/vg_stavatron_00-lv_boot
# mkfs.ext4 /dev/mapper/vg_stavatron_00-lv_root
# mkfs.ext4 /dev/mapper/vg_stavatron_00-lv_var
# mkfs.ext4 /dev/mapper/vg_stavatron_00-lv_home

Is Brtfs Right for me?

http://marc.merlins.org/linux/talks/2015/Btrfs-LCA2015/Btrfs.pdf

Make backups. Always. With Brtfs snapshots, there's no excuse.

Copy over Data with Rsync

I mounted the lv_home partition and sent my 9GB /home directory over in just 30 seconds at 91MB/second.

# mkdir /mnt/lv_device
# mount /dev/mapper/vg_stavatron_00-lv_home  /mnt/lv_device
# rsync -aAXv /home/* /mnt/lv_device/
# umount /dev/mapper/vg_stavatron_00-lv_home

Next, I sent over /var. I made sure to clean up my pacman cache before doing this step.

# mount /dev/mapper/vg_stavatron_00-lv_var  /mnt/lv_device
# rsync -aAXv /var/* /mnt/lv_device/
# umount /dev/mapper/vg_stavatron_00-lv_var

After that, I sent over /boot. Unlike the previous ones, we will use the a new /mnt/lv_boot to reduce confusion.

# mkdir /mnt/lv_boot
# mount /dev/mapper/vg_stavatron_00-lv_boot  /mnt/lv_boot
# rsync -aAXv /boot/* /mnt/lv_boot/
# umount /dev/mapper/vg_stavatron_00-lv_boot

Finally, I sent over / with some directories excluded. I excluded /home and /var since they have their own partitions already (which need to be set in fstab).

# mount /dev/mapper/vg_stavatron_00-lv_root  /mnt/lv_device
# rsync -aAXv --exclude={"/dev/*","/home/*","/var/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} /* /mnt/lv_device/
# umount /dev/mapper/vg_stavatron_00-lv_root

Chroot into the root

Now, we need to enter the filesystem to set it up further.

# arch-chroot /mnt/lv_device

Edit fstab

Edit your fstab to match the new configurations. In particular, you need to add mount points for the new /home and /var logical volumes, as well as SSD optimizaiton options.

Edit the fstab:

# nano /etc/fstab

Then, edit the entry in the fstab to reference the correct root, home, and var logical volumes. Also add some SSD optimization options, such as noatime and discard. The following is what I used:

/dev/mapper/vg_stavatron_00-lv_boot /boot ext4 defaults,noatime,discard 0 2
/dev/mapper/vg_stavatron_00-lv_root / ext4 defaults,noatime,discard 0 1
/dev/mapper/vg_stavatron_00-lv_home /home ext4 defaults,noatime,discard 0 2
/dev/mapper/vg_stavatron_00-lv_var /var ext4 defaults,noatime,discard 0 0

Finally, remount all partitions.

# mount -a

Note: LVM logical volumes should be referenced using /dev/mapper/vgname-lvname, not UUIDs.

Generate kernel and GRUB Config

We need to create a new Linux kernel with the LVM option, and regenerate GRUB.

First, edit your /etc/mkinitcpio.conf to add the lvm2 HOOK between block and filesystems. Next, edit the following:

# mkinitcpio -p linux

Then, edit /etc/lvm/lvm.conf and set use_lvmetad = 0. This disables the errant option.

Next, edit /etc/default/grub and add the lvm2 hook to GRUB_PRELOAD_MODULES. Then regenerate grub.

# grub-mkconfig -o /boot/grub/grub.cfg

Finally, install GRUB to the MBR of the drive.

# grub-install /dev/sda

Sources