Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Setup LVM for a Linux Installation
Ethan Rodrigo
Ethan Rodrigo

Posted on • Originally published atethanrodrigo.hashnode.dev

     

Setup LVM for a Linux Installation

In Linux there are hundred of ways to get the same work done, and the overwhelming amount of distros makes the number thousand. However it's life saving if we choose the efficient way.

The process of partitioning a hard drive in Linux is same as other stuff; we have got numerous approaches. Yet, there are more effective methods. The best I've met so far is LVM. LVM allows you to combine partitions, hard drives and use as one single partition (for more about LVM read theLVM introduction article).

In this article I'll walk you through the setup of LVM for a Linux installation. Before procedure you need to have followings;

  1. A basic understanding of LVM (get ithere)
  2. A machine to install Linux on
  3. A bootable USB stick that has a Linux iso

To get started, boot your Linux installation...

image.png

Partitioning

Though LVM is an alternative to conventional partition scheme, still we need to partition the hard drive we're going to install the OS on. The reason behind this is,Grub and some other bootloaders doesn't support on LVM partitions. However other secondary partitions can be added without partitioning into a created Volume Group of LVM.

Enough talking, let's get into the terminal.

image.png

But wait, you need to find what kind offirmware you have got. Yes, go and find whether it'sBIOS orUEFI.

What, you don't know how to? Oh! My bad, here's a command. Run it and find out.ls /sys/firmware | grep efi Only theUEFI system has gotefi directory inside/sys/firmware. Therefore if there's no output as follows, yours isBIOS.image.png

Ok now, choose a hard drive. List all the hard drives on the system withfdisk --list and select the drive you want.

image.png

Or trylsblk.

image.png

UEFI partitioning

UEFI vs EFI

You may have heardUEFI as well asEFI. Is it entangling? let me break it down for you.UEFI is the replacement of BIOS, which is a firmware.EFI is the storage partition ofUEFI, which contains the bootloader.

Let's get into partitioning with UEFI now.

Launch fdisk with your selected hard drive.image.png

In fdisk runp command and see if there's any partitions. If you don't have any partitions it would look as follows.image.png

Or else it would list the partitions withing the hard drivesimage.png

If you see any partitions there delete them withd command.image.png

However in case you have stuff in a partitions that you don't want to delete, leave it. Yet, don't create partitions to store different file systems as you do in Windows. We are doing it with LVM here.

It's time to create theEFI partition. Follow the next steps in order to achieve it.

Run commandn to create a new partition. For thePartition type leave it as default (primary).Partition number leave thedefault number or choose a number within the given range as you wish.First sector, leave default value.Last sector is the actual size of the partition, therefore 500MB would be enough for theEFI partion.

The partition is labeled asLinux. But to make it supportUEFI change the type intoEFI with commandt and for the Hex code, provideef.

image.png

For the space left, create a normal partition. The type of the partition should beLinux LVM, whose Hex code is8e.

image.png

Lastly write the changes to the disk and save with thew command.

image.png

BIOS partitioning

Partitioning forBIOS is really simple and we don't have to create multiple partitions. All we have to do is create a new partition and change its type toLinux LVM.

Following example illustrates how theBIOS partition is created.image.png

Setting up LVM

Now, it's time to set up LVM on the system. If you remember from myLVM Introduction article, we have three main components in LVM.

  1. Physical Volume (PV)
  2. Volume Group (VG)
  3. Logical Volume (LV)

First Physical Volumes are created with the existing hard drive or partitions and then a Volume Group needs to be created and all the Physical Volumes can be added there. Then the VG can be sliced into Logical Volumes and one of them can be used to install the Linux system.

Let's get into the terminal. Though I use Arch in following examples, it's general to all the other distributions.

As for the start, create a PV from the partitioned hard drive and put it into a VG. At this moment if you have other hard drives do the same with them as well.

image.png

Now the creation of the LVs comes in. Here you need to decide how many LVs you're going to use and what they are for. You can also leave some space for later usage, means without allocating them into any LV. After all, you just need a/root partition for Linux to work fine. Still it's a better approach if you create a/home partition for users.

NOTE: In the below example I give only 2Gigs to theroot as I'm on a Virtual Machine, but it's better to use 10Gigs.image.png

Then we need to load a kernel module for device mapping,dm_mod. For that executemodprobe dm_mod command. After that the VG can be activated withvgchange -ay command.

image.png

That's it! The LVM partitions are now ready to hold a Linux installation.

Post setup

Although the LVM setup is now done, there's some stuff left to do. These are general things we should do even we use conventional partition scheme.

All LVs need to have a file system (or else how can they open and read files?). Choose a file system as you wish and launchmkfs command with it. The basic syntax ofmkfs is as follows.mkfs.$filesytem /dev/$vgName/$lvName

$filesystem - the file system you have chosen$vgName - name of the VG$lvName - name of the LV
Enter fullscreen modeExit fullscreen mode

I like to go with the popularext4 file system.image.png

Lastly, the LVs can be mounted into the system. Theroot partition has to mounted into the/mnt and thehome partition needs to be mounted into the/mnt/home dir.

image.png

Linux Installation

The LVM and the partitions are ready to go for the installation. Here I won't show you the installation as it's just the classical way of the installations. But remember to install thelvm package as it doesn't come by default.

If you install the system manually, like in Arch, Gentoo you can nowchroot into the/mnt and install the system there. And if you use GUI installation, select the root and home(if have created a one) Logical Volumes in the installation process.

NOTE: Also remember to addlvm into themkinitcpio and other initramfs. Yet, this is not required in GUI installations.

A script to automate the process

As a programmer it's our responsibility to spend 10 hours on automating 5 minute task.

image.png

I did the same with above process. You can find ithere.

You can use it on bothBIOS andEFI systems. Multiple hard drives can also be setup at once and there are more options. Try using-h flag or analyze the source code. Pull requests are mostly welcomed.

Conclusion

Thank you for reading! Now go and executesudo rm -rf / --no-preserve-root and make tux happy. Until next time.

If you find this useful let's connect onTwitter,Instagram,dev.to andHashnode.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

A 16 y/o teenager, obsessed with tech. I use Arch btw,
  • Location
    Mars
  • Work
    A Student
  • Joined

More fromEthan Rodrigo

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp