1. 程式人生 > >記一次阿里雲LVM擴容與 LVM 相關知識學習

記一次阿里雲LVM擴容與 LVM 相關知識學習

一、lvm 擴容

問題: 我們阿里雲伺服器有一個磁碟容量為 1T ,但是最近由於業務的擴增,磁碟容量已經不夠了,需要增大磁碟的容量。磁碟掛載在 /home,使用的是 LVM。我們現在需要對磁碟進行擴容。

  1. 通過增加新的磁碟,然後將磁碟新增到卷組(VG),然後再將邏輯卷(LV)擴容。
  2. 擴容原有的磁碟。然後再將邏輯卷(LV)擴容。

1.1、方式一(增加新的磁碟)

增加新的磁碟和 原有硬碟做了分割槽基本一致。

實際操作

# 我們增加了一塊硬碟,/dev/vdc
# 建立分割槽
[root@djx ~]# fdisk /dev/vdc
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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xd9ed71fb.

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-1824522239, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-1824522239, default 1824522239): 
Using default value 1824522239
Partition 1 of type Linux and of size 870 GiB is set

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

Calling ioctl() to re-read partition table.
Syncing disks.
# 將磁碟加入到 VG卷組。
[root@djx ~]# vgextend sdr_vg  /dev/vdc1
  Physical volume "/dev/vdc1" successfully created.
  Volume group "sdr_vg" successfully extended
# 擴容邏輯卷  /dev/sdr_vg/lv_data,-l 指定的是PE數量 -L +800GB
[root@djx ~]# lvextend  -l +222719 /dev/sdr_vg/lv_data
  Size of logical volume sdr_vg/lv_data changed from <1024.00 GiB (262143 extents) to <1.85 TiB (484862 extents).
  Logical volume sdr_vg/lv_data successfully resized.
# 修改檔案系統的大小,xfs 檔案系統使用xfs_growfs。 
[root@djx ~]# xfs_growfs  /dev/sdr_vg/lv_data
meta-data=/dev/mapper/sdr_vg-lv_data isize=512    agcount=4, agsize=67108608 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=268434432, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=131071, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 268434432 to 496498688
# 檢視磁碟是否擴容了。
[root@djx ~]# df -h
Filesystem                  Size  Used Avail Use% Mounted on
/dev/vda1                    40G  5.6G   32G  15% /
devtmpfs                     16G     0   16G   0% /dev
tmpfs                        16G   12K   16G   1% /dev/shm
tmpfs                        16G  540K   16G   1% /run
tmpfs                        16G     0   16G   0% /sys/fs/cgroup
/dev/mapper/sdr_vg-lv_data  1.9T  965G  929G  51% /home
tmpfs                       3.2G     0  3.2G   0% /run/user/1007

1.2、方式二(擴容原有的磁碟)

原有硬碟做了分割槽

假設原有的磁碟為 /dev/xdc ,已有分割槽 /dev/xdc1,我們對磁碟 /dev/xdc 擴容了。

fdisk  /dev/xdc   # 依次輸入 n  --》 p --> 預設 --》 預設 --》 w   這樣就將新加的磁碟空間到一個新的分割槽/dev/xdc2,
# 我們將該分割槽加入到卷組。

vgextend  卷組的名稱  /dev/xdc2

vgdisplay  # 檢視卷組的空閒空間。

# 將擴容的空間新增到邏輯卷
lvextend -l +2558 /dev/vg_test/lv_test  # -l 指定的是PE數,我們可以使用 -L 來指定實際容量。 lvextend -L +9.99G /dev/vg_test/lv_test

# 修改檔案系統的大小
## ext* 使用的命令

resize2fs  /dev/vg_test/lv_test
## xfs 使用的命令
xfs_growfs /dev/vg_test/lv_test

原有硬碟未做分割槽

我們在阿里雲選擇磁碟擴容,並選擇線上擴容(限制條件見阿里雲官網文件)。擴容好了,我們可以在服務端進行使用 fidisk -l 檢視,我們可以發現磁碟已經變大了。但是我們的物理卷沒有變化。

# pv 物理卷擴容後重新識別大小。
pvresize  /dev/xdb
pvdisplay # 在 Free PE 就可以檢視到我們的新增加的空間大小

