最近遇到一个生产环境扩容的需求,本来是一件很简单的事情,但做到一半却傻眼了,甲方爸爸原来的硬盘没有做LVM,并且KVM新添加的是整块硬盘而不是原有硬盘扩容,这种情况要做热扩容简直比五彩斑斓的黑还要丧心病狂。
首先说明下,LINUX系统中 ext4 分区可以离线用 resize2fs 扩容,但扇区必须连续。lvm btrfs 都可以在线跨扇区扩容。xfs 也是可以在线扩容的,使用xfs_growfs 不过扇区要连续。
目前使用较多的扩容一个是xfs的growpart,一个是LVM体系(LVM体系都这么多年还没被淘汰确实是非常优秀的体系,不过现在大部分云端开始用新的体系了)
1:LVM下扩容
命令:
先查容量
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 35G 5.5G 30G 16% /
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 3.9G 11M 3.9G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 275M 740M 28% /boot
tmpfs 783M 52K 783M 1% /run/user/0
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
fd0 2:0 1 4K 0 disk
sda 8:0 0 500G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 39G 0 part
├─centos-root 253:0 0 35G 0 lvm /
└─centos-swap 253:1 0 4G 0 lvm [SWAP]
[root@localhost ~]# fdisk /dev/sda
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令(输入 m 获取帮助):n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p):
Using default response p
分区号 (3,4,默认 3):
起始 扇区 (83886080-1048575999,默认为 83886080):
将使用默认值 83886080
Last 扇区, +扇区 or +size{K,M,G} (83886080-1048575999,默认为 1048575999):+85G
分区 3 已设置为 Linux 类型,大小设为 85 GiB
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
更新分区表
[root@localhost ~]# partprobe
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
fd0 2:0 1 4K 0 disk
sda 8:0 0 500G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 39G 0 part
│ ├─centos-root 253:0 0 35G 0 lvm /
│ └─centos-swap 253:1 0 4G 0 lvm [SWAP]
└─sda3 8:3 0 85G 0 part
sr0 11:0 1 1024M 0 rom
LVM系列操作
[root@localhost ~]# lvm
lvm> pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created.
lvm> pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size <39.00 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 9983
Free PE 0
Allocated PE 9983
PV UUID twmUZh-Vnqq-W5Pd-v3ms-6OkH-sSTs-nv8aA8
"/dev/sda3" is a new physical volume of "85.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sda3
VG Name
PV Size 85.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID vTD91f-b28E-GgL0-XsqI-ipQ5-rscT-GcQbT6
lvm> vgdisplay
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size <39.00 GiB
PE Size 4.00 MiB
Total PE 9983
Alloc PE / Size 9983 / <39.00 GiB
Free PE / Size 0 / 0
VG UUID 238Yz8-MEQa-NT6a-3aHC-J3TZ-mGux-e9vqPf
lvm> vgextend centos /dev/sda3
Volume group "centos" successfully extended
lvm> vgdisplay
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 2
Act PV 2
VG Size 123.99 GiB
PE Size 4.00 MiB
Total PE 31742
Alloc PE / Size 9983 / <39.00 GiB
Free PE / Size 21759 / <85.00 GiB
VG UUID 238Yz8-MEQa-NT6a-3aHC-J3TZ-mGux-e9vqPf
lvm> lvextend -l +100%FREE /dev/centos/root
Size of logical volume centos/root changed from <35.00 GiB (8959 extents) to 119.99 GiB (30718 extents).
Logical volume centos/root successfully resized.
[root@localhost ~]# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=2293504 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=9174016, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=4479, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 9174016 to 31455232
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 120G 5.5G 115G 5% /
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 3.9G 11M 3.9G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 275M 740M 28% /boot
tmpfs 783M 52K 783M 1% /run/user/0
2:growpart扩容(仅针对单硬盘还有未使用空间,多用于云供应商提供的机器购买扩容空间)
1、先安装 gdisk 工具
yum install epel-release
yum install cloud-utils-growpart
lsblk
2、growpart /dev/vda1
3、xfs_growfs /mnt/vda1 #xfs
resize2fs /dev/vda1 #ext4
测试操作记录
原服务器硬盘原40G, 云管上将系统盘扩到80G,扩大40G
1、先安装growpart 工具
yum install epel-release
yum install cloud-utils-growpart
2、LANG=en_US.UTF-8