Linux Basics Learning --Linux disk management and maintenance of the LVM Logical Volume

LVM concepts

LVM is a Logical Volume Manager (LVM) shorthand, it is a mechanism to manage disk partitions under Linux. LVM is a logical layer between the partition and the file system is added to the lower shielding disk partition layout to the file system provides an abstraction of the storage volume, a file system on the storage volume.

/ Boot partition can not be created on LVM, must be independent

LVM term

  1. PE (Physical Extend) Each physical volume is divided into a basic unit called PE (Physical Extents) having a unique number that PE may be the smallest addressable unit LVM . PE size is configurable, default to 4MB.
  2. PV (Physical Volume) physical volume refers to a hard disk partition or device (e.g., RAID) having the same function from a logical disk partition, is the basic storage logical blocks LVM, but basic physical storage medium (such as a partition, disk, etc.) compare, but contains management parameters associated with LVM.
  3. VG (Volume Group) LVM volume group non-LVM system similar to the physical hard disk, which is a physical volumes. Can create one or more groups on the volume, "the LVM partitions" (logical volume), LVM volume group by one or more physical volumes.
  4. LV (Logical Volume) LVM logical volume similar to non-LVM system hard disk partition, logical volume on the file system can be established (such as / home or / usr, etc.).
  5. LE (Logical Extent) is divided into logical volumes is referred to as LE (Logical Extents) of the basic unit can be addressed . In the same volume group, LE and PE are the same size, and one correspondence.

LV substituted partitions formatted by a logical volume, and then use the mount

the term meaning
PV Physical disk partition
VG LVM physical disk partition, PV must join VG
LV Divided from the logical partition VG

Here Insert Picture Description

LVM management command

PV-related commands

command meaning
pvcreate Creating pv
pvs Check the information pv
pvdisplay View pv of detailed information
pvscan Scan the hard disk in the system, listed in the list of physical volumes found
pvremove Removing the physical volume
pvmove Removing the physical volume PE

vg-related commands

command meaning
vgcreate Create a volume group
vgextend Add members to the volume group
vgreduce Remove members from the volume group
cgremove Delete volume group
vgs Check volume group information
vgdisplay View Details volume group
vgrename Change the name of the volume group
vgchange Changing the operating state of the volume group
vgexport Export volume group
vgimport Import volume group

lv-related commands

command meaning
lvcreate Creating a Logical Volume
lvextend Expand the size of the logical volume
lvreduce Reduce the size of the logical volume
lvrename Change the name of the logical volume
lvs View lv Information
lvdisplay View details of lv
lvremove Removing a Logical Volume
lvconvert Revert to the snapshot, the snapshot is deleted after restoration

Creating LVM

  1. Prepare physical disk
[root@localhost ~]# ll /dev/sdb{1,2,5}
brw-rw----. 1 root disk 8, 17 Nov 12 21:07 /dev/sdb1
brw-rw----. 1 root disk 8, 18 Nov 12 21:07 /dev/sdb2
brw-rw----. 1 root disk 8, 21 Nov 12 21:07 /dev/sdb5
  1. Creating PV

pvcreate

[root@localhost ~]# yum install lvm2 -y
[root@localhost ~]# pvcreate /dev/sdb1 /dev/sdb2
  Physical volume "/dev/sdb1" successfully created.
  Physical volume "/dev/sdb2" successfully created.
[root@localhost ~]# pvs
  PV         VG   Fmt  Attr PSize  PFree
  /dev/sdb1  myvg lvm2 a--  <5.00g <5.00g
  /dev/sdb2  myvg lvm2 a--  <5.00g <5.00g
[root@localhost ~]# pvscan
[root@localhost ~]# pvdisplay
  1. Creating vg

vgcreate

[root@localhost ~]# vgcreate  myvg /dev/sdb1 /dev/sdb2
  Volume group "myvg" successfully created
[root@localhost ~]# vgs
  VG   #PV #LV #SN Attr   VSize VFree
  myvg   2   0   0 wz--n- 9.99g 9.99g
[root@localhost ~]# vgscan
[root@localhost ~]# vgdisplay
  --- Volume group ---
……
  PE Size               4.00 MiB	;没有指定默认4M
……
  1. Creating lv

lvcreate

-n  Specifies the name of the logical volume
-L  use of space specified logical volume size  -L ## absolute capacity, -L + ## incremental capacity
-l  use of logical PE number specified size  -l absolute number ## , -l + ## increments the number, Free ##%, ##% VG
-s  specified logical volume created a snapshot
-p  the read-only attribute r

[root@localhost ~]# lvcreate -l 10 -n mylv1 myvg	;使用PE数量设置大小
  Logical volume "mylv1" created.
[root@localhost ~]# lvcreate -L 100M -n mylv2 myvg	;直接指定大小
  Logical volume "mylv2" created.
