LVM: Difference between revisions

From Bibliotheca Anonoma
(Created page with "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, fo...")
 
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
LVM is greatly extensible and can be set up into Volume Groups.
LVM is greatly extensible and can be set up into Volume Groups.


```
<pre>
<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> 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> It's not really partition it's just volume groups.
Line 12: Line 12:
<antonizoon> I'll note this down
<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
<x404102> Yeah LVM is recommended it's extensible. You can grow a RAID https://raid.wiki.kernel.org/index.php/Growing
```
</pre>
 
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) ==
 
<code>system-config-lvm</code> or <code>kvpm</code>
 
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 <code>fdisk</code> and create one partition with type <code>8e</code> (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 ===
 
<blockquote>In this example, the SSD partition is at <code>/dev/sda1</code>. This may not always be the case, so be very careful.
</blockquote>
<pre># pvcreate /dev/sda1</pre>
=== 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.
 
<pre># vgcreate vg_stavatron_00 /dev/sda</pre>
You can extend it to other devices as well using <code>vgextend</code>.
 
<pre># vgextend vg_stavatron_00 /dev/sdc</pre>
* [http://tanguy.ortolo.eu/blog/article72/naming-vgs Volume Group Naming Convention]
 
=== Create Logical Volume ===
 
Now we need to create some logical volumes in the volume group. The following partitions are usually prudent choices:
 
<blockquote>'''Note:''' Reserve about 20% of free space above the minimum to handle increased sizes.
</blockquote>
* <code>/boot</code> (100-200MB) - Contains the kernel and other boot information.
* <code>/</code> (15-20GB) - The root partition, containing most of the system.
* <code>/var</code> (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 <code>/var/</code>, which can reduce it's working lifetime, you should consider storing it on a hard drive.
* <code>/tmp</code> (tmpfs) - To significantly enhance system performance, the data in <code>/tmp</code> 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 [https://wiki.archlinux.org/index.php/Swap#Swap_file a variable-size on-demand swapfile] on an <code>ext4</code> partition. NEVER create a swapfile on a <code>brtfs</code> partition.
* You should reduce the swappiness to a very low level, such as 1.
 
<pre># lvcreate -L &lt;size&gt; &lt;volume_group&gt; -n &lt;logical_volume&gt;
# 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</pre>
* [https://wiki.archlinux.org/index.php/Tmpfs Arch Linux Wiki - Tmpfs]
 
=== 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.
 
* <code>ext4</code> - Rock solid, well tested, known to be reliable. <code>/boot</code> also requires you to use an <code>ext</code> filesystem, so this is the best choice.
* <code>/boot</code> <code>/home</code>, <code>/</code>
* <code>brtfs</code> - 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, <code>brtfs</code> is not stable enough in comparison to <code>ext4</code> for situations where corruption caused by the filesystem is unacceptable, in the case of the <code>/home</code> partition.
* The problem with btrfs is that when it fails, [https://www.reddit.com/r/linux/comments/3dl2nw/is_btrfs_production_ready/ct68pnw 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 &quot;btrfs check&quot;.
* In addition, with LVM you cannot resize a <code>brtfs</code> partition. On the flip side, you can make subvolumes under brtfs.
* <code>/</code>, <code>/var/</code>
 
Format the logical volumes you made:
 
<pre># 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</pre>
 
-----
 
* [http://unix.stackexchange.com/questions/685/why-put-things-other-than-home-to-a-separate-partition Stack Overflow - Why put things other than /home/ to a separate partition?]
* [https://wiki.archlinux.org/index.php/File_systems#Types_of_file_systems Arch Linux Wiki - File Systems: Types of File Systems]
* [https://docs.fedoraproject.org/en-US/Fedora/15/html/Installation_Guide/s2-diskpartrecommend-x86.html Fedora 15: Recommended Partition Scheme]
 
=== 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 <code>lv_home</code> partition and sent my 9GB <code>/home</code> directory over in just 30 seconds at 91MB/second.
 
<pre># 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</pre>
Next, I sent over <code>/var</code>. I made sure to clean up my pacman cache before doing this step.
 
<pre># 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</pre>
After that, I sent over <code>/boot</code>. Unlike the previous ones, we will use the a new <code>/mnt/lv_boot</code> to reduce confusion.
 
<pre># 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</pre>
Finally, I sent over <code>/</code> with some directories excluded. I excluded <code>/home</code> and <code>/var</code> since they have their own partitions already (which need to be set in <code>fstab</code>).
 
<pre># mount /dev/mapper/vg_stavatron_00-lv_root  /mnt/lv_device
# rsync -aAXv --exclude={&quot;/dev/*&quot;,&quot;/home/*&quot;,&quot;/var/*&quot;,&quot;/proc/*&quot;,&quot;/sys/*&quot;,&quot;/tmp/*&quot;,&quot;/run/*&quot;,&quot;/mnt/*&quot;,&quot;/media/*&quot;,&quot;/lost+found&quot;} /* /mnt/lv_device/
# umount /dev/mapper/vg_stavatron_00-lv_root</pre>
* [https://wiki.archlinux.org/index.php/Full_system_backup_with_rsync Arch Linux Wiki - Full System Backup with Rsync]
 
=== Chroot into the root ===
 
Now, we need to enter the filesystem to set it up further.
 
<pre># arch-chroot /mnt/lv_device</pre>
=== Edit <code>fstab</code> ===
 
Edit your fstab to match the new configurations. In particular, you need to add mount points for the new <code>/home</code> and <code>/var</code> logical volumes, as well as SSD optimizaiton options.
 
Edit the <code>fstab</code>:
 
<pre># nano /etc/fstab</pre>
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 <code>noatime</code> and <code>discard</code>. The following is what I used:
 
<pre>/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</pre>
Finally, remount all partitions.
 
<pre># mount -a</pre>
<blockquote>'''Note:''' LVM logical volumes should be referenced using <code>/dev/mapper/vgname-lvname</code>, not UUIDs.
</blockquote>
=== Generate kernel and GRUB Config ===
 
We need to create a new Linux kernel with the LVM option, and regenerate GRUB.
 
First, edit your <code>/etc/mkinitcpio.conf</code> to add the <code>lvm2</code> HOOK between block and filesystems. Next, edit the following:
 
<pre># mkinitcpio -p linux</pre>
Then, edit <code>/etc/lvm/lvm.conf</code> and set <code>use_lvmetad = 0</code>. This disables the errant option.
 
Next, edit <code>/etc/default/grub</code> and add the <code>lvm2</code> hook to <code>GRUB_PRELOAD_MODULES</code>. Then regenerate grub.
 
<pre># grub-mkconfig -o /boot/grub/grub.cfg</pre>
Finally, install GRUB to the MBR of the drive.
 
<pre># grub-install /dev/sda</pre>
=== Sources ===
 
* [https://wiki.archlinux.org/index.php/LVM#Create_physical_volumes Arch Linux Wiki - LVM: Create LVM Physical Volumes]
* [https://wiki.archlinux.org/index.php/LVM#Create_volume_group Arch Linux WIki - LVM: Create Volume Group]
* [https://wiki.archlinux.org/index.php/Partitioning#Partition_scheme Partitioning]

Latest revision as of 19:53, 31 January 2018

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)[edit]

system-config-lvm or kvpm

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

LVM (Command Line)[edit]

Create MBR or GPT Partitions[edit]

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[edit]

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[edit]

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[edit]

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[edit]

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?[edit]

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[edit]

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[edit]

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

# arch-chroot /mnt/lv_device

Edit fstab[edit]

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[edit]

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[edit]