有一个在用的系统,根分区只有30G,需要进行动态扩容,步骤如下:
1、首先查看当前空间使用情况
df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs 3.8G 84K 3.8G 1% /dev/shm
tmpfs 3.8G 137M 3.7G 4% /run
tmpfs 3.8G 0 3.8G 0% /sys/fs/cgroup
/dev/mapper/cl-root 35G 6.6G 29G 19% /
/dev/vda1 976M 130M 779M 15% /boot
tmpfs 777M 0 777M 0% /run/user/0
2、再查看分区情况
fdisk -l
Disk /dev/vda: 40 GiB, 42949672960 bytes, 83886080 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
Disklabel type: dos
Disk identifier: 0x3653de0b
Device Boot Start End Sectors Size Id Type
/dev/vda1 * 2048 2099199 2097152 1G 83 Linux
/dev/vda2 2099200 83886079 81786880 39G 8e Linux LVM
Disk /dev/vdb: 500 GiB, 536870912000 bytes, 1048576000 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 /dev/mapper/cl-root: 35 GiB, 37576769536 bytes, 73392128 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 /dev/mapper/cl-swap: 4 GiB, 4294967296 bytes, 8388608 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
可以看到/dev/vdb 有500G 但没有分区,也就是没使用。/dev/vda2 是LVM的物理卷
3、查看LVM物理卷情况
pvdisplay
--- Physical volume ---
PV Name /dev/vda2
VG Name cl
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 tnd71k-8LfH-OGms-Xd2C-UYE2-EPnI-BRkfUm
可以看出只有/dev/vda2 这一个物理卷 ,且没有空间了
4、查看逻辑卷情况
vgdisplay
--- Volume group ---
VG Name cl
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 4aq1pS-l84e-Pgx2-sw54-wbRX-KZYv-RsAy5e
可以看到只有一个VG 名称是c1,空闲空间是0
5、先对硬盘进行分区
fdisk /dev/vdb
先用p查看分区,当前是没有分区的
Command (m for help): p
Disk /dev/vdb: 500 GiB, 536870912000 bytes, 1048576000 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
Disklabel type: dos
Disk identifier: 0x41b34d74
再用n新建分区
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
建立主分区
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-1048575999, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-1048575999, default 1048575999): +200G
Created a new partition 1 of type 'Linux' and of size 200 GiB.
再用t修改分区,改成LVM分区
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'.
这是再用p查看,会发现有了一个分区了
Command (m for help): p
Disk /dev/vdb: 500 GiB, 536870912000 bytes, 1048576000 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
Disklabel type: dos
Disk identifier: 0x41b34d74
Device Boot Start End Sectors Size Id Type
/dev/vdb1 2048 419432447 419430400 200G 8e Linux LVM
存盘
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
重新查看分区,会发现多了一个分区/dev/vdb1
fdisk -l
Disk /dev/vda: 40 GiB, 42949672960 bytes, 83886080 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
Disklabel type: dos
Disk identifier: 0x3653de0b
Device Boot Start End Sectors Size Id Type
/dev/vda1 * 2048 2099199 2097152 1G 83 Linux
/dev/vda2 2099200 83886079 81786880 39G 8e Linux LVM
Disk /dev/vdb: 500 GiB, 536870912000 bytes, 1048576000 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
Disklabel type: dos
Disk identifier: 0x41b34d74
Device Boot Start End Sectors Size Id Type
/dev/vdb1 2048 419432447 419430400 200G 8e Linux LVM
Disk /dev/mapper/cl-root: 35 GiB, 37576769536 bytes, 73392128 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 /dev/mapper/cl-swap: 4 GiB, 4294967296 bytes, 8388608 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
6、创建物理卷并查看
pvcreate /dev/vdb1
pvdisplay
--- Physical volume ---
PV Name /dev/vda2
VG Name cl
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 tnd71k-8LfH-OGms-Xd2C-UYE2-EPnI-BRkfUm
"/dev/vdb1" is a new physical volume of "200.00 GiB"
--- NEW Physical volume ---
PV Name /dev/vdb1
VG Name
PV Size 200.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID J5UF5L-0DkO-nKdP-W4Me-A1UW-GzEc-G3Kzz4
有了一个新物理卷,且没有加入任何VG
7、扩展vg
vgextend cl /dev/vdb1
再查看vg
vgdisplay
--- Volume group ---
VG Name cl
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 238.99 GiB
PE Size 4.00 MiB
Total PE 61182
Alloc PE / Size 9983 / <39.00 GiB
Free PE / Size 51199 / <200.00 GiB
VG UUID 4aq1pS-l84e-Pgx2-sw54-wbRX-KZYv-RsAy5e
可以看到VG的FREE空间多出了200G
8、扩展逻辑卷
lvextend -L +190G /dev/mapper/cl-root
之后查看空间
df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs 3.8G 84K 3.8G 1% /dev/shm
tmpfs 3.8G 137M 3.7G 4% /run
tmpfs 3.8G 0 3.8G 0% /sys/fs/cgroup
/dev/mapper/cl-root 35G 6.6G 29G 19% /
/dev/vda1 976M 130M 779M 15% /boot
tmpfs 777M 0 777M 0% /run/user/0
发现没有增长
需要增加手工分区空间,不同的分区类型命令不一样
先要查看分区类型
df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs tmpfs 3.8G 84K 3.8G 1% /dev/shm
tmpfs tmpfs 3.8G 137M 3.7G 4% /run
tmpfs tmpfs 3.8G 0 3.8G 0% /sys/fs/cgroup
/dev/mapper/cl-root xfs 35G 6.6G 29G 19% /
/dev/vda1 ext4 976M 130M 779M 15% /boot
tmpfs tmpfs 777M 0 777M 0% /run/user/0
可以看到我们要扩的分区/dev/mapper/cl-root类型是xfs
需要使用xfs_growfs调整xfs分区,如果是ext4则是使用resize2fs 命令
xfs_growfs /dev/mapper/cl-root
提示失败,分区不是xfs分区:xfs_growfs: /dev/mapper/cl-root is not a mounted XFS filesystem
在网上查了,发现如果是根分区要用xfs_growfs /命令
xfs_growfs /
meta-data=/dev/mapper/cl-root isize=512 agcount=4, agsize=2293504 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=9174016, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log 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 58981376
这次成功了,重新查看一下
df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs 3.8G 84K 3.8G 1% /dev/shm
tmpfs 3.8G 137M 3.7G 4% /run
tmpfs 3.8G 0 3.8G 0% /sys/fs/cgroup
/dev/mapper/cl-root 225G 8.0G 218G 4% /
/dev/vda1 976M 130M 779M 15% /boot
tmpfs 777M 0 777M 0% /run/user/0
扩容成功