Brief LinuxLVM tutorial

Source: Internet
Author: User
Tags hex code
Logical Volume management LVM is a versatile hard disk system tool. Both Linux and other similar systems are very easy to use. It is very troublesome to adjust the size of a traditional partition by using a fixed size partition. However, LVM can create and manage logical volumes, rather than directly using physical hard drives.

Logical Volume management LVM is a versatile hard disk system tool. Both Linux and other similar systems are very easy to use. It is very troublesome to adjust the size of a traditional partition by using a fixed size partition. However, LVM can create and manage "logical" volumes, rather than directly using physical hard disks. The administrator can elastically scale up and down the management logic volume, and the operation is simple without damaging the stored data. You can add new hard disks to the LVM at will to directly expand existing logical volumes. The LVM does not need to be restarted to let the kernel know the existence of the partition.

LVM uses a layered structure, as shown in.

At the top of the figure, the actual physical disk, its partition, and its physical volume (PV) are used ). One or more physical volumes can be used to create a volume Group (VG ). Then, you can create logical volumes (LV) based on the volume group ). As long as there is space available in the volume group, you can create logical volumes as you like. The file system is created on a logical volume and can be mounted and accessed in the operating system.

LVM test description

This article describes how to create and manage LVM volumes in linux. We will divide it into two parts. First, create multiple logical volumes on one hard disk and mount them to the/lvm-mount directory. Then we will resize the created volume. In the second part, we will add additional volumes from the other hard disk to the LVM.

Prepare disk partitions

Create a disk partition by using fdisk. We need to create three 1G partitions. Note that the partition size is not required to be consistent. Similarly, partitions must use the '8e' type to make them available for LVM.

 
 
  1. # fdisk /dev/sdb
 
 
  1. Command (m for help): n # Create
  2. Command action
  3. E extended
  4. P primary partition (1-4)
  5. P # primary partition
  6.  
  7. Partition number (1-4): 1 # Partition number
  8. First cylinder (1-1044, default 1): # default 1 for vehicle return
  9. Last cylinder, + cylinders or + size {K, M, G} (1-1044, default 1044): + 1G # size
  10.  
  11. Command (m for help): t # change the type
  12. Selected partition 1
  13. Hex code (type L to list codes): 8e # LVM partition code
  14. Changed system type of partition 1 to 8e (Linux LVM)

Repeat the preceding operation to create the other two partitions. After the partition is created, we should have output similar to the following:

 
 
  1. # fdisk -l
 
 
  1. Device Boot Start End Blocks Id System
  2. /dev/sdb1 1 132 1060258+ 8e Linux LVM
  3. /dev/sdb2 133 264 1060290 8e Linux LVM
  4. /dev/sdb3 265 396 1060290 8e Linux LVM

Prepare physical volume (PV)

The created partition is used to store physical volumes. LVM can use physical volumes of different sizes.

 
 
  1. # pvcreate /dev/sdb1
  2. # pvcreate /dev/sdb2
  3. # pvcreate /dev/sdb3

Run the following command to check the creation of a physical volume. The following part of the output is truncated. "/Dev/sdb2" is a new physical volume of "1.01 GiB.

 
 
  1. # pvdisplay
 
 
  1. --- NEW Physical volume ---
  2. PV Name /dev/sdb2
  3. VG Name
  4. PV Size 1.01 GiB
  5. Allocatable NO
  6. PE Size 0
  7. Total PE 0
  8. Free PE 0
  9. Allocated PE 0
  10. PV UUID jszvzz-ENA2-g5Pd-irhV-T9wi-ZfA3-0xo092

Use the following command to delete a physical volume.

 
 
  1. # pvremove /dev/sdb1

Prepare a volume Group (VG)

Run the following command to create a volume group named 'volume-group1' and use/dev/sdb1,/dev/sdb2 and/dev/sdb3.

 
 
  1. # vgcreate volume-group1 /dev/sdb1 /dev/sdb2 /dev/sdb3

