【Linux】擴展阿里雲數據盤分區和文件系統

擴容雲盤只是擴大存儲容量,不會擴容文件系統html

1、準備工做

在擴展數據盤擴展分區和文件系統前,請提早完成如下工做。node

2、確認分區格式和文件系統

ECS實例的操做系統爲CentOS 6.8 64 位,數據盤設備名爲/dev/vdb。ide

一、運行fdisk -lu <DeviceName>確認數據盤是否分區。

本示例中,原有的數據盤空間已作分區 /dev/vdb1。"System"="Linux"說明數據盤使用的是MBR分區格式,若是"System"="GPT"則說明數據盤使用的是GPT格式。
 
[root@test ~]# fdisk -lu /dev/vdb

Disk /dev/vdb: 1374.4 GB, 1374389534720 bytes
16 heads, 63 sectors/track, 2663050 cylinders, total 2684354560 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 identifier: 0x7c4016b5

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1              63  2147483519  1073741728+  83  Linux

二、運行blkid <PartitionName>確認文件系統的類型。

本示例中, /dev/vdb1的文件系統類型爲ext4。
[root@test ~]# blkid /dev/vdb1
/dev/vdb1: UUID="ee04ddf2-ad0f-40bc-9b5c-984be7e19283" TYPE="ext4"
 
說明 未分區而且未建立文件系統的數據盤,以及已分區但未建立文件系統的數據盤,不會返回結果。
[root@test ~]# blkid /dev/vdb1
[root@test ~]#

運行如下命令確認文件系統的狀態。工具

  • ext*文件系統:e2fsck -n <dst_dev_part_path>url

  • xfs文件系統:xfs_repair -n <dst_dev_part_path>spa

警告 本示例中,文件系統狀態爲 clean。若是狀態不是clean,請排查並修復。
[root@test~]# e2fsck -n /dev/vdb1
e2fsck 1.41.12 (17-May-2010)
/dev/vdb1: clean, 115952/67108864 files, 140800686/268435432 blocks

3、擴展已有MBR分區

說明 爲了防止數據丟失,不建議擴容已掛載的分區和文件系統。請先取消掛載( umount)分區,完成擴容並正常使用後,從新掛載(mount)。針對不一樣的Linux內核版本,推薦如下操做方式:
  • 實例內核版本 < 3.6:先取消掛載該分區,再修改分區表,最後擴容文件系統。
  • 實例內核版本 ≥ 3.6:先修改對應分區表,再通知內核更新分區表,最後擴容文件系統。

若是新增空間用於擴容已有的MBR分區,按照如下步驟在實例中完成擴容:操作系統

一、修改分區表

查看磁盤信息.net

[root@test~]# fdisk -lu /dev/vdb

Disk /dev/vdb: 1374.4 GB, 1374389534720 bytes
16 heads, 63 sectors/track, 2663050 cylinders, total 2684354560 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 identifier: 0x7c4016b5

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1              63  2147483519  1073741728+  83  Linux

查看數據盤的掛載路徑,根據返回的文件路徑卸載分區,直至徹底卸載已掛載的分區。

[root@test ~]# mount | grep "/dev/vdb"
/dev/vdb1 on /data type ext4 (rw,relatime,data=ordered)
[root@test ~]# umount /dev/vdb1
[root@test ~]# mount | grep "/dev/vdb"

使用fdisk工具刪除舊分區。

  1. 運行fdisk -u /dev/vdb:分區數據盤。
  2. 輸入p:打印分區表。
  3. 輸入d:刪除分區。
  4. 輸入p:確認分區已刪除。
  5. 輸入w:保存修改並退出。
[root@test~]# fdisk -u /dev/vdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c').

Command (m for help): p

Disk /dev/vdb: 1374.4 GB, 1374389534720 bytes
16 heads, 63 sectors/track, 2663050 cylinders, total 2684354560 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 identifier: 0x7c4016b5

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1              63  2147483519  1073741728+  83  Linux

Command (m for help): d
Selected partition 1

Command (m for help): p

Disk /dev/vdb: 1374.4 GB, 1374389534720 bytes
16 heads, 63 sectors/track, 2663050 cylinders, total 2684354560 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 identifier: 0x7c4016b5

   Device Boot      Start         End      Blocks   Id  System

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

Calling ioctl() to re-read partition table.
Syncing disks.

使用fdisk命令新建分區。

  1. 運行fdisk -u /dev/vdb:分區數據盤。
  2. 輸入p:打印分區表。
  3. 輸入n:新建分區。
  4. 輸入p:選擇分區類型爲主分區。
  5. 輸入<分區號>:選擇分區號。本示例選取了1。
    警告 新分區的起始位置必須和舊分區的起始位置相同,結束位置必須大於舊分區的結束位置,不然會致使擴容失敗。
  6. 輸入w:保存修改並退出。

本示例中,將/dev/vdb1由1024 GiB擴容到1280 GiB。

[root@test~]# fdisk -u /dev/vdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c').

Command (m for help): p

Disk /dev/vdb: 1374.4 GB, 1374389534720 bytes
16 heads, 63 sectors/track, 2663050 cylinders, total 2684354560 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 identifier: 0x7c4016b5

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): w
Partition number (1-4): 1
First sector (63-2684354559, default 63): 
Using default value 63
Last sector, +sectors or +size{K,M,G} (63-2684354559, default 2684354559): 
Using default value 2684354559

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

Calling ioctl() to re-read partition table.
Syncing disks.

運行lsblk /dev/vdb確保分區表已經增長。

運行e2fsck -n /dev/vdb1再次檢查文件系統,確認擴容分區後的文件系統狀態爲clean。

二、通知內核更新分區表

