扩容数据盘_Linux

1,前提条件
实例处于 运行中 (Running) 或 已停止(Stopped) 状态。

数据盘的状态为 待挂载 或 使用中。

数据盘已做分区。

建议在扩容数据盘之前手动创建快照,以备份数据。

假设数据盘挂载在一台Linux实例上,实例的操作系统为CentOS 7.3 64位,未扩容前的数据盘只有一个主分区(/dev/vdb1,ext4文件系统),文件系统的挂载点为 /kdata,文件系统扩容完成后,数据盘仍然只有一个主分区。

1,先卸载主分区 /dev/vdb1

umount /dev/vdb1

如果

[root@lcoalhost /]# umount /dev/sdc1
umount: /data1: target is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))

显示占用的进程

[root@localhost /]# fuser -m /dev/sdc1
-bash: fuser: command not found

没有fuser 安装psmisc

yum install -y psmisc

在次查看

[root@localhost /]# fuser -m /dev/sdc1
/dev/sdc1:           12766c
[root@localhost /]# ps aux | grep 12766
root     10246  0.0  0.0 116460  3212 pts/0    Ss+  Oct19   0:00 -bash

使用kill命令关闭进程:(如果进程多,可能要多杀几次)

kill -9 12766

2,查看分区是否存在

df -h

3,使用fdisk命令删除原来的分区并创建新分区(输入后回车)–fdisk

1,fdisk -l
2,fdisk 【‘数据盘设备名’】
输入d		--删除原来的分区
输入n		--开始创建新的分区
输入p		--开始创建新的分区。		
输入1		--创建主分区。
输入第一个可用的扇区编号:为了保证数据的一致性,First sector需要与原来的分区保持一致。在本示例中,按回车键采用默认值。
输入最后一个扇区编号:因为这里仅创建一个分区,所以按回车键采用默认值。
输入wq并按回车键,开始分区。

--------------------------------demo---------------------------
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): d
Selected partition 1
Partition 1 is deleted
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-62914559, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-62914559, default 62914559):
Using default value 62914559
Partition 1 of type Linux and of size 30 GiB is set
Command (m for help): wq
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

注意:demo里面可能有WARNING

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)

解决方式:实例重启或者reboot或者partprobe重新读取分区

[root@localhost ~]# partprobe
-d:不更新内核;
-s:显示摘要和分区;
-h:显示帮助信息;
-v:显示版本信息。

运行partprobe。错误提示

Error: Error informing the kernel about modifications to partition /dev/vdb1 
-- Device or resource busy.  
This means Linux won't know about any changes you made to /dev/vdb1 until you reboot
 -- so you shouldn't mount it or use it in any way before rebooting.
Error: Failed to add partition 1 (Device or resource busy)
错误:通知内核修改分区/DEV/VBD1的错误-设备或资源忙。
这意味着在重新启动之前,Linux不会知道对/dev/vdb1所做的任何更改——因此在重新启动之前,不应该以任何方式挂载或使用它。
错误:添加分区1失败(设备或资源忙)

最后只能重启实例。

4,部分操作系统里,修改分区后可能会重新自动挂载文件系统。

df -h 查看是否挂载    如果挂载,执行umount /dev/vdb1

5,检查文件系统,并变更文件系统大小。

e2fsck -f /dev/vdb1 # 检查文件系统
resize2fs /dev/vdb1 # 变更文件系统大小

说明

使用 e2fsck 时,由于系统需要检查并订正文件系统元数据,所以速度较慢、耗时较长,请耐心等待。
正确使用 e2fsck 和 resize2fs 指令,不会造成原有数据丢失。

事例

root@iXXXXXX ~]# e2fsck -f /dev/vdb1
e2fsck 1.42.9 (28-Dec-2013)
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/vdb1: 11/1835008 files (0.0% non-contiguous), 159218/7339776 blocks
[root@iXXXXXX ~]# resize2fs /dev/vdb1
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vdb1 to 7864064 (4k) blocks.
The filesystem on /dev/vdb1 is now 7864064 blocks long.

6,将扩容完成的文件系统挂载到原来的挂载点(/kdata)

mount /dev/vdb1 /kdata

7, 执行df -h查看。–success

通过–parted 操作
说明 如果发现First sector显示的位置和之前记录的不一致,说明之前可能使用 parted 来分区,那么就停止fdisk 操作,使用 parted 重新操作。
parted /dev/vdb
rm+ 序列号 删除老的分区表
unit s 定义起始位置
mkpart primary ext4 1 100G 命令来创建即可
p 查看

猜你喜欢

转载自blog.csdn.net/network_dream/article/details/83930760