Disk management - logical volumes
introduce
Introduction to logical volumes
-
Logical volume manager is a mechanism used by Linux system to manage hard disk partitions
-
The original intention is to solve the defect that the hard disk device is not easy to modify the partition size after creating the partition. Although it is theoretically feasible to forcibly expand or shrink the traditional hard disk partition, it may cause data loss.
-
An abstraction layer based on physical storage devices, which has the advantage of flexible management
-
characteristic:
- Dynamic online capacity expansion (key)
- Offline clipping
- Data striping
- data mirroring
working principle
-
Physical Volume: Physical Volume (PV), which is at the bottom of LVM, can be understood as physical hard disk, hard disk partition or RAID disk array
-
Volume group: Volume Group (VG) is based on physical volumes. A volume group can contain multiple physical volumes, and new physical volumes can be added to the volume group after it is created. An LVM system can have only one volume group or multiple volume groups
-
Logical Volume: Logical Volume (LV). The Logical Volume is built on the volume group, and the Logical Volume can dynamically expand or reduce the space after it is built. It is a logical device used by the end user
-
Physical area: physical extent (PE). A physical volume can be divided into multiple PES. PE with unique number is the smallest unit that can be addressed by LVM. The size of PE can be specified, and the default is 4 MB. Once the PE size is determined, it cannot be changed. The PE size of all physical volumes in the same volume group is the same.
-
Logical area: logical extent (LE). A logical volume can be divided into multiple les, which is also the smallest unit addressed by LVM. In the same volume group, the size of Le and PE are the same and correspond to each other one by one.
Logical volume management
Common commands
Functions / commands | Physical volume management | Volume group management | Logical volume management |
---|---|---|---|
scanning | pvscan | vgscan | lvscan |
establish | pvcreate | vgcreate | lvcreate |
display | pvs | vgs | lvs |
Detailed display | pvdisplay | vgdisplay | lvdisplay |
delete | pvremove | vgremove | lvremove |
extend | vgextend | lvextend | |
narrow | vgreduce | lvreduce |
Logical volume creation
Physical device - > physical volume (pv) - > volume group (vg) - > logical volume (lv) - > logical volume format - > mount
Physical device: hard disk partition [root@server1 ~]# fdisk /dev/sdb [root@server1 ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 1G 0 part /boot └─sda2 8:2 0 19G 0 part ├─centos-root 253:0 0 17G 0 lvm / └─centos-swap 253:1 0 2G 0 lvm [SWAP] sdb 8:16 0 5G 0 disk ├─sdb1 8:17 0 1G 0 part └─sdb2 8:18 0 2G 0 part sr0 11:0 1 973M 0 rom [root@server1 ~]# partprobe /dev/sdb Create physical volume [root@server1 ~]# pvcreate /dev/sdb1 /dev/sdb2 Physical volume "/dev/sdb1" successfully created. Physical volume "/dev/sdb2" successfully created. [root@server1 ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 centos lvm2 a-- <19.00g 0 /dev/sdb1 lvm2 --- 1.00g 1.00g /dev/sdb2 lvm2 --- 2.00g 2.00g [root@server1 ~]# pvdisplay /dev/sdb* "/dev/sdb2" is a new physical volume of "2.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb2 VG Name PV Size 2.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID ogYeQh-kmXp-SguX-IE5I-VY1T-HFSE-mgVzh2 "/dev/sdb1" is a new physical volume of "1.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb1 VG Name PV Size 1.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID O0FhDl-4USu-DBGD-skhu-zFVA-rEiZ-JVC3T3 Create volume group [root@server1 ~]# vgcreate vg01 /dev/sdb1 /dev/sdb2 Volume group "vg01" successfully created [root@server1 ~]# vgs VG #PV #LV #SN Attr VSize VFree centos 1 2 0 wz--n- <19.00g 0 vg01 2 0 0 wz--n- 2.99g 2.99g Create logical volume [root@server1 ~]# lvcreate -n lv01 -L 2.5G vg01 Option Description: -n: Specifies the logical volume name -L: Specify the logical volume size directly -l: Indirectly specify the logical volume size (plus) PE number/Volume group free space( VFree)Percentage) [root@server1 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert root centos -wi-ao---- <17.00g swap centos -wi-ao---- 2.00g lv01 vg01 -wi-a----- 2.50g View mapping relationships [root@server1 ~]# ll /dev/vg01/lv01 lrwxrwxrwx 1 root root 7 8 December 21:17 /dev/vg01/lv01 -> ../dm-2 [root@server1 ~]# ll /dev/mapper/vg01-lv01 lrwxrwxrwx 1 root root 7 8 December 21:17 /dev/mapper/vg01-lv01 -> ../dm-2 format [root@server1 ~]# mkfs.ext4 /dev/vg01/lv01 [root@server1 ~]# blkid /dev/vg01/lv01 /dev/vg01/lv01: UUID="acc1fc83-a64c-4d60-9f32-955169625ac1" TYPE="ext4" mount [root@server1 ~]# mkdir /mnt/lv01 [root@server1 ~]# mount /dev/vg01/lv01 /mnt/lv01/ [root@server1 ~]# df -Th file system type Capacity used available used% Mount point devtmpfs devtmpfs 899M 0 899M 0% /dev tmpfs tmpfs 910M 0 910M 0% /dev/shm tmpfs tmpfs 910M 9.6M 901M 2% /run tmpfs tmpfs 910M 0 910M 0% /sys/fs/cgroup /dev/mapper/centos-root xfs 17G 1.9G 16G 11% / /dev/sda1 xfs 1014M 194M 821M 20% /boot tmpfs tmpfs 182M 0 182M 0% /run/user/0 /dev/mapper/vg01-lv01 ext4 2.4G 7.5M 2.3G 1% /mnt/lv01
Logical volume deletion
Uncoupling [root@server1 ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 1G 0 part /boot └─sda2 8:2 0 19G 0 part ├─centos-root 253:0 0 17G 0 lvm / └─centos-swap 253:1 0 2G 0 lvm [SWAP] sdb 8:16 0 5G 0 disk ├─sdb1 8:17 0 1G 0 part │ └─vg01-lv01 253:2 0 2.5G 0 lvm /mnt/lv01 └─sdb2 8:18 0 2G 0 part └─vg01-lv01 253:2 0 2.5G 0 lvm /mnt/lv01 sr0 11:0 1 973M 0 rom [root@server1 ~]# umount /dev/vg01/lv01 Delete logical volume [root@server1 ~]# lvremove /dev/vg01/lv01 Do you really want to remove active logical volume vg01/lv01? [y/n]: y Logical volume "lv01" successfully removed Delete volume group [root@server1 ~]# vgremove vg01 Volume group "vg01" successfully removed Delete physical volume [root@server1 ~]# pvremove /dev/sdb[12] Labels on physical volume "/dev/sdb1" successfully wiped. Labels on physical volume "/dev/sdb2" successfully wiped. [root@server1 ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 centos lvm2 a-- <19.00g 0 [root@server1 ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 1G 0 part /boot └─sda2 8:2 0 19G 0 part ├─centos-root 253:0 0 17G 0 lvm / └─centos-swap 253:1 0 2G 0 lvm [SWAP] sdb 8:16 0 5G 0 disk ├─sdb1 8:17 0 1G 0 part └─sdb2 8:18 0 2G 0 part sr0 11:0 1 973M 0 rom
Dynamic expansion of logical volume
Prepare 0.5G Logical volumes for [root@server1 ~]# pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created. [root@server1 ~]# vgcreate vg01 /dev/sdb1 Volume group "vg01" successfully created [root@server1 ~]# lvcreate -n lv01 -L 0.5G vg01 Logical volume "lv01" created. [root@server1 ~]# ll /dev/vg01/lv01 lrwxrwxrwx 1 root root 7 8 September 21-13:10 /dev/vg01/lv01 -> ../dm-2 [root@server1 ~]# ll /dev/mapper/vg01-lv01 lrwxrwxrwx 1 root root 7 8 September 21-13:10 /dev/mapper/vg01-lv01 -> ../dm-2 [root@server1 ~]# mkfs.ext4 /dev/vg01/lv01 [root@server1 ~]# mount /dev/vg01/lv01 /mnt/lv01/ [root@server1 ~]# df -Th file system type Capacity used available used% Mount point devtmpfs devtmpfs 899M 0 899M 0% /dev tmpfs tmpfs 910M 0 910M 0% /dev/shm tmpfs tmpfs 910M 9.6M 901M 2% /run tmpfs tmpfs 910M 0 910M 0% /sys/fs/cgroup /dev/mapper/centos-root xfs 17G 1.9G 16G 11% / /dev/sda1 xfs 1014M 194M 821M 20% /boot tmpfs tmpfs 182M 0 182M 0% /run/user/0 /dev/mapper/vg01-lv01 ext4 488M 780K 452M 1% /mnt/lv01 see/mnt/lv01 Which volume group does it belong to [root@server1 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert root centos -wi-ao---- <17.00g swap centos -wi-ao---- 2.00g lv01 vg01 -wi-ao---- 512.00m see vg01 Remaining space [root@server1 ~]# vgs VG #PV #LV #SN Attr VSize VFree centos 1 2 0 wz--n- <19.00g 0 vg01 1 1 0 wz--n- 1020.00m 508.00m #Insufficient volume group space: expand the volume group first, and then the logical volume #The volume group has enough space. Only the logical volume needs to be expanded Capacity expansion volume group: directly add other physical volumes to the volume group (if there are no free physical volumes, you can create physical volumes through physical devices) [root@server1 ~]# vgextend vg01 /dev/sdb2 Physical volume "/dev/sdb2" successfully created. Volume group "vg01" successfully extended [root@server1 ~]# vgs VG #PV #LV #SN Attr VSize VFree centos 1 2 0 wz--n- <19.00g 0 vg01 2 1 0 wz--n- 2.99g 2.49g Expanding logical volumes [root@server1 ~]# lvextend /dev/vg01/lv01 -L +2G Size of logical volume vg01/lv01 changed from 512.00 MiB (128 extents) to 2.50 GiB (640 extents). Logical volume vg01/lv01 successfully resized. [root@server1 ~]# df -Th file system type Capacity used available used% Mount point devtmpfs devtmpfs 899M 0 899M 0% /dev tmpfs tmpfs 910M 0 910M 0% /dev/shm tmpfs tmpfs 910M 9.6M 901M 2% /run tmpfs tmpfs 910M 0 910M 0% /sys/fs/cgroup /dev/mapper/centos-root xfs 17G 1.9G 16G 11% / /dev/sda1 xfs 1014M 194M 821M 20% /boot tmpfs tmpfs 182M 0 182M 0% /run/user/0 /dev/mapper/vg01-lv01 ext4 488M 780K 452M 1% /mnt/lv01 Synchronous file system [root@server1 ~]# resize2fs /dev/vg01/lv01 resize2fs 1.42.9 (28-Dec-2013) Filesystem at /dev/vg01/lv01 is mounted on /mnt/lv01; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 1 The filesystem on /dev/vg01/lv01 is now 655360 blocks long. [root@server1 ~]# df -Th file system type Capacity used available used% Mount point devtmpfs devtmpfs 899M 0 899M 0% /dev tmpfs tmpfs 910M 0 910M 0% /dev/shm tmpfs tmpfs 910M 9.6M 901M 2% /run tmpfs tmpfs 910M 0 910M 0% /sys/fs/cgroup /dev/mapper/centos-root xfs 17G 1.9G 16G 11% / /dev/sda1 xfs 1014M 194M 821M 20% /boot tmpfs tmpfs 182M 0 182M 0% /run/user/0 /dev/mapper/vg01-lv01 ext4 2.5G 1.5M 2.4G 1% /mnt/lv01
Striping logical volumes
Striping: divide the data saved to the logical volume into n equal parts and write them to different physical volumes respectively, which can improve the data reading and writing efficiency; If any of the physical volumes involved fails, the data cannot be recovered.
Create partition [root@server1 ~]# fdisk /dev/sdb [root@server1 ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 1G 0 part /boot └─sda2 8:2 0 19G 0 part ├─centos-root 253:0 0 17G 0 lvm / └─centos-swap 253:1 0 2G 0 lvm [SWAP] sdb 8:16 0 5G 0 disk ├─sdb1 8:17 0 2G 0 part └─sdb2 8:18 0 2G 0 part sr0 11:0 1 973M 0 rom Refresh partition table (unsuccessful, you can try to restart) [root@server1 ~]# partprobe /dev/sdb Create physical volume [root@server1 ~]# pvcreate /dev/sdb[12] Physical volume "/dev/sdb1" successfully created. Physical volume "/dev/sdb2" successfully created. [root@server1 ~]# pvs /dev/sdb[12] PV VG Fmt Attr PSize PFree /dev/sdb1 lvm2 --- 2.00g 2.00g /dev/sdb2 lvm2 --- 2.00g 2.00g Create volume group [root@server1 ~]# vgcreate vg01 /dev/sdb[12] Volume group "vg01" successfully created [root@server1 ~]# vgs vg01 VG #PV #LV #SN Attr VSize VFree vg01 2 0 0 wz--n- 3.99g 3.99g Create logical volumes for striping [root@server1 ~]# lvcreate -n lv01 -l 100%free vg01 -i 2 /dev/sdb[12] Using default stripesize 64.00 KiB. Logical volume "lv01" created. Option Description: -i: Specifies the number of stripes [root@server1 ~]# pvs /dev/sdb[12] PV VG Fmt Attr PSize PFree /dev/sdb1 vg01 lvm2 a-- <2.00g 0 /dev/sdb2 vg01 lvm2 a-- <2.00g 0 format [root@server1 ~]# mkfs.xfs /dev/vg01/lv01 meta-data=/dev/vg01/lv01 isize=512 agcount=8, agsize=130800 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=1046400, imaxpct=25 = sunit=16 swidth=32 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=16 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 mount [root@server1 ~]# mkdir /mnt/lv01 [root@server1 ~]# mount /dev/vg01/lv01 /mnt/lv01 test #Copy session for monitoring [root@server1 ~]# yum install -y sysstat.x86_64 [root@server1 ~]# iostat -m -d /dev/sdb[12] 3 Option Description: -d: View disk -m: At what speed, M/s 3: Every 3 s Display once [root@server1 ~]# dd if=/dev/zero of=/mnt/lv01/dd_file bs=1M count=1000 #Monitoring display Device: tps MB_read/s MB_wrtn/s MB_read MB_wrtn sdb1 181.33 0.00 11.29 0 33 sdb2 180.33 0.00 11.27 0 33 Device: tps MB_read/s MB_wrtn/s MB_read MB_wrtn sdb1 731.33 0.00 45.58 0 136 sdb2 729.33 0.00 45.58 0 136
Logical volume snapshot
The snapshot size is much smaller than the logical volume instance size, and the snapshot space also comes from the volume group. When the available space of the volume group is 0, the snapshot cannot be created
Environment: now it is planned to vg01-lv01 The logical volume takes a snapshot, but the free space of the volume group is 0, which is proposed to be used sdb3 Capacity expansion [root@server1 ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 1G 0 part /boot └─sda2 8:2 0 19G 0 part ├─centos-root 253:0 0 17G 0 lvm / └─centos-swap 253:1 0 2G 0 lvm [SWAP] sdb 8:16 0 5G 0 disk ├─sdb1 8:17 0 2G 0 part │ └─vg01-lv01 253:2 0 4G 0 lvm /mnt/lv01 ├─sdb2 8:18 0 2G 0 part │ └─vg01-lv01 253:2 0 4G 0 lvm /mnt/lv01 └─sdb3 8:19 0 1023M 0 part sr0 11:0 1 973M 0 rom Create physical volume [root@server1 ~]# pvcreate /dev/sdb3 Physical volume "/dev/sdb3" successfully created. sdb3 join vg01 Volume group [root@server1 ~]# vgextend vg01 /dev/sdb3 Volume group "vg01" successfully extended [root@server1 ~]# dmsetup ls --tree vg01-lv01 (253:2) ├─ (8:18) └─ (8:17) centos-swap (253:1) └─ (8:2) centos-root (253:0) └─ (8:2) take snapshot [root@server1 ~]# lvcreate -n lv01_snap -L 50M -s /dev/vg01/lv01 Logical volume "lv01_snap" created. [root@server1 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert root centos -wi-ao---- <17.00g swap centos -wi-ao---- 2.00g lv01 vg01 owi-aos--- 3.99g lv01_snap vg01 swi-a-s--- 50.00m lv01 0.15 [root@server1 ~]# dmsetup ls --tree vg01-lv01_snap (253:5) ├─vg01-lv01_snap-cow (253:4) #Save the data before the original volume changes │ └─ (8:19) └─vg01-lv01-real (253:3) #Real logical volume (original volume) ├─ (8:18) └─ (8:17) vg01-lv01 (253:2) └─vg01-lv01-real (253:3) ├─ (8:18) └─ (8:17) centos-swap (253:1) └─ (8:2) centos-root (253:0) └─ (8:2) Snapshot auto expansion [root@server1 ~]# vim /etc/lvm/lvm.conf snapshot_autoextend_threshold = 75#Snapshot space utilization (if the value is set to 100, automatic capacity expansion is turned off) snapshot_autoextend_percent = 20 #The snapshot utilization rate meets the above requirements, and the capacity is automatically expanded by 20%
Snapshot backup is often used in production environment
Lock table – > create snapshot – > unlock – > mount snapshot – > back up to a specified place – > delete snapshot