LVM涉及到了PV、VG、LV三個層次,每一個層次都有對應的命令來管理centos
管理PV的工具:pvs, pvscan, pvdisplay, pvcreate, pvremove, pvmovebash
經常使用命令:app
♦ pvs [OPTIONS] 顯示簡要的PV信息ide
♦ pvdisplay [OPTIONS] 顯示詳細的PV信息工具
♦ pvcreate /dev/DEVICE 建立PV性能
♦ pvmove /dev/DEVICE 刪除PVui
♦ pvscan [OPTIONS] 檢查PVcentos7
管理VG工具:vgs, vgscan, vgdisplay, vgcreate, vgremove, vgreduce, vgextend, vgchangecode
經常使用命令:rem
♦ vgs [OPTIONS] 顯示簡要的VG信息
♦ vgdisplay [OPTIONS] 顯示詳細的VG信息
♦ vgcreate [-s #[kKmMgGtTpPeE]] VG_NAME /dev/DEVICE [/dev/DEVICE...] 建立VG
♦ vgextend VG_NAME /dev/DEVICE [/dev/DEVICE...] 擴展VG
♦ vgreduce VG_NAME /dev/DEVICE [/dev/DEVICE...] 縮減VG
♦ vgremove VG_NAME 刪除VG
♦ vgscan [OPTIONS] 檢查VG
♦ vgchange [OPTIONS] 修改VG屬性
管理LV工具:lvs, lvscan, lvdisplay, lvcreate, lvremove, lvreduce, lvextemd, kvresize
經常使用命令:
♦ lvs [OPTIONS] 查看簡單的LV信息
♦ lvdisplay [OPTIONS] 查看詳細的LV信息
♦ lvcreate -L SIZE[mMgGtT] -n LV_NAME VG_NAME 建立LV
lvcreate -l 100% FREE -n LV_NAME VG_NAME :指定LV大小爲全部可用的VG空間,-l指定PE數量
♦ lvremove /dev/VG_NAME/LV_NAME 刪除LV
♦ lvextend -L [+]SIZE[mMgGtT] /dev/VG_NAME/LV_NAME 擴展LV
配合同步ext系列文件系統的命令才生效:resize2fs /dev/VG_NAME/LV_NAME
或同步xfs文件系統的命令才生效:xfs_growfs /dev/VG_NAME/LV_NAME
lvresize -r -l +100%FREE /dev/VG_NAME/LV_NAME
說明:執行一條命令後同時完成擴展LV和同步文件系統的功能,使用全部的VG空間擴展LV。SIZE表示爲擴展到LV 的總容量大小,+SIZE表示在用來基礎上增長的容量大小。
♦ lvreduce -L [-]SIZE[mMgGtT] /dev/VG_NAME/LV_NAME 縮減LV
說明:縮減LV時,注意步驟,先執行umount命令卸載,而後進行文件系統檢查,再重置文件系統大小,最後才執行lvreduce命令
♦ lvscan [OPTIONS] 檢查LV
前提準備好分區
#建立PV [root@centos82s ~]$pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created. [root@centos82s ~]$pvcreate /dev/sdb2 Physical volume "/dev/sdb2" successfully created. #查看 [root@centos82s ~]$pvs PV VG Fmt Attr PSize PFree /dev/sdb1 lvm2 --- 5.00g 5.00g /dev/sdb2 lvm2 --- 5.00g 5.00g #建立VG [root@centos82s ~]$vgcreate -s 16M myvg /dev/sdb{1,2} Volume group "myvg" successfully created [root@centos82s ~]$vgs VG #PV #LV #SN Attr VSize VFree myvg 2 0 0 wz--n- <9.97g <9.97g #建立LV [root@centos82s ~]$lvcreate -n mylv -L 2G myvg Logical volume "mylv" created. [root@centos82s ~]$lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv myvg -wi-a----- 2.00g #查看LV路徑 [root@centos82s ~]$lvdisplay --- Logical volume --- LV Path /dev/myvg/mylv LV Name mylv #建立ext4系統 [root@centos82s ~]$mkfs.ext4 /dev/myvg/mylv #建立掛載點 [root@centos82s ~]$mkdir /mnt/mylvs #開機自動掛載文件系統,在/etc/fstab中添加下面內容 /dev/myvg/mylv /mnt/mylvs ext4 defaults 0 0 #手動掛載該文件系統 [root@centos82s ~]$mount -a
#添加硬盤,建立PV [root@centos82s ~]$pvcreate /dev/sdb3 Physical volume "/dev/sdb3" successfully created. #擴展VG [root@centos82s ~]$vgextend myvg /dev/sdb3 Volume group "myvg" successfully extended #擴展LV [root@centos82s ~]$lvextend -r -L +3G /dev/myvg/mylv Size of logical volume myvg/mylv changed from 2.00 GiB (128 extents) to 5.00 GiB (320 extents). Logical volume myvg/mylv successfully resized. resize2fs 1.45.4 (23-Sep-2019) Filesystem at /dev/mapper/myvg-mylv is mounted on /mnt/mylvs; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 1 The filesystem on /dev/mapper/myvg-mylv is now 1310720 (4k) blocks long. #若是沒有-r參數,需執行 [root@centos82s ~]$resize2fs /dev/myvg/mylv
#卸載目錄掛載點 [root@centos82s ~]$umount /mnt/mylvs/ #檢測文件系統 [root@centos82s ~]$fsck -f /dev/myvg/mylv #把文件系統空間大小縮減到10G [root@centos82s ~]$resize2fs /dev/myvg/mylv #縮減LV [root@centos82s ~]$lvreduce -L 10G /dev/myvg/mylv #查看 [root@centos82s ~]$lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv myvg -wi-a----- 10.00g
#建立PV [root@centos82s ~]$pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created. #建立vg [root@centos82s ~]$vgcreate myvg /dev/sdb1 Volume group "myvg" successfully created #把/dev/sdb硬盤上已分配的PE移動到其它LVM上 [root@centos82s ~]$pvmove /dev/sdb1 /dev/sdb1: Moved: 4.30% /dev/sdb1: Moved: 100.00% #把sdb1移動到其它lvm上 [root@centos82s ~]$pvmove /dev/sdb1 /dev/sdb1: Moved: 11.72% /dev/sdb1: Moved: 100.00% #把/dev/sdb1從VG移除 [root@centos82s ~]$vgreduce myvg /dev/sdb1 Removed "/dev/sdb1" from volume group "myvg"
把centos6上的LVM遷移到centos7上,centos6LVM總容量20G,centos7LVM總容量40G
#查看centos6磁盤使用狀況 [root@centos610 ~]#lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sr0 11:0 1 3.7G 0 rom sda 8:0 0 200G 0 disk ├─sda1 8:1 0 1G 0 part /boot ├─sda2 8:2 0 97.7G 0 part / ├─sda3 8:3 0 48.8G 0 part /data ├─sda4 8:4 0 1K 0 part └─sda5 8:5 0 2G 0 part [SWAP] sdb 8:16 0 20G 0 disk [root@centos610 ~]#pvs #建立PV [root@centos610 ~]#pvcreate /dev/sdb Physical volume "/dev/sdb" successfully created [root@centos610 ~]#pvs PV VG Fmt Attr PSize PFree /dev/sdb lvm2 ---- 20.00g 20.00g #建立VG [root@centos610 ~]#vgcreate centos6myvg /dev/sdb Volume group "centos6myvg" successfully created [root@centos610 ~]#vgs VG #PV #LV #SN Attr VSize VFree centos6myvg 1 0 0 wz--n- 20.00g 20.00g #建立LV [root@centos610 ~]#lvcreate -l 100%FREE -n mylv centos6myvg Logical volume "mylv" created. [root@centos610 ~]#lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv centos6myvg -wi-a----- 20.00g #修改LV名稱 [root@centos610 ~]#lvrename /dev/centos6myvg/mylv /dev/centos6myvg/centos6mylv Renamed "mylv" to "centos6mylv" in volume group "centos6myvg" [root@centos610 ~]#lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert centos6mylv centos6myvg -wi-a----- 20.00g #卸載 [root@centos610 ~]#umount /data/mylvm/ #禁用VG [root@centos610 ~]#vgchange -an centos6myvg 0 logical volume(s) in volume group "centos6myvg" now active [root@centos610 ~]#lvscan inactive '/dev/centos6myvg/centos6mylv' [20.00 GiB] inherit #導出VG後硬盤拆下來安裝在centos7主機上 [root@centos7s ~]#lsblk #導入原VG [root@centos7s ~]#vgimport centos6myvg [root@centos7s ~]#vgdisplay [root@centos7s ~]#lvdisplay #激活原VG [root@centos7s ~]#vgchange -ay centos6myvg [root@centos7s ~]#lvdisplay #建立掛載點 [root@centos7s ~]#mkdir /data/centos6lvm #掛載 [root@centos7s ~]#mount /dev/centos6myvg/centos6mylv /data/centos6mylvm
注意:因爲快照區與本來的LV共用不少PE的區塊,所以快照與被快照的LV必須在同一個VG中。系統恢復時候的文件數量不能高於快照區的實際容量。快照實際上也是一個邏輯卷,因此建立快照前,要保證現有LVM系統中VG有足夠的剩餘空間。
建議:如非須要,不要使用快照,快照越多,對原LV和快照LV的IO寫性能損失巨大,急劇降低。
#建立快照 [root@centos610 ~]#lvcreate -n centos6mylv-snapshot -s -L 1G -p r /dev/centos6myvg/centos6mylv Logical volume "centos6mylv-snapshot" created. #查看,-p r表示快照卷是隻讀 [root@centos610 ~]#lvdisplay --- Logical volume --- LV Path /dev/centos6myvg/centos6mylv-snapshot LV Name centos6mylv-snapshot VG Name centos6myvg LV UUID mj0Hqr-d468-W24p-edVH-GBZH-PIpE-bYa1hp LV Write Access read only LV Creation host, time centos610, 2020-08-18 01:58:35 +0800 LV snapshot status active destination for centos6mylv #建立掛載點 [root@centos610 ~]#mkdir /data/centos6snapshot #掛載快照卷 [root@centos610 ~]#mount /dev/centos6myvg/centos6mylv-snapshot /data/centos6snapshot/ mount: block device /dev/mapper/centos6myvg-centos6mylv--snapshot is write-protected, mounting read-only #卸載原始卷和快照卷 [root@centos610 ~]#umount /data/mylvm/ [root@centos610 ~]#umount /data/centos6snapshot/ #恢復快照卷在掛載使用 [root@centos610 ~]#lvconvert --merge /dev/centos6myvg/centos6mylv-snapshot Merging of volume centos6mylv-snapshot started. centos6mylv: Merged: 100.0% Merge of snapshot into logical volume centos6mylv has finished. Logical volume "centos6mylv-snapshot" successfully removed [root@centos610 ~]#mount /dev/centos6myvg/centos6mylv /data/mylvm/