Create and use LVM logical volumes in CentOS

Source: Internet
Author: User

Create and use LVM logical volumes in CentOS

Linux disk management: the basic concept of LVM logical volume and the operating principle of LVM. It describes in detail the basic concepts of Linux Dynamic Disk management LVM logical volume and the operating principle of LVM, including the four most important basic points of LVM (PE, PV, VG, and LV). This article will explain in detail how to create, use, and delete LVM logical volumes.

1. Create an LVM logical volume

In the previous article, we learned how to create our LVM. First, we need to format our physical hard disk into PV, add multiple pvs to the created VG, and then create the LV through VG. So our first step is to format our physical hard disk into a PV (physical volume)

① The pvcreate command is used to format a physical hard disk into a PV (physical volume ).

I have already virtualized 3 fast physical hard disks in advance. The size of each hard disk is 8 GB, which can be viewed through the fdisk-l command.

[root@xiaoluo ~]# fdisk -lDisk /dev/sda: 21.5 GB, 21474836480 bytes255 heads, 63 sectors/track, 2610 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00093d90   Device Boot      Start         End      Blocks   Id  System/dev/sda1               1         523     4194304   82  Linux swap / SolarisPartition 1 does not end on cylinder boundary./dev/sda2   *         523        2611    16776192   83  LinuxDisk /dev/sdb: 8589 MB, 8589934592 bytes255 heads, 63 sectors/track, 1044 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00000000Disk /dev/sdc: 8589 MB, 8589934592 bytes255 heads, 63 sectors/track, 1044 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00000000Disk /dev/sdd: 8589 MB, 8589934592 bytes255 heads, 63 sectors/track, 1044 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00000000

As shown in the figure above, we first format the two hard disks/dev/sdb and/dev/sdc into PV

[root@xiaoluo ~]# pvcreate /dev/sdb /dev/sdc   Physical volume "/dev/sdb" successfully created  Physical volume "/dev/sdc" successfully created

After the PV is created, we can use the pvdisplay (display details) and pvs commands to view the current pv information.

[root@xiaoluo ~]# pvdisplay  "/dev/sdb" is a new physical volume of "8.00 GiB"  --- NEW Physical volume ---  PV Name               /dev/sdb  VG Name                 PV Size               8.00 GiB  Allocatable           NO  PE Size               0     Total PE              0  Free PE               0  Allocated PE          0  PV UUID               93UEEl-cxBU-A4HC-LNSh-jp9G-uU5Q-EG8LM9     "/dev/sdc" is a new physical volume of "8.00 GiB"  --- NEW Physical volume ---  PV Name               /dev/sdc  VG Name                 PV Size               8.00 GiB  Allocatable           NO  PE Size               0     Total PE              0  Free PE               0  Allocated PE          0  PV UUID               lH1vul-KBHx-H2C6-wbt1-8AdK-yHpr-bBIul5   [root@xiaoluo ~]# pvs  PV         VG   Fmt  Attr PSize PFree  /dev/sdb        lvm2 a--  8.00g 8.00g  /dev/sdc        lvm2 a--  8.00g 8.00g

Through these two commands, we can see the created PV information. The two PVS are both 8 GB and are not used yet, And PFree is both 8 GB.

② Create a volume group (VG) and add PV to the volume group using the vgcreate command

After creating a PV, we need to create a VG and add all our PVS to this volume group. We need to name this volume group when creating the volume group.

[root@xiaoluo ~]# vgcreate xiaoluo /dev/sdb /dev/sdc   Volume group "xiaoluo" successfully created

Similarly, after creating the VG, we can use vgdisplay or vgs to view the VG information.

[Root @ xiaoluo ~] # Vgdisplay --- Volume group --- VG Name xiaoluo System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 1 VG Access read/write VG Status resizable max lv 0 Cur LV 0 Open LV 0 Max PV 0 cur PV 2 // currently there are two PVS, here are our/dev/sdb and/dev/sdc Act PV 2 VG Size 15.99 GiB // The current VG Size PE Size 4.00 MiB // we can also see our LVM the default PE size is 4 M Total PE 4094 // because the VG stores the PE in each PV, therefore, the number of PES is the VG Size divided by the default PE Size Alloc PE/Size 0 /0 Free PE/Size 4094/15 .99 GiB vg uuid B8eavI-21kD-Phnm-F1t1-eo4K-wgvg-T5qUbt [root @ xiaoluo ~] # Vgs VG # PV # LV # SN Attr VSize VFree xiaoluo 2 0 0 wz -- n-15.99g 15.99g

③ Use the lvcreate command to create a logical volume (LV) based on the volume group (VG)

Because the created PV and VG are the underlying things, we use logical volumes on the upper layer, so we need to create our logical volumes based on VG.

[root@xiaoluo ~]# lvcreate -n mylv -L 2G xiaoluo  Logical volume "mylv" created

Use the lvcreate command to create a logical volume based on VG. Its name is mylv and its size is 2 GB. You can also use the lvdisplay or lvs command to view the information of the created logical volume.

[Root @ xiaoluo ~] # Lvdisplay --- Logical volume --- LV Path/dev/xiaoluo/mylv // Path of the Logical volume LV Name mylv // Name of the Logical volume VG Name xiaoluo // Name of the volume group to which the Logical volume belongs name lv uuid PYuiYy-WpI6-XZB8-IhnQ-ANjM-lcz0-dlk4LR LV Write Access read/write LV Creation host, time xiaoluo, 23:45:08 + 0800 LV Status available # open 0 LV Size 2.00 GiB // Current LE 512 Segments 1 Allocation inherit Read ahead sectors auto-currently set to 2 56 Block device 253: 0 [root @ xiaoluo ~] # Lvs lv vg Attr LSize Pool Origin Data % Move Log Cpy % Sync Convert mylv xiaoluo-wi-a ---- 2.00g

In this way, our logical volume has been created. At this time, we can use the vgs and pvs commands to view the PV and VG information.

[Root @ xiaoluo mnt] # vgs VG # PV # LV # SN Attr VSize VFree xiaoluo 2 1 0 wz -- n-15.99g 13.99g // we can see that the number of LV changes 1, because we have just created an LV, LVFree also has 14G [root @ xiaoluo mnt] # pvs pv vg Fmt Attr PSize PFree/dev/sdb xiaoluo lvm2 a -- 8.00g 6.00g // the newly created LV is/ dev/sdb hard disk, therefore, the PFree disk has 6G/dev/sdc xiaoluo lvm2 a -- 8.00g 8.00g

We found that when each LV is created, the VG and PV information changes from time to time, and the size of the created LV is determined based on the size of the current VG, cannot exceed the remaining size of the current VG!

As we mentioned in the previous article, every time a logical volume is created, a folder named after this volume group will appear in the/dev directory, all logical volumes created based on the volume group are stored in this folder. Let's take a look.

[root@xiaoluo ~]# ls /dev/xiaoluo/mylv /dev/xiaoluo/mylv

Each time we create a new logical volume, such a device exists in the VG directory.

Ii. Format and use our logical volume

We have created PV, VG, and LV. If we want to use a logical volume, we must format it into the file system we need and mount it, then you can use the logical volume just like using a partition.

[root@xiaoluo ~]# mkfs.ext4 /dev/xiaoluo/mylv mke2fs 1.41.12 (17-May-2010)Filesystem label=OS type: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks131072 inodes, 524288 blocks26214 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=53687091216 block groups32768 blocks per group, 32768 fragments per group8192 inodes per groupSuperblock backups stored on blocks:     32768, 98304, 163840, 229376, 294912Writing inode tables: done                            Creating journal (16384 blocks): doneWriting superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 31 mounts or180 days, whichever comes first.  Use tune2fs -c or -i to override.

After formatting our logical volume, you can use the mount command to mount it. We can mount it to the/mnt directory.

[root@xiaoluo ~]# mount /dev/xiaoluo/mylv /mnt[root@xiaoluo ~]# mount/dev/sda2 on / type ext4 (rw)proc on /proc type proc (rw)sysfs on /sys type sysfs (rw)devpts on /dev/pts type devpts (rw,gid=5,mode=620)tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)/dev/mapper/xiaoluo-mylv on /mnt type ext4 (rw)[root@xiaoluo ~]# cd /mnt/[root@xiaoluo mnt]# lslost+found[root@xiaoluo mnt]# touch xiaoluo.txt[root@xiaoluo mnt]# lslost+found  xiaoluo.txt

We can see that our volume group has been mounted and can perform file operations on it just like using partitions.

Iii. Delete logical volumes

After creating a logical volume, we can create a file system and mount the logical volume to use it. If we don't want to use it, we can delete it.

[Note:] We strictly follow the sequence for creating physical volumes, Volume groups, and logical volumes. Similarly, logical volume deletion, volume group deletion, and physical volume deletion are strictly ordered.

① First uninstall the used logical volume and run the umount command.

② Use the lvremove command to delete the logical volume first

③ Use the vgremove command to delete a volume group

④ Delete our physical volume. Run the pvremove command.

[root@xiaoluo /]# mount /dev/xiaoluo/mylv /mnt/[root@xiaoluo /]# umount /mnt/[root@xiaoluo /]# lvremove /dev/xiaoluo/mylv Do you really want to remove active logical volume mylv? [y/n]: y  Logical volume "mylv" successfully removed[root@xiaoluo /]# vgremove xiaoluo  Volume group "xiaoluo" successfully removed[root@xiaoluo /]# pvremove /dev/sdb   Labels on physical volume "/dev/sdb" successfully wiped

At this time, the created logical volume mylv, volume group xiaoluo, and physical volume/dev/sdb have been deleted from our current operating system, run the lvs, vgs, and pvs commands.

[Root @ xiaoluo/] # lvs No volume groups found // The logical volume has No [root @ xiaoluo/] # vgs No volume groups found // The volume group has No [root @ xiaoluo/] # pvs pv vg Fmt Attr PSize PFree/dev/sdc lvm2 a -- 8.00g 8.00g // sdb physical volume is gone, only the sdc physical volume is left.

This article explains in detail how to create, use, and delete LVM logical volumes. In the next article, we will continue to explain the knowledge of LVM logical volumes, including how to dynamically stretch and compress logical volumes !!!


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.