这篇文章上次修改于 693 天前,可能其部分内容已经发生变化,如有疑问可询问作者。

Vmware虚拟机Debian/Centos系统扩充磁盘方法。注意,本文要求系统安装时要求文件系统使用逻辑卷方式。

现磁盘磁盘使用情况:

doufu@Debian:~$ df -hT
文件系统                        类型      容量  已用  可用 已用% 挂载点
udev                            devtmpfs  3.9G     0  3.9G    0% /dev
tmpfs                           tmpfs     793M  2.0M  791M    1% /run
/dev/mapper/Debian--vg-root     ext4       30G   28G  1.1G   97% /
tmpfs                           tmpfs     3.9G     0  3.9G    0% /dev/shm
tmpfs                           tmpfs     5.0M  4.0K  5.0M    1% /run/lock
/dev/sda1                       ext2      470M  111M  335M   25% /boot
/dev/mapper/Debian--vg-home     ext4       30G  5.9G   23G   21% /home
tmpfs                           tmpfs     793M  168K  793M    1% /run/user/118
tmpfs                           tmpfs     793M  140K  793M    1% /run/user/1000

可以看到,根分区空间不足了。

1. 修改虚拟机设置,增大磁盘

直修改虚拟即的配置,Vmware和ESXi分别修改方法如下图:
Vmware:
vm.png
ESXi:
esxi.png
本文由80G增加10G到90GB。

2. 创建新分区

使用 fdisk 命令创建新分区。

doufu@Debian:~$ sudo fdisk -l
[sudo] doufu 的密码:
Disk /dev/sda: 90 GiB, 96636764160 bytes, 188743680 sectors
Disk model: Virtual disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xdb8ab933

Device     Boot   Start       End   Sectors  Size Id Type
/dev/sda1  *       2048    999423    997376  487M 83 Linux
/dev/sda2       1001470 167770111 166768642 79.5G  5 Extended
/dev/sda5       1001472 167770111 166768640 79.5G 8e Linux LVM


Disk /dev/mapper/Debian--vg-root: 30 GiB, 32212254720 bytes, 62914560 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/Debian--vg-swap_1: 976 MiB, 1023410176 bytes, 1998848 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/Debian--vg-home: 30.55 GiB, 32799457280 bytes, 64061440 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
doufu@Debian:~$ sudo fdisk /dev/sda

Welcome to fdisk (util-linux 2.36.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): m              ####### 查看帮助(可选)

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table


Command (m for help): n                      ####### 添加新分区
Partition type
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): p                        ####### 主分区
Partition number (3,4, default 3):           ####### 分区号
First sector (167770112-188743679, default 167770112):           ####### 第一个扇面(默认即可)
Last sector, +/-sectors or +/-size{K,M,G,T,P} (167770112-188743679, default 188743679):   ####### 最后扇面(默认即可)

Created a new partition 3 of type 'Linux' and of size 10 GiB.

Command (m for help): w                      ####### 写入修改并推出
The partition table has been altered.
Syncing disks.

3. 创建物理卷

重启系统后创建物理卷

doufu@Debian:~$ sudo pvcreate /dev/sda3
[sudo] doufu 的密码:
  Physical volume "/dev/sda3" successfully created.

查看物理卷信息

doufu@Debian:~$ sudo pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda5
  VG Name               Debian-vg
  PV Size               79.52 GiB / not usable 2.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              20357
  Free PE               4613
  Allocated PE          15744
  PV UUID               ePbJHF-ieGm-8ACl-p0zV-QEMX-IZdx-eMYtZ7

  "/dev/sda3" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda3
  VG Name
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               VZPj7u-xBae-ITF9-8BMF-1J5h-xQMZ-jSrhRk

可以看到多了新创建的sda3,,大小10GB。

4. 加入逻辑卷

查看当前系统逻辑分区情况:

doufu@Debian:~$ sudo vgdisplay
  --- Volume group ---
  VG Name               Debian-vg
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  6
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <79.52 GiB
  PE Size               4.00 MiB
  Total PE              20357
  Alloc PE / Size       15744 / 61.50 GiB
  Free  PE / Size       4613 / <0.02 GiB
  VG UUID               1a3US5-PaKG-vnYy-ds5D-NC7g-OWs7-yMe1Ss

将新物理卷加入到根目录分区逻辑卷中。

doufu@Debian:~$ sudo vgextend Debian-vg /dev/sda3
  Volume group "Debian-vg" successfully extended

再次查看逻辑卷信息,空间已增加

doufu@Debian:~$ sudo vgdisplay
  --- Volume group ---
  VG Name               Debian-vg
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  7
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               <89.52 GiB
  PE Size               4.00 MiB
  Total PE              22917
  Alloc PE / Size       15744 / 61.50 GiB
  Free  PE / Size       7173 / <10.02 GiB
  VG UUID               1a3US5-PaKG-vnYy-ds5D-NC7g-OWs7-yMe1Ss

可以看到,当前有10GB Free PE,将其加入到根分区逻辑卷中(本文只增加了8GB),逻辑卷名可使用本文开头 df 命令查看。
doufu@Debian:~$ sudo lvresize -L +8G /dev/mapper/Debian--vg-root
Size of logical volume Debian-vg/root changed from 30.00 GiB (7680 extents) to 38.00 GiB (9728 extents).
Logical volume Debian-vg/root successfully resized.

4. 刷新容量

最后,根据分区类型使用不同命令刷新磁盘
ext2、ext3、ext4文件系统使用 resize2fs 命令;xfs文件系统使用 xfs_growfs 命令。

doufu@Debian:~$ sudo resize2fs /dev/mapper/Debian--vg-root
resize2fs 1.46.2 (28-Feb-2021)
Filesystem at /dev/mapper/Debian--vg-root is mounted on /; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 5
The filesystem on /dev/mapper/Debian--vg-root is now 9961472 (4k) blocks long.

再次查看分区使用情况,已扩容成功

doufu@Debian:~$ df -hT
文件系统                        类型      容量  已用  可用 已用% 挂载点
udev                            devtmpfs  3.9G     0  3.9G    0% /dev
tmpfs                           tmpfs     793M  2.0M  791M    1% /run
/dev/mapper/Debian--vg-root     ext4       38G   27G  8.6G   76% /
tmpfs                           tmpfs     3.9G     0  3.9G    0% /dev/shm
tmpfs                           tmpfs     5.0M  4.0K  5.0M    1% /run/lock
/dev/sda1                       ext2      470M  111M  335M   25% /boot
/dev/mapper/Debian--vg-home     ext4       30G  5.9G   23G   21% /home
tmpfs                           tmpfs     793M  168K  793M    1% /run/user/118
tmpfs                           tmpfs     793M  140K  793M    1% /run/user/1000