1. Home
  2. Linux

How to create a basic LVM volume on Linux

LVM, (also known as Logical Volume Management), is a brilliant way Linux users can extend, shrink and modify partitions in hard drives in real time, without the need to unmount the file system.┬áIn this guide, we’ll be taking the mystery out of LVM. We’ll go over how to set up a basic LVM volume on Linux, how to create a volume group and more!

Before we begin

In this guide, we’ll be going over how to create a basic LVM setup. Creating an LVM volume involves erasing data, so before we start, please create a system backup.

There are many different ways to create a system-backup on Linux. If you’re unsure how to create one, please check out Deja-Dup. It allows users to create a backup and upload it to the internet, or a network server.

Install LVM2

Now that you’ve made a backup, it’s time to install the LVM software. Open up a terminal and enter the installation instructions that correspond with your operating system.

Ubuntu

sudo apt install lvm2*

Debian

sudo apt-get install lvm2*

Arch Linux

sudo pacman -S lvm2

Fedora

Fedora uses Logical Volume Management heavily, so there’s a good chance the LVM2 package and utilities are already on your computer. Still, if you’re unsure and want to install it anyways, the command is:

sudo dnf install lvm2*

OpenSUSE

sudo zypper install lvm2

Prepare your devices

So, you’re in the live environment. From here, open up the Ubuntu dash by pressing the Windows key. Then, click on the search box, type “Terminal” and press enter to launch it.

In the terminal, gain root access. Obtaining root is critical, as it is not possible to interact with LVM volumes without root.

sudo -s

Once you have root access, confirm it by entering the whoami command. If the output is “root,” you have root access.

whoami

Open up the hard drive you’d like to create the LVM on with the cfdisk partition editor.

Note: change sdX to the drive you plan to use. Check lsblk for more information.

cfdisk /dev/sdX

Highlight each of the partitions on the drive with the Up/Down arrow keys. Then, use the Left/Right arrow keys to highlight the “Delete” option. Press Enter to confirm the deletion. Do this until every partition on the drive is gone, and it is blank.

When all partitions are gone, use the Left/Right arrow keys to find “New” and press it to create a new partition.

After selecting “New,” you’ll be prompted to create a new partition. Use the entirety of the hard drive. For example, if the drive I want LVM set up on is 18GB, I’d enter “18G” in the prompt, and so on.

Highlight “primary” when asked, and once again press Enter on the keyboard. Then, highlight the “Type” option and select Linux LVM.

Finish up the partitioning process by highlighting “Write”. Select “quit” to exit Cfdisk.

Set up LVM physical volume

Before we can create our LVM logical volumes and format them, we must create a physical LVM volume. Making LVM volumes on Linux is done with the pvcreate tool.

In the terminal, run the pvcreate command against the LVM partition on the drive you wish to use for LVM. In this example, the drive letter is /dev/sdb.

pvcreate /dev/sdb1

Running the pvcreate command overtop of the /dev/sdb1 partition creates a new LVM physical volume. We can confirm this by running:

lsblk

Create LVM volume group

Now that we have an LVM physical volume set up, the next step is to create an LVM volume group. To make a new VG, go to the terminal and run the following vgcreate command. Remember to replace /dev/sdb1 with your LVM partition.

Note: in this example our LVM volume group name is LVM1. If you do not like this name, feel free to change it to something else.

vgcreate -s 16M lvm1 /dev/sdb1

If the command above is successful, our LVM1 volume group is ready to work with.

Set up LVM logical volumes

Logical volumes are where all of the data is stored in an LVM. To create a new logical volume in your LVM, use the lvcreate command.

For example, to create a 2GB logical volume, you’d do:

lvcreate -L 2G -n lvm1a lvm1

Repeat this process to create as many volumes as you want. The basic syntax for creating logical volumes is:

lvcreate -L somenumberG -n logicalvolumename logicalvolumegroup

When you’re satisfied with the size of your new LVM logical partition, it’s time to format it. So, for example, to format our new 2GB partition with the “LVM1A” label.

First, CD into the /dev/mapper directory and locate the label of the logical partition we wish to format. In our example, it is /dev/mapper/lvm1-lvm1a.

cd /dev/mapper
ls

To format the volume, do the following command, as root:



mkfs.ext4 /dev/mapper/lvm1-lvm1a

Once formatting is complete, access your new LVM volume by doing the following commands with root privileges.

Note: if you make multiple volumes, replace lvm1-lvm1a with what you find in /dev/mapper.

mkdir /mnt/vfs/
mount /dev/mapper/lvm1-lvm1a /mnt/vfs/
cd /mnt/vfs/

Leave a comment