背景:源码编译软件的时候,发现原先定的磁盘容量不够了,root目录17G都用完了,不想换目录,那就需要扩展磁盘容量,需要扩展root目录
一.扩展物理磁盘
1.先关闭虚拟机,右键虚拟机设置:点扩展
启动VMware环境下的Linux操作系统,需要root账号身份登陆
2、[root@localhost ~]# fdisk -l
最大分区为/dev/sda2,说明新创建的分区将会是sda3
3、[root@localhost ~]# fdisk /dev/sda 使用fdisk /dev/sda进入菜单项,m是列出菜单,p是列出分区表,n是增加分区,w是保存并退出
[root@localhost ~]# fdisk /dev/sda
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/sda: 32.2 GB, 32212254720 bytes, 62914560 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: 0x000ba2b5
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
Command (m for help): n
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e):p
Partition number (1-4): 3
4、有默认值的就回车使用默认值
5、Command (m for help): w 保存修改
6、[root@localhost ~]# reboot
二、虚拟机扩容
1、查看磁盘编号
[dyx@localhost ~]$ ls /dev/sd*
/dev/sda /dev/sda1 /dev/sda2 /dev/sda3
2、创建pv
[root@localhost dyx]# pvcreate /dev/sda3
Physical volume “/dev/sda3” successfully created.
3、把pv加入vg中,相当于扩充vg的大小
[root@localhost dyx]# vgs *** 先使用vgs查看vg组
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz–n- <19.00g 0
[root@localhost dyx]# vgextend centos /dev/sda3 *** 扩展vg,使用vgextend命令
Volume group “centos” successfully extended
4、成功把vg卷扩展了,再用vgs查看一下
[root@localhost dyx]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 2 2 0 wz–n- 19.99g <10.00g
[root@localhost dyx]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <17.00g
swap centos -wi-ao---- 2.00g
虽然我们把vg扩展了,但是lv还没有扩展
5、扩展lv,使用lvextend命令
[root@localhost dyx]# lvextend -L +9G /dev/mapper/centos-root
6、命令使系统重新读取大小
[root@localhost dyx]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512 agcount=7, agsize=1113856 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=6814720, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
7、再使用df -h查看
[root@localhost dyx]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 13M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 26G 17G 9.1G 66% /
/dev/sda1 1014M 185M 830M 19% /boot
tmpfs 379M 20K 379M 1% /run/user/0
/dev/sr0 4.4G 4.4G 0 100% /run/media/root/CentOS 7 x86_64
容量已经从17G扩展到26G了,扩容成功。