[Linux] 加大 CentOS 機器上 LVM 磁碟的空間

[Linux] 加大 CentOS 機器上 LVM 磁碟的空間

原本在 AWS 的 CentOS 8.2 機器上,準備了 200 GB 的 EBS 空間,

給專案的建置機器使用,想說應該足夠了。

結果才跑一下,磁碟空間就爆了,

檢查了一下,發現磁碟空間並沒有 200 GB,

/ 只有 36 GB,/home 也只有 18 GB,加起來頂多 54 GB:

root@localhost ~ # df -h

Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/cl-root   36G   22G   14G  61% /
/dev/mapper/cl-home   18G   18G  176K 100% /home
/dev/xvda1           976M  214M  695M  24% /boot

 

AWS 當然是不可能騙我們…

用 fdisk 指令可以看到實體硬碟 /dev/xvda 確實有 200 GB 的容量:

root@localhost ~ # fdisk -l

Disk /dev/xvda: 200 GiB, 214748364800 bytes, 419430400 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
Disklabel type: dos
Disk identifier: 0xe44e88fe

Device     Boot   Start       End   Sectors Size Id Type
/dev/xvda1 *       2048   2099199   2097152   1G 83 Linux
/dev/xvda2      2099200 125829119 123729920  59G 8e Linux LVM


Disk /dev/mapper/cl-root: 35.6 GiB, 38235275264 bytes, 74678272 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/cl-swap: 6 GiB, 6442450944 bytes, 12582912 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/cl-home: 17.4 GiB, 18668847104 bytes, 36462592 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

 

好消息是因為 CentOS 預設有使用 LVM,因此調整磁碟空間比較容易。

(但說容易大概是對 Linux 超級熟的人來說的…

依我做過一次的經驗來說,其實不算很容易…)

 

1. 檢視目前的磁碟狀況

這個基本上是用各種指令,

查看一下 Physical volume 和 Logical volume 的目前配置。

像是用 pvdisplay 看一下 physical volume 的大小,目前是約 59 GB,

可見 physical volume 因為受到分割表 (partition table) 的限制,只看的到這個容量:

root@localhost ~ # pvdisplay

--- Physical volume ---
PV Name               /dev/xvda2
VG Name               cl
PV Size               <59.00 GiB / not usable 3.00 MiB
Allocatable           yes (but full)
PE Size               4.00 MiB
Total PE              15103
Free PE               0
Allocated PE          15103
PV UUID               fcOMye-trU3-g4hN-51Xi-kGov-Tgsw-wy6ZGb

 

用 vgdisplay 查看目前的 volume groups,目前只有一個 cl 群組:

root@localhost ~ # vgdisplay

  --- Volume group ---
  VG Name               cl
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <59.00 GiB
  PE Size               4.00 MiB
  Total PE              15103
  Alloc PE / Size       15103 / <59.00 GiB
  Free  PE / Size       0 / 0
  VG UUID               1UWni4-qWkt-3HNN-Ifqc-yu19-7yb8-ulamVm

 

用 lvdisplay 查看目前的 logical volumes,

有 /dev/cl/swap, /dev/cl/home, 和 /dev/cl/root 三組:

root@localhost ~ # lvdisplay

  --- Logical volume ---
  LV Path                /dev/cl/swap
  LV Name                swap
  VG Name                cl
  LV UUID                kMCR2N-OGVr-xnVd-l13x-F3hT-A1Nj-o1jyAK
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2020-07-28 09:35:42 +0800
  LV Status              available
  # open                 2
  LV Size                6.00 GiB
  Current LE             1536
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

  --- Logical volume ---
  LV Path                /dev/cl/home
  LV Name                home
  VG Name                cl
  LV UUID                St3Doq-bDHO-wYAL-KEnf-WkHu-ZD6Z-eknf24
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2020-07-28 09:35:42 +0800
  LV Status              available
  # open                 1
  LV Size                <17.39 GiB
  Current LE             4451
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

  --- Logical volume ---
  LV Path                /dev/cl/root
  LV Name                root
  VG Name                cl
  LV UUID                kwfk1v-uhsf-7qMT-oyxS-KiVW-yQXG-cky3lS
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2020-07-28 09:35:43 +0800
  LV Status              available
  # open                 1
  LV Size                <35.61 GiB
  Current LE             9116
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

 