Use the following command to verify the volume group.

 
 
  1. # vgdisplay
 
 
  1. --- Volume group ---
  2. VG Name volume-group1
  3. System ID
  4. Format lvm2
  5. Metadata Areas 3
  6. Metadata Sequence No 1
  7. VG Access read/write
  8. VG Status resizable
  9. MAX LV 0
  10. Cur LV 0
  11. Open LV 0
  12. Max PV 0
  13. Cur PV 3
  14. Act PV 3
  15. VG Size 3.02 GiB
  16. PE Size 4.00 MiB
  17. Total PE 774
  18. Alloc PE / Size 0 / 0
  19. Free PE / Size 774 / 3.02 GiB
  20. VG UUID bwd2pS-fkAz-lGVZ-qc7C-TaKv-fFUC-IzGNBK

From the output, we can see the volume group usage/total. The physical volume provides space for the volume group. As long as there is space available in this volume group, we can create logical volumes at will.

Use the following command to delete a volume group.

 
 
  1. # vgremove volume-group1

Create logical volume (LV)

The following command creates a logical volume named '1v1 'with a size of MB. We use small partitions to reduce execution time. This logical volume uses the space of the previously created volume group.

 
 
  1. # lvcreate -L 100M -n lv1 volume-group1

You can run the lvdisplay command to view logical volumes.

 
 
  1. # lvdisplay
 
 
  1. --- Logical volume ---
  2. LV Name /dev/volume-group1/lv1
  3. VG Name volume-group1
  4. LV UUID YNQ1aa-QVt1-hEj6-ArJX-I1Q4-y1h1-OFEtlW
  5. LV Write Access read/write
  6. LV Status available
  7. # open 0
  8. LV Size 100.00 MiB
  9. Current LE 25
  10. Segments 1
  11. Allocation inherit
  12. Read ahead sectors auto
  13. - currently set to 256
  14. Block device 253:2

Now that the logical volume is ready, we can format and mount the logical volume, just like other ext2/3/4 partitions!

 
 
  1. # mkfs.ext4 /dev/volume-group1/lv1
  2. # mkdir /lvm-mount
  3. # mount /dev/volume-group1/lv1 /lvm-mount/

Once the logical volume is mounted, we can read and write it to the mount point/lvm-mount. To create and Mount other logical volumes, repeat this process.

Finally, we can use lvremove to delete the logical volume.

 
 
  1. # umount /lvm-mount/
  2. # lvremove /dev/volume-group1/lv1

Expand an LVM volume

The most useful feature of LVM is to adjust the logical volume size. This section describes how to expand an existing logical volume. Next, we will expand the logical volume 'lv1' created earlier to 200 MB.

Note: After adjusting the logical volume size, you also need to adjust the file system size to match. This extra step varies depending on the type of the file system to be created. In this article, we use 'lv1' to create an ext4 file system, so the operation here is for the ext4 file system. (Ext2/3 file systems are also similar ). The command execution sequence is very important.

First, we unmount the lv1 volume.

 
 
  1. # umount /lvm-mount/

Then, set the volume size to 200 MB.

 
 
  1. # lvresize -L 200M /dev/volume-group1/lv1

Next, check for disk errors

 
 
  1. # e2fsck -f /dev/volume-group1/lv1

After you run the following command to expand the file system, the ext4 information is updated.

 
 
  1. # resize2fs /dev/volume-group1/lv1

Now, the logical volume has been expanded to MB. We check the LV status for verification.

 
 
  1. # lvdisplay
 
 
  1. --- Logical volume ---
  2. LV Name /dev/volume-group1/lv1
  3. VG Name volume-group1
  4. LV UUID 9RtmMY-0RIZ-Dq40-ySjU-vmrj-f1es-7rXBwa
  5. LV Write Access read/write
  6. LV Status available
  7. # open 0
  8. LV Size 200.00 MiB
  9. Current LE 50
  10. Segments 1
  11. Allocation inherit
  12. Read ahead sectors auto
  13. - currently set to 256
  14. Block device 253:2

Now, this logical volume can be mounted again, and this method can also be used for other partitions.

Scale down an LVM volume

This section describes how to reduce the LVM volume size. Command Order is equally important. In addition, the following commands are equally valid for the ext2/3/4 file system.

Note: If the size of the logical volume is smaller than the size of the stored data, the data stored in the logical volume will be lost.

First, uninstall the volume.

 
 
  1. # umount /dev/volume-group1/lv1

Then, check for disk errors.

 
 
  1. # e2fsck -f /dev/volume-group1/lv1

Next, narrow down the file system and update the ext4 information.

 
 
  1. # resize2fs /dev/volume-group1/lv1 100M