[root@localhost ~]# lvs                            
[root@localhost ~]# lvdisplay
[root@localhost ~]# lvscan
  ACTIVE            '/dev/myvg/mylv1' [40.00 MiB] inherit
  ACTIVE            '/dev/myvg/mylv2' [100.00 MiB] inherit
  1. Create a file system and mount
[root@localhost ~]# mkfs.ext4 /dev/myvg/mylv1
[root@localhost ~]# mkfs.ext4 /dev/myvg/mylv2
[root@localhost ~]# mount /dev/myvg/mylv1 /mydir/mylv1
[root@localhost ~]# mount /dev/myvg/mylv2 /mydir/mylv2
[root@localhost ~]# df -Th
Filesystem             Type      Size  Used Avail Use% Mounted on
……
/dev/mapper/myvg-mylv1 ext4       35M  782K   32M   3% /mydir/mylv1
/dev/mapper/myvg-mylv2 ext4       93M  1.6M   85M   2% /mydir/mylv2

VG Management

1.vgextend expand VG (Volume Group)

[root@localhost ~]# pvcreate /dev/sdb5
  Physical volume "/dev/sdb5" successfully created.
[root@localhost ~]# vgextend myvg /dev/sdb5
  Volume group "myvg" successfully extended
[root@localhost ~]# vgs
  VG   #PV #LV #SN Attr   VSize   VFree
  myvg   3   2   0 wz--n- <14.99g 14.85g  

2.vgreduce reduced VG

[root@localhost ~]# pvs
  PV         VG   Fmt  Attr PSize  PFree
  /dev/sdb1  myvg lvm2 a--  <5.00g <4.86g
  /dev/sdb2  myvg lvm2 a--  <5.00g <5.00g
  /dev/sdb5  myvg lvm2 a--  <5.00g <5.00g
[root@localhost ~]#
[root@localhost ~]# dd if=/dev/zero of=/mydir/mylv1/file bs=1M count=20
20+0 records in
20+0 records out
20971520 bytes (21 MB) copied, 0.304384 s, 68.9 MB/s
[root@localhost ~]# pvs
  PV         VG   Fmt  Attr PSize  PFree
  /dev/sdb1  myvg lvm2 a--  <5.00g <4.86g
  /dev/sdb2  myvg lvm2 a--  <5.00g <5.00g
  /dev/sdb5  myvg lvm2 a--  <5.00g <5.00g
[root@localhost ~]# pvmove /dev/sdb1	;将数据挪到其他pv上
  /dev/sdb1: Moved: 28.57%
  /dev/sdb1: Moved: 100.00%
[root@localhost ~]# pvs
  PV         VG   Fmt  Attr PSize  PFree
  /dev/sdb1  myvg lvm2 a--  <5.00g <5.00g
  /dev/sdb2  myvg lvm2 a--  <5.00g <4.86g
  /dev/sdb5  myvg lvm2 a--  <5.00g <5.00g
[root@localhost ~]# vgreduce myvg /dev/sdb1
  Removed "/dev/sdb1" from volume group "myvg"
[root@localhost ~]# vgs
  VG   #PV #LV #SN Attr   VSize VFree
  myvg   2   2   0 wz--n- 9.99g <9.86g

lvextent LVM expansion

VG enough since you can expand lv

[root@localhost ~]# lvextend -L +60M /dev/myvg/mylv1
  Size of logical volume myvg/mylv1 changed from 40.00 MiB (10 extents) to 100.00 MiB (25 extents).
  Logical volume myvg/mylv1 successfully resized.
[root@localhost ~]# lvs
  LV    VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv1 myvg -wi-ao---- 100.00m                                                
  mylv2 myvg -wi-ao---- 100.00m                                                
[root@localhost ~]# df -h | grep mylv1
/dev/mapper/myvg-mylv1   35M   21M   12M  66% /mydir/mylv1	;虽然lv扩容了,但是挂载磁盘大小依然是40M,我们需要给新增的lv创建文件系统,这样系统才能识别
[root@localhost ~]# resize2fs /dev/myvg/mylv1
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/myvg/mylv1 is mounted on /mydir/mylv1; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/myvg/mylv1 is now 102400 blocks long.

[root@localhost ~]# df -h | grep mylv1
/dev/mapper/myvg-mylv1   93M   22M   66M  25% /mydir/mylv1

lvreduce LV reduction

Note: The size of the file system and logical volume size must be the same job. If the logical volume is larger than the file system, since part of the unwritten region formatted
member will result in waste of space. If the logical volume is less than the file system, which data will be a problem.

The size of the file system and logical volume must be consistent

  1. umount unloading logical volume  must uninstall logical to reduce
