Russian language Ubuntu documentation. Installing Ubuntu Desktop on LVM Lvm logical volume larger than disk size

System Administration

What is LVM?

LVM stands for Logical Volume Manager. I will not give official definitions, but I will tell you briefly 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, abbreviated VG), a physical volume (Physical volume, abbreviated PV) and a logical volume (Logical Volume, abbreviated LV). Multiple volume groups can be created. Physical volumes must be added 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 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, prohibit the execution of files), 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 slows down read and write speeds. You have to pay for everything. I use LVM to conveniently manage disk space for virtual machines. Usually, as virtual disk a regular file is used. Firstly, it is inconvenient, because in KVM there is no mechanism for taking snapshots of a virtual disk, and copying even a few gigabytes is long, and the virtual machine will have to be stopped. Second, if the virtual disk file is stored in the file system, then we will get additional delays associated with reading and writing this file. Therefore, I use LVM logical volumes as the virtual disk.

Quick Reference Commands

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
To create a new 10GB logical volume:
  1. lvcreate -L10G -n lv_ubuntu_vm vg_virt
Logical volumes can be named with 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
Taking 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 files that have not changed since the snapshot was taken. Therefore, the size of the occupied space on 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 with a snapshot of a volume are much slower than operations with 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 \u003d / dev / vgroup1 / lvolume1 of \u003d / dev / vgroup1 / lvolume_copy
Naturally, logical volumes must exist.

The classic sections into which it is most often divided hDD for system installation and data storage, I have a number of significant disadvantages. Their size is very difficult to change, they are in a strict sequence, and you simply cannot take a piece from the first section and add to the last one if there are more sections between them. Therefore, very often during the initial partitioning of the hard drive, users puzzle themselves - 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 that are visible in the system like regular partitions, but are not. It has several 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 at all.

    Logical volumes can be expanded on the fly, and unmounted volumes can also be easily reduced in size without leaving the system.

    If necessary, you can spread logical volumes across multiple 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 hard drives... You can do the opposite by removing the hard disk from LVM, thus freeing it up for other use.

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

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

Ubuntu fully supports LVM, however from the Desktop version installation disk Removed the utilities necessary for working with LVM. In addition, the Desktop installer cannot modify LVM volumes. Therefore, if you want to use LVM, then you 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 disc is not at hand, so let's consider the option with the LiveCD.

Please note that LVM is controlled through the terminal, therefore, in order not to break anything, you must first learn to work more or less comfortably with it. Also, familiarize yourself with how LVM works and basic concepts to understand what it is. This 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 the 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 need the Gparted Partition Editor, which is located in the System → Administration menu.

Due to the nature of the GRUB boot loader, it is best to make / boot a separate small partition when using LVM. Let's say 200Mb should be enough.

Use Gparted / boot to create a partition and partition over which you will deploy LVM. If you are ready to allocate an entire hard disk for LVM, then do not forget that your Ubuntu / boot partition should not be placed on LVM. Thus, you need two partitions - 200Mb under / boot and everything else under LVM. Do not select any file system for LVM partition - just an empty section (unformatted). Do not 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 the changes to the markup have been 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. This completes the preparatory stage.

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

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 amount 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. Reducing the size of a logical volume, however, is much more difficult.

Typically a system installation 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 option, if you haven't figured it out yet, is the name of the logical volume, -L is 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 installing. Otherwise, the installer will want to create an MBR partition table on top of each LVM volume, which is highly undesirable.

This can be done 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 LVM logical volume names on the system look like / dev / (volumegroup_name) / (volume_name).

In addition, the ext4 filesystem 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. For this, the commands come in handy

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!

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

sudo tune2fs -L Data / dev / local / data

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

System installation

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

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

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

Then follow the instructions below.

Activating LVM on the Installed System

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

To begin with, without leaving the LiveCD, mount the logical volume that you have allocated under 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, this is / dev / sda1):

sudo mount / dev / sda1 / mnt / boot

