1. 扩展背景

虚拟机初始分配20G内存,扩容到80G。
Linux Centos 根目录扩展分区(保级教程)-LMLPHP

2.列出磁盘信息

可以得知容量信息以及即将创建的PV路径(通常为“/dev/sda累加数字”),下面例子为sda2,预期扩展60G

# 列出各分区使用情况
[root@master02 ~]# df -hl
# 查看现有磁盘情况
[root@master02 ~]# fdisk -l

Linux Centos 根目录扩展分区(保级教程)-LMLPHP

Disk /dev/sda: 85.9 GB, 85899345920 bytes, 167772160 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: 0x000ae5b5

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM

Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 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/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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

3. 对磁盘进行分区

    sudo fdisk /dev/sda(你的磁盘号)
    命令行提示下输入:
  【n】添加新分区。
  【p】创建主分区。
  【回车】分区号   --默认分区编号 
  【回车】--默认起始扇区位置。
  【回车】--默认结束扇区位置。 如果要把100G全部分给这个分区,直接两次回车。如果分60G输入+60G
  【t】      --修改分区类型
  【8e】   --修改为LVM
  【w】    --保存

具体操作如下:

[root@master02 ~]# 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):【n】
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): 【p】
Partition number (3,4, default 3): 【回车】
First sector (41943040-167772159, default 41943040): 【回车】
Using default value 41943040
Last sector, +sectors or +size{K,M,G} (41943040-167772159, default 167772159): 【回车】
Using default value 167772159
Partition 3 of type Linux and of size 60 GiB is set

Command (m for help): t
Partition number (1-3, default 3): 【回车】
Hex code (type L to list all codes): 【8e]
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.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
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)
Syncing disks.

4. 重启Linux

重启linux,必须reboot,否则无法格式化。

reboot

5. 将PV加入卷组centos并分区

  • 始化PV并加入卷组centos
 pvcreate /dev/sda3
 vgextend centos /dev/sda3
  • 将空余容量分配给逻辑分区
    +60G根据本次要扩展的容量得出,具体数值看分配扩展空间的大小,如果60G空间溢出,可以选择59G
 lvextend -L +60G /dev/mapper/centos-root
  • 重载
xfs_growfs /dev/centos/root

具体操作步骤如下:

[root@master02 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                7FxqiT-LSfr-380m-aXO0-i8k4-O7TC-oRxCA0
  LV Write Access        read/write
  LV Creation host, time localhost, 2023-11-10 18:42:59 +0800
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  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                2MCadI-YApB-DtaL-a8uo-eubT-SwzJ-CYvmif
  LV Write Access        read/write
  LV Creation host, time localhost, 2023-11-10 18:42:59 +0800
  LV Status              available
  # open                 1
  LV Size                <17.00 GiB
  Current LE             4351
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
[root@master02 ~]# lvextend -L +60G /dev/mapper/centos-root
  Insufficient free space: 15360 extents needed, but only 15359 available
[root@master02 ~]# lvextend -L +59G /dev/mapper/centos-root
  Size of logical volume centos/root changed from <17.00 GiB (4351 extents) to <76.00 GiB (19455 extents).
  Logical volume centos/root successfully resized.
[root@master02 ~]# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=1113856 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=4455424, 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
data blocks changed from 4455424 to 19921920

6.查看分区结果

/dev/mapper/centos-root已经扩展到75G

[root@master02 ~]# df -hl
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 1.4G     0  1.4G   0% /dev
tmpfs                    1.4G     0  1.4G   0% /dev/shm
tmpfs                    1.4G   11M  1.4G   1% /run
tmpfs                    1.4G     0  1.4G   0% /sys/fs/cgroup
/dev/mapper/centos-root   76G  4.6G   72G   7% /
/dev/sda1               1014M  223M  792M  22% /boot
tmpfs                    283M   24K  283M   1% /run/user/1001
11-14 16:57