Linux LVM扩容()

Ubuntu默认安装自带了LVM的结构,可以很方便扩容

root@node3:~# lsblk 
NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
loop0                       7:0    0 109.6M  1 loop /snap/lxd/24326
loop1                       7:1    0  43.2M  1 loop /snap/snapd/18363
loop2                       7:2    0  59.1M  1 loop /snap/core20/1826
loop3                       7:3    0  59.1M  1 loop /snap/core20/1832
sr0                        11:0    1  1024M  0 rom  
nvme0n1                   259:0    0   100G  0 disk 
├─nvme0n1p1               259:1    0   953M  0 part /boot/efi
├─nvme0n1p2               259:2    0   1.8G  0 part /boot
└─nvme0n1p3               259:3    0  17.3G  0 part 
  └─ubuntu--vg-ubuntu--lv 253:0    0  17.3G  0 lvm  /

需求是在VM中上调大磁盘,在虚拟机中扩大容量

  • 先用gdisk创建新磁盘
 root@node3:~# gdisk
GPT fdisk (gdisk) version 1.0.8

Type device filename, or press <Enter> to exit: /dev/nvme0n1
Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): p

#打印现有分区

Disk /dev/nvme0n1: 209715200 sectors, 100.0 GiB
Model: VMware Virtual NVMe Disk
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 85321C7A-EE62-404B-8273-93113AB86745
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 209715166
Partitions will be aligned on 2048-sector boundaries
Total free space is 167776189 sectors (80.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         1953791   953.0 MiB   EF00  
   2         1953792         5623807   1.8 GiB     8300  
   3         5623808        41940991   17.3 GiB    8300 
Command (? for help): n
Partition number (4-128, default 4): 
First sector (34-209715166, default = 41940992) or {+-}size{KMGTP}: 
Last sector (41940992-209715166, default = 209715166) or {+-}size{KMGTP}: 
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'
# 全部默认即可

按w保存

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/nvme0n1.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.

重启

  • 扩大PV
root@node3:~# vgextend ubuntu-vg /dev/nvme0n1p4
Physical volume "/dev/nvme0n1p4" successfully created.
Volume group "ubuntu-vg" successfully extended
  • 扩大LV
root@node3:~# lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
Size of logical volume ubuntu-vg/ubuntu-lv changed from <17.32 GiB (4433 extents) to 97.31 GiB (24912 extents).
Logical volume ubuntu-vg/ubuntu-lv successfully resized.
  • 扩大文件系统(ext4)
root@node3:~# resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/mapper/ubuntu--vg-ubuntu--lv is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 13
The filesystem on /dev/mapper/ubuntu--vg-ubuntu--lv is now 25509888 (4k) blocks long.

综上三步

vgextend ubuntu-vg /dev/nvme0n1p4
lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv


root@node3:~# df -Th
Filesystem                        Type   Size  Used Avail Use% Mounted on
...
/dev/mapper/ubuntu--vg-ubuntu--lv ext4    96G   11G   81G  12% /
...

可以看到已经成功扩容

————————

Ubuntu默认安装自带了LVM的结构,可以很方便扩容

root@node3:~# lsblk 
NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
loop0                       7:0    0 109.6M  1 loop /snap/lxd/24326
loop1                       7:1    0  43.2M  1 loop /snap/snapd/18363
loop2                       7:2    0  59.1M  1 loop /snap/core20/1826
loop3                       7:3    0  59.1M  1 loop /snap/core20/1832
sr0                        11:0    1  1024M  0 rom  
nvme0n1                   259:0    0   100G  0 disk 
├─nvme0n1p1               259:1    0   953M  0 part /boot/efi
├─nvme0n1p2               259:2    0   1.8G  0 part /boot
└─nvme0n1p3               259:3    0  17.3G  0 part 
  └─ubuntu--vg-ubuntu--lv 253:0    0  17.3G  0 lvm  /

需求是在VM中上调大磁盘,在虚拟机中扩大容量

  • 先用gdisk创建新磁盘
 root@node3:~# gdisk
GPT fdisk (gdisk) version 1.0.8

Type device filename, or press <Enter> to exit: /dev/nvme0n1
Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): p

#打印现有分区

Disk /dev/nvme0n1: 209715200 sectors, 100.0 GiB
Model: VMware Virtual NVMe Disk
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 85321C7A-EE62-404B-8273-93113AB86745
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 209715166
Partitions will be aligned on 2048-sector boundaries
Total free space is 167776189 sectors (80.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         1953791   953.0 MiB   EF00  
   2         1953792         5623807   1.8 GiB     8300  
   3         5623808        41940991   17.3 GiB    8300 
Command (? for help): n
Partition number (4-128, default 4): 
First sector (34-209715166, default = 41940992) or {+-}size{KMGTP}: 
Last sector (41940992-209715166, default = 209715166) or {+-}size{KMGTP}: 
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'
# 全部默认即可

按w保存

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/nvme0n1.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.

重启

  • 扩大PV
root@node3:~# vgextend ubuntu-vg /dev/nvme0n1p4
Physical volume "/dev/nvme0n1p4" successfully created.
Volume group "ubuntu-vg" successfully extended
  • 扩大LV
root@node3:~# lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
Size of logical volume ubuntu-vg/ubuntu-lv changed from <17.32 GiB (4433 extents) to 97.31 GiB (24912 extents).
Logical volume ubuntu-vg/ubuntu-lv successfully resized.
  • 扩大文件系统(ext4)
root@node3:~# resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/mapper/ubuntu--vg-ubuntu--lv is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 13
The filesystem on /dev/mapper/ubuntu--vg-ubuntu--lv is now 25509888 (4k) blocks long.

综上三步

vgextend ubuntu-vg /dev/nvme0n1p4
lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv


root@node3:~# df -Th
Filesystem                        Type   Size  Used Avail Use% Mounted on
...
/dev/mapper/ubuntu--vg-ubuntu--lv ext4    96G   11G   81G  12% /
...

可以看到已经成功扩容