[Linux]LVM擴展卷


LVM

LVM是邏輯盤卷管理(Logical Volume Manager)的簡稱,它是Linux環境下對磁盤分區進行管理的一種機制,LVM是建立在硬盤和分區之上的一個邏輯層,來提高磁盤分區管理的靈活性。LVM是在磁盤分區和文件系統之間添加的一個邏輯層,來為文件系統屏蔽下層磁盤分區布局,提供一個抽象的盤卷,在盤卷上建立文件系統。物理卷(physical volume)物理卷就是指硬盤分區或從邏輯上與磁盤分區具有同樣功能的設備(如RAID),是LVM的基本存儲邏輯塊,但和基本的物理存儲介質(如分區、磁盤等)比較,卻包含有與LVM相關的管理參數。

  • 優勢:能夠在線擴容當前的分區容量。

  • 結構:在整個LVM結構當中以此分為:“物理卷、物理卷分區、卷組、邏輯卷”,下面我詳細解釋一下這四種結構:

  1. 物理卷:Physical Volume,簡稱PV,一個物理卷只不過是一個有LVM管理數據添加在里面的物理存儲介質。要使用LVM系統,首先對要用於LVM的磁盤進行初始化,初始化的目的就是將磁盤或分區標識為LVM 的物理卷。使用pvcreate 命令可以將一個磁盤標記為 LVM 物理卷。

  2. 物理分區:Physical Extents,簡稱PE,LVM將每個物理卷分別叫做物理分區的可尋址存儲單元,存儲單元的大小通常為幾MB。磁盤的開頭部分為LVM元數據,之后從索引為零開始,每個物理分區的索引依次遞增一,按順序進行分配。

  3. 卷組:Volume Group,簡稱VG,物理卷可以組織為卷組。卷組可以由一個或多個物理卷組成,同時系統中可以有多個卷組。創建了卷組之后,該卷組(而不是磁盤)便是表示數據存儲的實體。因此,盡管以前是將磁盤從一個系統移動到另一個系統,使用了 LVM 之后,會將卷組從一個系統移動到另一個系統。出於這種原因,通常在一個系統上創建多個卷組會比較方便。

  4. 邏輯分區:Logical Extents,簡稱LE,邏輯卷的基本分配單元稱為邏輯分區。邏輯分區映射到物理分區,因此,如果物理分區的尺寸小為4MB,那么邏輯分區的尺寸也將為4MB。邏輯卷的大小取決於所分配的邏輯分區數量。

生產環境

[root@Nexus3 ~]# cat /etc/redhat-release 
CentOS Linux release 7.7.1908 (Core)
[root@Nexus3 ~]# lvm version
  LVM version:     2.02.185(2)-RHEL7 (2019-05-13)
  Library version: 1.02.158-RHEL7 (2019-05-13)
  Driver version:  4.37.1

硬盤擴容20G->100G

[root@Nexus3 ~]# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  3.9G     0  3.9G   0% /dev
tmpfs                   tmpfs     3.9G     0  3.9G   0% /dev/shm
tmpfs                   tmpfs     3.9G  8.8M  3.9G   1% /run
tmpfs                   tmpfs     3.9G     0  3.9G   0% /sys/fs/cgroup
-----/dev/mapper/centos-root是我們需要擴的卷-------------------------
/dev/mapper/centos-root xfs        17G  1.2G   16G   7% /
/dev/sda1               xfs      1014M  136M  879M  14% /boot
tmpfs                   tmpfs     799M     0  799M   0% /run/user/0

查看本地物理硬盤

[root@Nexus3 ~]# fdisk -l
-----磁盤容量100G-------------------------
Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000c35ff

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
-----正在使用18.2G,我們需要將此硬盤擴容-------------------------
Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

硬盤分區

根據實際情況選擇自己的硬盤

