目录

目录

磁盘扩容

系列 - 磁盘管理与扩容
目录

针对 Ubuntu22.04 系统进行磁盘扩容,部分命令在不同操作系统或许会有区别。

查看磁盘会用到一些命令fdisk -ldf -h

lilac@lilac:~/source$ sudo fdisk -l
Disk /dev/sda: 25 GiB, 26843545600 bytes, 52428800 sectors
Disk model: VMware Virtual S
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: gpt
Disk identifier: D3A783CB-8E8D-4E98-AA86-9D5BA6A5F1D6

Device       Start      End  Sectors  Size Type
/dev/sda1     2048     4095     2048    1M BIOS boot
/dev/sda2     4096  3719167  3715072  1.8G Linux filesystem
/dev/sda3  3719168 41940991 38221824 18.2G Linux filesystem


Disk /dev/mapper/ubuntu--vg-ubuntu--lv: 12.5 GiB, 13421772800 bytes, 26214400 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
lilac@lilac:~/source$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              193M   21M  173M  11% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   13G  9.7G  2.0G  84% /

上述两个命令用的比较多,我们需要了解逻辑卷管理(LVM)的概念:

1)/dev/sda3 是物理卷(Physical Volume,PV),它是硬盘上的一个分区,用于存储 LVM 卷组(Volume Group,VG)。

2)/dev/mapper/ubuntu--vg-ubuntu--lv 是逻辑卷(Logical Volume,LV),它是从 LVM 卷组中划分出来的逻辑存储卷。

通常情况下,LVM 允许你将一个或多个物理卷(例如 /dev/sda3)组合成一个卷组(例如 ubuntu–vg),然后从卷组中划分出一个或多个逻辑卷(例如 /dev/mapper/ubuntu–vg-ubuntu–lv),以供文件系统或其他用途使用。这种灵活性使得对存储空间的管理更加方便和灵活。

我们可以使用命令查看逻辑卷组的信息:

lilac@lilac:~$ sudo lvdisplay
  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/ubuntu-lv
  LV Name                ubuntu-lv
  VG Name                ubuntu-vg
  LV UUID                Jm6qHV-cz2m-6QTn-AhRO-XDpR-71o3-qBRb5V
  LV Write Access        read/write
  LV Creation host, time ubuntu-server, 2024-05-01 09:25:34 +0000
  LV Status              available
  # open                 1
  LV Size                10.00 GiB
  Current LE             2560
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

在这个例子中,ubuntu–vg 是卷组的名称,ubuntu–lv 是逻辑卷的名称。

逻辑卷的大小只分配了10G,能否扩容取决于当前是否有未分配的物理空间用于扩展逻辑卷。 我们通过命令查看 “Free PE / Size” 部分,它将显示卷组中的剩余空间量:

lilac@lilac:~$ sudo vgdisplay
  --- Volume group ---
  VG Name               ubuntu-vg
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               18.22 GiB
  PE Size               4.00 MiB
  Total PE              4665
  Alloc PE / Size       2560 / 10.00 GiB
  Free  PE / Size       2105 / 8.22 GiB
  VG UUID               gK26XX-lBuB-CwQm-hBLZ-QkDA-Oc5h-sHpaZR

卷组 ubuntu-vg 共有 18.22 GiB 的空间。目前,其中 10.00 GiB 已被分配给逻辑卷,剩余空间为 8.22 GiB。

卷组中既然有剩余容量,我们来进行扩容,可以使用 lvextend 命令将逻辑卷 /dev/ubuntu-vg/ubuntu-lv 扩展到卷组中的可用空间:

lilac@lilac:~/source$ sudo lvextend -L +2.5G /dev/ubuntu-vg/ubuntu-lv
  Size of logical volume ubuntu-vg/ubuntu-lv changed from 10.00 GiB (2560 extents) to 12.50 GiB (3200 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.
lilac@lilac:~/source$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              193M   21M  173M  11% /run
/dev/mapper/ubuntu--vg-ubuntu--lv  9.8G  9.7G     0 100% /

此时还没有应用到文件系统中,需要特定的工具来扩展文件系统,以便它能够利用新分配的空间,使用resize2fs命令来扩展文件系统:


lilac@lilac:~/source$ sudo resize2fs /dev/ubuntu-vg/ubuntu-lv
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/ubuntu-vg/ubuntu-lv is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 2
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 3276800 (4k) blocks long.

lilac@lilac:~/source$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              193M   21M  173M  11% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   13G  9.7G  2.0G  84% /

执行这些命令后,你的逻辑卷就会扩展到卷组中的所有可用空间,并且文件系统也会相应地进行扩展。

① 物理分区是硬盘上的分区,它们被用来创建卷组。 ② 卷组由一个或多个物理分区组成,它们被用来创建逻辑卷。 ③ 逻辑卷是在卷组上创建的虚拟分区,它们被用来存储文件系统和用户数据。