当前位置:   article > 正文

ECS磁盘扩容与添加新的硬盘_fdisk 阿里云扩容

fdisk 阿里云扩容

环境准备:首先要准备一台实验用的ECS主机
关于ECS主机磁盘容量不够,扩充磁盘容量有两种方案

  • 一:使用在线扩容,提高原先磁盘的容量大小 (不支持卸载,无需挂载扩容后直接使用)
  • 二:创建新的云硬盘,并将其挂载到ECS主机上(支持卸载,卸载后可挂载到其他主机上,需要挂载才能使用)

一、在线扩容

在这里插入图片描述
此时的硬盘容量为20G
点击实例后管理该实例的硬盘
在这里插入图片描述
点击硬盘扩容,勾选在线扩容,选择要将磁盘扩容到的容量,确定扩容
在这里插入图片描述
此时硬盘扩容成功,但是扩容后的硬盘并不会直接能提供使用
在这里插入图片描述
此时就需要在服务器上手动的扩展系统盘,在扩展系统盘前最好先做好备份或是快照以免数据丢失

1、安装扩容格式化工具

[root@web ~]# yum install cloud-utils-growpart -y
[root@web ~]# yum install xfsprogs -y
  • 1
  • 2

2、运行fdisk -l命令查看现有云盘大小

[root@web ~]# fdisk -l

Disk /dev/vda: 32.2 GB, 32212254720 bytes, 62914560 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: 0x000b2d99

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    41943039    20970496   83  Linux
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

3、运行df -h命令查看云盘分区大小

[root@web ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        20G  1.7G   17G  10% /
devtmpfs        486M     0  486M   0% /dev
tmpfs           496M     0  496M   0% /dev/shm
tmpfs           496M  416K  496M   1% /run
tmpfs           496M     0  496M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user/0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

4、运行growpart 命令调用growpart为需要扩容的云盘和对应的第几个分区扩容。
示例命令表示为系统盘的第一个分区扩容。

[root@web ~]# growpart /dev/vda 1
CHANGED: partition=1 start=2048 old: size=41940992 end=41943040 new: size=62912479 end=62914527
  • 1
  • 2

若运行命令后报以下错误,您可以运行LANG=en_US.UTF-8切换ECS实例的字符编码类型。

[root@web ~]# growpart /dev/vda 1
unexpected output in sfdisk --version [sfdisk,来自 util-linux 2.23.2]
[root@web ~]# LANG=en_US.UTF-8
  • 1
  • 2
  • 3

5、运行resize2fs 命令调用resize2fs扩容文件系统
示例命令表示为扩容系统盘的/dev/vda1分区文件系统。

[root@web ~]# resize2fs /dev/vda1 
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vda1 is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 2
The filesystem on /dev/vda1 is now 7864059 blocks long.
  • 1
  • 2
  • 3
  • 4
  • 5

6、此时硬盘扩容成功
在这里插入图片描述
详细扩展可参考阿里云文档

二、购买云硬盘扩容

点击创建云盘,创建新的云硬盘
在这里插入图片描述
选择硬盘可用区。需要与需要挂载的主机在同一可用区内才能满足挂载需求,选择硬盘和设置硬盘大小,购买数量及硬盘名称
在这里插入图片描述
创建成功后就可以进行挂载操作

在这里插入图片描述
选择要挂在硬盘的设备,选择释放行为(建议不要勾选)
硬盘随实例释放会在释放实例时自动释放硬盘但是数据丢失
在这里插入图片描述

此时挂载成功
在这里插入图片描述
挂载成后不能直接使用,还需要在设备上将磁盘格式化分区,添加并挂载到某一目录上
在这里插入图片描述

运行fdisk -l命令查看实例上的数据盘

[root@web ~]# fdisk -l

Disk /dev/vda: 32.2 GB, 32212254720 bytes, 62914560 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: 0x000b2d99

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    62914526    31456239+  83  Linux

Disk /dev/vdb: 21.5 GB, 21474836480 bytes, 41943040 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

此时可以看到有新的硬盘/dev/vdb带格式化分区使用

1、创建新的分区

[root@web ~]# 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 command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x18c3dd34.

Command (m for help): p

Disk /dev/vdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x18c3dd34

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): 
Using default value 41943039
Partition 1 of type Linux and of size 20 GiB is set

Command (m for help): p

