CentOS 根分区扩容指南

当前环境

名称属性值
服务器系统CentOS-7
虚拟化方式Hyper-V

磁盘情况

目前只有一块50G的硬盘,给到了/boot分区1G,SWAP4G,剩余的45G全部给了根分区。

[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0               2:0    1    4K  0 disk 
sda               8:0    0   50G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   49G  0 part 
  ├─centos-root 253:0    0 45.1G  0 lvm  /
  └─centos-swap 253:1    0  3.9G  0 lvm  [SWAP]
sr0              11:0    1 1024M  0 rom  

注意:并不是所有的服务器文件系统都支持扩容,操作前请注意所在环境是否与本文相同。

开始扩容

  1. 添加一块10G大小的磁盘,该步骤本文不做详细说明,可以参考网上其他教程。

    [root@localhost ~]# lsblk
    NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    fd0               2:0    1    4K  0 disk 
    sda               8:0    0   50G  0 disk 
    ├─sda1            8:1    0    1G  0 part /boot
    └─sda2            8:2    0   49G  0 part 
      ├─centos-root 253:0    0 45.1G  0 lvm  /
      └─centos-swap 253:1    0  3.9G  0 lvm  [SWAP]
    sdb               8:16   0   10G  0 disk 
    sr0              11:0    1 1024M  0 rom
  2. 经过对比增加磁盘前后的服务器磁盘情况,我们可以得知,新增加的磁盘在/dev/sdb,我们需要使用fdisk将该磁盘初始化和分区。

    [root@localhost ~]# fdisk /dev/sdb
    欢迎使用 fdisk (util-linux 2.23.2)。
    
    更改将停留在内存中,直到您决定将更改写入磁盘。
    使用写入命令前请三思。
    
    Device does not contain a recognized partition table
    使用磁盘标识符 0xc2d7607a 创建新的 DOS 磁盘标签。
    
    The device presents a logical sector size that is smaller than
    the physical sector size. Aligning to a physical sector (or optimal
    I/O) size boundary is recommended, or performance may be impacted.
    
    命令(输入 m 获取帮助):n
    Partition type:
    p   primary (0 primary, 0 extended, 4 free)
    e   extended
    Select (default p): p
    分区号 (1-4,默认 1):
    起始 扇区 (2048-20971519,默认为 2048):
    将使用默认值 2048
    Last 扇区, +扇区 or +size{K,M,G} (2048-20971519,默认为 20971519):
    将使用默认值 20971519
    分区 1 已设置为 Linux 类型,大小设为 10 GiB
    
    命令(输入 m 获取帮助):t
    已选择分区 1
    Hex 代码(输入 L 列出所有代码):8e
    已将分区“Linux”的类型更改为“Linux LVM”
    
    命令(输入 m 获取帮助):w
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    正在同步磁盘。
  3. 查看分区后的磁盘情况,如下代码所示,我们已经成功将新磁盘初始化并分配到了/dev/sdb1位置

    [root@localhost ~]# lsblk
    NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    fd0               2:0    1    4K  0 disk 
    sda               8:0    0   50G  0 disk 
    ├─sda1            8:1    0    1G  0 part /boot
    └─sda2            8:2    0   49G  0 part 
      ├─centos-root 253:0    0 45.1G  0 lvm  /
      └─centos-swap 253:1    0  3.9G  0 lvm  [SWAP]
    sdb               8:16   0   10G  0 disk 
    └─sdb1            8:17   0   10G  0 part 
    sr0              11:0    1 1024M  0 rom 
  4. 查看根分区的文件系统,并将新磁盘格式化为该系统,我们从下文中得知,根分区的文件系统为xfs

    [root@localhost ~]# parted -l
    Model: Msft Virtual Disk (scsi)
    Disk /dev/sda: 53.7GB
    Sector size (logical/physical): 512B/4096B
    Partition Table: msdos
    Disk Flags: 
    
    Number  Start   End     Size    Type     File system  标志
     1      1049kB  1075MB  1074MB  primary  xfs          启动
     2      1075MB  53.7GB  52.6GB  primary               lvm
    
    
    Model: Msft Virtual Disk (scsi)
    Disk /dev/sdb: 10.7GB
    Sector size (logical/physical): 512B/4096B
    Partition Table: msdos
    Disk Flags: 
    
    Number  Start   End     Size    Type     File system  标志
     1      1049kB  10.7GB  10.7GB  primary               lvm
    
    
    Model: Linux device-mapper (linear) (dm)
    Disk /dev/mapper/centos-swap: 4161MB
    Sector size (logical/physical): 512B/4096B
    Partition Table: loop
    Disk Flags: 
    
    Number  Start  End     Size    File system     标志
     1      0.00B  4161MB  4161MB  linux-swap(v1)
    
    
    Model: Linux device-mapper (linear) (dm)
    Disk /dev/mapper/centos-root: 48.4GB
    Sector size (logical/physical): 512B/4096B
    Partition Table: loop
    Disk Flags: 
    
    Number  Start  End     Size    File system  标志
     1      0.00B  48.4GB  48.4GB  xfs
    
    
    [root@localhost ~]# mkfs.xfs /dev/sdb1 
    Discarding blocks...Done.
    meta-data=/dev/sdb1              isize=512    agcount=4, agsize=655296 blks
          =                       sectsz=4096  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=4096  sunit=1 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0
  5. 查看卷组信息,由下文可知,根分区所在卷组名称为centos

    [root@localhost ~]# lvdisplay
      --- Logical volume ---
      LV Path                /dev/centos/root
      LV Name                root
      VG Name                centos
      LV UUID                YPcNpO-7DRd-8KYu-qLyJ-zoVi-26Wi-EwUtgf
      LV Write Access        read/write
      LV Creation host, time localhost.localdomain, 2023-12-13 17:46:09 +0800
      LV Status              available
      # open                 1
      LV Size                <45.12 GiB
      Current LE             11550
      Segments               1
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     8192
      Block device           253:0
    
      --- Logical volume ---
      LV Path                /dev/centos/swap
      LV Name                swap
      VG Name                centos
      LV UUID                O8jYkL-fnYw-SClp-3ugE-6fzF-4l3K-sPVGfq
      LV Write Access        read/write
      LV Creation host, time localhost.localdomain, 2023-12-13 17:46:10 +0800
      LV Status              available
      # open                 2
      LV Size                <3.88 GiB
      Current LE             992
      Segments               1
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     8192
      Block device           253:1
  6. 扩容

    [root@localhost ~]# pvcreate /dev/sdb1
    WARNING: xfs signature detected on /dev/sdb1 at offset 0. Wipe it? [y/n]: y
      Wiping xfs signature on /dev/sdb1.
      Physical volume "/dev/sdb1" successfully created.
    [root@localhost ~]# vgextend centos /dev/sdb1 
      Volume group "centos" successfully extended
    [root@localhost ~]# lvextend -l +100%FREE /dev/centos/root 
      Size of logical volume centos/root changed from <45.12 GiB (11550 extents) to <55.12 GiB (14110 extents).
      Logical volume centos/root successfully resized.
    [root@localhost ~]# xfs_growfs /dev/centos/root 
    meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=2956800 blks
          =                       sectsz=4096  attr=2, projid32bit=1
          =                       crc=1        finobt=0 spinodes=0
    data     =                       bsize=4096   blocks=11827200, imaxpct=25
          =                       sunit=0      swidth=0 blks
    naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
    log      =internal               bsize=4096   blocks=5775, version=2
          =                       sectsz=4096  sunit=1 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0
    data blocks changed from 11827200 to 14448640
    [root@localhost ~]# df -Th
    文件系统                类型      容量  已用  可用 已用% 挂载点
    devtmpfs                devtmpfs  1.9G     0  1.9G    0% /dev
    tmpfs                   tmpfs     1.9G   16K  1.9G    1% /dev/shm
    tmpfs                   tmpfs     1.9G  9.0M  1.9G    1% /run
    tmpfs                   tmpfs     1.9G     0  1.9G    0% /sys/fs/cgroup
    /dev/mapper/centos-root xfs        56G  3.5G   52G    7% /
    /dev/sda1               xfs      1014M  150M  865M   15% /boot
    tmpfs                   tmpfs     379M     0  379M    0% /run/user/0