使用LVM给Centos根分区扩容

发布时间 2023-08-30 19:34:23作者: tomoncle

使用LVM给Centos根分区扩容

1.查看磁盘使用

root@tomoncle:~# df -h
文件系统                 容量  已用  可用 已用% 挂载点
devtmpfs                 1.9G     0  1.9G    0% /dev
tmpfs                    2.0G     0  2.0G    0% /dev/shm
tmpfs                    2.0G  1.1M  2.0G    1% /run
tmpfs                    2.0G     0  2.0G    0% /sys/fs/cgroup
/dev/mapper/centos-root   27G   21G  7.0G   75% /
/dev/sda1               1014M  185M  830M   19% /boot
tmpfs                    391M     0  391M    0% /run/user/0

2.查看磁盘分区,总量107.4 GB,3个块设备

root@tomoncle:~# fdisk -l

磁盘 /dev/sda:107.4 GB, 107374182400 字节,209715200 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000b6687

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
/dev/sda3        41943040    62914559    10485760   83  Linux

磁盘 /dev/mapper/centos-root:29.0 GB, 28982640640 字节,56606720 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/centos-swap:2147 MB, 2147483648 字节,4194304 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

3.查看卷组名称,以及卷组使用情况

root@tomoncle:~# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               28.99 GiB
  PE Size               4.00 MiB
  Total PE              7422
  Alloc PE / Size       7422 / 28.99 GiB
  Free  PE / Size       0 / 0
  VG UUID               rpGD9T-QbcR-ogOf-9x5W-D7ZI-46Sp-x2pR4d

4.创建分区

root@tomoncle:~# fdisk /dev/sda
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。


命令(输入 m 获取帮助):n
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): p
已选择分区 4
起始 扇区 (62914560-209715199,默认为 62914560):
将使用默认值 62914560
Last 扇区, +扇区 or +size{K,M,G} (62914560-209715199,默认为 209715199):
将使用默认值 209715199
分区 4 已设置为 Linux 类型,大小设为 70 GiB

命令(输入 m 获取帮助):p

磁盘 /dev/sda:107.4 GB, 107374182400 字节,209715200 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000b6687

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
/dev/sda3        41943040    62914559    10485760   83  Linux
/dev/sda4        62914560   209715199    73400320   83  Linux

命令(输入 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)
正在同步磁盘。

5.刷新分区并创建物理卷

root@tomoncle:~# partprobe /dev/sda
root@tomoncle:~# pvcreate /dev/sda4
  Physical volume "/dev/sda4" successfully created.

6.将物理卷 /dev/sda4 扩展到 卷组centos

root@tomoncle:~# vgextend centos /dev/sda4
  Volume group "centos" successfully extended

7.查看当前逻辑卷的空间状态

root@tomoncle:~# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                XcSWL1-RnsO-qudm-RbKm-lydl-DCeT-hXS0iz
  LV Write Access        read/write
  LV Creation host, time tomoncle, 2019-07-02 11:37:33 +0800
  LV Status              available
  # open                 0
  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                oJqxNE-gMsu-EtXn-1GNt-1Vk3-h6KV-7Peqoh
  LV Write Access        read/write
  LV Creation host, time tomoncle, 2019-07-02 11:37:33 +0800
  LV Status              available
  # open                 1
  LV Size                26.99 GiB
  Current LE             6910
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

8.将卷组中的空闲空间扩展到根分区逻辑卷 /dev/centos/root

root@tomoncle:~# lvextend -l +100%FREE /dev/centos/root
  Size of logical volume centos/root changed from 26.99 GiB (6910 extents) to <96.99 GiB (24829 extents).
  Logical volume centos/root successfully resized.

9.刷新根分区

root@tomoncle:~# xfs_growfs /dev/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=7075840, 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 7075840 to 25424896

10.查看扩容后磁盘

root@tomoncle:~# df -h
文件系统                 容量  已用  可用 已用% 挂载点
devtmpfs                 1.9G     0  1.9G    0% /dev
tmpfs                    2.0G     0  2.0G    0% /dev/shm
tmpfs                    2.0G  1.1M  2.0G    1% /run
tmpfs                    2.0G     0  2.0G    0% /sys/fs/cgroup
/dev/mapper/centos-root   97G   21G   77G   21% /
/dev/sda1               1014M  185M  830M   19% /boot
tmpfs                    391M     0  391M    0% /run/user/0