Ubuntu 磁盘动态分区调整
当我们调整虚拟机的虚拟硬盘大小或将磁盘映像恢复到更大的磁盘时,Ubuntu检测到的分区的可用空间不会增加,因为分区表没有改变。过去,我们可以借助 .ext4 轻松调整 ext4 根分区的大小resize2fs。然而,当 Ubuntu 使用 LVM 分区作为默认根分区时,事情会变得复杂。
LVM 介绍
逻辑卷管理器(LVM)类似于Windows下的动态磁盘,它可以将不同硬盘上的几个GPT/MBR分区作为一个存储池(LVM称之为Volume Groups,VG),并从这个池中分配空间,然后Linux将将每个空间(LVM 称为逻辑卷,LV)识别为可用分区。
PV、VG、LV 介绍
- PV(Phsical Volume,物理卷),PV是VG的组成部分,由分区构成,多块盘的时候,可以把一块盘格式化成一个主分区,然后用这个分区做成一个PV,只有一块盘的时候,可以这块盘的某一个分区做成一个PV,实际上一个PV就一个分区。
- VG(Volume Group, 卷组),有若干个PV组成,作用就是将PV组成到一起,然后再重新划分空间。
- LV(Logical Volume,逻辑卷),LV就是从VG中划分出来的卷,LV的使用要比PV灵活的多,可以在空间不够的情况下,增加空间。
命令集合
PV
pvcreate: 根据物理盘,创建pv
pvscan: 查询目前系统里的pv
pvdisplay: 显示pv的状态
pvremove: 将pv属性移除
VG
vgcreate: 创建vg
vgscan: 查找当前系统里面的vg
vgdisplay: 显示当前系统vg的状态
vgextend: 给vg添加额外的pv
vgreduce: 在vg内删除pv
vgchange: 设置vg是否是启动状态(active)
vgremove: 删除一个vg
LV
lvcreate: 创建lv
lvscan: 查询当前系统的lv
lvdisplay: 显示lv的属性
lvextend: 给lv添加容量
lvredurce: 给lv减少容量
lvremove: 删除一个lv
lvresize: 对lv大小的容量进行调整
扩展 ubuntu--vg-ubuntu--lv
- 查看磁盘使用情况
df -hl
Filesystem Size Used Avail Use% Mounted on
tmpfs 792M 1.7M 790M 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 37G 13G 23G 36% /
tmpfs 3.9G 28K 3.9G 1% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda2 2.0G 126M 1.7G 7% /boot
/dev/sda1 1.1G 5.3M 1.1G 1% /boot/efi
ncdata 39G 39M 39G 1% /mnt/ncdata
overlay 37G 13G 23G 36% /var/lib/docker/overlay2/06991ebd4b2aa71bad195055f4c25f56505fe8f827f22f255bc63a1efe53094b/merged
tmpfs 792M 4.0K 792M 1% /run/user/1000
- 创建分区 sda4,设置为 lvm 类型
sudo fdisk /dev/sda
Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
GPT PMBR size mismatch (83886079 != 2097151999) will be corrected by write.
This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.
Command (m for help): p
Disk /dev/sda: 1000 GiB, 1073741824000 bytes, 2097152000 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: 469C7B79-92EE-49A6-B150-E85497A85FF9
Device Start End Sectors Size Type
/dev/sda1 2048 2203647 2201600 1G EFI System
/dev/sda2 2203648 6397951 4194304 2G Linux filesystem
/dev/sda3 6397952 83884031 77486080 36.9G Linux filesystem
Command (m for help): n
Partition number (4-128, default 4): 4
First sector (83884032-2097151966, default 83884032): 83884032
Last sector, +/-sectors or +/-size{K,M,G,T,P} (83884032-2097151966, default 2097151966): 2097151966
Created a new partition 4 of type 'Linux filesystem' and of size 960 GiB.
Command (m for help): p
Disk /dev/sda: 1000 GiB, 1073741824000 bytes, 2097152000 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: 469C7B79-92EE-49A6-B150-E85497A85FF9
Device Start End Sectors Size Type
/dev/sda1 2048 2203647 2201600 1G EFI System
/dev/sda2 2203648 6397951 4194304 2G Linux filesystem
/dev/sda3 6397952 83884031 77486080 36.9G Linux filesystem
/dev/sda4 83884032 2097151966 2013267935 960G Linux filesystem
Command (m for help): t
Partition number (1-4, default 4): 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: 1000 GiB, 1073741824000 bytes, 2097152000 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: 469C7B79-92EE-49A6-B150-E85497A85FF9
Device Start End Sectors Size Type
/dev/sda1 2048 2203647 2201600 1G EFI System
/dev/sda2 2203648 6397951 4194304 2G Linux filesystem
/dev/sda3 6397952 83884031 77486080 36.9G Linux filesystem
/dev/sda4 83884032 2097151966 2013267935 960G Linux LVM
Command (m for help): w
The partition table has been altered.
Syncing disks.
- 查看可用空间
sudo lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 45M 1 loop /snap/certbot/3566
loop1 7:1 0 63.9M 1 loop /snap/core20/2105
loop2 7:2 0 40.9M 1 loop /snap/snapd/20290
sda 8:0 0 1000G 0 disk
├─sda1 8:1 0 1G 0 part /boot/efi
├─sda2 8:2 0 2G 0 part /boot
├─sda3 8:3 0 36.9G 0 part
│ └─ubuntu--vg-ubuntu--lv 253:0 0 36.9G 0 lvm /
└─sda4 8:4 0 960G 0 part
sdb 8:16 0 1000G 0 disk
├─sdb1 8:17 0 40G 0 part
└─sdb9 8:25 0 8M 0 part
- 将sda4创建成 PV
sudo pvcreate /dev/sda4
Physical volume "/dev/sda4" successfully created.
sudo pvdisplay
--- Physical volume ---
PV Name /dev/sda3
VG Name ubuntu-vg
PV Size <36.95 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 9458
Free PE 0
Allocated PE 9458
PV UUID TnNwXh-24oI-B948-5AOY-Lv1O-gg8P-0Obn9i
"/dev/sda4" is a new physical volume of "960.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sda4
VG Name
PV Size 960.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 8SzDuW-QOPe-mS7m-DFgn-HrRX-RK4E-u9ICJZ
- 查看当前 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 <36.95 GiB
PE Size 4.00 MiB
Total PE 9458
Alloc PE / Size 9458 / <36.95 GiB
Free PE / Size 0 / 0
VG UUID cvhIN3-FiJn-5R27-fId2-q3aF-QSeI-GC2v7e
- 将 PV 添加到 VG
sudo vgextend ubuntu-vg /dev/sda4
Volume group "ubuntu-vg" successfully extended
sudo vgdisplay
lume 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 996.94 GiB
PE Size 4.00 MiB
Total PE 255217
Alloc PE / Size 9458 / <36.95 GiB
Free PE / Size 245759 / <960.00 GiB
VG UUID cvhIN3-FiJn-5R27-fId2-q3aF-QSeI-GC2v7e
- 调整 LV 占用 VG 的全部空间
sudo lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
Size of logical volume ubuntu-vg/ubuntu-lv changed from <36.95 GiB (9458 extents) to 996.94 GiB (255217 extents).
Logical volume ubuntu-vg/ubuntu-lv successfully resized.
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 = 5, new_desc_blocks = 125
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 261342208 (4k) blocks long.