生产环境中由于业务的持续增长,磁盘容量必然会越来越少。公司里有台生产服务器数据盘的分区使用率已接近95%,于是抽了个时间,做一下停机扩容维护。以下是详细记录: 系统环境:阿里云主机
[root@BZ ~]$ cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
[root@BZ ~]$ uname -r
3.10.0-514.21.1.el7.x86_64
1,首先看下磁盘使用率:
[root@BZ ~]$ df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/xvda1 ext4 40G 27G 11G 72% /
devtmpfs devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs tmpfs 7.7G 0 7.7G 0% /dev/shm
tmpfs tmpfs 7.7G 492K 7.7G 1% /run
tmpfs tmpfs 7.7G 0 7.7G 0% /sys/fs/cgroup
/dev/xvdb1 ext4 197G 174G 13G 94% /mnt
/dev/xvdc ext3 493G 396G 72G 85% /disk/1
/dev/xvdd ext4 985G 425G 510G 46% /data
tmpfs tmpfs 1.6G 0 1.6G 0% /run/user/0
可以看到/mnt所在的分区使用率已接近极限,再查看下磁盘:
[root@BZ ~]$ fdisk /dev/xvdb
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/xvdb: 1073.7 GB, 1073741824000 bytes, 2097152000 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: 0x052b7897
可以看到这是一个1T容量的磁盘,但是分区只用了200G,现在要将/dev/xvdb1分区扩展为整个磁盘的容量,涉及到数据的操作一定要谨慎,如果是物理机,一定要先备份,我用的是阿里云的主机,在操作之前要对磁盘做一个快照,总之,一定要备份!!!
2,关闭对外服务,备份磁盘:
[root@BZ ~]# service nginx stop #关闭nginx服务
[root@BZ ~]# service php-fpm stop #关闭php-fpm服务
确认服务关闭:
[root@BZ ~]# netstat -tnlp | grep :80 |wc -l
0
[root@BZ ~]# ps -ef | grep php-fpm | grep -v grep | wc -l
0
登陆阿里云,创建磁盘快照: 稍等20分钟左右,快照就创建好了,一定要确认快照创建完成,数据无价!
3,使用fdisk命令进行分区扩展:
[root@BZ ~]$ umount /mnt #卸载磁盘设备,如果卸载不了,请使用fuser -m -v /mnt 找出使用该设备的进程, 强制卸载使用umount -lf /mnt
[root@BZ ~]$ fdisk /dev/xvdb
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): p #选择创建主分区
Partition number (1-4, default 1): 1 #分区号
First sector (2048-2097151999, default 2048): #这里要和之前的分区起始扇区保持一致,否则数据会出问题!!!
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151999, default 2097151999): #默认回车会使用磁盘所有容量
Using default value 2097151999
Partition 1 of type Linux and of size 1000 GiB is set
Command (m for help): p
Disk /dev/xvdb: 1073.7 GB, 1073741824000 bytes, 2097152000 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: 0x052b7897
Device Boot Start End Blocks Id System
/dev/xvdb1 2048 2097151999 1048574976 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
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)
Syncing disks.
提示要重启机器
4,重启服务器,调整分区大小:
[root@BZ ~]# systemctl reboot
[root@BZ ~]# umount /mnt
[root@BZ ~]# 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/xvdb1: xxxxxxxxxxxxxxxxxxxxxxxxxxxx
e2fsck 检查磁盘这一步特别耗时,分区从200G扩展到1T用了2个多小时,其中Pass1, Pass2耗时最多,如果要做扩展,一定要慎重!
[root@BZ ~]# resize2fs /dev/xvdb1 #此步也比较耗时
5,重新挂分区,查看数据:
[root@BZ ~]# mount /dev/xvdb1 /mnt
[root@ ~]$ df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/xvda1 ext4 40G 27G 11G 72% /
devtmpfs devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs tmpfs 7.7G 0 7.7G 0% /dev/shm
tmpfs tmpfs 7.7G 492K 7.7G 1% /run
tmpfs tmpfs 7.7G 0 7.7G 0% /sys/fs/cgroup
/dev/xvdb1 ext4 985G 176G 759G 19% /mnt
/dev/xvdc ext3 493G 396G 72G 85% /disk/1
/dev/xvdd ext4 985G 425G 510G 46% /data
tmpfs tmpfs 1.6G 0 1.6G 0% /run/user/0
OK了,历经两个多小时,终于搞定了,真是漫长的等待。 做任何操作前,一定要评估好数据,时间等等因素,头脑要清醒,操作要谨慎。