[root@localhost ~]# umount /mnt/mylv1
[root@localhost ~]# df -h|grep mylv1
  1. By vacant space on the logical volume command detection e2fsck
[root@localhost ~]# e2fsck -f /dev/myvg/mylv1	;必须强制检测,否则缩减的时候会提示
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/myvg/mylv1: 12/26624 files (8.3% non-contiguous), 29494/102400 blocks
  1. Use resize2fs will reduce the file system
[root@localhost ~]# resize2fs /dev/myvg/mylv1 50M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/myvg/mylv1 to 51200 (1k) blocks.
The filesystem on /dev/myvg/mylv1 is now 51200 blocks long.
  1. Use lvreduce order to reduce the logical volume
[root@localhost ~]# lvreduce -L -50M /dev/myvg/mylv1
  Rounding size to boundary between physical extents: 48.00 MiB.
  WARNING: Reducing active logical volume to 252.00 MiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce myvg/mylv1? [y/n]: y
  Size of logical volume myvg/mylv1 changed from 300.00 MiB (75 extents) to 252.00 MiB (63 extents).
  Logical volume myvg/mylv1 successfully resized.
  1. To Mount
[root@localhost ~]# mount /dev/myvg/mylv1 /mydir/mylv1
[root@localhost ~]# df -Th|grep mylv1
/dev/mapper/myvg-mylv1 ext4       45M   22M   21M  52% /mydir/mylv1

Removing a Logical Volume

  1. umount unloading logical volume
[root@localhost ~]# umount /mydir/mylv1
[root@localhost ~]# df -Th|grep mylv1
  1. Mount information modify / etc / fstab inside the logical volume (if you added one), otherwise the system may not start up.
  2. lvremove Removing a Logical Volume
[root@localhost ~]# lvremove /dev/myvg/mylv1
Do you really want to remove active logical volume myvg/mylv1? [y/n]: y
  Logical volume "mylv1" successfully removed
  1. vgremove delete the volume group
[root@localhost ~]# vgremove myvg
  1. pvremove converted to ordinary physical volume partition
[root@localhost ~]# pvremove /dev/sdb1 /dev/sdb2

LVM snapshot lvm snapshot

Precautions

  1. VG is required to reserve storage space snapshot itself
  2. VG must be the same snapshot where LV is backed up, i.e. the snapshot storage location must be stored on the same volume illuminated with a VG otherwise fail snapshot
  3. If the snapshot volume is full, automatic failure
  1. Create a snapshot
[root@localhost ~]# lvcreate -L 20M -s -n mysnop /dev/myvg/mylv2
  Logical volume "mysnop" created.
[root@localhost ~]# lvdisplay
……
--- Logical volume ---
  LV Path                /dev/myvg/mysnop
  LV Name                mysnop
  VG Name                myvg
 ……
  COW-table size         20.00 MiB	;快照实际容量
  COW-table LE           5
  Allocated to snapshot  0.06%	;快照已使用百分比
  Snapshot chunk size    4.00 KiB
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:3
  1. Deleting a snapshot
[root@localhost ~]# lvremove /dev/myvg/mysnop
Do you really want to remove active logical volume myvg/mysnop? [y/n]: y
  Logical volume "mysnop" successfully removed

Example: create a snapshot and restore data snapshots

  1. First, to create a good lv write data
[root@localhost ~]# cd /mydir/mylv2
[root@localhost mylv2]# echo "hello" > myspon_file
[root@localhost mylv2]# cat myspon_file
hello
  1. Create a snapshot
[root@localhost mylv2]# lvcreate -L 20M -s -n mysnop1 /dev/myvg/mylv2
  Logical volume "mysnop1" created.
  1. The original volume data is written, the snapshot volume of usage increases
[root@localhost mylv2]# lvdisplay |grep %
  Allocated to snapshot  0.29%
  Allocated to snapshot  0.06%
[root@localhost mylv2]# dd if=/dev/zero of=/mydir/mylv2/file bs=1M count=5
5+0 records in
5+0 records out
5242880 bytes (5.2 MB) copied, 0.00892103 s, 588 MB/s
[root@localhost mylv2]# lvdisplay |grep %
  Allocated to snapshot  0.31%
  Allocated to snapshot  0.10%
[root@localhost mylv2]# echo "hello" > myspon_file2
[root@localhost mylv2]# ls
file  lost+found  myspon_file  myspon_file2
  1. Restore the original data
[root@localhost ~]# umount /myfile/mylv2	;必须卸载
[root@localhost ~]# lvconvert --merge /dev/myvg/mysnop1
  Merging of volume myvg/mysnop1 started.
  myvg/mylv2: Merged: 99.86%
Published 43 original articles · won praise 30 · views 5935

Guess you like

Origin blog.csdn.net/qq_42049496/article/details/103099886