How to manage and use logical volumes to manage LVM in Ubuntu

Source: Internet
Author: User
Tags gparted
In our previous article, we introduced what is LVM and what can be done with LVM. Today we will introduce you to some major LVM management tools, this makes it easier for you to configure and scale the installation. As mentioned earlier, LVM is an abstraction layer between your operating system and your physical hard drive. This means that your physical hard drive and partition no longer depend on their hard drive and partition. What you see in your operating system is the hard drive and partition which can be composed of any number of independent hard disks or a software disk array.

In our previous article, we introduced what is LVM and what can be done with LVM. Today we will introduce you to some major LVM management tools, this makes it easier for you to configure and scale the installation.

As mentioned earlier, LVM is an abstraction layer between your operating system and your physical hard drive. This means that your physical hard drive and partition no longer depend on their hard drive and partition. What you see in your operating system is the hard drive and partition which can be composed of any number of independent hard disks or a software disk array.

To manage LVM, there are many available GUI tools, but to really understand what happens in LVM configuration, it is best to know some command line tools. This is especially useful when you manage LVM on a server or a release that does not provide GUI tools.

Most LVM commands are very similar to each other. Each available command starts with one of the following:

  • Physical Volume (Physical Volume) = pv
  • Volume Group (Volume Group) = vg
  • Logical Volume (Logical Volume) = lv

The physical volume command is used to add or delete a hard drive in a volume group. The volume group command is used to change the physical partition abstraction set displayed for your logical volume operations. The logical volume command displays the volume group in the form of a partition so that your operating system can use the specified space.

 

LVM memo list that can be downloaded

To help you understand the available commands for each prefix, we made a memo. We will introduce some commands in this article, but there are still many commands available but not introduced.

All commands in the list must be run as root, because the system-level settings of the entire machine are affected.

 

How to view the current LVM Information

The first thing you need to do is check your LVM settings. The s and display commands can be used with physical volume (pv), volume group (vg), and logical volume (lv), which is a good start point for finding the current settings.

The display command will format the output information, so it is easier to understand than the s command. For each command, you will see the name and pv/vg path. It will also provide information about idle and used space.

The most important information is the PV name and VG name. With these two pieces of information, we can continue with LVM settings.

 

Create a logical volume

Logical volume is the partition used by your operating system in LVM. To create a logical volume, you must first have a physical volume and a volume group. The following are all the commands required to create a new logical volume.

 

Create physical volume

We will start with a brand new hard disk without any partitions and information. First, find the disk you want to use. (/Dev/sda, sdb, etc)

Note: Remember to run all commands as root or add 'sudo' before the command '.

  1. fdisk -l

If your hard disk has never been formatted or partitioned before, you may see similar information in the fdisk output. This is completely normal, because we will create the required partition in the following steps.

Our new disk location is/dev/sdb. Let's use the fdisk command to create a new partition on the disk.

There are a lot of GUI tools that can create new partitions, including Gparted. But since we have opened the terminal, we will use the fdisk command to create the required partition.

Enter the following command in the terminal:

  1. fdisk /dev/sdb

This will enable you to enter a special fdisk prompt.

Enter the command in the specified order to create a primary partition that uses 100% space of the new hard disk and prepare for LVM. If you want to change the partition size or want multiple partitions, we recommend that you use GParted or use the fdisk command.

Warning the following steps will format your hard drive. Before proceeding to the following steps, make sure that your hard drive does not contain any useful information.

  • N = create a new partition
  • P = create primary Partition
  • 1 = become the first partition on the disk

Enter the enter key twice to accept the default first and last cylinder.

Use the following command to prepare the partitions used by LVM.

  • T = change partition type
  • 8e = changed to LVM partition type

Verify and write the information to the hard disk.

  • P = view partition settings so that you can view the partition settings before writing changes to the disk.
  • W = write change to disk

After running these commands, the fdisk prompt will exit and return to the terminal's bash prompt.

Enter pvcreate/dev/sdb1 to create an LVM physical volume in the created partition.

You may ask why we don't need to format partitions in a file system, so don't worry. This step is later.

 

Create a volume group

Now we have a specified partition and a created physical volume. We need to create a volume group. Fortunately, only one command is needed.

  1. vgcreate vgpool /dev/sdb1

Vgpool is the name of the newly created volume group. You can use any name you like, but it is recommended that the label start with vg so that you can realize this is a volume group when using it later.

 

Create logical volume

Logical volume to be used when creating LVM:

  1. lvcreate -L 3G-n lvstuff vgpool

-L command to specify the logical volume size, which is 3 GB in this case,-n command to specify the volume name. Specify the vgpool so that the lvcreate Command knows from which volume to obtain space.

 

Format and mount a logical volume

The last step is to format the new logical volume with a file system. If you need to select a Linux File System for help, please read if you select the most appropriate file system as needed.

  1. mkfs -t ext3 /dev/vgpool/lvstuff

Create a mount point and mount the volume to a place you can use.

  1. mkdir /mnt/stuff
  2. mount -t ext3 /dev/vgpool/lvstuff /mnt/stuff

 

Reset logical volume size

One advantage of logical volumes is that you can increase or decrease your storage physically without moving everything to a larger hard disk. In addition, you can add new hard disks and expand your volume group at the same time. Or if you have an unused hard disk, you can remove it from the volume group to reduce the logical volume.

Here there are three basic tools used to increase or decrease physical volumes, Volume groups, and logical volumes.

Note: Each of these commands must start with pv, vg, or lv, depending on your work object.

  • Resize-can compress or expand physical and logical volumes, but the volume group cannot
  • Extend-enables the volume group and logical volume to become larger but not smaller
  • Reduce-reduce the volume group and logical volume, but not the size.

