前言

环境:centos7.9、Vmware 12
前些天发现自己的虚拟机的根分区已经用了92%,给根分区扩容迫在眉睫,本篇就来记录一下如何给根分区扩容。

查看磁盘空间

[root@master ~]# df -Th
Filesystem          Type      Size  Used Avail Use% Mounted on
/dev/mapper/cl-root xfs        47G   30G   18G  63% /					#根分区只剩63%了,需要给根分区扩容
devtmpfs            devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs               tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs               tmpfs     1.9G  9.0M  1.9G   1% /run
tmpfs               tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda2           xfs       497M  167M  330M  34% /boot
/dev/loop0          iso9660   4.1G  4.1G     0 100% /media/iso
tmpfs               tmpfs     378M     0  378M   0% /run/user/0

关闭虚拟机并添加一块硬盘

在这里插入图片描述
由于我已经有两块硬盘了,这里我就添加一块10G的,步骤选项选择默认即可,添加成功之后我们开启虚拟机即可。

查看磁盘信息

我们可以使用lsblk 命令来查看磁盘空间使用情况,已经来查看我们新添加的磁盘,系统是否识别到了:

[root@master ~]# lsblk							#查看有多少快磁盘以及分区情况,发现一共有3块硬盘,sda和sdb及sdc
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   40G  0 disk 					#sda磁盘,有3个分区,分别是sda1、sda2、sda3
├─sda1        8:1    0    1M  0 part 
├─sda2        8:2    0  500M  0 part /boot
└─sda3        8:3    0 39.5G  0 part 
  ├─cl-root 253:0    0 46.5G  0 lvm  /
  └─cl-swap 253:1    0    8G  0 lvm  [SWAP]
sdb           8:16   0   15G  0 disk 					#sdb磁盘,有1个分区,即sdb1
└─sdb1        8:17   0   15G  0 part 
  └─cl-root 253:0    0 46.5G  0 lvm  /
sdc           8:32   0   10G  0 disk 					#sdc磁盘,这就是我们刚才添加的磁盘,还没有分区
sr0          11:0    1 1024M  0 rom  
loop0         7:0    0  4.1G  0 loop /media/iso			#这是本机挂载的iso镜像做yum本地源使用的,不用管它

系统已经成功识别到了我们刚才添加的磁盘并命名为sdc,下面我们就来对sdc进行分区操作。

使用fdisk 命令对磁盘进行分区

[root@master ~]# fdisk /dev/sdc									#注意不要选错磁盘
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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x1a6ca3e9.

Command (m for help): m												#打印帮助文档		
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition											#n表示创建1个新分区
   o   create a new empty DOS partition table
   p   print the partition table									#打印分区表
   q   quit without saving changes									#q表示退出且不保存
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit									#w表示保存并退出
   x   extra functionality (experts only)
Command (m for help): n												#输入 n,我们开始新建一个分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p												#输入 p,我们将分区创建为主分区
Partition number (1-4, default 1): 1								#输入1,我们将分区定义为第1个分区,即sdc1
First sector (2048-20971519, default 2048):							#回车保持默认即可
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519):			#回车保持默认即可
Using default value 20971519
Partition 1 of type Linux and of size 10 GiB is set
Command (m for help): t												#输入t,我们来改变一下分区的类型
Selected partition 1
Hex code (type L to list all codes): 8e								#输入8e,8e就是LVM
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help):w												#退出并保存
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@master ~]#

[root@master ~]# lsblk												#再次查看磁盘信息
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   40G  0 disk 
├─sda1        8:1    0    1M  0 part 
├─sda2        8:2    0  500M  0 part /boot
└─sda3        8:3    0 39.5G  0 part 
  ├─cl-root 253:0    0 46.5G  0 lvm  /
  └─cl-swap 253:1    0    8G  0 lvm  [SWAP]
sdb           8:16   0   15G  0 disk 
└─sdb1        8:17   0   15G  0 part 
  └─cl-root 253:0    0 46.5G  0 lvm  /
sdc           8:32   0   10G  0 disk 
└─sdc1        8:33   0   10G  0 part 								#多了1个sdc1分区了
sr0          11:0    1 1024M  0 rom  
loop0         7:0    0  4.1G  0 loop /media/iso

parted -l 查看根分区的文件系统是什么类型

[root@master ~]# parted -l								# 输入parted -l命令
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 42.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Model: Linux device-mapper (linear) (dm)
Disk /dev/mapper/cl-swap: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags: 

Number  Start  End     Size    File system     Flags
 1      0.00B  8590MB  8590MB  linux-swap(v1)


Model: Linux device-mapper (linear) (dm)
Disk /dev/mapper/cl-root: 49.9GB					#这个/dev/mapper/cl-root就是df-Th 命令查看到的根分区的文件系统
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags: 

Number  Start  End     Size    File system  Flags
 1      0.00B  49.9GB  49.9GB  xfs							#可知,根分区使用xfs的文件系统类型

[root@master ~]#

开始格式化sdc1

注意:不同的文件系统类型需使用不同的命令,如xfs类型的使用mkfs.xfs,ext4的使用mkfs.ext4,这要根据根分区的文件系统类型决定。

[root@master ~]# mkfs.xfs /dev/sdc1 						#使用mkfs.xfs 命令格式化sdc1分区为xfs类型的文件系统
meta-data=/dev/sdc1              isize=512    agcount=4, agsize=655296 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=2621184, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@master ~]# 

