Ubuntu 动态调整分区空间

2023-10-18 15:45:31 浏览数 (2)

今天发现虚拟机磁盘满了报警了,明明 50 GiB 可以用很久,怎么会这么快满了呢,找了各种数据库日志文件等半天始终找不出不对劲的文件。

最后一看,尼玛怎么只有 24 GiB

代码语言:javascript复制
ubuntu@ubuntu:~$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              197M  852K  196M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   24G   20G  2.5G  89% /
tmpfs                              982M   16K  982M   1% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          2.0G  253M  1.6G  14% /boot
tmpfs                              197M  4.0K  197M   1% /run/user/1000

可是我明明分了 50 GiB 啊!?吓得我赶紧去后台看看

千真万确,不可能有问题,就是 50 GiB ,于是我用 fdisk 看了一下

代码语言:javascript复制
ubuntu@ubuntu:~$ sudo fdisk -l
Disk /dev/sda: 50 GiB, 53687091200 bytes, 104857600 sectors
Disk model: QEMU HARDDISK   
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: 4ECC6C3D-8F13-4874-9686-81850F57E419

Device         Start       End   Sectors    Size Type
/dev/sda1       2048      4095      2048      1M BIOS boot
/dev/sda2       4096   4198399   4194304      2G Linux filesystem
/dev/sda3    4198400 104855551 100657152     48G Linux filesystem


Disk /dev/mapper/ubuntu--vg-ubuntu--lv: 24 GiB, 25765609472 bytes, 50323456 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

还好,问题不大,50 GiB 读到了,也确实分给了 sda3 ,但是没有分给根分区

系统采用的是 LVM (Logical Volume Manager) 的方式管理磁盘,于是我又用 lsblk 看了一下

代码语言:javascript复制
ubuntu@ubuntu:~$ sudo lsblk
NAME                      MAJ:MIN RM    SIZE RO TYPE MOUNTPOINTS
sda                         8:0    0     50G  0 disk 
├─sda1                      8:1    0      1M  0 part 
├─sda2                      8:2    0      2G  0 part /boot
└─sda3                      8:3    0     48G  0 part 
  └─ubuntu--vg-ubuntu--lv 253:0    0     24G  0 lvm  /
sr0                        11:0    1    1.8G  0 rom  

嗯,果然如此,Logical Volume 没有用尽 Volume Group 的空间

再看一下 Volume Group 的剩余空间

代码语言:javascript复制
ubuntu@ubuntu:~$ sudo vgdisplay -A
  --- 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               <48.00 GiB
  PE Size               4.00 MiB
  Total PE              12287
  Alloc PE / Size       6143 / <24.00 GiB
  Free  PE / Size       6144 / 24.00 GiB
  VG UUID               NuezL3-DcLx-Rnxv-zCLD-Nq1N-GiJU-SFZrFY

这好像看不看没区别,那就直接扩容吧

代码语言:javascript复制
ubuntu@ubuntu:~$ sudo lvextend -l  100%FREE /dev/ubuntu-vg/ubuntu-lv
  Size of logical volume ubuntu-vg/ubuntu-lv changed from <24.00 GiB (6143 extents) to <48.00 GiB (12287 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.
ubuntu@ubuntu:~$ 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 = 3, new_desc_blocks = 6
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 12581888 (4k) blocks long.

扩容成功,现在再用 lsblk 看一下

代码语言:javascript复制
ubuntu@ubuntu:~$ sudo lsblk
NAME                      MAJ:MIN RM    SIZE RO TYPE MOUNTPOINTS
sda                         8:0    0     50G  0 disk 
├─sda1                      8:1    0      1M  0 part 
├─sda2                      8:2    0      2G  0 part /boot
└─sda3                      8:3    0     48G  0 part 
  └─ubuntu--vg-ubuntu--lv 253:0    0     48G  0 lvm  /
sr0                        11:0    1    1.8G  0 rom  

这下没问题了

但是我发现 Volume Group 没有将所有的 Physical Volume 用完,或者没有关联到物理分区

根据我在 CSDN 看到的教程

以不损坏数据为前提,不对原分区 /dev/sda3 进行调整。而是将磁盘可用空间创建一个新的 /dev/sda4 分区,然后去关联 Volume Group,毕竟 Logical Volume Manager 非常灵活。

创建分区 sda4,将设置为 lvm 类型

(我已经加过了,就直接复制他的例子)

其实也可以用我在别的地方看到的方法:n , enter , enter , enter , w ,然后他会提示盘符创建成功(谨慎使用

代码语言:javascript复制
$ sudo fdisk /dev/sda
==> Command (m for help): p

Disk /dev/sda: 50 GiB, 53687091200 bytes, 104857600 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: 3A0A0460-1FC2-46FF-A278-3299E8F2B745

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

===> Command (m for help): n
Partition number (4-128, default 4): 
First sector (41940992-104857566, default 41940992): 
Last sector,  /-sectors or  /-size{K,M,G,T,P} (41940992-104857566, default 104857566): 

Created a new partition 4 of type 'Linux filesystem' and of size 30 GiB.

===> Command (m for help): p
Disk /dev/sda: 50 GiB, 53687091200 bytes, 104857600 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: 3A0A0460-1FC2-46FF-A278-3299E8F2B745

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
/dev/sda4  41940992 104857566 62916575   30G Linux filesystem

===> Command (m for help): t
===> Partition number (1-4, default 4): 
===> Partition type or alias (type L to list all): lvm

Changed type of partition 'Linux filesystem' to 'Linux LVM'.

===> Command (m for help): p
Disk /dev/sda: 50 GiB, 53687091200 bytes, 104857600 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: 3A0A0460-1FC2-46FF-A278-3299E8F2B745

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
/dev/sda4  41940992 104857566 62916575   30G Linux LVM

===> Command (m for help): w
The partition table has been altered.
Syncing disks.

创建之后可以看到 我多出了一个 接近 1M 的 sda4 ( 就当他是 30GiB 嗯对 )

代码语言:javascript复制
ubuntu@ubuntu:~$ sudo lsblk
NAME                      MAJ:MIN RM    SIZE RO TYPE MOUNTPOINTS
sda                         8:0    0     50G  0 disk 
├─sda1                      8:1    0      1M  0 part 
├─sda2                      8:2    0      2G  0 part /boot
├─sda3                      8:3    0     48G  0 part 
│ └─ubuntu--vg-ubuntu--lv 253:0    0     48G  0 lvm  /
└─sda4                      8:4    0 1007.5K  0 part 
sr0                        11:0    1    1.8G  0 rom

将刚刚创建好的 sda4 创建成 Physical Volume

(这里依然用他的例子,因为我这里报错了,原因分区过小)

代码语言:javascript复制
$ sudo pvcreate /dev/sda4
  Physical volume "/dev/sda4" successfully created.

$ sudo pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda3
  VG Name               ubuntu-vg
  PV Size               <18.23 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              4665
  Free PE               0
  Allocated PE          4665
  PV UUID               vO0lgC-X0bG-nUat-kevg-FONv-OpNh-qwY5zF
   
  "/dev/sda4" is a new physical volume of "30.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda4
  VG Name               
  PV Size               30.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               A87gcC-J2mc-3jPD-4emh-8Ute-VQ52-Ceg7zu

将 sda4 扩容到当前的 Volume Group

代码语言:javascript复制
# 查看当前 VG
$ sudo vgdisplay
  --- Volume group ---
  VG Name               ubuntu-vg
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  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       4665 / 18.22 GiB
  Free  PE / Size       0 / 0   
  VG UUID               gHs11o-PLvl-3cw4-SIax-mcw6-7RCx-D4BEpo

# 将 PV 添加到 VG
$ sudo vgextend ubuntu-vg /dev/sda4

# 扩展成功
$ sudo vgdisplay
  --- Volume group ---
  VG Name               ubuntu-vg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               <48.22 GiB
  PE Size               4.00 MiB
  Total PE              12344
  Alloc PE / Size       4665 / 18.22 GiB
  Free  PE / Size       7679 / <30.00 GiB
  VG UUID               gHs11o-PLvl-3cw4-SIax-mcw6-7RCx-D4BEpo

接下来同一开始操作

代码语言:javascript复制
$ sudo lvextend -l  100%FREE /dev/ubuntu-vg/ubuntu-lv
$ sudo resize2fs /dev/ubuntu-vg/ubuntu-lv

$ sudo lsblk
NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
fd0                         2:0    1    4K  0 disk 
loop0                       7:0    0 79.9M  1 loop /snap/lxd/22923
loop1                       7:1    0   62M  1 loop /snap/core20/1587
loop2                       7:2    0   47M  1 loop /snap/snapd/16292
sda                         8:0    0   50G  0 disk 
├─sda1                      8:1    0    1M  0 part 
├─sda2                      8:2    0  1.8G  0 part /boot
├─sda3                      8:3    0 18.2G  0 part 
│ └─ubuntu--vg-ubuntu--lv 253:0    0 48.2G  0 lvm  /
└─sda4                      8:4    0   30G  0 part 
  └─ubuntu--vg-ubuntu--lv 253:0    0 48.2G  0 lvm  /

参考文献

https://blog.csdn.net/dagjj/article/details/131259096

https://blog.51cto.com/u_15127659/4659100

https://blog.csdn.net/qq1090504117/article/details/131598038

0 人点赞