Windows.  Viruses.  Notebooks.  Internet.  office.  Utilities.  Drivers

On my home server Linux installed disk on 250 GB. I just bought a new 250GB SATA drive and I want to add a new drive to my existing volume LVM to increase its size to 500 GB. How to add 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 disks 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 transfer;
  7. Resizing storage pools (adding or removing disks) without necessarily reformatting disks.
This tutorial shows you how to partition, format, and add a new drive to an LVM volume on Linux. For demo purpose, I'm 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 with device names, as if the device name is set incorrectly, it may destroy all data. Be careful and always keep full 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 grouped into volume groups. (e.g. 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 physical volumes:

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

$ sudo pvdisplay

Examples of possible data outputs:

It is clear from the above output 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)

Type 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)

Type any of the following lvs command / lvdisplay commands 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 - Swap space.
Based on the commands above, you can get a basic idea of ​​how LVM organizes storage into physical volumes (PVs), volume groups (VGs), and logical volumes (LVs):

Step 2 - Find out information about the new drive

You need to add a new drive to your server. In this example, for demonstration purposes, I have added a new drive that is 5GiB in size. To find out information about the launch of new discs:

$ sudo fdisk -l

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

Examples of possible data outputs:

Another option is to scan all 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 drive 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 a newly created physical volume (pv) named /dev/vdb to an existing logical volume (lv)

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

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

Examples of possible data outputs:

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 in the system itself will be visible as ordinary block devices with data (that is, as ordinary partitions). The main advantages of LVM are that, firstly, one logical volume group can be created over any number of physical partitions, and secondly, the size of logical volumes can be easily changed right during operation. In addition, LVM supports the snapshot mechanism, on-the-fly partition copying, and RAID-1-like mirroring.

If large work is planned with LVM, then you can run a special "shell" sudo command lvm . The help command will show a list of commands.

Create and delete

As already noted, LVM is built on the basis of 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 see the 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 the volume group you can create logical volumes lv1 And lv2 20 GB and 30 GB respectively:

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

We now have block devices /dev/vg1/lv1 And /dev/vg1/lv2.

It remains to create a file system on them. There are no differences with the usual sections:

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

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

Adding Physical Volumes

To add new hard drive sdc into a 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).

Deleting physical volumes

To remove a hard drive from a running volume group sda First, we will 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 just 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 makes it easy to 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 used. It is more reasonable and safer to remove the physical volume, resize the partition, and recreate the volume.

How easy it is to try

If LVM is installed not for further use, but “to look at”, 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 each, but you can create another number of larger or smaller sizes as you like. We create the files ourselves, simulating 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

Create loopback devices from 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 the support for the snapshot mechanism. Snapshots allow you to instantaneous a snapshot of the logical volume and use it later 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 a certain state of the file system and copy all the necessary data from it, while you do not need to stop recording on the original file system.

Snapshots can also be used to organize support file server With Samba mechanism of archival copies, about this in the corresponding article:

LVM with LiveCD

If you need to work with LVM from the 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 report that all of your logical volumes are activated. Now you can work with them in the usual way.

The classic sections into which it is most often broken HDD for system installation and data storage, I have a number of significant drawbacks. Their size is very difficult to change, they are in strict sequence and just taking a piece from the first section and adding to the last one will not work if there are more sections between them. Therefore, very often, during the initial partitioning of a hard drive, users puzzle over how much space to allocate for a particular 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 that are visible in the system as ordinary partitions, but they 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 logical volume order at all.

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

    If necessary, you can spread logical volumes over 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 do the opposite by removing the hard drive from LVM, thus freeing it up for other uses.

    LVM supports the snapshot mechanism - instant copies of the 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 Desktop version installation disk utilities necessary for working with LVM have been removed. In addition, the Desktop version installer does not know how to modify LVM volumes. So if you want to use LVM, then you will either have to 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 LiveCD option.