[root@Nexus3 ~]# fdisk /dev/sda
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/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000c35ff

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
-----注意后面Id System 后面用到-------------------------
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
-----創建分區n,一路回車就會創建sda3全部容量-------------------------
Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): 
Using default response p
Partition number (3,4, default 3): 
First sector (41943040-209715199, default 41943040): 
Using default value 41943040
Last sector, +sectors or +size{K,M,G} (41943040-209715199, default 209715199): 
Using default value 209715199
Partition 3 of type Linux and of size 80 GiB is set
-----驗證分區-------------------------
Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000c35ff

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
-----看看后面Id System需要修改-------------------------
/dev/sda3        41943040   209715199    83886080   83  Linux
-----修改Id-------------------------
Command (m for help): t
Partition number (1-3, default 3): 
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000c35ff

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
/dev/sda3        41943040   209715199    83886080   8e  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.
-----同步磁盤狀態-------------------------
[root@Nexus3 ~]# partprobe

創建物理卷

  • pvcreate <物理分區>
-----查詢當前PV-------------------------
[root@Nexus3 ~]# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <19.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              4863
  Free PE               0
  Allocated PE          4863
  PV UUID               2MrxDf-dTQK-I7iK-5VUs-bcrv-Af2t-fVaAiL
-----創建新的PV-------------------------
[root@Nexus3 ~]# pvcreate /dev/sda3 
  Physical volume "/dev/sda3" successfully created.
[root@Nexus3 ~]# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <19.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              4863
  Free PE               0
  Allocated PE          4863
  PV UUID               2MrxDf-dTQK-I7iK-5VUs-bcrv-Af2t-fVaAiL

-----這個是我們創建的-------------------------   
  "/dev/sda3" is a new physical volume of "80.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda3
  VG Name               
  PV Size               80.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               kfO9Yc-ToQb-2vnf-8muc-MNsZ-oxJV-vi1vVe

擴展卷組

  • vgextend <卷組VG Name> <物理卷PV Name>
-----查看當前VG-------------------------
[root@Nexus3 ~]# vgdisplay 
  --- Volume group ---
-----VG Name需要用到-------------------------
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <19.00 GiB
  PE Size               4.00 MiB
  Total PE              4863
  Alloc PE / Size       4863 / <19.00 GiB
  Free  PE / Size       0 / 0   
  VG UUID               Wx79ds-a3LO-VhXo-HJdk-fr0X-C9UX-KOqfSK
-----擴展卷組-------------------------
[root@Nexus3 ~]# vgextend centos /dev/sda3 
  Volume group "centos" successfully extended

邏輯卷擴展

  • lvextend -l +100%free </dev/mapper/centos-root> #前面df -hT看到的信息
-----查看邏輯卷-------------------------
[root@Nexus3 ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                bgsynr-YUZE-LX2j-NIgG-sqqU-wVxb-GIZDg7
  LV Write Access        read/write
  LV Creation host, time localhost, 2019-11-22 14:16:37 +0800
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
-----我們需要擴展該卷-------------------------
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                lkRm4c-8g6X-ZWm2-Y86h-aKXs-i2lN-ZKdGaD
  LV Write Access        read/write
  LV Creation host, time localhost, 2019-11-22 14:16:37 +0800
  LV Status              available
  # open                 1
  LV Size                <17.00 GiB
  Current LE             4351
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
-----擴充-------------------------
[root@Nexus3 ~]# lvextend -l +100%free /dev/mapper/centos-root 
  Size of logical volume centos/root changed from <17.00 GiB (4351 extents) to 96.99 GiB (24830 extents).
  Logical volume centos/root successfully resized.

動態格式化擴容磁盤

[root@Nexus3 ~]# xfs_growfs /dev/mapper/centos-root 
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=1113856 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=4455424, 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=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 4455424 to 25425920

驗證

[root@Nexus3 ~]# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  3.9G     0  3.9G   0% /dev
tmpfs                   tmpfs     3.9G     0  3.9G   0% /dev/shm
tmpfs                   tmpfs     3.9G  8.9M  3.9G   1% /run
tmpfs                   tmpfs     3.9G     0  3.9G   0% /sys/fs/cgroup
-----100G-------------------------
/dev/mapper/centos-root xfs        97G  1.2G   96G   2% /
/dev/sda1               xfs      1014M  136M  879M  14% /boot
tmpfs                   tmpfs     799M     0  799M   0% /run/user/0

END


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM