Windows.  Viruses.  Laptops.  Internet.  Office.  Utilities.  Drivers

Logical Volume Manager (LVM) is a very powerful data volume management system for Linux. It allows you to create logical volumes on top of physical partitions (or even unpartitioned hard drives), which will be visible in the system itself as ordinary block devices with data (i.e., as ordinary partitions). The main advantages of LVM are that, firstly, one group of logical volumes can be created on top of any number of physical partitions, and secondly, the size of logical volumes can be easily changed during operation. In addition, LVM supports a snapshot mechanism, on-the-fly copying of partitions, and mirroring similar to RAID-1.

If you plan to do a lot of work with LVM, you can launch a special “shell” with the sudo lvm command. The help command will show a list of commands.

Creation and deletion

As noted, LVM is built on partitions hard drive and/or whole hard drives. On each of the disks/partitions must be created physical volume(physical volume). For example, we use a disk for LVM sda and section sdb2:

Pvcreate /dev/sda pvcreate /dev/sdb2

On these physical volumes we create volume group, which will be called, say, vg1:

Vgcreate -s 32M vg1 /dev/sda /dev/sdb2

Let's look at information about our volume group:

Vgdisplay vg1

You can create several groups, each with its own set of volumes. But usually this is not required.

Now in a volume group you can create logical volumes lv1 And lv2 20 GB and 30 GB in size respectively:

Lvcreate -n lv1 -L 20G vg1 lvcreate -n lv2 -L 30G vg1

Now we have block devices /dev/vg1/lv1 And /dev/vg1/lv2.

All that remains is to create a file system on them. There are no differences here with regular sections:

Mkfs.ext4 /dev/vg1/lv1 mkfs.reiserfs /dev/vg1/lv2

Removing LVM (or individual parts of it, for example, logical volumes or volume groups) occurs in reverse order- first you need to unmount the partitions, then remove logical volumes (lvremove), after which you can remove volume groups (vgremove) and unnecessary physical volumes (pvremove).

Adding physical volumes

To add new hard drive sdc to the volume group, create a physical volume:

Pvcreate /dev/sdc

And add it to our group:

Vgextend vg1 /dev/sdc

Now you can create another logical disk (lvcreate) or increase the size of the existing one (lvresize).

Removing physical volumes

To remove a hard drive from a working volume group sda First, let's transfer all the data from it to other disks:

Pvmove /dev/sda

Then remove it from the volume group:

Vgreduce vg1 /dev/sda

And finally, delete the physical volume:

Pvremove /dev/sda

Actually, the last command simply removes the mark that the disk is a member of lvm, and does not bring much benefit. After removal from LVM, the disk will have to be repartitioned/reformatted for further use.

Resizing

LVM allows you to easily resize logical volumes. To do this, you must first change the logical volume itself:

Lvresize -L 40G vg1/lv2

and then the file system on it:

Resize2fs /dev/vg1/lv2 resize_reiserfs /dev/vg1/lv2

Resizing a physical volume is a very complex task and is not usually done. It is more practical and safer to delete the physical volume, resize the partition, and create the volume again.

How easy it is to try

If LVM is installed not for further use, but “for viewing,” then disks and partitions can be replaced with files. No additional disks or virtual machines are needed. We will create virtual drives and work with them. For example, you can create 4 disks of 1 GB, but you can create another number of larger or smaller sizes as you wish. We create the files ourselves that simulate devices:

Mkdir /mnt/sdc1/lvm cd /mnt/sdc1/lvm dd if=/dev/zero of=./d01 count=1 bs=1G dd if=/dev/zero of=./d02 count=1 bs=1G dd if=/dev/zero of=./d03 count=1 bs=1G dd if=/dev/zero of=./d04 count=1 bs=1G

We create loopback devices from the files:

Losetup -f --show ./d01 losetup -f --show ./d02 losetup -f --show ./d03 losetup -f --show ./d04

Pvcreate /dev/loop0 pvcreate /dev/loop1 pvcreate /dev/loop2 pvcreate /dev/loop3 vgcreate -s 32M vg /dev/loop0 /dev/loop1 /dev/loop2 /dev/loop3 lvcreate -n first -L 2G vg lvcreate -n second -L 400M vg ...

Snapshots