Let's look at an example of how to add a new hard drive to the created logical volume "lvstuff.

 

Install and format the new hard drive

Follow the steps above to create a new partition and change the partition type to LVM (8e) to install a new hard drive. Run the pvcreate command to create a physical volume that the LVM can recognize.

 

Add new hard disk to volume group

To add a new hard disk to a volume group, you only need to know your new partition. In our example, It is/dev/sdc1 and the name of the volume group you want to add.

This adds a new physical volume to an existing volume group.

  1. vgextend vgpool /dev/sdc1

 

Extended logical volume

To adjust the logical volume size, we need to point out that the logical volume is extended by size rather than by devices. In our example, we will add an 8 GB hard drive to our 3 GB vgpool. You can use the lvextend or lvresize command to make the space available.

  1. lvextend -L8G /dev/vgpool/lvstuff

When this command works, you will find that it actually resets the logical volume size to 8 GB instead of the 8 GB we expect to add to an existing volume. To add the remaining 3 GB available, use the following command.

  1. lvextend -L+3G/dev/vgpool/lvstuff

Now our logical volume is 11 GB.

 

Extended File System

The logical volume is 11 GB, but the above file system is still 3 GB. To make the file system use the entire 11 GB available space, you need to use the resize2fs command. You only need to specify the resize2fs to 11 GB logical volume to help you complete the rest of the work.

  1. resize2fs /dev/vgpool/lvstuff

Note: If you are using a file system other than ext3/4, check the tool to resize your file system.

 

Compressing logical volumes

If you want to remove a hard drive from the volume group, follow the steps above to reverse the operation and replace it with the lvreduce or vgreduce command.

  1. Adjust the file system size (make sure that the file has been moved to the Safe Place of the hard drive)
  2. Reduce the logical volume (in addition to +, you can also use-to compress the size)
  3. Remove hard disk from volume group with vgreduce

 

Back up logical volumes

Snapshots are functions provided by some new advanced file systems, but ext3/4 file systems do not have the snapshot function. What's best about LVM snapshots is that your file system never goes offline. You can have any size you want without extra hard disk space.

When the LVM obtains a snapshot, there will be a "photo" that is exactly the same as the logical volume. This "photo" can be used for backup on different hard disks. When a backup is generated, any new information that needs to be added to the logical volume will be written to the disk as usual, but the changes will be tracked so that the original snapshot will never be damaged.

To create a snapshot, we need to create a logical volume with enough free space to save any new information that will be written to the logical volume during Backup. If the driver is not frequently written, you can use a small bucket. When the backup is complete, we only need to remove the temporary logical volume. The original logical volume will be the same as the original logical volume.

 

Create a new snapshot

Create a snapshot of lvstuff and use the lvcreate command with the-s mark.

  1. lvcreate -L512M -s -n lvstuffbackup /dev/vgpool/lvstuff

Here we create a logical volume of only 512 MB, because the hard disk is not actually used. 512 MB space will save any new data generated during Backup.

 

Attach a new snapshot

As before, we need to create a mount point and mount a new snapshot to copy the file.

  1. mkdir /mnt/lvstuffbackup
  2. mount /dev/vgpool/lvstuffbackup /mnt/lvstuffbackup

 

Copying snapshots and deleting logical volumes

All you need to do is copy all files from/mnt/lvstuffbackup/to an external hard disk or package all files to one file.

Note: tar-c creates an archive file.-f specifies the name and path of the archive file. To obtain help information about the tar command, enter man tar in the terminal.

  1. tar -cf /home/rothgar/Backup/lvstuff-ss /mnt/lvstuffbackup/

Remember that all files written to lvstuff during Backup will be tracked in the temporary logical volume we created earlier. Make sure that you have sufficient free space during Backup.

After the backup is complete, unmount the volume and remove the temporary snapshot.

  1. umount /mnt/lvstuffbackup
  2. lvremove /dev/vgpool/lvstuffbackup/

 

Delete logical volume

To delete a logical volume, you must first ensure that the volume has been detached, and then you can use the lvremove command to delete it. After deleting a logical volume, you can remove the volume group. After deleting the logical volume group, you can delete the physical volume.

This is all the commands for removing the volumes and groups we created.

  1. umount /mnt/lvstuff
  2. lvremove /dev/vgpool/lvstuff
  3. vgremove vgpool
  4. pvremove /dev/sdb1 /dev/sdc1

These include most of the knowledge you need to know about LVM. If you have any experience with these discussions, please share them in the comment box below.

  • Use LVM to create elastic disk storage-Part 1 http://www.linuxidc.com/Linux/2014-10/107697.htm
  • Extend/Contract LVM (Part 2) http://www.linuxidc.com/Linux/2014-10/107786.htm in Linux
  • Recording logical volume snapshot in LVM and restoring (part 3) http://www.linuxidc.com/Linux/2014-11/108867.htm
  • Set up a thin provisioning volume (part 4) http://www.linuxidc.com/Linux/2014-11/109757.htm in LVM
  • Managing multiple LVM disks with striped I/O (Part 5) http://www.linuxidc.com/Linux/2014-12/110532.htm

For more information about Ubuntu, see Ubuntu special page http://www.linuxidc.com/topicnews.aspx? Tid = 2

Via: http://www.howtogeek.com/howto/40702/how-to-manage-and-use-lvm-logical-volume-management-in-ubuntu/

Translator: ictlyh Proofreader: wxy

This article was originally translated by LCTT and launched with the Linux honor in China

This article permanently updates the link address: Http://www.linuxidc.com/Linux/2015-08/121117.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.