lvm新增磁盘扩容lv逻辑卷

系统磁盘不够用,通过新增磁盘形式扩容空间。

env

  • ubuntu22.04.5 lvm管理磁盘
  • lvm ext4文件系统

LVM 的一些基本概念:

  • 物理卷 (PV): 物理卷是 LVM 的基本构建块,通常是硬盘分区或整个硬盘。
  • 卷组 (VG): 卷组将多个物理卷组合成一个存储池。
  • 逻辑卷 (LV): 逻辑卷是从卷组中分配的虚拟分区,可以像普通分区一样格式化和挂载。
  • 物理扩展 (PE): 物理扩展是物理卷中最小的可分配存储单元。卷组中的所有物理卷的 PE 大小必须相同,且在创建卷组时确定,之后不能更改(除非重建卷组)

1.新增磁盘及格式化

查看新增磁盘:
fdisk -l

如果磁盘扩容形式(/dev/sda)

fdisk /dev/sda  
n  //新增磁盘分区
...
p
w

格式化新增磁盘
mkfs.ext4 /dev/sda4

2.添加pv及扩展 vg

2.1格式化磁盘为pv

pvcreate /dev/sda4
pvs

2.2vg新增pv磁盘

vgs
vgextend ubuntu-vg  /dev/sda4

过程

root@gaga:~# pvcreate /dev/sda4
  Physical volume "/dev/sda4" successfully created.
root@gaga:~# pvscan
  PV /dev/sda3   VG ubuntu-vg       lvm2 [<38.00 GiB / 19.00 GiB free]
  PV /dev/sda4                      lvm2 [40.00 GiB]
  Total: 2 [<78.00 GiB] / in use: 1 [<38.00 GiB] / in no VG: 1 [40.00 GiB]
root@gaga:~#

root@gaga:~# vgscan
  Found volume group "ubuntu-vg" using metadata type lvm2

root@gaga:~# vgextend ubuntu-vg  /dev/sda4
  Volume group "ubuntu-vg" successfully extended

3.扩容lv

3.1查看逻辑卷名称

lvs   
/dev/ubuntu-vg/ubuntu-lv

3.2扩容逻辑卷剩余100%空间

扩容剩余空间100%
lvextend -l 100%FREE  /dev/ubuntu-vg/ubuntu-lv 

扩容特定大小空间50G
vgs
lvextend -L +50G /dev/myvg/mylv

扩容特定大小pe  +100pe
lvextend -l +100 /dev/卷组名/逻辑卷名
lvextend -l +50%FREE /dev/卷组名/逻辑卷名    百分比pe
lvextend -l 100%FREE /dev/卷组名/逻辑卷名

3.3校准文件大小

resize2fs  /dev/ubuntu-vg/ubuntu-lv 

3.4检查生效情况

df -hT

过程