One of the most important features of LVM is its support for the snapshot mechanism. Snapshots allow you to make instantaneous take a snapshot of the logical volume and use it in the future to work with data.

Examples of using

LVM is actively used when a snapshot mechanism is needed. For example, this mechanism is extremely important when backing up constantly changing files. LVM allows you to freeze some state of the FS and copy all the necessary data from it, while you do not need to stop recording on the original FS.

Snapshots can also be used to organize support file server with Samba backup mechanism, more about this in the corresponding article:

LVM with LiveCD

If you need to work with LVM from an Ubuntu LiveCD, then you will have to perform a few additional steps, since there are no utilities for working with LVM by default.

First you need to install these utilities:

Sudo vgscan sudo vgdisplay YOUR_VGNAME

Sudo vgchange -a y

This command should indicate that all of your logical volumes are activated. Now you can work with them as usual.

Classic sections into which it is most often divided HDD for installing the system and storing data, I have a number of significant disadvantages. Their size is very difficult to change, they are in strict sequence and simply taking a piece from the first section and adding to the last will not work if there are more sections between them. Therefore, very often, when initially partitioning a hard drive, users rack their brains about how much space to allocate for this or that partition. And almost always, in the process of using the system, they come to the conclusion that they made the wrong choice.

LVM technology can solve most of these problems. It creates an additional abstraction - logical volumes, which are visible in the system as regular partitions, but are not. It has a number of advantages:

    LVM logical volumes are no longer tied to a physical location. Within LVM, there is no such thing as the order of logical volumes.

    The size of logical volumes can be increased on the fly, and mounted volumes can also be easily reduced in size without leaving the system.

    If necessary, you can spread logical volumes across several physical hard drives, thus increasing the available space. In this case, the system will still see only one logical volume, although its size will exceed the available sizes of hard drives. You can also perform the reverse operation by removing the hard drive from LVM, thus freeing it for other use.

    LVM supports a snapshot mechanism - instant copies of a volume's file system. This can be very useful for creating backups.

    There are many more advantages that you can read about in specialized articles about LVM.

Ubuntu fully supports LVM, however from the Desktop version installation disk The utilities necessary for working with LVM have been removed. In addition, the Desktop version installer cannot change LVM volumes. Therefore, if you want to use LVM, then you will have to either install the system from an Alternate disk, or cheat a little with a regular LiveCD. Installation with Alternate is inconvenient and causes discomfort for many, and besides, most often the Alternate disk is not at hand, so let's consider the option with LiveCD.

Please note that LVM is controlled through the terminal, therefore, in order not to break anything, you must first learn how to work more or less comfortably with it. Also, familiarize yourself with the operating principles and basic concepts of LVM to understand what it is. The article is not intended for beginners, but for those who have already understood the basics of Ubuntu.

Preliminary preparation

You need to start the system from the LiveCD and connect your computer to the Internet. Next, open a terminal and install the necessary utilities directly into the LiveCD session with the command:

sudo apt-get install lvm2

That's it, now you can start working with LVM. But first we need to allocate a place where we will create LVM. To do this, you will need the Gparted partition editor, which is located in the System→Administration menu.

Due to the features GRUB bootloader It is better to make a separate small partition for /boot when using LVM. Let's say 200Mb should be enough.

Create with Gparted / boot partition and the partition on top of which you will deploy LVM. If you are ready to allocate LVM whole hard drive, then do not forget that the /boot partition of your Ubuntu should not be placed on LVM. Thus, you will need two partitions - 200Mb for /boot and everything else for LVM. Do not select any file system for LVM partition- just an empty section (unformatted). Don’t forget, to apply all the changes you made through Gparted, you need to click on the green checkmark on the top panel or select Apply from the Edit menu.

After changes to the markup have been successfully made, you will need to change the type of section you need to Linux LVM. To do this, right-click on the section and select “Manage flags”. In the window that opens, check the box with the name lvm, wait until all changes are applied and close Gparted. This completes the preparatory stage.

Creating LVM Logical Volumes

Now it's time to start creating the LVM itself. For example, we will assume that we are creating LVM on top of the /dev/sda1 partition. In this case, you first need to initialize the physical partition with the command:

sudo pvcreate /dev/sda1 sudo vgcreate local /dev/sda1

