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

https://juejin.im/post/5e6f3264518825496e786a8cnode


導語 | 隨着業務的發展,業務數據不停的增加,原有的磁盤空間可能會出現磁盤空間不夠用的狀況,所以,須要對磁盤空間進行擴容,以知足業務數據增加的需求。本文總結了Linux環境下雲服務器存儲擴容的三種方式及其操做步驟,與你們一同交流。
環境說明:Linux操做系統:CentOS Linux release 7.6.1810 (Core)
平臺環境:騰訊雲CVM,CBS

1. 存儲擴容概述

如下圖爲例,假如起初購買了100G磁盤空間,隨着數據的不停增加,原有100G空間不夠用了,將空間擴容至200G。python

CBS是騰訊雲提供用於雲服務器的持久性數據塊級存儲服務,雲盤具備彈性,按需擴容特性,雲盤能夠任意的按需擴容。linux

不過雲盤擴容時不是全部的場景下均可以任意擴容,雲盤擴容具備兩個限制:git

  • 系統盤不支持擴容,理論上系統盤也可以支持擴容,系統盤存放操做系統數據,擴容可能會形成系統啓動異常
  • 本地盤不支持擴容,本地盤使用宿主機的本地盤,本地盤彈性擴容能力沒有云盤靈活,沒法支持彈性按需擴容

要深刻掌握雲盤的擴容,須要掌握雲盤的使用方式,在Linux下一般有以下三種使用方式:github

  1. 裸設備建立文件系統,不須要建立任何分區,直接在裸盤上建立文件系統
  2. 磁盤上建立MBR格式的分區,適用於小於2T的多個磁盤分區的場景
  3. 磁盤上建立GPT格式的分區,適用於大於2T的多個磁盤分區的場景

2. Linux雲服務器存儲擴容

2.1. 裸設備磁盤使用

Linux系統中能夠直接在裸設備磁盤上建立文件系統,即磁盤不須要建立任何磁盤分區,直接在磁盤上創文件文件系統並掛載使用,其具備以下特色:vim

  • 磁盤不須要多個分區
  • 適用於大於2T磁盤
  • 簡單易使用
  • 後期支持在線擴容

上圖所示是在Linux系統中直接在裸設備建立文件系統的操做步驟,先在控制檯購買好雲盤並將其掛載到CVM中,而後登錄到Linux系統中對磁盤格式化文件系統並掛載使用,控制檯購買和掛載請自行操做,以下演示在Linux系統中裸設備文件系統的使用步驟:centos

一、 登錄操做系統,經過fdisk -l獲取磁盤的名稱,系統有兩塊磁盤vda和vdb,/dev/vdb是咱們操做的磁盤bash

[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複製代碼

二、直接在磁盤上建立文件系統(注:並未在磁盤上建立任何分區),根據須要格式化爲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複製代碼

三、建立好文件系統後便可掛載到系統中使用,使用方式有兩種:臨時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 複製代碼

四、掛載點目錄/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 裸設備磁盤擴容

上述步驟是裸設備磁盤的擴容方式,擴容過程當中存在數據損壞的風險,爲了不擴容過程當中誤操做,強烈建議擴容前對磁盤作快照,避免數據損壞時能夠作恢復,謹記!作完快照後在控制檯完成磁盤的擴容,擴容完畢後須要在操做系統中識別到擴容的空間,以磁盤擴容至200G爲例,演示在Linux中裸設備擴容的操做過程:

一、 確認磁盤的使用方式,經過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文件系統複製代碼

二、確認爲雲盤的擴容方式爲裸盤後,根據文件系統的類型,在文件系統層面擴容以識別到雲盤的擴容空間

[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擴容複製代碼

三、校驗磁盤空間擴容狀況,使用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個擴展分區

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

一、使用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複製代碼

二、使用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.複製代碼

三、使用partprobe將分區的信息同步給內核,讓內核識別到該分區信息

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

四、接下來能夠對分區製做文件系統,製做成操做系統可以識別的文件系統,以製做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複製代碼

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

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

六、執行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 版本

如上圖爲MBR格式磁盤的擴容步驟,如今控制檯中作好快照,並擴容,而後登陸到操做系統中完成分區和文件系統的擴容,以下以擴容至200G爲例演示在MBR格式的磁盤分區擴容方法

一、 檢查磁盤的擴容,確認擴容的方式,根據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複製代碼

二、獲取擴容的腳本,該腳本能夠完成分區的擴容和文件系統的自動擴容,須要離線執行,所以先須要卸載磁盤

[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複製代碼

三、執行擴容步驟

[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複製代碼

四、從新掛載文件系統並確認擴容空間

[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雲盤是按需彈性擴展,推薦使用單磁盤單分區掛載,雲上推薦使用裸盤建立文件系統

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

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

二、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複製代碼

三、擴容磁盤上新建立一個磁盤分區 /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.複製代碼

四、刷新內核分區以識別到分區內容

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

五、格式化磁盤分區,建立操做系統能識別的文件系統,以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複製代碼

六、掛載至系統中,推薦使用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格式進行分區的使用方式。

先在控制檯購買雲盤並掛載到雲主機中,而後再操做系統中對磁盤進行GPT格式分區並建立文件系統,以建立一個2T磁盤爲例演示Linux中使用parted對磁盤建立GPT格式分區使用過程。

一、登陸操做系統檢查磁盤的名稱和空間大小

[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複製代碼

二、使用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 複製代碼

三、磁盤上建立文件系統,以製做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複製代碼

四、編寫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複製代碼

五、加載掛載配置信息並確認

[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磁盤分區擴容

上圖是GPT格式磁盤分區的擴容步驟,須要在控制檯中製做快照並完成擴容,而後在GPT分區中識別到擴容空間,文件文件系統識別到空間,以下以2T擴容至3T爲例演示擴容過程

一、確認磁盤空間和擴容方式爲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複製代碼

二、分區擴容需是離線操做,所以須要先卸載磁盤,卸載前請中止業務使用

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

三、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複製代碼

四、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

備註:若是是ext三、ext4則使用resize2fs /dev/vdb1實現文件系統的擴容。複製代碼

至此,基於GPT的磁盤格式磁盤分區擴容完畢,整個擴容過程當中磁盤須要卸載操做,所以業務須要離線進行,擴容過程當中須要刪除分區,操做較危險,操做前務必作好快照避免誤操做致使磁盤分區信息丟失。

2.8 GPT新增磁盤分區

對於須要多個磁盤分區的場景,能夠在GPT磁盤上新建另一個分區的方式實現,實現方式和新建立一個分區的過程相相似,以下爲基於GPT格式磁盤新增分區的操做過程:

以磁盤擴容至3.5T爲例,經過GPT格式新建立另一個/dev/vdb2的分區,並掛載到系統中使用,以下演示操做過程

一、 登陸操做系統,確認磁盤空間擴容大小和磁盤格式爲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複製代碼

二、獲取磁盤上一個分區的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複製代碼

三、 磁盤上新建一個分區/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複製代碼

四、 磁盤格式化建立文件系統,以製做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複製代碼

五、 掛載磁盤分區到系統中,以手動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…

相關文章
相關標籤/搜索