root@gaga:~# lvextend -l 100%FREE  /dev/ubuntu-vg/ubuntu-lv 
  Size of logical volume ubuntu-vg/ubuntu-lv changed from <19.00 GiB (4863 extents) to <59.00 GiB (15103 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.

root@gaga:~# 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 = 8
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 15465472 (4k) blocks long.

root@gaga:~# df -hT
Filesystem                        Type   Size  Used Avail Use% Mounted on
tmpfs                             tmpfs  781M  9.6M  772M   2% /run
/dev/mapper/ubuntu--vg-ubuntu--lv ext4    58G   19G   37G  34% /
tmpfs                             tmpfs  3.9G     0  3.9G   0% /dev/shm
tmpfs                             tmpfs  5.0M     0  5.0M   0% /run/lock
tmpfs                             tmpfs  3.9G     0  3.9G   0% /run/qemu
/dev/sda2                         ext4   2.0G  125M  1.7G   7% /boot
tmpfs                             tmpfs  781M  4.0K  781M   1% /run/user/1000

root@gaga:~# lvextend -l 100%FREE /dev//ubuntu-vg/ubuntu-lv
  Size of logical volume ubuntu-vg/ubuntu-lv changed from <19.00 GiB (4863 extents) to <59.00 GiB (15103 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.
root@gaga:~# 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               77.99 GiB
  PE Size               4.00 MiB
  Total PE              19966
  Alloc PE / Size       15103 / <59.00 GiB
  Free  PE / Size       4863 / <19.00 GiB      //存在剩余pe
  VG UUID               Kd8ov3-5tlM-eNzm-deAh-Oof8-dt4v-IWYrO3

分析原因:大概率是vmdk动态磁盘及磁盘扩容多次。最初20g -> 40g -> 80g
20g
+20g 未分配
+40g 分配

root@gaga:~# lvm  lvmdiskscan|grep LVM
  /dev/sda3  [     <38.00 GiB] LVM physical volume
  /dev/sda4  [      40.00 GiB] LVM physical volume
  0 LVM physical volume whole disks
  2 LVM physical volumes
root@gaga:~#

root@gaga:~# lvextend -l +4863 /dev//ubuntu-vg/ubuntu-lv    //基于pe分配磁盘
  Size of logical volume ubuntu-vg/ubuntu-lv changed from <59.00 GiB (15103 extents) to 77.99 GiB (19966 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.
root@gaga:~# vgdisplay
  --- Volume group ---
  VG Name               ubuntu-vg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  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               77.99 GiB
  PE Size               4.00 MiB
  Total PE              19966
  Alloc PE / Size       19966 / 77.99 GiB
  Free  PE / Size       0 / 0   
  VG UUID               Kd8ov3-5tlM-eNzm-deAh-Oof8-dt4v-IWYrO3

root@gaga:~# 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 = 10
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 20445184 (4k) blocks long.

root@gaga:~# df -hT
Filesystem                        Type   Size  Used Avail Use% Mounted on
tmpfs                             tmpfs  781M  1.6M  779M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv ext4    77G  7.5G   66G  11% /

xfs_growfs /挂载点

lvm

root@gaga:/opt/lede# lvm --help
  Available lvm commands:
  Use 'lvm help <command>' for more information
   
  config          Display and manipulate configuration information
  devtypes        Display recognised built-in block device types
  dumpconfig      Display and manipulate configuration information
  formats         List available metadata formats
  help            Display help for commands
  fullreport      Display full report
  lastlog         Display last command's log report
  lvchange        Change the attributes of logical volume(s)
  lvconvert       Change logical volume layout
  lvcreate        Create a logical volume
  lvdisplay       Display information about a logical volume
  lvextend        Add space to a logical volume
  lvmchange       With the device mapper, this is obsolete and does nothing.
  lvmconfig       Display and manipulate configuration information
  lvmdiskscan     List devices that may be used as physical volumes
  lvmsadc         Collect activity data
  lvmsar          Create activity report
  lvreduce        Reduce the size of a logical volume
  lvremove        Remove logical volume(s) from the system
  lvrename        Rename a logical volume
  lvresize        Resize a logical volume
  lvs             Display information about logical volumes
  lvscan          List all logical volumes in all volume groups
  pvchange        Change attributes of physical volume(s)
  pvresize        Resize physical volume(s)
  pvck            Check metadata on physical volumes
  pvcreate        Initialize physical volume(s) for use by LVM
  pvdata          Display the on-disk metadata for physical volume(s)
  pvdisplay       Display various attributes of physical volume(s)
  pvmove          Move extents from one physical volume to another
  lvpoll          Continue already initiated poll operation on a logical volume
  pvremove        Remove LVM label(s) from physical volume(s)
  pvs             Display information about physical volumes
  pvscan          List all physical volumes
  segtypes        List available segment types
  systemid        Display the system ID, if any, currently set on this host
  tags            List tags defined on this host
  vgcfgbackup     Backup volume group configuration(s)
  vgcfgrestore    Restore volume group configuration
  vgchange        Change volume group attributes
  vgck            Check the consistency of volume group(s)
  vgconvert       Change volume group metadata format
  vgcreate        Create a volume group
  vgdisplay       Display volume group information
  vgexport        Unregister volume group(s) from the system
  vgextend        Add physical volumes to a volume group
  vgimport        Register exported volume group with system
  vgimportclone   Import a VG from cloned PVs
  vgmerge         Merge volume groups
  vgmknodes       Create the special files for volume group devices in /dev
  vgreduce        Remove physical volume(s) from a volume group
  vgremove        Remove volume group(s)
  vgrename        Rename a volume group
  vgs             Display information about volume groups
  vgscan          Search for all volume groups
  vgsplit         Move physical volumes into a new or existing volume group
  version         Display software and driver version information