超詳盡!Linux雲伺服器儲存擴容實操

騰訊雲加社群發表於2020-03-16
導語 | 隨著業務的發展,業務資料不停的增長,原有的磁碟空間可能會出現磁碟空間不夠用的情況,因此,需要對磁碟空間進行擴容,以滿足業務資料增長的需求。本文總結了Linux環境下雲伺服器儲存擴容的三種方式及其操作步驟,與大家一同交流。
環境說明:Linux作業系統:CentOS Linux release 7.6.1810 (Core)
平臺環境:騰訊雲CVM,CBS

1. 儲存擴容概述

以下圖為例,假如起初購買了100G磁碟空間,隨著資料的不停增長,原有100G空間不夠用了,將空間擴容至200G。

CBS是騰訊雲提供用於雲伺服器的永續性資料塊級儲存服務,雲盤具有彈性,按需擴容特性,雲盤可以任意的按需擴容。

超詳盡!Linux雲伺服器儲存擴容實操

不過雲盤擴容時不是所有的場景下都可以任意擴容,雲盤擴容具有兩個限制:

  • 系統盤不支援擴容,理論上系統盤也能夠支援擴容,系統盤存放作業系統資料,擴容可能會造成系統啟動異常
  • 本地盤不支援擴容,本地盤使用宿主機的本地盤,本地盤彈性擴容能力沒有云盤靈活,無法支援彈性按需擴容

要深入掌握雲盤的擴容,需要掌握雲盤的使用方式,在Linux下通常有如下三種使用方式:

  1. 裸裝置建立檔案系統,不需要建立任何分割槽,直接在裸盤上建立檔案系統
  2. 磁碟上建立MBR格式的分割槽,適用於小於2T的多個磁碟分割槽的場景
  3. 磁碟上建立GPT格式的分割槽,適用於大於2T的多個磁碟分割槽的場景

2. Linux雲伺服器儲存擴容

2.1. 裸裝置磁碟使用

Linux系統中可以直接在裸裝置磁碟上建立檔案系統,即磁碟不需要建立任何磁碟分割槽,直接在磁碟上創檔案檔案系統並掛載使用,其具有如下特點:

  • 磁碟不需要多個分割槽
  • 適用於大於2T磁碟
  • 簡單易使用
  • 後期支援線上擴容
超詳盡!Linux雲伺服器儲存擴容實操

上圖所示是在Linux系統中直接在裸裝置建立檔案系統的操作步驟,先在控制檯購買好雲盤並將其掛載到CVM中,然後登陸到Linux系統中對磁碟格式化檔案系統並掛載使用,控制檯購買和掛載請自行操作,如下演示在Linux系統中裸裝置檔案系統的使用步驟:

1、 登陸作業系統,通過fdisk -l獲取磁碟的名稱,系統有兩塊磁碟vda和vdb,/dev/vdb是我們操作的磁碟

[root@VM_0_89_centos ~]# fdisk -l

Disk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 sectors   #vda為系統盤
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: 0x0009ac89

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048   104857566    52427759+  83  Linux

Disk /dev/vdb: 107.4 GB, 107374182400 bytes, 209715200 sectors   #vdb為資料盤
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes複製程式碼

2、直接在磁碟上建立檔案系統(注:並未在磁碟上建立任何分割槽),根據需要格式化為ext4或xfs格式

[root@VM_0_89_centos ~]# mkfs.ext4 /dev/vdb		#建立檔案ext4檔案系統
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
6553600 inodes, 26214400 blocks
1310720 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2174746624
800 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000, 7962624, 11239424, 20480000, 23887872

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done  

備註:如果要建立為xfs則使用mkfs.xfs /dev/vdb複製程式碼

3、建立好檔案系統後即可掛載到系統中使用,使用方式有兩種:臨時mount和編寫fstab實現永續性掛載,線上環境推薦使用編寫fstab的方式,避免機器下次重啟後磁碟資訊未載入,掛載名稱推薦使用UUID

blkid獲取磁碟UUID資訊
[root@VM_0_89_centos ~]# blkid /dev/vdb    
/dev/vdb: UUID="7fb2c90a-fcd1-472c-b07c-8a20e2e9a436" TYPE="ext4"

編寫/etc/fstab,其內容如下
UUID="7fb2c90a-fcd1-472c-b07c-8a20e2e9a436" /data		  ext4 	  defaults	0 0 複製程式碼

