LVM卷擴容

1.簡介

LVM是Logical Volume Manager(邏輯卷管理)的縮寫,是Linux環境下對磁盤分區進行管理的一種機制。
LVM邏輯卷三種狀態:

  1. PV: Physical Volume 物理卷
  2. VG: Volume Group 卷組
  3. LV: Logical Volume 邏輯卷

創建順序:

mkpart
LV
VG
PV

2.問題介紹

通過df查看,/home所在目錄空間即將用盡,
df.png
而且通過lsblk查看磁盤空間,發現可以分配給此LVM卷的空間只剩餘100G。如何增加此邏輯卷的容量呢?
img-w2EFXMy4-1585218480437
此時,可以選擇直接擴展磁盤剩餘的100G空間給該邏輯卷,本爲未對此做介紹,但是已經包含了執行方法,本文介紹是整個該邏輯卷所在的磁盤分區空間用盡時,如何增加邏輯卷的空間。

3.擴展邏輯卷所在分區空間的方法

兩種方法:

  1. 既然邏輯卷所在的分區空間用盡,那麼通過新建一個磁盤分區,在該磁盤創建PV,添加到邏輯磁盤所在的VG中,即可以實現邏輯卷的free PE擴展
  2. 通過先卸載此邏輯卷的掛載,通過磁盤管理工具修改該邏輯卷所在分區的大小,然後通過PV擴展添加到邏輯卷中。

3.1 新建磁盤分區添加到PV中

3.1.1 未擴展前PV狀態以及文件系統狀態

PV狀態

8cc36150c36d3f7793819328b7f80733.png

文件系統狀態

57ff168fd8f44e738bdbeee059c708a4

3.1.2 新建分區

可以通過fdisk,parted,gpart等工具新建磁盤分區,本文通過fdisk新建分區

[root@localhost 7:0:0:0]# fdisk /dev/sdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): p

Disk /dev/sdb: 40002.3 GB, 40002251653120 bytes, 78129397760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: gpt
Disk identifier: C15EE16D-E5C8-41A4-8CC2-F4702CF772F3

# Start End Size Type Name
1 2048 1048578047 500G Linux LVM

#新建分區,此處我將磁盤剩餘容量都分配給新分區

Command (m for help): n
Partition number (2-128, default 2):
First sector (34-78129397726, default 1048578048):
Last sector, +sectors or +size{K,M,G,T,P} (1048578048-78129397726, default 78129397726):
Created partition 2

Command (m for help): p

#查看分區創建成功
Disk /dev/sdb: 40002.3 GB, 40002251653120 bytes, 78129397760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: gpt
Disk identifier: C15EE16D-E5C8-41A4-8CC2-F4702CF772F3

# Start End Size Type Name
1 2048 1048578047 500G Linux LVM
2 1048578048 78129397726 35.9T Linux filesyste>
#設定分區類型爲Linux LVM,編號爲31
Command (m for help): t
Partition number (1,2, default 2):
Partition type (type L to list all types): 31
Changed type of partition ‘Linux filesystem’ to ‘Linux LVM’

Command (m for help): p
#查看分區類型
Disk /dev/sdb: 40002.3 GB, 40002251653120 bytes, 78129397760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: gpt
Disk identifier: C15EE16D-E5C8-41A4-8CC2-F4702CF772F3

# Start End Size Type Name
1 2048 1048578047 500G Linux LVM
2 1048578048 78129397726 35.9T Linux LVM
#寫入分區表,並退出
Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
#根據提示執行partprobe,實現分區同步
[root@localhost 7:0:0:0]# partprobe

3.1.3 對新分區創建PV

