云服务器挂载大于2TB的数据盘

## 查看磁盘分区类型

`sudo fdisk -l`

```
Disk /dev/vda: 500 GiB, 536870912000 bytes, 1048576000 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: 0x2c0bb34c

Device     Boot Start        End    Sectors  Size Id Type
/dev/vda1  *     2048 1048575966 1048573919  500G 83 Linux

Disk /dev/vdb: 3 TiB, 3298534883328 bytes, 6442450944 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: gpt
Disk identifier: 6E47E086-B25E-4B28-B38E-96AC049A2761

Device     Start        End    Sectors Size Type
/dev/vdb1   2048 6442448895 6442446848   3T Linux filesystem
```

```
Disklabel type: dos
Disklabel type: gpt
```

## 安装

`sudo apt install -y parted e2fsprogs`

## 查看磁盘类型

`sudo fdisk -l`

## 使用 Parted 工具为数据盘进行分区

`sudo parted /dev/vdb`

进入提示

```
GNU Parted 3.2
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)
```

运行以下命令，将默认的 MBR 分区格式转为 GPT 分区格式
`mklabel gpt`

运行以下命令，划分一个主分区，并设置分区的开始位置和结束位置
`mkpart primary 1 100%`

运行以下命令，检查分区是否对齐
`align-check optimal 1`

运行结果如下所示
`1 aligned`

运行以下命令，查看分区表
`print`

运行以下命令，退出 Parted 工具
`quit`

parted 执行命令清单：

```
(parted) mklabel gpt
(parted) mkpart primary 1 100%
(parted) align-check optimal 1
1 aligned
(parted) print
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 3299GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name     Flags
 1      1049kB  3299GB  3299GB               primary
```

## 系统重读分区表

运行`sudo partprobe /dev/vdb` 没有任何提示

然后再运行 `sudo fdisk -l`，看到 `Disklabel type: gpt`

```
Disk /dev/vdb: 3 TiB, 3298534883328 bytes, 6442450944 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: gpt
Disk identifier: 6E47E086-B25E-4B28-B38E-96AC049A2761

Device     Start        End    Sectors Size Type
/dev/vdb1   2048 6442448895 6442446848   3T Linux filesystem
```

## 创建一个 ext4 文件系统

`sudo mkfs -t ext4 /dev/vdb1`

## 挂载分区/dev/vdb1

```
sudo mkdir -p /mnt/vol

sudo mount /dev/vdb1 /mnt/vol
```

## 向/etc/fstab 里写入新分区信息

```
sudo cp /etc/fstab /etc/fstab.bak
diskid=`sudo blkid /dev/vdb1 | awk '{print $2}' | sed 's/\"//g'`
echo "$diskid /mnt/vol ext4 defaults 0 0" | sudo tee -a /etc/fstab
```

## 卸载

`sudo umount -v /dev/vdb1`

## SSD 在线扩容

在阿里云控制台执行在线扩操作后，执行 df 命令，可以看到可用磁盘空间并没有扩大。

```
df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/vda1      ext4       40G  4.4G   34G  12% /
/dev/vdb1      ext4      590G  545G   16G  98% /mnt/vol
```

通过查看云盘分区情况，在 ECS 实例内分区和文件系统并未扩容。

**如果分区为 GPT 格式，必须执行此步骤；如果分区为 MBR 格式，请跳过此步骤。**
`sudo apt install -y gdisk`

### 安装 cloud-guest-utils

```
sudo apt update && sudo apt install -y cloud-guest-utils
```

### 扩容文件系统

```
sudo growpart /dev/vdb 1

# CHANGED: partition=1 start=2048 old: size=1258287104 end=1258289152 new: size=2097149919,end=2097151967
```

```
sudo resize2fs /dev/vdb1

Filesystem at /dev/vdb1 is mounted on /mnt/vol; on-line resizing required
old_desc_blocks = 75, new_desc_blocks = 125
The filesystem on /dev/vdb1 is now 262143739 (4k) blocks long.
```

运行 `df -Th`检查扩容后结果。

---

参考：

https://help.aliyun.com/document_detail/34377.html

https://help.aliyun.com/document_detail/113316.html

https://zhuanlan.zhihu.com/p/35613150

https://askubuntu.com/questions/387351/how-can-i-detect-whether-my-disk-is-using-gpt-or-mbr-from-a-terminal