Please note that LVM is managed through the terminal, so in order not to break anything, you must first learn how to work more or less comfortably with it. Also, get familiar with the principles of operation and basic concepts of LVM in order to understand what it is. The article is not intended for beginners, but for those who have already figured out the basics of Ubuntu.

Preliminary preparation

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

sudo apt-get install lvm2

Everything, 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 (System → Administration).

Due to the features GRUB bootloader it's 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 over which you will deploy LVM. If you're willing to dedicate your entire hard drive to LVM, don't forget that your Ubuntu's /boot partition must be on a non-LVM partition. Thus, you will need two partitions - 200Mb under /boot and everything else under LVM. Don't choose any filesystem for the LVM partition - just an empty (unformatted) partition. Don't forget, to apply all the changes you made through Gparted, you need to click on the green check mark on the top bar or select Apply from the Edit menu.

After the changes to the markup are successfully made, you will need to change the type of the 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. At this preparatory stage is completed.

Creating LVM Logical Volumes

Now it's time to start creating LVM directly. For example, we will assume that we are creating LVM on top of the /dev/sda1 partition. In this case, you must first 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 it is easy to 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. As soon as 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 with 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 understood yet, sets the name of the logical volume, -L - its size.

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

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

You can do this with 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 the names of the LVM logical volumes on the system look like /dev/(volume_group_name)/(volume_name) .

In addition, the ext4 file system reserves some space for system data by default. 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

In no case do not cancel the reservation for the root partition, otherwise the system may stop working altogether!

Finally, you may want to give the data partition a proper label so that it displays nicely on the installed system. You can do this with the following command:

sudo tune2fs -L Data /dev/local/data

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

System installation

Actually the installation itself is standard, but when you are prompted to choose a method for partitioning the disk, select manual mode. In the window that opens, you will see all the LVM volumes you have created. Specify the appropriate mount point for each, but don't check the format box. For the data partition, you can specify the mount point /media/data . Don't forget the small /boot partition. He needs to specify the appropriate mount point and can be set as ext2 FS, in addition, he needs to be formatted.

Wait for the installation to complete, but don't restart your computer!

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

Then follow the instructions below.

Activating LVM on an Installed System

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

To get started, without exiting the LiveCD, mount the logical volume that you selected as the root to the /mnt folder. This can be done with the command

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 you suddenly have /var or other system directories on partitions other than the root partition in your newly installed system, 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

Everything, the 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 you no longer have enough 5 gigabytes that you allocated for the /home partition. No problem. See how much unused space is left in the volume group with the command

sudo vgdisplaylocal

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 total desired size, not an increment. The increment can be specified 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 the entire new volume. This can be done with the command

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 a lot of other useful things. For example, create instant snapshots. However, you can 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 describe 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 group of volumes (Volume Group, abbr. VG), a physical volume (Physical volume, abbr. PV) and a logical volume (Logical Volume, abbr. LV). You can create multiple volume groups. You need to 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 LVM be used?

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 using a regular file. Firstly, this is inconvenient, because KVM does not have a mechanism for taking instant snapshots of a virtual disk (snapshots), and copying even several gigabytes takes a long time, and virtual machine will have to stop. Second, 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 volume group:
  1. vgcreate vg_virt /dev/sda1 /dev/sdb1
Physical volume initialization:
  1. pvcreate /dev/sda2
Adding a physical volume to a volume group:
  1. vgextend vg_virt /dev/sda2
Create a new 10GB logical volume:
  1. lvcreate -L10G -n lv_ubuntu_vm vg_virt
Logical volumes can be given meaningful names. This is much more convenient for working with names like sdxx.
To grow a logical volume, you can specify the end 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
Deleting 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
Snapshots of logical volumes 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 any files that have not been modified since the snapshot was taken. Therefore, the size occupied place 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 drive, that is, clone it completely, you can use the simple dd utility.
  1. sudo dd if=/dev/vgroup1/lvolume1 of=/dev/vgroup1/lvolume_copy
Naturally, the logical volumes must exist.

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