Now you will need to enter installed system using chroot, but first you need to temporarily connect some important system resources... 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 different from the root partitions, 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, the installation is complete. Close the terminal, press Alt + Ctrl + Del and restart your computer. Remember 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 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 parameter specifies the full desired size, not an 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 over 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 expanding the size of logical volumes on the fly, LVM can do a lot more. For example, create instant snapshots. However, read about all the intricacies of working with this technology in specialized articles.

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 unbroken hard drives), which in the system itself will be visible 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 snapshots, on-the-fly partition copying, and RAID-1-like mirroring.

If you are planning a lot of work with LVM, then you can start a special "shell" with the command sudo lvm. The help command will display a list of commands.

Creation and deletion

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

Pvcreate / dev / sda pvcreate / dev / sdb2

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

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

Let's see information about our volume group:

Vgdisplay vg1

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

Now in the volume group you can create logical volumes lv1 and lv2 sizes of 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 regular sections:

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

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

Adding physical volumes

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

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 we remove it from the volume group:

Vgreduce vg1 / dev / sda

Finally, let's remove the physical volume:

Pvremove / dev / sda

Actually, the last command just removes the flag that the disk is a member of lvm, and is not particularly useful. 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 filesystem on it:

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

Resizing a physical volume is a complex task and usually not applied. It is more expedient and safer to delete 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 "for a look", then disks and partitions can be replaced with files. Neither need additional drivesnor virtual machines... We will create virtual drives and work with them. For example, you can create 4 disks of 1 GB each, but you can create a different number of larger or smaller as you wish. We create the files ourselves that simulate devices:

Mkdir / mnt / sdc1 / lvm cd / mnt / sdc1 / lvm dd if \u003d / dev / zero of \u003d. / D01 count \u003d 1 bs \u003d 1G dd if \u003d / dev / zero of \u003d. / D02 count \u003d 1 bs \u003d 1G dd if \u003d / dev / zero of \u003d. / d03 count \u003d 1 bs \u003d 1G dd if \u003d / dev / zero of \u003d. / d04 count \u003d 1 bs \u003d 1G

We 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 its support for the snapshot mechanism. Snapshots allow you to make instantaneous a snapshot of a 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 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.

Also, snapshots can be used to organize support file server with the Samba backup mechanism, see the related 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 report that all of your logical volumes are up. Now you can work with them in the usual way.

On my home linux server a 250 GB disk is installed. I just bought a new 250GB SATA drive and I want to add new disc to my existing LVM volume to increase its size to 500GB. How to add disk to LVM and expand LVM's volume 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 disk bandwidth
  3. Mirroring volumes for critical business data;
  4. Volume snapshots;
  5. Lung backup and recovery using snapshots;
  6. Easy data movement;
  7. Resizing storage pools (adding or removing drives) without having to reformat the drives.
In that study guide shows you how to partition, format, and add a new disk to an LVM volume in Linux. For demo purposes, I am using Ubuntu VM, but the commands remain the same for bare metal or any other virtualization technology such as KVM, Xen, VMware, etc.

Attention: Be careful with lvm / mkfs.ext4 and other commands and device names as if the device name is specified incorrectly, it can 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)) - the actual (e.g. / dev / sda, / dev, sdb, / dev / vdb, etc.)
  2. Volume groups (GT (VG)) - physical volumes are grouped into volume groups. (for example my_vg \u003d / 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, enter:

$ sudo pvdisplay

Examples of possible data outputs:

It can be clearly seen 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)

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 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 filesystem;
  2. / dev / ubuntu-box-1-vg / swap_1 is the swap space.
Based on the commands above, you can get a basic idea of \u200b\u200bhow LVM organizes storage in physical volumes (PV), volume groups (VG), and logical volumes (LV):

Step 2 - Find out information about the new disk

You need to add a new drive to your server. In this example, for demo purpose, I have added a new drive that is 5GiB in size. To find out information about starting new disks:

$ 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 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 physical volume / dev / vdb to the "ubuntu-box-1-vg" volume group:

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

Examples of possible data outputs: