问题:Linux系统挂载到根目录的磁盘空间满了,如何扩容?
用命令:lsblk 可以查看磁盘和分区情况,可以发现磁盘vda下面的还有大部分空间没有使用。
(用命令lsblk可以看到,挂载到根目录的分区是lvm。)
(并且系统有一块虚拟磁盘vda,有2个分区vda1 和 vda2)
(fdisk -l 查看硬盘进行分区。)
[root@host-172-20-1-113 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 252:0 0 1T 0 disk
├─vda1 252:1 0 1G 0 part /boot
└─vda2 252:2 0 49G 0 part
├─centos-root 253:0 0 44G 0 lvm /
└─centos-swap 253:1 0 5G 0 lvm [SWAP]
[root@host-172-20-1-113 ~]# fdisk -l #查看硬盘分区
磁盘 /dev/vda:1099.5 GB, 1099511627776 字节,2147483648 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x00095e6c
设备 Boot Start End Blocks Id System
/dev/vda1 * 2048 2099199 1048576 83 Linux
/dev/vda2 2099200 104857599 51379200 8e Linux LVM
磁盘 /dev/mapper/centos-root:47.2 GB, 47240445952 字节,92266496 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘 /dev/mapper/centos-swap:5368 MB, 5368709120 字节,10485760 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
用 fdisk 对硬盘vda进行分区操作:fdisk /dev/vda
[root@host-172-20-1-113 ~]# fdisk /dev/vda #对硬盘vda进行分区操作
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令(输入 m 获取帮助):n #n 代表新建new
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p #前面已有2个分区,所以分区从3开始
分区号 (3,4,默认 3):3
起始 扇区 (104857600-2147483647,默认为 104857600): #直接回车,选择默认值
将使用默认值 104857600
Last 扇区, +扇区 or +size{K,M,G} (104857600-2147483647,默认为 2147483647):
将使用默认值 2147483647 #直接回车,选择默认值
分区 3 已设置为 Linux 类型,大小设为 974 GiB
命令(输入 m 获取帮助):t #t 代表 change a partition's system id
分区号 (1-3,默认 3):3
Hex 代码(输入 L 列出所有代码):8e #8e 代表Linux LVM
已将分区“Linux”的类型更改为“Linux LVM”
命令(输入 m 获取帮助):w #写入并保存,退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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)
正在同步磁盘。
以及用lsblk查看已经给增加了一个分区vda3,并且有974G的空间
[root@host-172-20-1-113 ~]# partprobe
[root@host-172-20-1-113 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 252:0 0 1T 0 disk
├─vda1 252:1 0 1G 0 part /boot
├─vda2 252:2 0 49G 0 part
│ ├─centos-root 253:0 0 44G 0 lvm /
│ └─centos-swap 253:1 0 5G 0 lvm [SWAP]
└─vda3 252:3 0 974G 0 part
备注:系统是CentOS 7.6的系统,所以格式化的格式是xfs
可以用:df -T 查看一下文件格式系统
[root@host-172-20-1-113 ~]# mkfs.xfs /dev/vda3
meta-data=/dev/vda3 isize=512 agcount=4, agsize=63832064 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=255328256, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=124672, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
4.1 进入lvm模式。用pvdisplay查看物理卷详情,发现只有1个物理卷 /dev/vda2
[root@host-172-20-1-113 ~]# lvm
lvm> pvdisplay
--- Physical volume ---
PV Name /dev/vda2
VG Name centos
PV Size <49.00 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 12543
Free PE 0
Allocated PE 12543
PV UUID N3THeY-Gw8G-ejIj-S6S0-UoXL-j5BS-3c0Gwd
4.2 新建pv卷。命令:pvcreate /dev/vda3 (创建名称叫vda3的pv卷)
lvm> pvcreate /dev/vda3
WARNING: xfs signature detected on /dev/vda3 at offset 0. Wipe it? [y/n]: y
Wiping xfs signature on /dev/vda3.
Physical volume "/dev/vda3" successfully created.
4.3 再次查看pv卷,已经有两个pv卷了。可以创建看到第2个pv卷就是刚刚新建的vda3
lvm> pvdisplay
--- Physical volume ---
PV Name /dev/vda2
VG Name centos
PV Size <49.00 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 12543
Free PE 0
Allocated PE 12543
PV UUID N3THeY-Gw8G-ejIj-S6S0-UoXL-j5BS-3c0Gwd
"/dev/vda3" is a new physical volume of "974.00 GiB"
--- NEW Physical volume ---
PV Name /dev/vda3
VG Name
PV Size 974.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID ZIf1J5-ZjuB-kGiC-Gpqn-APLT-PWQO-XKCCjL
4.4 查看vg组。命令:vgdisplay 。可以发现VG Name 叫 centos
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 <49.00 GiB
PE Size 4.00 MiB
Total PE 12543
Alloc PE / Size 12543 / <49.00 GiB
Free PE / Size 0 / 0
VG UUID nYUiIj-mzYH-AT5v-7ZeX-HTU9-TR8v-xKcSRB
4.5 将刚刚生成的pv卷 /dev/vda3 加入到根目录所在的vg组:centos
命令:vgextend centos /dev/vda3
备注:这里在扩容另外一台服务器的时候碰到了一个问题(本台服务器没碰到):挂载根目录的磁盘使用100%,需要把根目录的文件移走到/boot 目录下,再进行加入组。
lvm> vgextend centos /dev/vda3
Volume group "centos" successfully extended
4.6 再次查看,根目录所在的vg组:centos 已经扩容成功
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 1022.99 GiB
PE Size 4.00 MiB
Total PE 261886
Alloc PE / Size 12543 / <49.00 GiB
Free PE / Size 249343 / <974.00 GiB
VG UUID nYUiIj-mzYH-AT5v-7ZeX-HTU9-TR8v-xKcSRB
# 可以看到 Free PE / Size 249343 / <974.00 GiB 已经有974G了,
# 注意249343这个是PE值,下面会用到。
4.7 查看lv卷。命令:lvdisplay
lvm> lvdisplay
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID 3wMMor-zryN-fe8u-1uhC-G5df-1IiX-rC3v2y
LV Write Access read/write
LV Creation host, time localhost, 2022-09-03 16:59:23 +0800
LV Status available
# open 2
LV Size 5.00 GiB
Current LE 1280
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID qFWSSU-jttE-dRZD-3S6g-2bnv-hVkZ-ENLM3P
LV Write Access read/write
LV Creation host, time localhost, 2022-09-03 16:59:23 +0800
LV Status available
# open 1
LV Size <44.00 GiB
Current LE 11263
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
4.8 使用 lvextend 为根目录进行扩容。命令: lvextend -l +249343 /dev/centos/root
备注:这个239343 就是上面vgdisplay下展示的 Free PE值。
也可以直接用:lvextend -L +974G /dev/centos/root 也可以
lvm> lvextend -l +249343 /dev/centos/root
Size of logical volume centos/root
changed from <44.00 GiB (11263 extents) to 1017.99 GiB (260606 extents).
Logical volume centos/root successfully resized.
#备注:这个249349 就是前面PE值
#这里还可以用另外一个方法,直接指定扩展大小:
# lvextend -L +974G /dev/centos/root
4.9 再次查看lv卷,LV Size 已经增加了974 GiB。命令:lvdisplay
lvm> lvdisplay
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID 3wMMor-zryN-fe8u-1uhC-G5df-1IiX-rC3v2y
LV Write Access read/write
LV Creation host, time localhost, 2022-09-03 16:59:23 +0800
LV Status available
# open 2
LV Size 5.00 GiB
Current LE 1280
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID qFWSSU-jttE-dRZD-3S6g-2bnv-hVkZ-ENLM3P
LV Write Access read/write
LV Creation host, time localhost, 2022-09-03 16:59:23 +0800
LV Status available
# open 1
LV Size 1017.99 GiB
Current LE 260606
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
lvm> exit
Exiting.
4.10 扩容文件系统。命令: xfs_growfs /dev/mapper/centos-root
[root@host-172-20-1-113 ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=2883328 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=11533312, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=5631, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 11533312 to 266860544
4.11 扩容成功。
用df -h 查看,挂载到根目录的系统已经有1017G了,扩容成功。
[root@host-172-20-1-113 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 1018G 2.0G 1017G 1% /
devtmpfs 16G 0 16G 0% /dev
tmpfs 16G 0 16G 0% /dev/shm
tmpfs 16G 25M 16G 1% /run
tmpfs 16G 0 16G 0% /sys/fs/cgroup
/dev/vda1 1014M 166M 849M 17% /boot
tmpfs 3.2G 0 3.2G 0% /run/user/0