And finally, create the logical volumes you need. It is worth noting here that since you can easily increase the size of LVM volumes right in a running system, it is best to allocate the minimum required size for logical volumes. Do not be afraid that a large volume will remain unallocated within the volume group; it will not be lost. Whenever you need additional space, you can easily add it to any logical volume. But reducing the size of a logical volume is much more difficult.

Typically, installing a system requires a root partition, a /home partition, a swap partition, and sometimes a data partition. You can create logical volumes for all these four tasks using the commands:

sudo lvcreate -L 7G -n root local sudo lvcreate -L 5G -n home local sudo lvcreate -L 3G -n swap local sudo lvcreate -L 10G -n data local

The -n parameter, if you haven’t already understood, specifies the name of the logical volume, -L is its size.

Now you need to create file systems on the created logical volumes.

In modern Ubuntu versions you need to create file systems manually before installation. Otherwise, the installer will want to create an MBR partition table on top of each LVM volume, which is extremely undesirable.

You can do this with approximately the following commands:

sudo mkfs.ext4 /dev/ local/ root sudo mkfs.ext4 /dev/ local/ home sudo mkswap -f /dev/ local/ swap sudo mkfs.ext4 /dev/ local/ data

Note that LVM logical volume names on the system look like /dev/(volume_group_name)/(volume_name) .

In addition, the ext4 file system by default reserves some space for system data. Since there will never be any system data on /home , and even more so on the partition with user files, it is better to cancel this reservation in order to free up wasted space. Commands are useful for this

sudo tune2fs -r 0 /dev/ local/ home sudo tune2fs -r 0 /dev/ local/ data

Do not under any circumstances cancel the reservation for the root partition, otherwise the system may stop working altogether!

And finally, you may want to assign a normal label to the data section so that it appears beautifully in the installed system. You can do this with something like this command:

sudo tune2fs -L Data /dev/local/data

Now we can proceed directly to installation on the logical volumes we created.

System installation

The installation itself is standard, but when you are asked to select a disk partitioning method, select manual mode. In the window that opens, you will see all the LVM volumes you created. Specify the appropriate mount point for each, but do not check the format box. For the data partition, you can specify the mount point /media/data . Don't forget about the small /boot partition. It needs to be specified with the appropriate mount point and can be set to ext2 as a file system; in addition, it needs to be formatted.

Wait for the installation to complete, but do not restart your computer!

If you restart your computer after installation, your new system won't start. In this case, you will need to go into the LiveCD again, install the LVM utilities, and then run sudo command vgchange -a y

Then follow the instructions below.

Activating LVM on an installed system

You have installed the system, but there is still one small problem - in installed Ubuntu there are no utilities for working with LVM, which means it simply won’t start. This is easy enough to fix.

To begin, without leaving the LiveCD, mount the logical volume that you allocated to the root in the /mnt folder. This can be done with a team

sudo mount /dev/local/root/mnt

Then mount the /boot partition in place (in the example below it is /dev/sda1):

sudo mount /dev/sda1 /mnt/boot

Now you will need to log in installed system using chroot, but first you need to temporarily connect some important system resources to it. To do this, run the commands

sudo mount --bind / dev / mnt/ dev sudo mount --bind / proc / mnt/ proc sudo mount --bind / sys / mnt/ sys

If suddenly in your newly installed system /var or other system directories are located on partitions other than the root, do not forget to mount them in their places in /mnt.

Now go to the installed system with the command

sudo chroot /mnt/bin/bash

And install the necessary utilities with the command

apt-get install lvm2

That's it, installation is complete. Close the terminal, press Alt + Ctrl + Del and restart your computer. Don't forget to remove the LiveCD from the drive. The computer should boot into the newly installed system.

Further work

Let's say at some point the 5 gigabytes that you allocated for the /home partition are no longer enough for you. No problem. See how much unused space is left in the volume group by the command

sudo vgdisplay local

Now increase the size of the logical volume /dev/local/home to the desired size with the command

sudo lvresize -L 15G /dev/local/home

Note that when used this way, the -L option specifies the full desired size, not its increment. The increment can be set using the “+” symbol:

sudo lvresize -L +5G /dev/local/home

After increasing the size of the logical volume, all that remains is to stretch the file system to cover the entire new volume. This can be done with a team

sudo resize2fs /dev/local/home

That's it, the size of the logical volume has been increased.