運行partprobe <dst_dev_path>或者partx -u <dst_dev_path>,以通知內核數據盤的分區表已經修改,須要同步更新。

三、擴容文件系統

ext*文件系統(例如ext3和ext4):運行resize2fs /dev/vdb1並從新掛載分區。

[root@test ~]# resize2fs /dev/vdb1
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vdb1 to 7864320 (4k) blocks.
The filesystem on /dev/vdb1 is now 7864320 blocks long.
[root@test ~]# mount /dev/vdb1 /data

xfs文件系統:先運行mount /dev/vdb1 /data/命令,再運行xfs_growfs /dev/vdb1

[root@test ~]# mount /dev/vdb1 /data/
[root@test ~]# xfs_growfs /dev/vdb1
meta-data=/dev/vdb1              isize=512    agcount=4, agsize=1310720 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=5242880, 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 5242880 to 7864320

4、新增並格式化MBR分區

若是新增空間用於增長新的MBR分區,按照如下步驟在實例中完成擴容:

運行fdisk -u /dev/vdb命令新建分區。

本示例中,爲新增的20GiB新建分區,做爲/dev/vdb2使用。

[root@test ~]# fdisk -u /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 commad.

Command (m for help): p

Disk /dev/vdb: 42.9 GB, 42949672960 bytes, 83886080 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: 0x2b31a2a3

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048    41943039    20970496   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): 2
First sector (41943040-83886079, default 41943040):
Using default value 41943040
Last sector, +sectors or +size{K,M,G} (41943040-83886079, default 83886079):
Using default value 83886079
Partition 2 of type Linux and of size 20 GiB is set

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

Calling ioctl() to re-read partition table.
Syncing disks.

運行命令lsblk /dev/vdb查看分區。

[root@test ~]# lsblk /dev/vdb
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vdb    253:16   0  40G  0 disk
├─vdb1 253:17   0  20G  0 part
└─vdb2 253:18   0  20G  0 part

格式化新的分區。

建立ext4文件系統:mkfs.ext4 /dev/vdb2

[root@test ~]# mkfs.ext4 /dev/vdb2
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
1310720 inodes, 5242880 blocks
262144 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2153775104
160 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

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
[root@test ~]# blkid /dev/vdb2
/dev/vdb2: UUID="e3f336dc-d534-4fdd-****-b6ff1a55bdbb" TYPE="ext4"

建立ext3文件系統:mkfs.ext3 /dev/vdb2

[root@test ~]# mkfs.ext3 /dev/vdb2
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
1310720 inodes, 5242880 blocks
262144 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
160 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

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
[root@ecshost ~]# blkid /dev/vdb2
/dev/vdb2: UUID="dd5be97d-a630-4593-****-5056def914ea" SEC_TYPE="ext2" TYPE="ext3"

建立xfs文件系統:mkfs.xfs -f /dev/vdb2

[root@test ~]# mkfs.xfs -f /dev/vdb2
meta-data=/dev/vdb2              isize=512    agcount=4, agsize=1310720 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=5242880, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@test ~]# blkid /dev/vdb2
/dev/vdb2: UUID="66251477-3ae4-4b44-****-5604420dbecb" TYPE="xfs"

建立btrfs文件系統:mkfs.btrfs /dev/vdb2

[root@test ~]# mkfs.btrfs /dev/vdb2
btrfs-progs v4.9.1
See http://btrfs.wiki.kernel.org for more information.

Label:              (null)
UUID:               6fb5779b-57d7-4aaf-bf09-82b46f54a429
Node size:          16384
Sector size:        4096
Filesystem size:    20.00GiB
Block group profiles:
  Data:             single            8.00MiB
  Metadata:         DUP               1.00GiB
  System:           DUP               8.00MiB
SSD detected:       no
Incompat features:  extref, skinny-metadata
Number of devices:  1
Devices:
   ID        SIZE  PATH
    1    20.00GiB  /dev/vdb2
[root@test ~]# blkid /dev/vdb2
/dev/vdb2: UUID="6fb5779b-57d7-4aaf-****-82b46f54a429" UUID_SUB="9bdd889a-ab69-4653-****-d1b6b8723378" TYPE="btrfs"

運行mount /dev/vdb2 /mnt掛載文件系統。

運行df -h查看目前數據盤空間和使用狀況。

顯示新建文件系統的信息,表示掛載成功。
[root@test ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 1.6G 36G 5% /
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 3.9G 460K 3.9G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/vdb2 9.8G 37M 9.2G 1% /mnt
tmpfs 783M 0 783M 0% /run/user/0

5、重點來了,若是磁盤沒有分區,可是有數據怎麼辦,怎麼辦?

一、查看磁盤狀況

[root@test ~]# fdisk -lu /dev/vdb

Disk /dev/vdb: 1374.4 GB, 1374389534720 bytes
16 heads, 63 sectors/track, 2663050 cylinders, total 2684354560 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 identifier: 0x00000000

[root@test ~]# blkid /dev/vdb
/dev/vdb: UUID="c7a3bad3-c0f9-4cdc-9778-15abb90953ab" SEC_TYPE="ext2" TYPE="ext3" 
[root@test ~]# lsblk /dev/vdb
NAME MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vdb  252:16   0  1.3T  0 disk

二、直接擴容

[root@test ~]# e2fsck -f /dev/vdb
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vdb: 115952/67108864 files (32.8% non-contiguous), 140800686/268435432 blocks
[root@test ~]# 
[root@test ~]# resize2fs /dev/vdb
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vdb1 to 335544312 (4k) blocks.
The filesystem on /dev/vdb is now 335544312 blocks long.
相關文章
相關標籤/搜索