In the last essay , CentOS for Linux learning (twenty-five)--Linux disk management: the basic concept of LVM logical volume and the working principle of LVM, it explained in detail the basic concept of Linux dynamic disk management LVM logical volume and LVM's The working principle, including the four most important basic points in LVM (PE, PV, VG and LV), this essay will explain in detail the creation, use and deletion of LVM logical volumes.
1. Create an LVM logical volume
Let's take a look at how to create our LVM through pictures and texts. In the last essay, we are already familiar with the working principle of LVM. First, we need to format our physical hard disk into PV, and then add multiple PVs to the creation. In the good VG, we finally create our LV through the VG. So our first step is to format our physical hard disk into PV (physical volume)
①Format the physical hard disk into PV (physical volume) using the pvcreate command
Here I have virtualized 3 physical hard disks in advance, and the size of each hard disk is 8G, which can be viewed through the fdisk -l command
[root@xiaoluo ~]# fdisk -l Disk /dev/sda: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00093d90 Device Boot Start End Blocks Id System /dev/sda1 1 523 4194304 82 Linux swap / Solaris Partition 1 does not end on cylinder boundary. /dev/sda2 * 523 2611 16776192 83 Linux Disk /dev/sdb: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Disk /dev/sdc: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Disk /dev/sdd: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000
Here we format the two hard disks /dev/sdb and /dev/sdc into PV according to the above figure.
[root@xiaoluo ~]# pvcreate /dev/sdb /dev/sdc
Physical volume "/dev/sdb" successfully created Physical volume "/dev/sdc" successfully created
After creating the PV, we can use the pvdisplay (show 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 PV information we have created. Both PVs are 8G, which are not used yet, and PFree is 8G.
②Create a volume group (VG) and add PV to the volume group through the vgcreate command
After creating the PV, we need to create a VG at this time, and then add our PVs to this volume group. When creating the volume group, we need to give the volume group a name
[root@xiaoluo ~]# vgcreate xiaoluo /dev/sdb /dev/sdc
Volume group "xiaoluo" successfully created
Similarly, after creating the VG, we can also use the vgdisplay or vgs command to view the information of the VG
[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 // There are currently two PVs here, our /dev/sdb and /dev/sdc Act PV 2 VG Size 15.99 GiB // current VG size PE Size 4.00 MiB // Through this we can also see that the default PE size of our LVM is 4M Total PE 4094 // Because the PE in each PV is stored in the VG, the number of PEs is the size of the VG divided by the size of the default PE 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
③ Create a logical volume (LV) based on a volume group (VG) through the lvcreate command
Because the created PV and VG are the underlying things, we use the logical volume in the upper layer, so we need to create our logical volume based on the VG.
[root@xiaoluo ~]# lvcreate -n mylv -L 2G xiaoluo
Logical volume "mylv" created
Create our logical volume based on VG through the lvcreate command. The name is mylv and the size is 2G. Similarly, we can 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 logical volume LV Name mylv // The name of the logical volume VG Name xiaoluo // The name of the volume group to which the logical volume belongs LV UUID PYuiYy-WpI6-XZB8-IhnQ-ANjM-lcz0-dlk4LR LV Write Access read/write LV Creation host, time xiaoluo, 2013-05-23 23:45:08 +0800 LV Status available # open 0 LV Size 2.00 GiB // size of the logical volume Current LE 512 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 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 will check the information of our PV and VG through the vgs and pvs commands.
[root@xiaoluo mnt]# vgs VG #PV #LV #SN Attr VSize VFree xiaoluo 2 1 0 wz--n- 15.99g 13.99g // We see that the number of LVs has now become 1, because we have just created a LV, and LVFree still 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 uses the /dev/sdb hard disk, so the PFree of this hard disk still has 6G left /dev/sdc xiaoluo lvm2 a-- 8.00g 8.00g
We found that every time we create an LV, the information of VG and PV is constantly changing, and the size of the LV we create is determined according to the size of the current VG, which cannot exceed the remaining size of the current VG!
As we mentioned in the previous essay, every time a logical volume is created, a folder named after the volume group will appear in the /dev directory. All logical volumes created based on the volume group are stored in this Below the folder, we can check
[root@xiaoluo ~]# ls /dev/xiaoluo/mylv
/dev/xiaoluo/mylv
Every time we create a new logical volume, such a device will be added to the VG directory.
2. Format and use our logical volume
We have created our PV, VG and LV. At this time, if we want to use a logical volume, we must format it into the file system we need to use, mount it, and then we can use it as a partition. Logical volume is used
[root@xiaoluo ~]# mkfs.ext4 /dev/xiaoluo/mylv
mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 131072 inodes, 524288 blocks 26214 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=536870912 16 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912 Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 31 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
After formatting our logical volume, we can use the mount command to mount it, we will 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]# ls lost+found [root@xiaoluo mnt]# touch xiaoluo.txt [root@xiaoluo mnt]# ls lost+found xiaoluo.txt
We see that our volume group is mounted and can be used for file operations just like partitions.
3. Delete the logical volume
After we create a logical volume, we can use it by creating a file system and mounting the logical volume. If we don't want to use it, we can delete it.
[Note:] We have strict order for creating physical volumes, creating volume groups and creating logical volumes. Similarly, there are strict order requirements for deleting logical volumes, deleting volume groups and deleting physical volumes.
① First unmount the logical volume in use through the umount command
②Delete the logical volume first through the lvremove command
③ Delete the volume group through the vgremove command
④ Finally, delete our physical volume through 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 point, our newly created logical volume mylv, volume group xiaoluo and physical volume /dev/sdb have been deleted from our current operating system. You can check it through the lvs, vgs, and pvs commands.
[root@xiaoluo /]# lvs No volume groups found // The logical volume is gone
[root@xiaoluo /]# vgs No volume groups found // The volume group is also gone
[root@xiaoluo /]# pvs PV VG Fmt Attr PSize PFree /dev/sdc lvm2 a-- 8.00g 8.00g // The sdb physical volume is gone, only the sdc physical volume is left