linux · 2021年7月4日 0

Centos7 EXT4分区扩容,非LVM

上个运维不知道哪门的傻逼,对某业务的Oracle数据库分区只给了100G,容量一下就满了,vSphere扩容分区之后,进系统看,结果还不是LVM分区,直接物理磁盘分区之后就ext4格式化了,就变成以下这状态

...
sdd      8:16   0  300G  0 disk 
└─sdd1   8:17   0  100G  0 part /oracle
...

百度了半天,找到了解决方法,确实把问题解决了,但是有风险,文件系统有概率损坏。对生产环境操作前一定要有++免责声明++,让领导背锅,以下操作均在虚拟机中操作。 下面讲述如何扩容ext4分区,本文仅供学习与交流,请勿用于商业通途。 首先创建个虚拟机,模拟当时的生产环境

[root@localhost ~]# lsblk -a
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   20G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0    2G  0 part [SWAP]
└─sda3   8:3    0   17G  0 part /
sdb      8:16   0    1G  0 disk 
└─sdb1   8:17   0  100M  0 part /data
[root@localhost ~]# df -hT | grep sdb
/dev/sdb1      ext4       93M  1.6M   85M   2% /data

sdb硬盘,已经分区sdb1并格式化ext4挂载到data目录,在里面随便创建一个大文件以验证试验可靠性

[root@localhost data]# dd if=/dev/zero of=/data/nmsl bs=4000 count=23040
23040+0 records in
23040+0 records out
92160000 bytes (92 MB) copied, 0.171641 s, 537 MB/s
[root@localhost data]# du -sh nmsl
88M	nmsl

下面开始扩容步骤。

一,卸载分区

umount /dev/sdb1

如果提示设备繁忙无法卸载,使用lsof找的正在使用该磁盘的进程,kill掉 修改fstab,注释掉该分区,避免自动挂载。

[root@localhost ~]# vi /etc/fstab
UUID=c6e82cc8-97ae-49bb-aefb-3f373969906f /                       xfs     defaults        0 0
UUID=19dd6293-c47e-4c7a-86f8-4042f17e54d4 /boot                   xfs     defaults        0 0
UUID=05d06568-0a57-43e3-abb6-e3e3edbeeba8 swap                    swap    defaults        0 0
# /dev/sdb1 /data ext4 defaults 0 0

二,使用fdisk修改分区容量

先用p查看磁柱位置,纪录下来,这里是2048开始

[root@localhost ~]# fdisk /dev/sdb
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/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x5aeb1834

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      206847      102400   83  Linux

Command (m for help):

删除1分区,再创建大容量的1分区,磁柱位置一定要与原来一样

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): p
Partition number (1-4, default 1): 
First sector (2048-2097151, default 2048): 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): 
Using default value 2097151
Partition 1 of type Linux and of size 1023 MiB is set

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x5aeb1834

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2097151     1047552   83  Linux

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

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

查看当前分区容量

[root@localhost ~]# lsblk  | grep sdb
sdb      8:16   0    1G  0 disk 
└─sdb1   8:17   0 1023M  0 part

三,扩容文件系统

分区已经扩容到300G了,文件系统还是原来的大小,现在扩容文件系统

e2fsck检查文件系统

resize2fs重新调整分区容量

[root@localhost ~]# e2fsck -f /dev/sdb1
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/sdb1: 12/25688 files (8.3% non-contiguous), 98898/102400 blocks
[root@localhost ~]# resize2fs /dev/sdb1
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/sdb1 to 1047552 (1k) blocks.
The filesystem on /dev/sdb1 is now 1047552 blocks long.

四,再次挂载文件系统到原路径

[root@localhost ~]# vi /etc/fstab 
[root@localhost ~]# mount -a
[root@localhost ~]# lsblk 
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   20G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0    2G  0 part [SWAP]
└─sda3   8:3    0   17G  0 part /
sdb      8:16   0    1G  0 disk 
└─sdb1   8:17   0 1023M  0 part /data
sr0     11:0    1    1G  0 rom  
[root@localhost ~]# cd /data/
[root@localhost data]# ls
lost+found  nmsl
[root@localhost data]# du -sh nmsl
88M	nmsl

可见,nmsl文件还存在该文件系统下。扩容完成。

扩容分区存在风险,请谨慎再谨慎操作。