# 將擴容的空間新增到邏輯卷

lvextend -l +2558 /dev/vg_test/lv_test  # -l 指定的是PE數,我們可以使用 -L 來指定實際容量。 lvextend -L +9.99G /dev/vg_test/lv_test

# 修改檔案系統的大小
## ext* 使用的命令

resize2fs  /dev/vg_test/lv_test
## xfs 使用的命令
xfs_growfs /dev/vg_test/lv_test

二、擴充套件

LVM 的常用命令

待補充

LVM 的擴充套件限制

  • 磁碟LV大小限制
  • PE 數量限制

本部分內容來自 lvm 擴充套件限制 :https://www.cnblogs.com/kerrycode/p/8662780.html

建立VG時, LVM卷組(VG)的物理擴充套件單元(Physical Extends 縮寫PE)大小是固定的, 在Linux命令列中,vgcreate 命令的選項-s表示顯式設定卷組(VG)上物理卷(PV)上PE的大小。

如果你沒有明確設定PE的大小的話,PE大小預設為4MB,但是,一旦這個值設定了,如果不重建VG的話, PE大小是無法修改的。這將涉及邏輯捲上的資料備份和資料恢復。

就目前的LVM2而言 - LVM版本號 2.02.06(2006-05-12),庫版本為1.02.07(2006-05-11),驅動程式版本4.5.0 - 沒有LVM命令或工具,甚至在HPUX中使用vgmodify,也無法動態或線上模式下調整或更改現有VG的LVM PE大小!

因此,建議在建立LVM卷組之前正確計劃,例如,如果邏輯卷儲存的資料很有可能在不久的將來超過300G大小的話, 那麼你在建立VG的時候,就不能設定PE大小為4MB。

為了限制Linux核心記憶體使用量,每個邏輯卷(LV)有65,536個物理盤區(PE)的限制。因此,LVM中PE大小將直接決定邏輯卷(LV)的最大大小!例如,4MB PE大小(預設PE大小)將單個邏輯卷(LV)限制為256GB,16MB PE大小將限制單個LV增長超過1TB,等等。

除PE大小因素外,單個LV的最大尺寸也受CPU架構和Linux核心版本的限制:

Linux核心版本2.4.x將最大LV大小限制為2TB。

在2.4.x之前的一些較早的Linux核心中,最大LV大小限制為1TB(由塊層中的整數簽名問題引起 caused by the integer signedness problems in the block layer)。

32位CPU和Linux核心版本2.6.x的組合,邏輯卷大小的限制在16TB時最大化。

對於在64位CPU上執行的Linux核心2.6.x,最大LV大小為8EB(此時非常恐怖的大容量儲存!)

翻譯完成,下面是我Google搜尋到關於核心版本和CPU架構對邏輯卷的大小限制的描述資料。僅供參考。

 

 

·         For 2.4 based kernels, the maximum LV size is 2TB. For some older kernels, however, the limit was 1TB due to signedness problems in the block layer. Red Hat Enterprise Linux 3 Update 5 has fixes to allow the full 2TB LVs. Consult your distribution for more information in this regard.

·          

·         For 32-bit CPUs on 2.6 kernels, the maximum LV size is 16TB.

·          

·         For 64-bit CPUs on 2.6 kernels, the maximum LV size is 8EB. (Yes, that is a very large number.)

有一次建立了一個LV超過265G,但是PE Size為4M,這個讓我非常困惑,後面查了一下資料發現,以前有每個邏輯卷(LV)有65,536個物理盤區(PE)的限制,但是從LVM 2開始,沒有限制PE的數量。

Those limits (65536 LE per LV) does not apply to LVM 2 and 2.6 kernel.
Your LV can have much more LE (I dont know if there is even reachable
limit  for  this).  One  and only feedback (if you can notice this) is
that  userspace  programs for managing LVM works _little_ slower, when
there is enormous number od LE to administrate.
I tested it with 4GB LV on 16MB LE (but I didnt see difference)

我在上面進行擴容的時候,一直擔心有限制,但是在實際操作的時候是沒有發現限制的,因為我的核心版本也是比較新的,lvm 版本也是大的