4、掛載點目錄/data需要提前建立好,然後執行mount -a進行載入,mount -a是系統啟動時之行的指令,如果fstab編寫有誤要及時修復,避免影響下次開機啟動

執行掛載操作
[root@VM_0_89_centos ~]# mount -a 

檢查掛載情況
[root@VM_0_89_centos ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        485M     0  485M   0% /dev
tmpfs           496M   24K  496M   1% /dev/shm
tmpfs           496M  444K  496M   1% /run
tmpfs           496M     0  496M   0% /sys/fs/cgroup
/dev/vda1        50G  1.8G   46G   4% /
tmpfs           100M     0  100M   0% /run/user/0
/dev/vdb         99G   61M   94G   1% /data				#vdb磁碟已順利掛載到系統中,大小為100G複製程式碼

至此,基於Linux系統上使用裸盤構建檔案系統並使用操作演示完畢,通過操作可知,裸盤上建立檔案系統方式不需要對磁碟進行分割槽,操作便捷容易,且方便擴容,下章節中演示基於裸裝置檔案檔案系統儲存空間擴容。

2.2 裸裝置磁碟擴容

超詳盡!Linux雲伺服器儲存擴容實操

上述步驟是裸裝置磁碟的擴容方式,擴容過程中存在資料損壞的風險,為了避免擴容過程中誤操作,強烈建議擴容前對磁碟做快照,避免資料損壞時可以做恢復,謹記!做完快照後在控制檯完成磁碟的擴容,擴容完畢後需要在作業系統中識別到擴容的空間,以磁碟擴容至200G為例,演示在Linux中裸裝置擴容的操作過程:

1、 確認磁碟的使用方式,通過fdisk -l檢視磁碟並未建立有任何磁碟分割槽

[root@VM_0_89_centos ~]# fdisk -l /dev/vdb

Disk /dev/vdb: 214.7 GB, 214748364800 bytes, 419430400 sectors    #磁碟空間已擴容至200G
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes        #當前磁碟上未建立任何分割槽

#使用blkid檢視vdb磁碟檔案系統的情況,可得知,當前/dev/vdb未建立磁碟分割槽,切構建了ext4檔案系統
[root@VM_0_89_centos ~]# blkid
/dev/sr0: UUID="2020-01-13-22-30-05-00" LABEL="config-2" TYPE="iso9660" 
/dev/vda1: UUID="4b499d76-769a-40a0-93dc-4a31a59add28" TYPE="ext4" 
/dev/vdb: UUID="7fb2c90a-fcd1-472c-b07c-8a20e2e9a436" TYPE="ext4"   #直接在裸盤上建立ext4檔案系統複製程式碼

2、確認為雲盤的擴容方式為裸盤後,根據檔案系統的型別,在檔案系統層面擴容以識別到雲盤的擴容空間

[root@VM_0_89_centos ~]# resize2fs /dev/vdb 
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vdb is mounted on /data; on-line resizing required
old_desc_blocks = 13, new_desc_blocks = 25
The filesystem on /dev/vdb is now 52428800 blocks long.    #提示已經擴容至52428800個block

備註:如果是xfs,則使用xfs_growfs /dev/vdb擴容複製程式碼

3、校驗磁碟空間擴容情況,使用df -H校驗,vdb磁碟已在檔案系統擴容至200G

[root@VM_0_89_centos ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        485M     0  485M   0% /dev
tmpfs           496M   24K  496M   1% /dev/shm
tmpfs           496M  444K  496M   1% /run
tmpfs           496M     0  496M   0% /sys/fs/cgroup
/dev/vda1        50G  1.8G   46G   4% /
tmpfs           100M     0  100M   0% /run/user/0
/dev/vdb        197G   60M  188G   1% /data    #已擴容至200G複製程式碼

通過上述的操作演示可知,裸盤上並未建立任何磁碟分割槽,因此擴容過程相當便捷,直接在檔案系統上擴容即可,使用簡單,這也是雲上推薦使用方式。

2.3 MBR磁碟分割槽

如果磁碟需要建立多個分割槽並將分割槽掛載到不同的目錄中使用需要對磁碟進行分割槽,傳統的磁碟分割槽方式會使用MBR的格式進行分割槽,MBR分割槽具有如下特性:

  • 適用於小於2T的磁碟,大於2T的空間將無法識別
  • 單塊磁碟需要建立多個分割槽的場景
  • MBR最多支援7個分割槽,即3個主分割槽+4個擴充套件分割槽
超詳盡!Linux雲伺服器儲存擴容實操

如上圖是對磁碟製作MBR格式分割槽的操作步驟,在控制檯購買並掛載到CVM雲主機後,登入到作業系統中對磁碟進行分割槽和使用,如下以新購的一塊100G磁碟為例演示基於cbs雲盤上建立MBR磁碟分割槽的過程

1、使用fdisk -l獲取到到磁碟的名稱,包含兩塊磁碟vda和vdb,vdb是我們需要操作的cbs雲盤

[root@VM_0_89_centos ~]# fdisk -l

Disk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x0009ac89

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048   104857566    52427759+  83  Linux

Disk /dev/vdb: 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複製程式碼

2、使用fdisk工具對磁碟進行分割槽,fdisk /dev/vdb進入到fdisk互動式介面,通過互動式完成分割槽建立

[root@VM_0_89_centos ~]# fdisk  /dev/vdb
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 0x5eeb4bc8.

Command (m for help): m      #輸入m可以獲取到操作指令的幫助
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition             #刪除分割槽
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu                #獲取幫助
   n   add a new partition            #建立分割槽
   o   create a new empty DOS partition table
   p   print the partition table      #顯示分割槽情況
   q   quit without saving changes    #退出,不儲存
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit   #儲存分割槽設定
   x   extra functionality (experts only)

Command (m for help): n         #新建分割槽,輸入n進入到新建分割槽互動介面
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p            #選擇分割槽型別,p表示主分割槽,e代表擴充套件分割槽
Partition number (1-4, default 1):          #分割槽號碼
First sector (2048-209715199, default 2048):   #起始扇區範圍 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199):    #結束刪除範圍
Using default value 209715199
Partition 1 of type Linux and of size 100 GiB is set

Command (m for help): p         #顯示分割槽情況,可以看到已建立一個vdb1的分割槽

Disk /dev/vdb: 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: 0x5eeb4bc8

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048   209715199   104856576   83  Linux   #磁碟分割槽的資訊

Command (m for help): w      #報錯退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.複製程式碼

3、使用partprobe將分割槽的資訊同步給核心,讓核心識別到該分割槽資訊

[root@VM_0_89_centos ~]# partprobe /dev/vdb複製程式碼

4、接下來可以對分割槽製作檔案系統,製作成作業系統能夠識別的檔案系統,以製作xfs檔案系統為例

[root@VM_0_89_centos ~]# mkfs.xfs /dev/vdb1 
meta-data=/dev/vdb1              isize=512    agcount=4, agsize=6553536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=26214144, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=12799, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

[root@VM_0_89_centos ~]# blkid 
/dev/sr0: UUID="2020-01-13-22-30-05-00" LABEL="config-2" TYPE="iso9660" 
/dev/vda1: UUID="4b499d76-769a-40a0-93dc-4a31a59add28" TYPE="ext4" 
/dev/vdb1: UUID="c387af37-56d7-436c-8ceb-0a735e0db5d5" TYPE="xfs"          #檔案系統製作為xfs

備註:如果是製作為ext4則使用mkfs.ext4 /dev/vdb1複製程式碼

5、編寫fstab掛載到系統中,確保下次開機後配置生效

[root@VM_0_89_centos ~]# vim/etc/fstab 
UUID="c387af37-56d7-436c-8ceb-0a735e0db5d5" /data		  xfs	  defaults 	  0 0複製程式碼

6、執行mount -a載入掛載配置,並檢查掛載情況

[root@VM_0_89_centos ~]# mount -a

[root@VM_0_89_centos ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        485M     0  485M   0% /dev
tmpfs           496M   24K  496M   1% /dev/shm
tmpfs           496M  448K  496M   1% /run
tmpfs           496M     0  496M   0% /sys/fs/cgroup
/dev/vda1        50G  1.9G   46G   4% /
tmpfs           100M     0  100M   0% /run/user/0
/dev/vdb1       100G   33M  100G   1% /data    #磁碟已經掛載到系統中複製程式碼

2.4 MBR磁碟擴容

MBR格式的磁碟擴容需要在MBR磁碟分割槽中擴充套件空間,並在檔案系統中擴充套件檔案系統空間以識別到擴容的空間,騰訊云為此提供了一個專門擴容的指令碼devresize.py,指令碼要求條件如下:

  • 檔案系統是 EXT2/EXT3/EXT4/XFS
  • 當前檔案系統不能有錯誤
  • 擴容後的磁碟大小不超過2TB
  • 當前工具僅支援 Python 2 版本,不支援 Python 3 版本
超詳盡!Linux雲伺服器儲存擴容實操

如上圖為MBR格式磁碟的擴容步驟,現在控制檯中做好快照,並擴容,然後登入到作業系統中完成分割槽和檔案系統的擴容,如下以擴容至200G為例演示在MBR格式的磁碟分割槽擴容方法

1、 檢查磁碟的擴容,確認擴容的方式,根據System型別為Linux判斷為MBR格式的分割槽

[root@VM_0_89_centos ~]# fdisk -l /dev/vdb

Disk /dev/vdb: 214.7 GB, 214748364800 bytes, 419430400 sectors   #空間大小已擴容至200G
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: 0x5eeb4bc8

   Device Boot      Start         End      Blocks   Id  System   #System型別為Linux表示使用MBR格式分割槽
/dev/vdb1            2048   209715199   104856576   83  Linux複製程式碼

2、獲取擴容的指令碼,該指令碼可以完成分割槽的擴容和檔案系統的自動擴容,需要離線執行,因此先需要解除安裝磁碟

[root@VM_0_89_centos ~]# wget -O /tmp/devresize.py https://raw.githubusercontent.com/tencentyun/tencentcloud-cbs-tools/master/devresize/devresize.py

無法線上執行,需解除安裝磁碟
[root@VM_0_89_centos ~]# python /tmp/devresize.py /dev/vdb
[ERROR] - Target partition /dev/vdb1 must be unmounted.
[root@VM_0_89_centos ~]# umount  /dev/vdb1複製程式碼

3、執行擴容步驟

[root@VM_0_89_centos ~]# python /tmp/devresize.py /dev/vdb
[INFO] - checking filesystem healthy
Phase 1 - find and verify superblock...
Phase 2 - using internal log
        - zero log...
        - scan filesystem freespace and inode maps...
        - found root inode chunk
Phase 3 - for each AG...
        - scan and clear agi unlinked lists...
        - process known inodes and perform inode discovery...
        - agno = 0
        - agno = 1
        - agno = 2
        - agno = 3
        - process newly discovered inodes...
Phase 4 - check for duplicate blocks...
        - setting up duplicate extent list...
        - check for inodes claiming duplicate blocks...
        - agno = 0
        - agno = 1
        - agno = 2
        - agno = 3
Phase 5 - rebuild AG headers and trees...
        - reset superblock...
Phase 6 - check inode connectivity...
        - resetting contents of realtime bitmap and summary inodes
        - traversing filesystem ...
        - traversal finished ...
        - moving disconnected inodes to lost+found ...
Phase 7 - verify and correct link counts...
done
[INFO] - mount /dev/vdb1 /tmp/mount_point_vdb_2020-01-14_16:04:30
[INFO] - umount /dev/vdb1
This operation will extend /dev/vdb1 to the last sector of device. 
To ensure the security of your valuable data, 
please create a snapshot of this volume before resize its file system, continue? [Y/n]
y                                #提示是否建立快照,確保做好快照後輸入y繼續
It will resize (/dev/vdb1).
This operation may take from several minutes to several hours, continue? [Y/n]  #備份並resize,確認輸入y      
y
[INFO] - Backup MBR to /tmp/MBR_vdb1_2020-01-14_16:04:40_bak
[INFO] - mount /dev/vdb1 /tmp/mount_point_vdb_2020-01-14_16:04:30
[INFO] - resize filesystem
meta-data=/dev/vdb1              isize=512    agcount=4, agsize=6553536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=26214144, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=12799, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 26214144 to 52428544
[INFO] - umount /dev/vdb1
[INFO] - Finished複製程式碼

4、重新掛載檔案系統並確認擴容空間

[root@VM_0_89_centos ~]# mount -a    #重新掛載

[root@VM_0_89_centos ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        485M     0  485M   0% /dev
tmpfs           496M   24K  496M   1% /dev/shm
tmpfs           496M  448K  496M   1% /run
tmpfs           496M     0  496M   0% /sys/fs/cgroup
/dev/vda1        50G  1.9G   46G   4% /
tmpfs           100M     0  100M   0% /run/user/0
/dev/vdb1       200G   33M  200G   1% /data        #空間已擴容至200G複製程式碼

注意事項

  1. 擴容前一定要先做好快照,避免資料損壞時可快速恢復。
  2. MBR格式磁碟擴容需要解除安裝雲盤,因此是離線擴容,如果雲盤有在使用,請先停止業務後再擴容。

2.5 MBR新增磁碟分割槽

如果需要分割槽的場景,可以將擴容的空間劃分成一個新的分割槽供作業系統使用,通過在磁碟上新建立分割槽的方式實現擴容,其具有如下特點:

  • 單盤需要多個分割槽的場景
  • 擴容空間小於2T
  • 擴容後原分割槽無法擴容
  • CBS雲盤是按需彈性擴充套件,推薦使用單磁碟單分割槽掛載,雲上推薦使用裸盤建立檔案系統
超詳盡!Linux雲伺服器儲存擴容實操

如下以一塊200G的CBS盤擴容至300G為例演示在MBR格式磁碟上新增分割槽的方式實現磁碟擴容使用方式

1、準備工作,控制檯做好快照,並在控制檯完成磁碟空間擴容

2、fdisk -l檢查磁碟擴容的大小,通過System型別為Linux可得知其為MBR分割槽格式的磁碟

[root@VM_0_89_centos ~]# fdisk -l /dev/vdb

Disk /dev/vdb: 322.1 GB, 322122547200 bytes, 629145600 sectors   #磁碟大小已擴容至300G
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: 0x5eeb4bc8

   Device Boot      Start         End      Blocks   Id  System   #磁碟分割槽型別為MBR格式
/dev/vdb1            2048   419430399   209714176   83  Linux複製程式碼

3、擴容磁碟上新建立一個磁碟分割槽 /dev/vdb2,大小為100G

[root@VM_0_89_centos ~]# fdisk /dev/vdb
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/vdb: 322.1 GB, 322122547200 bytes, 629145600 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: 0x5eeb4bc8

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048   419430399   209714176   83  Linux   #當前已有一個分割槽

Command (m for help): n      #新增一個分割槽
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p   #分割槽型別為主分割槽
Partition number (2-4, default 2): 
First sector (419430400-629145599, default 419430400):    #開始扇區大小
Using default value 419430400
Last sector, +sectors or +size{K,M,G} (419430400-629145599, default 629145599):    #結束扇區大小
Using default value 629145599
Partition 2 of type Linux and of size 100 GiB is set

Command (m for help): p   #檢視分割槽建立的情況

Disk /dev/vdb: 322.1 GB, 322122547200 bytes, 629145600 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: 0x5eeb4bc8

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048   419430399   209714176   83  Linux
/dev/vdb2       419430400   629145599   104857600   83  Linux   #分割槽已建立成功

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.複製程式碼

4、重新整理核心分割槽以識別到分割槽內容

[root@VM_0_89_centos ~]# partprobe /dev/vdb複製程式碼

5、格式化磁碟分割槽,建立作業系統能識別的檔案系統,以xfs為例

[root@VM_0_89_centos ~]# mkfs.xfs /dev/vdb2 
meta-data=/dev/vdb2              isize=512    agcount=4, agsize=6553600 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=26214400, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=12800, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0複製程式碼

6、掛載至系統中,推薦使用fstab掛載

獲取分割槽UUID號
[root@VM_0_89_centos ~]# blkid /dev/vdb2 
/dev/vdb2: UUID="448467bd-f3fa-41cc-a5ad-ab316f7b62e2" TYPE="xfs"

編寫fstab檔案
echo 'UUID="448467bd-f3fa-41cc-a5ad-ab316f7b62e2"    /data1    xfs    defaults    0 0' >>/etc/fstab

建立掛載目錄並掛載
[root@VM_0_89_centos ~]# mkdir /data1 
[root@VM_0_89_centos ~]# mount -a

檢查掛載情況
[root@VM_0_89_centos ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        485M     0  485M   0% /dev
tmpfs           496M   24K  496M   1% /dev/shm
tmpfs           496M  452K  496M   1% /run
tmpfs           496M     0  496M   0% /sys/fs/cgroup
/dev/vda1        50G  1.9G   46G   4% /
tmpfs           100M     0  100M   0% /run/user/0
/dev/vdb1       200G   33M  200G   1% /data
/dev/vdb2       100G   33M  100G   1% /data1    #磁碟分割槽已經掛載至作業系統複製程式碼

至此雲盤上使用MBR格式新增分割槽的演示使用完畢,這種方式適用於單塊磁碟多分割槽的場景,由於雲上的磁碟可以根據按需擴容,且採用分割槽擴容的方式原有分割槽(第一個分割槽)將無法擴容,雲上推薦使用裸盤方式。

2.6 GPT磁碟分割槽

前面章節介紹了對磁碟建立MBR格式磁碟分割槽的使用方式,對於大於2T的磁碟,需要使用GPT格式的方式進行分割槽,本章節介紹磁碟使用GPT格式進行分割槽的使用方式。

超詳盡!Linux雲伺服器儲存擴容實操

先在控制檯購買雲盤並掛載到雲主機中,然後再作業系統中對磁碟進行GPT格式分割槽並建立檔案系統,以建立一個2T磁碟為例演示Linux中使用parted對磁碟建立GPT格式分割槽使用過程。

1、登入作業系統檢查磁碟的名稱和空間大小

[root@VM_0_89_centos ~]# fdisk -l

Disk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x0009ac89

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048   104857566    52427759+  83  Linux

Disk /dev/vdb: 2147.5 GB, 2147483648000 bytes, 4194304000 sectors   #磁碟為vdb,大小為2T
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes複製程式碼

2、使用parted對磁碟打標籤和磁碟分割槽

磁碟打上label並檢視確認
[root@VM_0_89_centos ~]# parted /dev/vdb mklabel gpt
[root@VM_0_89_centos ~]# parted /dev/vdb print                            
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 2147GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt        #分割槽表型別為gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags

對磁碟進行分割槽
[root@VM_0_89_centos ~]# parted /dev/vdb mkpart primary 2048s 100%
Information: You may need to update /etc/fstab.

檢視分割槽建立情況
[root@VM_0_89_centos ~]# parted /dev/vdb print
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 2147GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
 1      1049kB  2147GB  2147GB               primary   #建立了一個磁碟分割槽,範圍是2048s到2147GB         複製程式碼

3、磁碟上建立檔案系統,以製作xfs檔案系統為例

[root@VM_0_89_centos ~]# mkfs.xfs /dev/vdb1 
meta-data=/dev/vdb1              isize=512    agcount=4, agsize=131071872 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=524287488, imaxpct=5
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=255999, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0複製程式碼

4、編寫fstab掛載到/data目錄中

獲取分割槽UUID
[root@VM_0_89_centos ~]# blkid /dev/vdb1 
/dev/vdb1: UUID="aa6fc333-e702-4daa-ad0b-10efde57a7f0" TYPE="xfs" PARTLABEL="primary" PARTUUID="7dace14b-3093-495b-9d0c-fc63f3b87fd7"

 編寫fstab將磁碟掛載到/data目錄中
 UUID="aa6fc333-e702-4daa-ad0b-10efde57a7f0"     /data             xfs     defaults        0 0複製程式碼

5、載入掛載配置資訊並確認

[root@VM_0_89_centos ~]# mount -a
[root@VM_0_89_centos ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        485M     0  485M   0% /dev
tmpfs           496M   24K  496M   1% /dev/shm
tmpfs           496M  448K  496M   1% /run
tmpfs           496M     0  496M   0% /sys/fs/cgroup
/dev/vda1        50G  1.9G   46G   4% /
tmpfs           100M     0  100M   0% /run/user/0
/dev/vdb1       2.0T   33M  2.0T   1% /data        #磁碟當前已經掛載至/data目錄複製程式碼

2.7 GPT磁碟分割槽擴容

超詳盡!Linux雲伺服器儲存擴容實操

上圖是GPT格式磁碟分割槽的擴容步驟,需要在控制檯中製作快照並完成擴容,然後在GPT分割槽中識別到擴容空間,檔案檔案系統識別到空間,如下以2T擴容至3T為例演示擴容過程

1、確認磁碟空間和擴容方式為GPT

[root@VM_0_89_centos ~]# fdisk -l /dev/vdb 

Disk /dev/vdb: 3221.2 GB, 3221225472000 bytes, 6291456000 sectors   #磁碟空間大小已擴容至3T
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: 0x00000000

   Device Boot      Start         End      Blocks   Id  System   #磁碟分割槽格式為GPT
/dev/vdb1               1  4194303999  2097151999+  ee  GPT複製程式碼

2、分割槽擴容需是離線操作,因此需要先解除安裝磁碟,解除安裝前請停止業務使用

[root@VM_0_89_centos ~]# umount  /data複製程式碼

3、GPT重新分割槽,需要先刪除原有分割槽,然後再重建分割槽,刪除前使用unit s獲取到磁碟柱面的大小,切記,操作前應要做好快照備份,刪除分割槽重建分割槽有風險

[root@VM_0_89_centos ~]# parted /dev/vdb 
GNU Parted 3.1
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print                                                            
Error: The backup GPT table is not at the end of the disk, as it should be.  This might mean that another operating system believes the disk is smaller.  Fix, by moving the backup to the end
(and removing the old backup)?
Fix/Ignore/Cancel? Fix     #磁碟空間有擴容,因此提示有ERR,輸入Fix修復                                                
Warning: Not all of the space available to /dev/vdb appears to be used, you can fix the GPT to use all of the space (an extra 2097152000 blocks) or continue with the current setting? 
Fix/Ignore? Fix            #空間並未完全使用提示有Waring,輸入Fix修復                                                   
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 3221GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
 1      1049kB  2147GB  2147GB  xfs          primary  #當前分割槽的情況

(parted) unit s           #使用單元的方式顯示大小範圍                                                
(parted) print                                                            
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 6291456000s
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End          Size         File system  Name     Flags
 1      2048s  4194301951s  4194299904s  xfs          primary #Start起始範圍為2048s,後面重建分割槽需要使用到

(parted) rm 1     #刪除分割槽資訊                                                          
(parted) print                                                             
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 6291456000s
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags #確認分割槽資訊已被刪除

(parted) mkpart primary 2048s 100%   #重新建立磁碟分割槽,起始範圍需要和原分割槽保持一致
(parted) print                                                            
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 6291456000s
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End          Size         File system  Name     Flags
 1      2048s  6291453951s  6291451904s  xfs          primary複製程式碼

4、GPT分割槽擴容完畢後,需要在檔案系統中擴容空間,切記,不要格式化檔案系統,否則資料將會丟失,根據檔案系統型別選擇擴容方式,以xfs為例

將磁碟掛載到系統中
[root@VM_0_89_centos ~]# mount -a
[root@VM_0_89_centos ~]# df -h 
檔案系統        容量  已用  可用 已用% 掛載點
devtmpfs        485M     0  485M    0% /dev
tmpfs           496M   24K  496M    1% /dev/shm
tmpfs           496M  488K  495M    1% /run
tmpfs           496M     0  496M    0% /sys/fs/cgroup
/dev/vda1        50G  1.9G   46G    4% /
tmpfs           100M     0  100M    0% /run/user/0
/dev/vdb1       2.0T   33M  2.0T    1% /data

擴容檔案系統空間
[root@VM_0_89_centos ~]# xfs_growfs /dev/vdb1 
meta-data=/dev/vdb1              isize=512    agcount=4, agsize=131071872 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=524287488, imaxpct=5
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=255999, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 524287488 to 786431488         #已完成空間擴容


校驗檔案系統空間
[root@VM_0_89_centos ~]# df -h
檔案系統        容量  已用  可用 已用% 掛載點
devtmpfs        485M     0  485M    0% /dev
tmpfs           496M   24K  496M    1% /dev/shm
tmpfs           496M  488K  495M    1% /run
tmpfs           496M     0  496M    0% /sys/fs/cgroup
/dev/vda1        50G  1.9G   46G    4% /
tmpfs           100M     0  100M    0% /run/user/0
/dev/vdb1       3.0T   33M  3.0T    1% /data         #已擴容至3T

備註:如果是ext3、ext4則使用resize2fs /dev/vdb1實現檔案系統的擴容。複製程式碼

至此,基於GPT的磁碟格式磁碟分割槽擴容完畢,整個擴容過程中磁碟需要解除安裝操作,因此業務需要離線進行,擴容過程中需要刪除分割槽,操作較危險,操作前務必做好快照避免誤操作導致磁碟分割槽資訊丟失。

2.8 GPT新增磁碟分割槽

對於需要多個磁碟分割槽的場景,可以在GPT磁碟上新建另外一個分割槽的方式實現,實現方式和新建立一個分割槽的過程相類似,如下為基於GPT格式磁碟新增分割槽的操作過程:

超詳盡!Linux雲伺服器儲存擴容實操

以磁碟擴容至3.5T為例,通過GPT格式新建立另外一個/dev/vdb2的分割槽,並掛載到系統中使用,如下演示操作過程

1、 登入作業系統,確認磁碟空間擴容大小和磁碟格式為GPT

[root@VM_0_89_centos ~]# fdisk -l /dev/vdb 
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.

Disk /dev/vdb: 3758.1 GB, 3758096384000 bytes, 7340032000 sectors #磁碟空間大小為3.5T
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: gpt          #磁碟分割槽格式型別為gpt
Disk identifier: 15BF58A1-BBE0-466A-B2C5-A243AD6A3108


#         Start          End    Size  Type            Name
 1         2048   6291453951      3T  Microsoft basic primary複製程式碼

2、獲取磁碟上一個分割槽的End範圍,print得知結束範圍3221GB ,下一個步驟需要使用到

[root@VM_0_89_centos ~]# parted /dev/vdb print
Error: The backup GPT table is not at the end of the disk, as it should be.  This might mean that another operating system believes the disk is smaller.  Fix, by moving the backup to the end
(and removing the old backup)?
Fix/Ignore/Cancel? Fix
Warning: Not all of the space available to /dev/vdb appears to be used, you can fix the GPT to use all of the space (an extra 1048576000 blocks) or continue with the current setting? 
Fix/Ignore? Fix                                                           
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 3758GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
 1      1049kB  3221GB  3221GB  xfs          primary複製程式碼

3、 磁碟上新建一個分割槽/dev/vdb2,開始範圍為上一個步驟中獲取的磁碟大小

[root@VM_0_89_centos ~]# parted /dev/vdb mkpart primary 3221GB 100% #開始範圍為步驟2獲取的大小
Information: You may need to update /etc/fstab.

[root@VM_0_89_centos ~]# parted /dev/vdb print
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 3758GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags #包含有兩個分割槽
 1      1049kB  3221GB  3221GB  xfs          primary
 2      3221GB  3758GB  537GB                primary複製程式碼

4、 磁碟格式化建立檔案系統,以製作xfs檔案系統為例

[root@VM_0_89_centos ~]# mkfs.xfs /dev/vdb2 
meta-data=/dev/vdb2              isize=512    agcount=4, agsize=32768000 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=131072000, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=64000, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0複製程式碼

5、 掛載磁碟分割槽到系統中,以手動mount為例演示,實際使用時應使用fstab實現持久掛載

[root@VM_0_89_centos ~]# mount /dev/vdb2 /data1/

[root@VM_0_89_centos ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        485M     0  485M   0% /dev
tmpfs           496M   24K  496M   1% /dev/shm
tmpfs           496M  484K  495M   1% /run
tmpfs           496M     0  496M   0% /sys/fs/cgroup
/dev/vda1        50G  1.9G   46G   4% /
tmpfs           100M     0  100M   0% /run/user/0
/dev/vdb1       3.0T   33M  3.0T   1% /data
/dev/vdb2       500G   33M  500G   1% /data1    #已掛載完畢複製程式碼

3. 總結

前文總結了在Linux環境中CBS三種使用方式:

  1. 裸裝置建立檔案系統;
  2. 使用MBR格式分割槽;
  3. 使用GPT格式分割槽。

以及這三種使用方式對應的擴容方式和操作步驟。相比於裸設建立檔案系統,使用MBR和GPT格式擴容時需要解除安裝磁碟,因此無法做到線上擴容。因此在雲環境下推薦使用直接在裸裝置上建立檔案系統的方式,其更加便捷,易於實現,且擴容過程中可以線上擴容。。

4. 作者簡介

HappyLau,高階雲端計算顧問,目前在騰訊雲從事公有云相關工作。曾就職於酷狗、EasyStack,擁有多年公有云+私有云雲端計算架構設計、運維、交付相關經驗。參與過酷狗、南方電網、國泰君安等大型私有云平臺建設,精通Linux、Kubernetes、OpenStack、Ceph等開源技術,具有豐富RHCA/OpenStack/Linux授課經驗。

雲加社群專欄:cloud.tencent.com/developer/c…

參考文獻

配套視訊教程:cloud.tencent.com/edu/learnin…

MBR格式磁碟初始化:cloud.tencent.com/document/pr…

GPT格式磁碟初始化:cloud.tencent.com/document/pr…

Linux雲伺服器儲存擴容:cloud.tencent.com/document/pr…

Windows雲伺服器儲存擴容:cloud.tencent.com/document/pr…


相關文章