查看卷组信息

查看卷组信息,我们关心的是根分区的卷组信息。

[root@master ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/cl/swap
  LV Name                swap
  VG Name                cl
  LV UUID                YXKtbv-hVpe-yzAJ-Ba5U-rVRs-OxOd-3aNN8B
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2021-07-16 16:20:52 +0800
  LV Status              available
  # open                 2
  LV Size                8.00 GiB
  Current LE             2048
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/cl/root								#这是LV Path,记住这个/dev/cl/root
  LV Name                root
  VG Name                cl											#这个卷组名,记住这个组名cl
  LV UUID                hNAQ4l-PuwJ-wVdv-2eGV-PoOk-5ash-6pZo7V
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2021-07-16 16:20:52 +0800
  LV Status              available
  # open                 1
  LV Size                46.50 GiB
  Current LE             11905
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
[root@master ~]# 

开始扩容

[root@master ~]# pvcreate /dev/sdc1											#将sdc1分区变为pv
WARNING: xfs signature detected on /dev/sdc1 at offset 0. Wipe it? [y/n]: y		#输入y
  Wiping xfs signature on /dev/sdc1.
  Physical volume "/dev/sdc1" successfully created.
[root@master ~]# vgextend cl /dev/sdc1				#将/dev/sdc1卷加入根目录所在的卷组名cl,cl就是查看卷组信息时看到的LG name
  Volume group "cl" successfully extended
[root@master ~]# vgdisplay 							 #查看现在卷组信息,记录下Free PE 的数字
  --- Volume group ---
  VG Name               cl
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  6
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               64.50 GiB
  PE Size               4.00 MiB
  Total PE              16512
  Alloc PE / Size       13953 / 54.50 GiB
  Free  PE / Size       2559 / 10.00 GiB								#关注这个可用Free PE
  VG UUID               dOh6BR-umrn-JEt2-83oS-R61C-VZDU-aftE7k
   
[root@master ~]# lvextend -l +2559 /dev/cl/root			#把可用空间全部加给根分区,2559就是上面的Free PE值,/dev/cl/root就是上面查看卷组信息的LV Path值
  Size of logical volume cl/root changed from 46.50 GiB (11905 extents) to 56.50 GiB (14464 extents).
  Logical volume cl/root successfully resized.
  #以上只是完成了卷的扩容,文件系统还没扩容,执行最后一步:
[root@master ~]# xfs_growfs /dev/cl/root
meta-data=/dev/mapper/cl-root    isize=512    agcount=6, agsize=2064896 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=12190720, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=4033, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 12190720 to 14811136
[root@master ~]# df -Th 			#这时查看磁盘空间根分区就真正扩容了

总结

1、关机添加磁盘–>开机
2、lsblk 查看磁盘信息,确认系统已经识别出磁盘sdxx
3、fdisk /dev/sdb 分区,如下:

[root@master ~]# fdisk /dev/sdb
Command (m for help): m											#打印菜单
Command (m for help): n											#新建分区
Select (default p): p											#输入 p,我们将分区创建为主分区
Partition number (1-4, default 1): 1							#输入1,我们将分区定义为第1个分区,即sdc1
First sector (2048-20971519, default 2048):						#回车保持默认即可
First sector (2048-20971519, default 2048):						#回车保持默认即可
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519):			#回车保持默认即可
Using default value 20971519
Partition 1 of type Linux and of size 10 GiB is set
Command (m for help): t											#输入t,我们来改变一下分区的类型
Hex code (type L to list all codes): 8e							#输入8e,8e就是LVM
Command (m for help):w											#保存更改并退出

4、 parted -l #查看根分区的文件系统类型

Model: Linux device-mapper (linear) (dm)
Disk /dev/mapper/cl-root: 60.7GB						#这个/dev/mapper/cl-root就是我们df -Th 经常看到的根分区
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags: 
Number  Start  End     Size    File system  Flags
 1      0.00B  60.7GB  60.7GB  xfs						#文件系统类型是xfs

[root@master ~]# 

5、开始格式化sdb1,指定文件系统类型

[root@master ~]# mkfs.xfs /dev/sdb1			#格式化分区并指定文件系统为xfs

6、开始扩容

[root@master ~]# lvdisplay						#查看卷组信息,记下跟分区的卷组名和路径,即LG name的名字和LV Path的值
[root@master ~]# pvcreate /dev/sdb1			#将分区变为pv
[root@master ~]# vgextend cl /dev/sdb1  		#将sdb1分区 加入根目录所在的卷组名,这里我的是cl,具体是什么名要根据LG name查看
[root@master ~]# vgdisplay   					#查看现在卷组信息,记录下Free PE 的数字
[root@master ~]# lvextend -l +3839 /dev/cl/root	#将可用空间扩容给根分区,3839就是上一步查看的可用Free PE,/dev/cl/root是LV Path		
[root@master ~]# df -Th 						#发现根目录还是原来的空间大小,这是因为以上只是完成了卷的扩容,文件系统还没扩容,最后执行下面这一步
[root@master ~]# xfs_growfs  /dev/cl/root		#/dev/cl/root是LV Path,这才是真正完成了根的扩容。
[root@master ~]# df -Th  						#查看磁盘空间,发现容量已经变大了
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