用 lvs 可以查看每個 logical volume 的大小:

root@localhost ~ # lvs

  LV   VG Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home cl -wi-ao---- <17.39g
  root cl -wi-ao---- <35.61g
  swap cl -wi-ao----   6.00g

 

2. 使用 fdisk 調整分割表

接著,就開始用 fdisk 來調整分割表吧~

fdisk 是破壞性的動作,因此每一步都要很小心的確認,

正確才進行下一步,不然你的硬碟資料可能就毀了…

 

執行 fdisk 並指定正確的硬碟 (我的是 /dev/xvda):

root@localhost ~ # fdisk /dev/xvda


Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

 

執行 m 可以看一下 fdisk 的說明:

Command (m for help): m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table

 

執行 p 看一下現在的 partition table,目前我們的 /dev/xvda2 就是 59GB:

Command (m for help): p
Disk /dev/xvda: 200 GiB, 214748364800 bytes, 419430400 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
Disklabel type: dos
Disk identifier: 0xe44e88fe

Device     Boot   Start       End   Sectors Size Id Type
/dev/xvda1 *       2048   2099199   2097152   1G 83 Linux
/dev/xvda2      2099200 125829119 123729920  59G 8e Linux LVM

 

按 F 可以看硬碟的未使用空間,可以看到目前有 140 GB 的未使用空間,

加上原本的 59GB 差不多就是 200 GB (AWS 沒有騙我們啦 XD):

Command (m for help): F
Unpartitioned space /dev/xvda: 140 GiB, 150323855360 bytes, 293601280 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes

    Start       End   Sectors  Size
125829120 419430399 293601280  140G

 

接下來,我們要把原本的 /dev/xvda2 這個 LVM 分割區刪掉重建,

按下 d 之後,要輸入刪除的分割區號碼,

我們有 /dev/xvda1 和 /dev/xvda2 兩個分割區,

要刪除的是 /dev/xvda2 因此選 2 (千萬別選錯了!!):

Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

 

接著,按下 n 建立一個新的分割區。

按下 p 建立主要 (primary) 分割區,分割區號碼一樣用 2,

起始與結束磁區都直接按 Enter 使用預設值,

基本上就會把剩下所有的空間都佔滿。

接著它會問說是不是要移除掉 LVM2_member 的標記,

我不太確定這是什麼,不過爬文是說絕對不要刪除掉,

因此就按 n 不要移除標記囉:

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (2099200-419430399, default 2099200):
Last sector, +sectors or +size{K,M,G,T,P} (2099200-419430399, default 419430399):

Created a new partition 2 of type 'Linux' and of size 199 GiB.
Partition #2 contains a LVM2_member signature.

Do you want to remove the signature? [Y]es/[N]o: n

 

接著,這個新建立的分割區預設型態是 Linux,我們要把它改成 Linux LVM,

因此按下 t 來改變型態,

一樣輸入 2 表示要處理 /dev/xvda2 分割表,

至於型態代碼不知道的話,可以按 L 列出所有的代碼,

而我們要用的 Linux LVM 就是 8e,因此接下來輸入 8e:

Command (m for help): t
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden or  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi ea  Rufus alignment
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         eb  BeOS fs
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ee  GPT
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        ef  EFI (FAT-12/16/
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f0  Linux/PA-RISC b
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f1  SpeedStor
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f4  SpeedStor
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      f2  DOS secondary
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fb  VMware VMFS
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fc  VMware VMKCORE
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fd  Linux raid auto
1c  Hidden W95 FAT3 75  PC/IX           bc  Acronis FAT32 L fe  LANstep
1e  Hidden W95 FAT1 80  Old Minix       be  Solaris boot    ff  BBT
Hex code (type L to list all codes): 8e

Changed type of partition 'Linux' to 'Linux LVM'.

 

按 p 看一下現在的分割表,

/dev/xvda2 已經是 199GB 了,且型態也是正確的 Linux LVM:

Command (m for help): p
Disk /dev/xvda: 200 GiB, 214748364800 bytes, 419430400 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
Disklabel type: dos
Disk identifier: 0xe44e88fe

Device     Boot   Start       End   Sectors  Size Id Type
/dev/xvda1 *       2048   2099199   2097152    1G 83 Linux
/dev/xvda2      2099200 419430399 417331200  199G 8e Linux LVM

 

確認都沒問題後,就可以按 w 將修改後的分割表寫入硬碟

(在這之前的修改都還只存在記憶體中,可以隨意測試,

但是按下 w 寫入後,就沒有回頭路了!!)

Command (m for help): w
The partition table has been altered.
Syncing disks.

 

修改完分割表後,重新開機,

讓系統可以讀到新的分割表資料~

 

3. 擴展 Physical Volume

執行 pvs 查看目前的 physical volume 大小,

仍然是 59 GB,並沒有因為分割表的調整,而自動放大:

root@localhost ~ # pvs

  PV         VG Fmt  Attr PSize   PFree
  /dev/xvda2 cl lvm2 a--  <59.00g    0

 

執行 pvresize /dev/xvda2,

讓這個 physical volume 依分割表的大小來擴展:

root@localhost ~ # pvresize /dev/xvda2

  Physical volume "/dev/xvda2" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized

 

跑 pvdisplay 再看一下,這個 physical volume 現在已經變成 199 GB 了:

root@localhost ~ # pvdisplay

  --- Physical volume ---
  PV Name               /dev/xvda2
  VG Name               cl
  PV Size               <199.00 GiB / not usable 2.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              50943
  Free PE               35840
  Allocated PE          15103
  PV UUID               fcOMye-trU3-g4hN-51Xi-kGov-Tgsw-wy6ZGb

 

pvs 也顯示出相同的結果,

同時它也列出了目前還有 140 GB 的可用空間:

root@localhost ~ # pvs

  PV         VG Fmt  Attr PSize    PFree
  /dev/xvda2 cl lvm2 a--  <199.00g 140.00g

 

4. 擴展 Logical Volume

這時就可以來擴展 logical volume 了~

執行 lvextend -r -l +100%FREE /dev/cl/root 來擴展 /dev/cl/root 這個 logical volume,

讓它吃下 physical volume 剩餘所有空間:

root@localhost ~ # lvextend -r -l +100%FREE /dev/cl/root

  Size of logical volume cl/root changed from <35.61 GiB (9116 extents) to <175.61 GiB (44956 extents).
  Logical volume cl/root successfully resized.
meta-data=/dev/mapper/cl-root    isize=512    agcount=4, agsize=2333696 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=9334784, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=4558, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 9334784 to 46034944

 

指令成功,我們再下 df -h 來確認一下磁碟的使用狀況,

可以看到我們的根目錄 / 現在整體空間已經變成 176 GB 了,

也就是原本的 36 GB 加上擴展出來的 140 GB 空間:

root@localhost ~ # df -h

Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             7.8G     0  7.8G   0% /dev
tmpfs                7.8G     0  7.8G   0% /dev/shm
tmpfs                7.8G  8.5M  7.8G   1% /run
tmpfs                7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/mapper/cl-root  176G   23G  153G  13% /
/dev/mapper/cl-home   18G   18G  176K 100% /home
/dev/xvda1           976M  214M  695M  24% /boot
tmpfs                1.6G     0  1.6G   0% /run/user/0
tmpfs                1.6G     0  1.6G   0% /run/user/1000

 

查看一下根目錄下的檔案,看起來也都沒有問題。

這樣,LVM 磁碟的空間擴展就成功完成啦~

真的很有成就感呢!^^/

 

參考資料:

LVM2: Extend file system – Tech Garage

Increase the size of the /dev/mapper/centos_centos7-root | vStack

(本頁面已被瀏覽過 3,426 次)

6 thoughts on “[Linux] 加大 CentOS 機器上 LVM 磁碟的空間

  1. 真的是看了這們多 就妳這邊最讚了
    另外請問假這我分割後再擴展 這樣我的資料不見嗎?

    1. 不確定您的操作,
      假設是照文章裡的步驟的話,
      被擴展的那個分割區裡的資料是會保留的喔~

  2. 感謝作者詳細的圖文說明
    爬了很多文章
    只有看到您的是有做刪除sda再新建sda的。
    很多文章都是直接新建一個sda,就不是把原先的pv做擴展了,而是將多出的空間新建一個新的sda。
    本來怕怕的先用測試機練習,確認資料都沒遺失才再正式機上作業。
    真是太感謝能找到這篇文章,謝謝!!!

發佈回覆給「ephrain」的留言 取消回覆

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料