After completion, reduce the logical volume size

 
 
  1. # lvresize -L 100M /dev/volume-group1/lv1

WARNING: indexing active logical volume to 100.00 MiB this may destroy your data (filesystem etc.) Do you really want to reduce lv1? [Y/n]: y Cing logical volume lv1 to 100.00 MiB Logical volume lv1 successfully resized

Finally, verify the adjusted logical volume size.

 
 
  1. # lvdisplay
 
 
  1. --- Logical volume ---
  2. LV Name /dev/volume-group1/lv1
  3. VG Name volume-group1
  4. LV UUID 9RtmMY-0RIZ-Dq40-ySjU-vmrj-f1es-7rXBwa
  5. LV Write Access read/write
  6. LV Status available
  7. # open 0
  8. LV Size 100.00 MiB
  9. Current LE 25
  10. Segments 1
  11. Allocation inherit
  12. Read ahead sectors auto
  13. - currently set to 256
  14. Block device 253:2

 

Expand a volume Group

This section describes how to extend a volume group by adding a physical volume to the volume group. Let's assume that our volume group 'volume-group1' is full and needs to be expanded. The hard drive (sdb) has no other free partitions. we have added another hard drive (sdc ). We will see how to add the sdc partition to the volume group for expansion.

Detect current volume group status

 
 
  1. # vgdisplay volume-group1
 
 
  1. --- Volume group ---
  2. VG Name volume-group1
  3. System ID
  4. Format lvm2
  5. Metadata Areas 3
  6. Metadata Sequence No 8
  7. VG Access read/write
  8. VG Status resizable
  9. MAX LV 0
  10. Cur LV 1
  11. Open LV 0
  12. Max PV 0
  13. Cur PV 3
  14. Act PV 3
  15. VG Size 3.02 GiB
  16. PE Size 4.00 MiB
  17. Total PE 774
  18. Alloc PE / Size 25 / 100.00 MiB
  19. Free PE / Size 749 / 2.93 GiB
  20. VG UUID bwd2pS-fkAz-lGVZ-qc7C-TaKv-fFUC-IzGNBK

First, create a 2 GB partition sdc1 with the type of LVM (8e), as described earlier in the tutorial.

 
 
  1. # fdisk /dev/sdc
 
 
  1. Command (m for help): n
  2. Command action
  3. e extended
  4. p primary partition (1-4)
  5. p
  6. Partition number (1-4): 1
  7. First cylinder (1-1044, default 1):
  8. Using default value 1
  9. Last cylinder, +cylinders or +size{K,M,G} (1-1044, default 1044): +2G
  10.  
  11. Command (m for help): t
  12. Selected partition 1
  13. Hex code (type L to list codes): 8e
  14. Changed system type of partition 1 to 8e (Linux LVM)
  15.  
  16. Command (m for help): w
  17. The partition table has been altered!

Then, we create a physical volume/dev/sdc1

 
 
  1. # pvcreate /dev/sdc1

Now that the physical volume is ready, we can simply add it to the existing volume group 'volume-group1.

 
 
  1. # vgextend volume-group1 /dev/sdc1

Use vgdisplay for verification (you can see that the volume group size has increased ).

 
 
  1. # vgdisplay
 
 
  1. --- Volume group ---
  2. VG Name volume-group1
  3. System ID
  4. Format lvm2
  5. Metadata Areas 4
  6. Metadata Sequence No 9
  7. VG Access read/write
  8. VG Status resizable
  9. MAX LV 0
  10. Cur LV 1
  11. Open LV 0
  12. Max PV 0
  13. Cur PV 4
  14. Act PV 4
  15. VG Size 5.03 GiB
  16. PE Size 4.00 MiB
  17. Total PE 1287
  18. Alloc PE / Size 25 / 100.00 MiB
  19. Free PE / Size 1262 / 4.93 GiB
  20. VG UUID bwd2pS-fkAz-lGVZ-qc7C-TaKv-fFUC-IzGNBK

Note that although we use a separate disk for demonstration, as long as it is an '8e' type disk partition, it can be used to expand the volume group.

To sum up, LVM is a powerful tool for creating and managing variable-size partitions. This article describes how to create and use dynamic partitions in LVM. We also introduced how to expand/contract logical volumes and volume groups, and how to add a new disk to LVM.

Hope to help you.

Related Article

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.