In addition to increasing the size of logical volumes on the fly, LVM can do many other useful things. For example, create instant snapshots. However, read about all the intricacies of working with this technology in specialized articles.

System Administration

What is LVM?

LVM stands for Logical Volume Manager. I will not give official definitions, but will briefly tell you in my own words. LVM is an additional layer of disk space abstraction. This level is located between the file system and the physical disk. LVM is similar to software RAID. In this very abstraction there are 3 elements: a volume group (Volume Group, abbreviated VG), a physical volume (abbr. PV) and a logical volume (Logical Volume, abbr. LV). You can create multiple volume groups. You must add physical volumes to each volume group. Physical volumes are disk partitions. After adding physical volumes, you can add logical volumes. And on logical volumes you can already create a file system. All this is very convenient, especially on the server.

How can you use LVM?

If you use LVM, you can simplify server maintenance. You can create many partitions with different file systems, you can mount file systems with different flags (for example, disable file execution), you can very quickly and easily expand the size of a partition if it runs out of space. Of course, the extra layer between the disk and the file system reduces read and write speeds. You have to pay for everything. I use LVM to conveniently manage disk space virtual machines. Usually, as virtual disk a regular file is used. Firstly, this is inconvenient, because KVM does not have a mechanism for taking snapshots of a virtual disk, and copying even a few gigabytes takes a long time, and virtual machine will have to stop. Secondly, if the virtual disk file is stored in file system, then we will get additional delays associated with reading and writing this file. Therefore, I use LVM logical volumes as a virtual disk.

Command Quick Reference

Create a volume group:
  1. vgcreate vg_virt /dev/sda1 /dev/sdb1
Initializing a physical volume:
  1. pvcreate /dev/sda2
Adding a physical volume to a volume group:
  1. vgextend vg_virt /dev/sda2
Creating a new 10GB logical volume:
  1. lvcreate -L10G -n lv_ubuntu_vm vg_virt
For logical volumes, you can specify names that carry meaning. This is much more convenient than working with names like sdxx.
To grow a logical volume, you can specify the final size of the volume, or you can specify the size by which you want to grow the volume.
  1. lvextend -L12G /dev/vg_virt/lv_ubuntu_vm
  2. lvextend -L+3G /dev/vg_virt/lv_ubuntu_vm
And, of course, after this operation you need to increase the size of the file system itself.
  1. resize2fs /dev/vg_virt/lv_ubuntu_vm
Removing a logical volume:
  1. lvremove /dev/vg_virt/lv_ubuntu_vm
Creating a snapshot from a logical volume:
  1. lvcreate --size 2G --snapshot --name snapshot_ubuntu_vm /dev/vg_virt/lv_ubuntu_vm
Logical volume snapshots are very fast and very convenient. A snapshot is something like an additional layer that stores all changes to a logical volume. The snapshot does not store files that have not changed since the snapshot was taken. Therefore the size occupied space in the volume snapshot depends on the number of changes. If you delete the volume from which the snapshot was taken, the snapshot will also be deleted. And, of course, operations on a volume snapshot are much slower than operations on the volume itself.
And to create a copy of a logical disk, that is, to clone it completely, you can use the simple dd utility.
  1. sudo dd if=/dev/vgroup1/lvolume1 of=/dev/vgroup1/lvolume_copy
Naturally, logical volumes must exist.

On my home server Linux installed 250 GB disk. I just bought a new 250GB SATA drive and I want to add the new drive to my existing volume LVM to increase its size to 500 GB. How to add a disk to LVM and expand LVM volume in operating system Linux?

Linux Volume Management (LVM) creates an easy-to-use layer on top of physical disks. You can combine multiple drives and create logical storage volumes. This provides specific benefits such as:

  1. No restrictions on disk size;
  2. Increased throughput disk
  3. Mirroring volumes for critical business data;
  4. Volume Snapshots;
  5. Lung backup and recovery using snapshots;
  6. Easy data movement;
  7. Resize storage pools (adding or removing disks) without necessarily reformatting the disks.
This tutorial shows you how to partition, format, and add a new disk to an LVM volume in Linux. For demonstration purpose I am using Ubuntu VM, but the commands remain the same for bare metal or any other virtualization technology like KVM, Xen, VMware, etc.

Attention: Be careful with lvm / mkfs.ext4 and other commands, as well as device names, because if the device name is set incorrectly, it may destroy all data. Be careful and always keep complete backups.

Step 1 – Find out information about existing LVMs

LVM storage management is divided into three parts:

  1. Physical volumes (FT(PV))– actual (for example, /dev/sda, /dev,sdb, /dev/vdb, etc.)
  2. Volume groups (GT(VG))– physical volumes are combined into volume groups. (for example, my_vg = /dev/sda + /dev/sdb .)
  3. Logical volumes (LT(LV))– the volume group, in turn, is divided into logical volumes (for example, my_vg is divided into my_vg/data, my_vg/backups, my_vg/home, my_vg/mysqldb, etc.)
Enter the following commands to find out information about each part.

How to display information about physical volumes (pv)

Enter the following pvs command to view information about the physical volumes:

So currently my LVM includes a physical volume (actual disk) called /dev/vda5 . To view detailed property information, enter:

$sudo pvdisplay

Examples of possible data outputs:

From the above output, we can clearly see that our volume group named ubuntu-box-1-vg is made from a physical volume named /dev/vda5.

How to display information about LVM volume group (vg)

Enter any of the following vgs /vgdisplay vgs commands to view information about volume groups and their properties:

$sudo vgdisplay

Examples of possible data outputs:

How to display information about LVM logical volume (lv)

Enter any of the following commands lvs command / lvdisplay to view information about volume groups and their properties:

$sudo lvdisplay

Examples of possible data outputs:

My ubuntu-box-1-vg volume group is split into two logical volumes:

  1. /dev/ubuntu-box-1-vg/root – root file system;
  2. /dev/ubuntu-box-1-vg/swap_1 – space for swapping.
Based on the above commands, you can get a basic idea of ​​how LVM organizes storage device into physical volumes (PV), volume groups (VG) and logical volumes (LV):

Step 2 – Find out information about the new drive

You need to add a new disk to your server. In this example, for demonstration purpose, I have added a new disk which is 5GiB in size. To find out information about launching new disks:

$ sudo fdisk –l

$ sudo fdisk -l | grep "^Disk /dev/"

Examples of possible data outputs:

Another option is to scan everything visible devices for LVM2:

$ sudo lvmdiskscan

Examples of possible data outputs:

/dev/ram0 [ 64.00 MiB] /dev/ubuntu-box-1-vg/root [ 37.49 GiB] /dev/ram1 [ 64.00 MiB] /dev/ubuntu-box-1-vg/swap_1 [ 2.00 GiB] /dev /vda1 [ 487.00 MiB] /dev/ram2 [ 64.00 MiB] /dev/ram3 [ 64.00 MiB] /dev/ram4 [ 64.00 MiB] /dev/ram5 [ 64.00 MiB] /dev/vda5 [ 39.52 GiB] LVM physical volume / dev/ram6 [ 64.00 MiB] /dev/ram7 [ 64.00 MiB] /dev/ram8 [ 64.00 MiB] /dev/ram9 [ 64.00 MiB] /dev/ram10 [ 64.00 MiB] /dev/ram11 [ 64.00 MiB] /dev/ ram12 [ 64.00 MiB] /dev/ram13 [ 64.00 MiB] /dev/ram14 [ 64.00 MiB] /dev/ram15 [ 64.00 MiB] /dev/vdb [ 5.00 GiB] 2 disks 18 partitions 0 LVM physical volume whole disks 1 LVM physical volume

Step 3 – Create physical volumes (pv) on a new disk called /dev/vdb

Enter the following command:

$ sudo pvcreate /dev/vdb

Examples of possible data outputs:

Physical volume "/dev/vdb" successfully created

Now run the following command to check:

$ sudo lvmdiskscan –l

Examples of possible data outputs:

WARNING: only considering LVM devices /dev/vda5 [ 39.52 GiB] LVM physical volume /dev/vdb [ 5.00 GiB] LVM physical volume 1 LVM physical volume whole disk 1 LVM physical volume

Step 4 – Adding the newly created physical volume (pv) named /dev/vdb to the existing logical volume (lv)

Enter the following command to add the /dev/vdb physical volume to the "ubuntu-box-1-vg" volume group:

$ sudo vgextend ubuntu-box-1-vg /dev/vdb

Examples of possible data outputs:

If you notice an error, select a piece of text and press Ctrl+Enter
SHARE: