Contacts

Add a new disk in that LVM. What is LVM? And why is it needed? Adding physical volumes

On my home server Linux A 250 GB disc is installed. I just bought a new 250 GB SATA disk, and I want to add new Disc In my existing LVM volume to increase its size up to 500 GB. How to add a disc in LVM and expand the volume of LVM in operating system Linux?

Linux Tomas Management (LVM) creates an easy-to-use level of physical disks. You can combine multiple disks and create logical storage volumes. This ensures specific advantages, such as:

  1. No restrictions on the size of the disk;
  2. Enharged bandwidth disk
  3. Mirroring volumes for critical business data;
  4. Tom pictures;
  5. Lung backup and recovery using snapshots;
  6. Simple data movement;
  7. Changing the size of storage pools (add or delete discs) without mandatory reformatting of the discs.
In that tutorial It is shown how to make partitions, formatting and adding a new disk to that LVM in Linux. For a demonstration goal, I use Ubuntu VM, but the teams remain the same for "bare iron" or any other virtualization technology, such as KVM, XEN, VMWare, etc.

Attention: Be careful with LVM / MKFS.EXT4 and other commands, as well as with devices names, because If the device name is defined, it can destroy all the data. Be careful and always keep full backups.

Step 1 - find out information about existing LVM

LVM repository management is divided into three parts:

  1. Physical volumes (FT (PV)) - Actual (for example, / dev / sda, / dev, sdb, / dev / vdb, etc.)
  2. Tomov Groups (GT (VG)) - Physical volumes are combined into groups of volumes. (for example, my_vg \u003d / dev / sda + / dev / sdb.)
  3. Logic volumes (LT (LV)) - A group of volumes, 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, at present, my LVM includes a physical volume (actual disk), called / DEV / VDA5. To view detailed information About properties, enter:

$ sudo pvdisplay

Examples of possible data conclusions:

From the above output, it is clear that our volume group named Ubuntu-Box-1-Vg is made of physical volume with the name / dev / vda5.

How to display information about LVM Group Tomov (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 conclusions:

How to display LVM logical volume information (LV)

Enter any of the following LVS Command / Lvdisplay commands to view information about volume groups and their properties:

$ sudo lvdisplay

Examples of possible data conclusions:

My group of volumes Ubuntu-Box-1-Vg is divided into two logical volumes:

  1. / dev / ubuntu-box-1-vg / root - root file system;
  2. / Dev / Ubuntu-Box-1-Vg / swap_1 - Speed \u200b\u200bspace.
Based on the above commands, you can get a basic idea of \u200b\u200bhow LVM organizes a storage device 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 disk to your server. In this example, for a demonstration goal, I added a new disk that has a size of 5GIB. To find out information about the launch of new disks:

$ sudo fdisk -l

$ sudo fdisk -l | Grep "^ Disk / Dev /"

Examples of possible data conclusions:

Another option - scan all visible devices For lvm2:

$ sudo lvmdiskscan.

Examples of possible data conclusions:

/ 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 - Creating Physical Toms (PV) on a new disk called / Dev / VDB

Enter the following command:

$ sudo pvcreate / dev / vdb

Examples of possible data conclusions:

Physical Volume "/ Dev / VDB" SuccessFully Created

Now run the following command to check:

$ sudo lvmdiskscan -l

Examples of possible data conclusions:

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 recently created physical volume (PV) with the name / dev / vdb to an existing logic volume (LV)

Enter the following command to add physical volume / dev / vdb to the volume group "Ubuntu-Box-1-VG":

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

Examples of possible data conclusions:

The classic partitions that most often break the hard disk to install the system and storage, have a number of significant drawbacks. Their size is very difficult to change, they are in a strict sequence and just take a piece from the first partition and add to the latter will not work if there are still partitions between them. Therefore, very often, with the initial splitting of the hard drive, users break their heads - how much space is to be allocated for a particular section. And almost always in the process of using the system, they come to the conclusion that they did not correctly choose.

Solve most of these problems can technology LVM. It creates an additional abstraction - logical volumes that are visible in the system, as ordinary sections, but they are not. It has several advantages:

    LVM logical volumes are no longer tied to physical location. In the framework of LVM, there is no such concept as the order of logical volumes.

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

    If necessary, you can smear the logical volumes in several physical hard disks, thus increasing the available place. In this case, the system will still see only one logical volume, although its size will exceed the available dimensions hard disks. You can also conduct a reverse operation, deleting a hard disk from LVM, thus freeing it for other use.

    LVM supports snapshot mechanism - instant copies of the volume system volume. It can be very useful for creating backups.

    There are also the mass of pros, which can be read in specialized articles about LVM.

Ubuntu fully supports LVM, but from Desktop version installation disk Removed necessary to work with LVM utilities. In addition, the Desktop version installer does not know how to change the LVM volume. Therefore, if you want to use LVM, you will have to either put the system with the Alternate disk, or a little peeling with the usual LiveCD. Installation with Alternate is uncomfortable and many cause discomfort, and besides, most often the disk alternate is not at hand, so let's consider the option with LiveCD.

Note that the LVM management is carried out through the terminal, therefore, in order not to break anything, you need to first learn more or less comfortable with it. In addition, familiarize yourself with the principles of work and the main concepts of LVM, in order to understand what it is. The article is designed not for newcomers, but on those who have already figured out in the basics of Ubuntu.

Preliminary preparation

You need to run the system with LiveCD and connect the computer to the Internet. Next, open the terminal and install directly in the LiveCD session the necessary utilities command:

Sudo Apt-Get Install LVM2

All, now you can start working with LVM. But first it is necessary to highlight the place where we will create LVM. To do this, you will need the Gparted sections editor, which is in the System → Administration menu (System → Administration).

Due to the features of the GRUB loader, it is better when using LVM to make a separate small section for / boot. Say, 200MB should be enough.

Create a section and partition using a gparted / boot, on top of which you will deploy LVM. If you are ready to highlight the entire hard disk under the LVM, then do not forget that the / boot section of your Ubuntu should not be placed on LVM. Thus, you will need two sections - 200MB under / boot and everything else under LVM. Do not choose any file system for LVM partition - just empty section (unformatted). Do not forget to apply all the changes you entered through gparted you need to click on the green tick on the top panel or select the Apply item on the Edit menu.

After the changes in the markup will be successfully made, it will be necessary to change the type of section you need to Linux LVM.. To do this click right key On the section, and select "Manage Flags". In the window that opens, check the box in the field named lVM., Wait for the end of applying all changes and close Gparted. On this preparatory stage finished.

Creating logical volumes LVM

Now it's time to proceed to the immediate creating lvm.. For example, we assume that LVM we create on top of section / dev / sda1. In this case, you must first initialize the physical section of the command:

Sudo Pvcreate / Dev / SDA1 Sudo Vgcreate Local / Dev / SDA1

And finally, create the logical volume you need. It is worth noting that how to increase the size of the LVM volumes can be easily directly in the working system, it is best to allocate for logical volumes the minimum required size. Do not be afraid that the big volume will remain unallocated within the volume group, it will not disappear. As soon as you need an extra place, you can easily add it to any logical. But to reduce the size of the logical volume is much more difficult.

Usually, a root section is required to install the system, the section under / home, section under SWAP and sometimes - section under the data. Create logical volumes under all these four tasks can be 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

Parameter -N, if you have not yet understood, sets 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 file systems manually before installing. Otherwise, the installer will want on top of each LVM volume to create more and MBR table partitions, which is extremely undesirable.

You can do this as follows:

Sudo MKFS.EXT4 / DEV / Local / Root Sudo MKFS.EXT4 / DEV / Local / Home Sudo Mkswap -f / Dev / Local / Swap Sudo MKFS.EXT4 / DEV / Local / Data

Please note that LVM logical volume names in the system look like / dev / (group_name) / (set).

In addition, the Ext4 file system default reserves a part of the site for system data. Because on / home, and even more so on the section with user files, no system data will never be, it is better to cancel this reservation in order to release the place disappearing in vain. Commands will use

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 section, otherwise the system can stop working at all!

Well, finally you may want to assign a normal label to the section with the data that it is beautifully displayed in the installed system. You can do this about such a team:

Sudo Tune2FS -L DATA / DEV / Local / Data

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

System installation

Actually the installation itself is standard, but when you are offered to choose a way to split the disk, select manual mode. In the window that opens, you will see all the LVM volumes created by you. Specify for each corresponding mount point, however, do not put the formatting checkbox. For the data section, you can specify the mounting point / Media / Data. Do not forget about a small / boot section. He needs to specify the appropriate mount point and can be set as an FS EXT2, in addition it needs to be formatted.

Wait for the installation is completed, but computer Do not reboot!

If you restart the computer after installation, then yours new system will not start. In this case, you will need to go back to LiveCD again, install the work utilities with LVM, and then execute the Sudo VGChange -a Y command

After that, execute the instructions below.

Activation of LVM in the installed system

You set the system, but one remained little problem - In the installed Ubuntu, there is no work utilities from LVM, which means it simply will not start. It is easy enough to fix it.

To begin with, without leaving the LiveCD, mount the logical volume that you highlighted the root into the folder / MNT. Make it can be a team

Sudo Mount / Dev / Local / Root / MNT

Then to connect to the place / boot section (in the example below it / dev / sda1):

Sudo Mount / Dev / SDA1 / MNT / BOOT

Now it will be necessary to enter installed system With the help of Chroot, however, it is necessary to temporarily connect some important in it. system resources. To do this, execute commands

Sudo Mount --Bind / Dev / MNT / DEV Sudo Mount --Bind / Proc / MNT / Proc Sudo Mount --Bind / Sys / MNT / SYS

If suddenly you have in the installed system / varials or other system directories are located on different from the root sections, do not forget to mount them in / MNT.

Now go to the icon set

Sudo Chroot / MNT / BIN / BASH

And install the necessary utilities command

APT-Get Install LVM2

All installation is completed. Close the terminal, press Alt + Ctrl + Del and restart the computer. Do not forget to get a LiveCD drive. The computer must boot in the newly installed system.

Further work

Suppose, at some point you stopped enough 5 gigabytes that you allocated under Section / Home. No problem. Look at how much unused place left in the volume group team

Sudo VgDisplay Local

Now increase the size of the logical volume / Dev / Local / Home to the desired command

Sudo Lvresize -L 15G / DEV / Local / Home

Note that with this use in the -L parameter, the full desired size is indicated, and 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, it will remain only to stretch the file system to the entire new volume. Make it can be a team

Sudo Resize2FS / Dev / Local / Home

All, the size of the logical volume is increased.

In addition to increasing the size of logical volumes on the fly, LVM can still have much beneficial. For example, create instant snapshots. However, you read about all the intricacies of work with this technology in specialized articles.

Logical Volume Manager (LVM) is a very powerful data management system with data for Linux. It allows you to create on top of the physical partitions (or even unwanted hard drives) logical volumes, which in the system itself will be visible as conventional 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 the logical volumes can be easily changed right during operation. In addition, LVM supports the mechanism of snapshots, copying partitions "on the fly" and mirroring similar to RAID-1.

If planned big works With LVM, you can run a special "shell" by the Sudo LVM command. The Help command will show a list of commands.

Creation and deletion

As already noted, LVM is based on sections hard disk and / or whole hard drives. On each of the disks / partitions should be created physical volume (PHYSICAL VOLUME). For example, we use for LVM disk sDA and section sDB2.:

PVCREATE / DEV / SDA PVCREATE / DEV / SDB2

On these physical volumes create group Tomovwhich will be called, say vG1:

VGCREATE -S 32M VG1 / DEV / SDA / DEV / SDB2

We will see the information about our volume group:

VgDisplay VG1

Groups can be created several, each with its set of volumes. But usually it is not required.

Now in the volume group you can create logical Toma lV1. and lV2. The size of 20 GB and 30 GB appropriate:

LVCREATE -N LV1 -L 20G VG1 LVCREATE -N LV2 -L 30G VG1

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

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

MKFS.EXT4 / DEV / VG1 / LV1 mkfs.reiserfs / dev / vg1 / lv2

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

Adding physical volumes

To add a new hard drive sDC In 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 logic disk (lvcreate) or increase the size of the existing (lvresize).

Removal of physical volumes

To remove from the working group of volumes Winchester sDA First transfer all the data from it to other discs:

PVMOVE / DEV / SDA

Then remove it from the volume group:

VGREDUCE VG1 / DEV / SDA

And finally, delete physical volume:

PVREMOVE / DEV / SDA

Actually, the last team simply removes the mark that the disk is a member of LVM, and does not bring much benefit. After deleting from LVM, for further use, the disk will have to rear / reformat.

Size change

LVM makes it easy to change the size of logical volumes. To do this, first need to 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

Changing the size of the physical volume - the task is very complex and usually does not apply. It is more expedient and safer to remove the physical volume, resize the section and create volume again.

Just try

If LVM is not installed for further use, and "Consider", then the discs and partitions can be replaced by files. Never need additional discsnor virtual machines. We will create virtual drives and work with them. For example, you can create 4 disks of 1 GB, but you can create another amount of more or smaller size as you want. Create files themselves, imitating 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

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 ...

Snapshot

One of the most important features of LVM is the support of the snapshot mechanism. Snapshot allow you to do instantaneous A snapshot of a logical volume and use it in the future to work with the data.

Examples of using

LVM is actively used when the mechanism of snaps is needed. For example, this mechanism is extremely important at backup constantly changing files. LVM allows you to freeze a certain state of the FS and copy it all the necessary data from it, and you do not need to stop the record on the original FS.

Also snapshots can be applied to support support. file Server From the Samba of the mechanism of archival copies, about this in the appropriate article:

LVM with LiveCD.

If you have a need to work with LVM with LiveCD Ubuntu, you will have to perform several additional actions, because by default utilities to work with LVM.

First you need to install these utilities:

Sudo Vgscan Sudo VgDisplay Your_VGNAME

Sudo vgchange -a y

This command must report that all your logical volumes are activated. Now you can work with them in the usual way.

System Administration

What is LVM?

LVM is decrypted as Logical Volume Manager. I will not give official definitions, but I will tell you in short words. LVM is an additional abstraction of disk space. This level is between the file system and the physical disk. LVM is similar to Software RAID. In this abstraction itself there are 3 items: volume group (Volume Group, Sokr. VG), physical volume (Physical Volume, PV) and logical volume (Logical Volume, Sokr. LV). You can create several volume groups. In each group of volumes, add physical volumes. Physical volumes are disk sections. 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 I use LVM?

If you use LVM, you can resist the server service. You can create a lot of sections with different file systems, you can mount file systems with different flags (for example, prohibit execution of files), you can very quickly and simply expand the size of the partition if it ends the place. Of course, an additional layer between the disk and the file system reduces the speed of reading and writing. For everything you need to pay. I use LVM to conveniently manage disk space Virtual machines. Usually, as virtual disk Using a regular file. First, it is inconvenient, because in KVM there is no mechanism for removing the snapshots of the virtual disk (snapshots), and even a few gigabytes for a long time, and virtual Machine You have to stop. Secondly, if the virtual disk file is stored in file SystemWe will receive additional delays associated with reading and writing this file. Therefore, as a virtual disk I use LVM logical volumes.

Brief reference book

Create a group of volumes:
  1. vGCREATE VG_VIRT / DEV / SDA1 / DEV / SDB1
Initialization of physical volume:
  1. pVCREATE / DEV / SDA2
Adding a physical volume into a group of volumes:
  1. vGEXTEND VG_VIRT / DEV / SDA2
Creating a new logical volume of 10GB size:
  1. lVCREATE -L10G -N LV_UBUNTU_VM VG_VIRT
For logical volumes, you can set the names that carry a semantic load. This is much more convenient With the names of the SDXX type.
In order to increase the logical volume, you can specify the final volume size, and you can specify the size you want to increase 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 and file system itself.
  1. resize2FS / DEV / VG_VIRT / LV_UBUNTU_VM
Removal of logical volume:
  1. lVREMOVE / DEV / VG_VIRT / LV_UBUNTU_VM
Creating a picture (Snapshot) with logical volume:
  1. lVCREATE --Size 2G --Snapshot --Name Snapshot_ubuntu_vm / dev / vg_virt / lv_ubuntu_vm
Pictures of logical volumes are made very quickly, they are very comfortable. The snapshot is something like an additional layer that keeps all changes in the logical volume. The snapshot does not store files that have not been changed from the time you create a picture. Therefore, the size of the occupied place in the image snapshot depends on the number of changes. If you delete the volume from which the picture was taken, then the removed will also be deleted. And, of course, transactions with a snapshot of Tom is much slower than with the Tom.
And to create a copy logical disk, That is, it is completely cloned, you can use a simple DD utility.
  1. sudo dd if \u003d / dev / vgroup1 / lvolume1 of \u003d / dev / vgroup1 / lvolume_copy
Naturally, logical volumes must exist.

Did you like the article? Share it