如何在不丢失数据的情况下增加 /dev/mapper/centos-root 的大小

如何在不丢失数据的情况下增加 /dev/mapper/centos-root 的大小

我正在运行虚拟服务器,提供商添加了额外的磁盘空间。但是,我需要以某种方式增加(合并)现有磁盘(包含数据)和新磁盘(空)。

如何增加尺寸/dev/mapper/centos-root

[root]#df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 3.9G     0  3.9G   0% /dev
tmpfs                    3.9G     0  3.9G   0% /dev/shm
tmpfs                    3.9G  243M  3.7G   7% /run
tmpfs                    3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root   98G   83G   16G  85% /
tmpfs                    3.9G  156K  3.9G   1% /tmp
/dev/sda1                497M  168M  330M  34% /boot
tmpfs                    797M     0  797M   0% /run/user/1000

[root]# fdisk -l

Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x000997be

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    41943039    20458496   8e  Linux LVM
/dev/sda3        41943040   209715199    83886080   8e  Linux LVM

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

Disk /dev/mapper/centos-root: 104.7 GB, 104694022144 bytes, 204480512 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

它显示总空间 > 200GB,所以我确实有 100GB 的额外空间(未使用),所以我尝试从卷组 (VG) 中增加空间,但它显示没有可用空间。所以我想我必须先以某种方式将可用空间添加到 VG 中?怎么做?

[root]# 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               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               99.50 GiB
  PE Size               4.00 MiB
  Total PE              25473
  Alloc PE / Size       25473 / 99.50 GiB
  Free  PE / Size       0 / 0
  VG UUID               mlGu82-tMEl-EB1I-LdvQ-V5Jz-FmRD-7wMPdk

下面您可以看到 LV 是如何划分的(在 SWAP 和 ROOT 之间),我猜这是相当默认的。

[root]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                E4q3BO-bxxu-Mj8J-nNYJ-ECdR-He20-UqTzHd
  LV Write Access        read/write
  LV Creation host, time localhost, 2017-07-12 03:09:26 +0700
  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:0

  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                fANu3W-nl0g-40rQ-NLSA-I77S-4vXE-Stg2D5
  LV Write Access        read/write
  LV Creation host, time localhost, 2017-07-12 03:09:27 +0700
  LV Status              available
  # open                 1
  LV Size                97.50 GiB
  Current LE             24961
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

只有在 CFDISK 中我才能真正看到同一磁盘上有可用空间:sda

 cfdisk (util-linux 2.23.2)

                              Disk Drive: /dev/sda
                       Size: 214748364800 bytes, 214.7 GB
             Heads: 255   Sectors per Track: 63   Cylinders: 26108

    Name        Flags      Part Type  FS Type          [Label]        Size (MB)
 ------------------------------------------------------------------------------
                            Pri/Log   Free Space                           1.05*
    sda1        Boot        Primary   xfs                                524.29*
    sda2                    Primary   LVM2_member                      20949.50*
    sda3                    Primary   LVM2_member                      85899.35*
                            Pri/Log   Free Space                      107374.19*

对于文件系统(请注意,并非所有文件系统都支持调整大小):

[root]# lsblk -f
NAME            FSTYPE      LABEL UUID                                   MOUNTPOINT
sda
├─sda1          xfs               9b1d00b3-24f5-4c0e-b84a-d6d9527d375d   /boot
├─sda2          LVM2_member       4rFgZ8-tAZH-8Mr0-hauE-vz9j-YCV4-3gVl3r
│ ├─centos-swap swap              456e16d8-3c60-46b5-812a-3ae2d54f5bcf   [SWAP]
│ └─centos-root xfs               fa159170-1c29-4387-80c4-d168e2cc64c4   /
└─sda3          LVM2_member       c6CKfP-RP63-F107-qJ7G-0lCC-0CJZ-pOKJ4x
  └─centos-root xfs               fa159170-1c29-4387-80c4-d168e2cc64c4   /

此外,fstab 中引用了 centos-root:

/dev/mapper/centos-root /                       xfs     defaults        1 1

我希望我提供的信息足以做出良好的评估,我真的希望我可以增加磁盘空间而不会丢失数据,否则我必须想办法将 100GB 转移到某个地方,然后删除所有内容,并重新创建一个更大的磁盘。我相信 2022 年会有更简单的方法 :)

提前致谢!

PS:我当然也读过类似的帖子,但这让我更加困惑。我只是害怕意外删除现有数据(即使我有备份)。

答案1

# resize the last partition to utilize the free disk space
growpart /dev/sda 3
# resize the physical volume to match the partition size
pvresize /dev/sda3
# extend the logical volume to utilize the additional space of the physical volume
lvextend -l +100%FREE /dev/centos/root
# resize the filesystem to match the size of the logical volume
xfs_growfs /dev/centos/root

答案2

遇到了同样的问题。我的情况是 sda 2,

我的盒子上还没有 growpart,所以我
yum install cloud-utils-growpart -y
按照其余的步骤操作,它就成功了,

当进度达到 80% 时,我开始在谷歌上搜索如何修复此问题。(4 个月过去了)
直到进度达到 98% 时,我才找到这个解决方案。你为我节省了很多时间

相关内容