1、什么是LVM
LVM(Logical Volume Manager)邏輯卷管理,是linux環境下將一種將一個或多個硬盤的分區在邏輯上集合來呈現給上層應用,對磁盤實現動態管理的機制。相對于普通的磁盤分區有很大的靈活性,使用LVM在一定程度上就可以解決普通磁盤分區帶來的問題。
2、專業術語
●物理卷(PhysicalVolume)就是指硬盤分區或從邏輯上與磁盤分區具有同樣功能的設備(可 以是RAID),是LVM的基本存儲邏輯塊
●卷組(VolumeGroup)是物理卷的集合
●LVM(LogicalVolumeManager)的邏輯卷類似于非LVM系統中的硬盤分區,在邏輯卷之上可以建立文件系統(比如/home或者/usr等)
●PE(PhysicalExtent)是每一個邏輯卷和邏輯卷的基本單元,具有唯一編號的PE是可以被LVM尋址 的最小單元。PE的大小是可配置的,默認為4MB
●LE(LogicalExtent)是邏輯卷的基本單位。在同一個卷組中,LE的大小和PE是相同的,并且一一對應
3、LVM工作原理圖
原理:在Disk的基礎上,創建PV,再在PV的基礎上,創建VG,要注意:它們的基本單位是PE。在VG的基礎上,創建LV,LV的基本單位是LE。PE和LE的單位相同。
4、簡單實現
<1>創建文件系統
Command (m for help): n # 新建分區 Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): First sector (2048-419430399, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-419430399, default 419430399): +3G Partition 1 of type Linux and of size 3 GiB is set # Command (m for help): T Selected partition 1 Hex code (type L to list all codes): 8e # 調整分區類型為8e Changed type of partition 'Linux' to 'Linux LVM' Command (m for help): w # 保存退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. # 多創建幾個分區,操作方法如上
<2>創建PV
[root@centos7~]#pvcreate /dev/sdc1 # 三個分區創建pv Physical volume "/dev/sdc1" successfully created [root@centos7~]#pvcreate /dev/sdc2 Physical volume "/dev/sdc2" successfully created [root@centos7~]#pvcreate /dev/sdc3 Physical volume "/dev/sdc3" successfully created
<3>創建VG
[root@centos7~]#vgcreate vg1 /dev/sdc{1,2,3} Volume group "vg1" successfully created
<4>創建LV
[root@centos7~]#lvcreate -n lv1 -L 6G /dev/vg1 Logical volume "lv1" created.
<5>為LV創建文件系統
[root@centos7~]#mke2fs -t ext4 /dev/vg1/lv1 mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 393216 inodes, 1572864 blocks 78643 blocks (5.00%) reserved for the super user .....
<6>掛載邏輯卷
# 創建掛載點 [root@centos7~]#mkdir /mnt/lv1 # 掛載lv1 [root@centos7~]#vim /etc/fstab ... UUID="82ed7610-1975-4433-a741-1db119e4bf0e" /mnt/lv1 ext4 default 0 0 ... # 掛載設備 [root@centos7~]#mount -a [root@centos7~]#
<7>測試邏輯卷大小
# 查看原始數據大小 [root@centos7/mnt/lv1]#df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 100G 6.0G 95G 6% / devtmpfs 474M 0 474M 0% /dev tmpfs 489M 84K 489M 1% /dev/shm tmpfs 489M 7.2M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sdg1 976M 2.6M 907M 1% /mnt/sdg1 /dev/mapper/centos-testdir 20G 33M 20G 1% /testdir /dev/sda1 197M 143M 55M 73% /boot tmpfs 98M 20K 98M 1% /run/user/42 tmpfs 98M 0 98M 0% /run/user/0 /dev/sde1 2.0G 55M 1.8G 3% /home /dev/mapper/vg1-lv1 5.8G 24M 5.5G 1% /mnt/lv1 # 原始大小 # 填充磁盤 [root@centos7/mnt/lv1]#dd if=/dev/zero of=/mnt/lv1/f1 bs=1M count=1024 1024+0 records in 1024+0 records out 1073741824 bytes (1.1 GB) copied, 2.58483 s, 415 MB/s # 再此查看磁盤空間 [root@centos7/mnt/lv1]#df -h ... /dev/mapper/vg1-lv1 5.8G 1.1G 4.5G 19% /mnt/lv1 # 已生效,只分配6g
<8>擴展LV,在線擴展(掛載即可擴展)
# 查看VG [root@centos7/mnt/lv1]#pvs PV VG Fmt Attr PSize PFree /dev/sda2 centos lvm2 a-- 122.00g 0 /dev/sdc1 vg1 lvm2 a-- 3.00g 3.00g /dev/sdc2 vg1 lvm2 a-- 5.00g 5.00g /dev/sdc3 vg1 lvm2 a-- 10.00g 4.00g # /dev/sdc一共18g,還有剩余,可擴展 # 擴展LV [root@centos7/mnt/lv1]#lvextend -L 12G /dev/vg1/lv1 # 擴展 Size of logical volume vg1/lv1 changed from 6.00 GiB (1536 extents) to 12.00 GiB (3072 extents). Logical volume lv1 successfully resized. # LV已同步,df -h查看未同步 [root@centos7/mnt/lv1]#lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert ... lv1 vg1 -wi-ao---- 12.00g [root@centos7/mnt/lv1]#df -h ... /dev/mapper/vg1-lv1 5.8G 1.1G 4.5G 19% /mnt/lv1 # 因為新加入的分區尚未創建文件系統,所以df -h查看同步失敗,同步文件系統 [root@centos7/mnt/lv1]#resize2fs /dev/vg1/lv1 # resize2fs,ext文件系統專用命令 resize2fs 1.42.9 (28-Dec-2013) Filesystem at /dev/vg1/lv1 is mounted on /mnt/lv1; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 2 The filesystem on /dev/vg1/lv1 is now 3145728 blocks long. # 再次查看 [root@centos7/mnt/lv1]#df -h Filesystem Size Used Avail Use% Mounted on ... /dev/mapper/vg1-lv1 12G 1.1G 11G 10% /mnt/lv1 # 同步完成
補充:擴展xfs的文件系統,使用xfs_growfs /mnt/lv1
當然,以上的步驟可以使用lvextend -r -l +100%FREE /dev/vg1/lv1實現:在全部擴展VG的基礎上并且同步文件系統。
<9>擴展之后,查看PV,并且有需要更換/dev/sdc1硬盤來提高磁盤的性能或者增加磁盤容量
# 查看PV使用情況 [root@centos7/mnt/lv1]#pvdisplay --- Physical volume --- PV Name /dev/sdc1 VG Name vg1 PV Size 3.00 GiB / not usable 4.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 767 # 2G空間,占用一部分PV Free PE 254 Allocated PE 513 PV UUID vrAd2v-StgB-DmLw-y284-3sJy-dLQu-e4KQwc --- Physical volume --- PV Name /dev/sdc2 VG Name vg1 PV Size 5.00 GiB / not usable 4.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 1279 # PV并沒有使用,需要轉移sdc1的PE至此。sdc2(PV)>sdc1(PV) Free PE 1279 Allocated PE 0 PV UUID 255jQO-0mTo-eeQF-NUAc-tmAB-1rFF-8419at --- Physical volume --- PV Name /dev/sdc3 VG Name vg1 PV Size 10.00 GiB / not usable 4.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 2559 # 10G空間全部被分完 Free PE 0 Allocated PE 2559 PV UUID eBQJHR-wfJH-PDs5-4XGo-3Z8s-Vy2Y-UFzvs9 ... # 轉移PV [root@centos7~]#pvmove /dev/sdc1 /dev/sdc1: Moved: 2.3% /dev/sdc1: Moved: 100.0% # 轉移/dev/sdc1的PV # 再次查看PV [root@centos7~]#pvdisplay --- Physical volume --- PV Name /dev/sdc1 VG Name vg1 PV Size 3.00 GiB / not usable 4.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 767 # 已經轉移,磁盤空間重置 Free PE 767 Allocated PE 0 PV UUID vrAd2v-StgB-DmLw-y284-3sJy-dLQu-e4KQwc --- Physical volume --- PV Name /dev/sdc2 VG Name vg1 PV Size 5.00 GiB / not usable 4.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 1279 # 已經將sdc1的PV轉移至此 Free PE 766 Allocated PE 513 PV UUID 255jQO-0mTo-eeQF-NUAc-tmAB-1rFF-8419at --- Physical volume --- PV Name /dev/sdc3 VG Name vg1 PV Size 10.00 GiB / not usable 4.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 2559 Free PE 0 Allocated PE 2559 PV UUID eBQJHR-wfJH-PDs5-4XGo-3Z8s-Vy2Y-UFzvs9 # 將/dev/sdc1從vg1中移除 [root@centos7~]#vgreduce vg1 /dev/sdc1 Removed "/dev/sdc1" from volume group "vg1" ###### 已移除 [root@centos7~]#pvs PV VG Fmt Attr PSize PFree /dev/sda2 centos lvm2 a-- 122.00g 0 /dev/sdb2 lvm2 --- 6.00g 6.00g /dev/sdb3 lvm2 --- 3.00g 3.00g /dev/sdc1 lvm2 --- 3.00g 3.00g # /dev/sdc1已不再屬于任何VG /dev/sdc2 vg1 lvm2 a-- 5.00g 2.99g /dev/sdc3 vg1 lvm2 a-- 10.00g 0 # 刪除PV [root@centos7~]#pvremove /dev/sdc1 Labels on physical volume "/dev/sdc1" successfully wiped [root@centos7~]#pvs PV VG Fmt Attr PSize PFree /dev/sda2 centos lvm2 a-- 122.00g 0 /dev/sdb2 lvm2 --- 6.00g 6.00g /dev/sdb3 lvm2 --- 3.00g 3.00g /dev/sdc2 vg1 lvm2 a-- 5.00g 2.99g # /dev/sdc1PV刪除成功 /dev/sdc3 vg1 lvm2 a-- 10.00g 0
<10>如果擴展LV的時候,發現VG空間不足,可繼續擴展VG,步驟如下
# 添加一塊新的磁盤或者分區,此處以分區為例,與<1>相同 # 再次創建PV [root@centos7~]#pvcreate /dev/sdc5 Physical volume "/dev/sdc5" successfully created [root@centos7~]#pvs PV VG Fmt Attr PSize PFree ... /dev/sdc1 vg1 lvm2 a-- 3.00g 1016.00m /dev/sdc2 vg1 lvm2 a-- 5.00g 5.00g /dev/sdc3 vg1 lvm2 a-- 10.00g 0 /dev/sdc5 lvm2 --- 1.00g 1.00g # 再次將其加入卷組vgextend vg1 /dev/sdc5 # 當然,如果VG空間足夠,也就不需要這一步驟了
<11>如果有需要縮減邏輯卷的話,步驟如下(注:嚴格遵守以下五步驟)
# 首先建議:縮減之間盡量備份 # 卸載 [root@centos7~]#umount /dev/vg1/lv1 # 檢查文件系統 [root@centos7~]#fsck -f /dev/vg1/lv1 fsck from util-linux 2.23.2 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/mapper/vg1-lv1: 12/786432 files (0.0% non-contiguous), 354533/3145728 blocks # 縮減文件系統 [root@centos7~]#resize2fs /dev/vg1/lv1 10G resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/vg1/lv1 to 2621440 (4k) blocks. The filesystem on /dev/vg1/lv1 is now 2621440 blocks long. # 縮減邏輯卷 [root@centos7~]#lvreduce -L 10G /dev/vg1/lv1 WARNING: Reducing active logical volume to 10.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce lv1? [y/n]: y Size of logical volume vg1/lv1 changed from 12.00 GiB (3072 extents) to 10.00 GiB (2560 extents). Logical volume lv1 successfully resized. # 掛載 [root@centos7~]#mount /dev/vg1/lv1 /mnt/lv1 完成
5、LVM快照
快照是對邏輯卷上的數據進行備份的空間,但是只備份修改的數據,最大空間與LVM相同。
<1>創建快照
[root@centos7~]#lvcreate -n lv1-snapshot -L 4G -p r -s /dev/vg1/lv1 # 先創建4G,默認只讀 Logical volume "lv1-snapshot" created.
<2>建立目錄
[root@centos7~]#mkdir /mnt/lv1-snapshot
<3>查看邏輯卷大小
[root@centos7/mnt/lv1-snapshot]#df Filesystem 1K-blocks Used Available Use% Mounted on ... /dev/mapper/vg1-lv1 10190100 1085468 8563964 12% /mnt/lv1 tmpfs 100136 0 100136 0% /run/user/0 /dev/mapper/vg1-lv1--snapshot 10190100 1085468 8563964 12% /mnt/lv1-snapshot # 和lv1大小相同 [root@centos7/mnt/lv1]#ls f1 lost+found [root@centos7/mnt/lv1-snapshot]#ls f1 lost+found # 雖然表面上和原始目錄的文件大小一樣,但實際并不占用空間
[root@centos7/mnt/lv1-snapshot]#lvdisplay ... --- Logical volume --- ... LV Size 10.00 GiB Current LE 2560 COW-table size 4.00 GiB COW-table LE 1024 Allocated to snapshot 0.00% # lvdisplay驗證:快照空間并未使用 Snapshot chunk size 4.00 KiB Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:
<4>修改數據
[root@centos7/mnt/lv1]#dd if=/dev/zero of=f2 bs=1M count=500 # 添加數據 500+0 records in 500+0 records out 524288000 bytes (524 MB) copied, 2.12795 s, 246 MB/s
<5>再次查看邏輯卷
[root@centos7/mnt/lv1-snapshot]#lvdisplay ... --- Logical volume --- ... # open 1 LV Size 10.00 GiB Current LE 2560 COW-table size 4.00 GiB COW-table LE 1024 Allocated to snapshot 12.26% # 空間已經被占用,存儲的是修改的數據 Snapshot chunk size 4.00 KiB Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:6 ... [root@centos7~]#df -h Filesystem Size Used Avail Use% Mounted on ... /dev/mapper/vg1-lv1 9.8G 1.6G 7.7G 17% /mnt/lv1 tmpfs 98M 0 98M 0% /run/user/0 /dev/mapper/vg1-lv1--snapshot 9.8G 1.1G 8.2G 12% /mnt/lv1-snapshot # 依然占用12% [root@centos7/mnt/lv1]#ls f1 f2 lost+found [root@centos7/mnt/lv1-snapshot]#ls f1 lost+found
6、刪除操作
不論是LV,還是snapshot,創建之后的如果要刪除,是不可以直接刪除PV的,要在卸載的基礎上刪除。并且Centos7之后,可以直接刪除VG,來很方便的刪除LV和snapshot,如果系統不支持,可以先刪除LV或者snapshot,再刪除VG,PV
原創文章,作者:mfwing,如若轉載,請注明出處:http://www.www58058.com/42772