pvcreate /dev/sdb2 對新分區創建PV

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-g8g1RNWu-1585218480450)(https://i.loli.net/2020/03/26/8mIHJxinZWztKDL.png)]

3.1.4 擴展該PV到VG

13a66be0dde2db8dea06cc0ae8075d98.png

擴展後查看VG

9841c5a5d34f8a0384f8169617c6a6b0.png

3.1.5 擴展LV

這裏選擇擴展1TB容量到LV, 注意命令中的參數-r可以直接resizefs,如果不加此參數,文件系統看到的卷大小還是原來的大小,需要額外使用resize2fs或者xfs_growfs來調整文件系統大小。

8795908b7f9f5e0a48cb4ce1761e7131.png

擴展後查看LV

d42978607dd898f98798ffcfed651365.png

此時執行partprobe同步文件系統,df查看,可以看到該LVM卷已經擴展

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-MlJdKzwM-1585218480465)(https://i.loli.net/2020/03/26/t3R2gOayP8JbKNx.png)]

3.2 通過擴展物理分區來擴展LVM卷

此方法只適用於物理分區爲最後一個,如下圖中分區3,分區1和分區2無法擴展容量,除非壓縮其他分區大小,可能會造成文件丟失,且磁盤有足夠剩餘空間。

868453dc830f9e19d75b50a82bbc503e.png

3.2.1 未擴展前PV狀態以及文件系統狀態

LV狀態如下

d619f23171bbd0c1d59864630c4bd11c.png

文件系統狀態

c0248897965b09ff6e0e2f1ea976c0b4.png

**磁盤狀態

ccaf1c268531bdb1e24e3c9880055e97.png

3.2.2 卸載目錄,並擴展分區大小

擴展分區大小前,需要先卸載掛載的邏輯卷,使用parted工具擴展分區大小。但是fdisk工具沒有直接擴展分區的功能,需要先刪除分區,然後新建分區,新建分區的開始位置必須和原來一樣,結束位置大於原分區位置,這個操作有風險,可能會丟失數據

以下以fdisk實例刪除分區後重新創建分區。

#先卸載目錄
[root@localhost ~]# umount /mnt/
[root@localhost ~]# fdisk /dev/sdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

#刪除原分區
Command (m for help): d
Selected partition 1
Partition 1 is deleted
#新建分區,開始位置必須和原來一樣的。
Command (m for help): n
Partition number (1-128, default 1):
First sector (2048-3750748814, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-3750748814, default 3750748814): +20G
Created partition 1

Command (m for help): p

Disk /dev/sdb: 1920.4 GB, 1920383410176 bytes, 3750748848 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: gpt
Disk identifier: 6290401B-C5C6-45C1-8286-50684ED971F8

# Start End Size Type Name
1 2048 41945087 20G Linux filesyste
#設定分區類型爲LVM
Command (m for help): t
Selected partition 1
Partition type (type L to list all types): 31
Changed type of partition ‘Linux filesystem’ to ‘Linux LVM’

Command (m for help): p

Disk /dev/sdb: 1920.4 GB, 1920383410176 bytes, 3750748848 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: gpt
Disk identifier: 6290401B-C5C6-45C1-8286-50684ED971F8

# Start End Size Type Name
1 2048 41945087 20G Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
#此時如下圖,分區大小還沒有變,需要執行partprobe同步分區表
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 1.8T 0 disk
└─sdb1 8:17 0 9.3G 0 part
└─Hansen_disk-lvol0 253:3 0 9.3G 0 lvm
#再次查看,分區已經擴展
[root@localhost ~]# partprobe
[root@localhost ~]# lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 1.8T 0 disk
└─sdb1 8:17 0 20G 0 part
└─Hansen_disk-lvol0 253:3 0 9.3G 0 lvm

3.2.3 擴展PV

使用pvresize命令擴展PV

[root@localhost ~]# pvresize /dev/sdb1
Physical volume “/dev/sdb1” changed
1 physical volume(s) resized or updated / 0 physical volume(s) not resized

然後查看PV

[root@localhost ~]# pvdisplay /dev/sdb1
— Physical volume —
PV Name /dev/sdb1
VG Name Hansen_disk
PV Size <20.00 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 5119
Free PE 2736
Allocated PE 2383
PV UUID hxwwoL-uHu1-nxII-B5rr-780P-4ODl-A18afG

可以看到PV已經擴展了,剩餘的工作和上文類似,擴展VG,然後擴展LV卷即可,請參考上一個方法。此方法使用lvextend擴展的時候需要注意擴展大小,應爲分區表不對應,需要比Free PE少一點才能擴展成功

[root@localhost ~]# lvextend -L +704M -r /dev/Hansen_disk/lvol0

Logical volume Hansen_disk/lvol0 successfully resized.
meta-data=/dev/mapper/Hansen_disk-lvol0 isize=512 agcount=10, agsize=262144 blks
= sectsz=4096 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=2440192, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=4096 sunit=1 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 2440192 to 5061632
[root@localhost ~]# lvdisplay Hansen_disk
— Logical volume —
LV Path /dev/Hansen_disk/lvol0
LV Name lvol0
VG Name Hansen_disk
LV UUID I3vlfg-tA3f-7B9g-Cb8B-MQ4C-vdxj-UYNsQ7
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2020-03-25 12:04:44 +0800
LV Status available
# open 0
LV Size <20.00 GiB
Current LE 5119
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:3

4. 總結

在分區空間不足,分區所在硬盤空間充足的情況下,可以看到以上兩種方法都可以實現LVM卷擴容,而且更重要的是保持原分區卷的數據。相對來說兩種方法各有利弊。

方法 優點 缺點
方法1 通過新建分區擴展LVM卷,不需要卸載願有分區,數據更加安全 磁盤需要有剩餘空間新建分區
方法2 通過擴展原物理分區實現擴展LVM卷,不需要新建一個分區,在原有分區上操作即可 需要卸載分區,有風險損壞分區數據,擴展分區需要有可擴展空間

5. 歡迎交流關注,謝謝

cli_300px.png

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章