Disk /dev/vdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x18c3dd34

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048    41943039    20970496   83  Linux

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

Calling ioctl() to re-read partition table.

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

2、运行fdisk -lu /dev/vdb命令查看新分区

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

Disk /dev/vdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x18c3dd34

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048    41943039    20970496   83  Linux
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

3、运行mkfs.ext4 /dev/vdb1命令在新分区上创建一个文件系统

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

Disk /dev/vdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x18c3dd34

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048    41943039    20970496   83  Linux
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

4、运行mkfs.ext4 /dev/vdb1命令在新分区上创建一个文件系统

[root@web ~]# mkfs.ext4 /dev/vdb1 
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, 5242624 blocks
262131 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 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

5、在配置文件中添加,时期永久挂载

备份配置文件
[root@web ~]# cp /etc/fstab /etc/fstab.bak

[root@web ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Thu Jul 11 02:52:01 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=1114fe9e-2309-4580-b183-d778e6d97397 /                       ext4    defaults        1 1
/dev/vdb1 /mnt ext4 defaults 0 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

6、mount 临时挂载,时期立即生效

[root@web ~]# mount /dev/vdb1 /mnt/

[root@web ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        30G  1.7G   27G   6% /
devtmpfs        486M     0  486M   0% /dev
tmpfs           496M     0  496M   0% /dev/shm
tmpfs           496M  460K  496M   1% /run
tmpfs           496M     0  496M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user/0
/dev/vdb1        20G   45M   19G   1% /mnt
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

详情可参考阿里云文档

创建的云硬盘,在挂载到设备上后使用,存储在磁盘内的数据在卸载挂载到其他设备的时依然存在

[root@web ~]# cd /mnt/
[root@web mnt]# ls
lost+found
[root@web mnt]# echo hello >> disk.txt
[root@web mnt]# ls
disk.txt  lost+found
[root@web mnt]# cat disk.txt 
hello
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

先将硬盘从Linux主机上卸载

将其在永久挂载的配置文件中注释,或删除
[root@web ~]# cat /etc/fstab
# /etc/fstab
# Created by anaconda on Thu Jul 11 02:52:01 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=1114fe9e-2309-4580-b183-d778e6d97397 /                       ext4    defaults        1 1
#/dev/vdb1 /mnt ext4 defaults 0 0

强制卸载
[root@web ~]# umount -l /mnt/

[root@web ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        30G  1.7G   27G   6% /
devtmpfs        486M     0  486M   0% /dev
tmpfs           496M     0  496M   0% /dev/shm
tmpfs           496M  460K  496M   1% /run
tmpfs           496M     0  496M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user/0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

将硬盘从实例上卸载
在这里插入图片描述
再将其挂载到其他的ECS设备上,挂载成此时在其他的主机中查看到硬盘的信息

[root@wwj ~]# fdisk -l

Disk /dev/vda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x000b2d99

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    41943039    20970496   83  Linux

Disk /dev/vdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x18c3dd34

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048    41943039    20970496   83  Linux

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

挂载到主机目录以供正常使用

创建挂载目录
[root@wwj ~]# mkdir /app

[root@wwj ~]# mount /dev/vdb1 /app/

[root@wwj ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        20G  1.6G   17G   9% /
devtmpfs        234M     0  234M   0% /dev
tmpfs           244M     0  244M   0% /dev/shm
tmpfs           244M  428K  244M   1% /run
tmpfs           244M     0  244M   0% /sys/fs/cgroup
tmpfs            49M     0   49M   0% /run/user/0
/dev/vdb1        20G   45M   19G   1% /app


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

进入挂载目录,验证数据是否存在

[root@wwj ~]# cd /app/
[root@wwj app]# ls
disk.txt  lost+found
[root@wwj app]# cat disk.txt 
hello
  • 1
  • 2
  • 3
  • 4
  • 5

三.如果磁盘是一个完整的数据盘扩容

在这里插入图片描述

此时扩容 /dev/vbc 出现报错, 因为该磁盘为数据盘是一个整盘并未做过分区,所以您不需要growpart扩容分区,直接扩容文件系统就可以了

~]# type xfs_growfs || yum install -y xfsprogs
~]# xfs_growfs /sensorsdata/hybriddata
  • 1
  • 2

此时磁盘扩容成功
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/853191
推荐阅读
相关标签
  

闽ICP备14008679号