运维笔记--KVM环境下-虚拟机磁盘LVM扩容

环境描述:

  宿主机操作系统:ubuntu20.04

  虚拟机操作系统:ubuntu20.04 分区格式用的默认LVM

  目标:对虚拟机内部的“/”分区进行LVM扩容(原50G基础上+30G)。

结果演示:

  扩容前:

  扩容后:

操作步骤:【分宿主机上操作+虚拟机内部操作】

宿主机"hm-ops"上操作:

----################# 宿主机上--操作步骤 ####################################
---查看虚拟机状态
hellojesson@hm-ops:~$ virsh list
 Id   Name             State
--------------------------------
 4    hm-ops-master1   running
 5    hm-ops-node01    running
 6    hm-ops-node02    running
 7    hm-ops-habor     running

---目标虚拟机
hm-ops-habor 
---我们要扩容hm-ops-habor虚拟机内部的“/”分区,在原来50G基础上增加30G,到80G

---关闭、启动虚拟机
hellojesson@hm-ops:/var/lib/libvirt/images$ virsh shutdown hm-ops-habor
Domain hm-ops-habor is being shutdown

---virsh domblklist:获取指定虚拟机的磁盘信息
hellojesson@hm-ops:~$ virsh domblklist hm-ops-habor
 Target   Source
-----------------------------------------------------------
 vda      /var/lib/libvirt/images/hm-ops-01-clone-1.qcow2
 sda      -
 
---查看hm-ops-habor虚拟机,所挂载的虚拟盘*.qcow2信息
hellojesson@hm-ops:/var/lib/libvirt/images$ sudo qemu-img info /var/lib/libvirt/images/hm-ops-01-clone-1.qcow2
image: /var/lib/libvirt/images/hm-ops-01-clone-1.qcow2
file format: qcow2
virtual size: 50 GiB (53687091200 bytes)
disk size: 10.4 GiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true
    refcount bits: 16
    corrupt: false

---扩容:例如 原先50g 增加 30G
sudo qemu-img resize /var/lib/libvirt/images/hm-ops-01-clone-1.qcow2 +30G

---使设置生效 --测试发现不执行也会生效,因为执行前后hm-ops-habor.xml文件没有变化
sudo virsh define /etc/libvirt/qemu/hm-ops-habor.xml
hellojesson@hm-ops:/etc/libvirt/qemu$ sudo virsh define /etc/libvirt/qemu/hm-ops-habor.xml
Domain hm-ops-habor defined from /etc/libvirt/qemu/hm-ops-habor.xml

---查看大小变化
hellojesson@hm-ops:/var/lib/libvirt/images$ sudo qemu-img info /var/lib/libvirt/images/hm-ops-01-clone-1.qcow2
image: /var/lib/libvirt/images/hm-ops-01-clone-1.qcow2
file format: qcow2
virtual size: 80 GiB (85899345920 bytes)
disk size: 10.4 GiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true
    refcount bits: 16
    corrupt: false

---启动虚拟机
hellojesson@hm-ops:~$ virsh start hm-ops-habor                                          
Domain hm-ops-habor started

目标虚拟机“hmops-harbor”--里边操作步骤

----################# 目标虚拟机内--文件系统LVM扩容--操作步骤 ####################################
---整体操作步骤:
---1. 查看磁盘空间和磁盘分区情况
  sudo df -h
  sudo lsblk
  sudo fdisk -l
---2.fdisk /dev/vda创建新分区 
---3. 查看当前LVM逻辑卷情况
---4. 创建物理卷
---5. 使用新增物理卷扩展卷组:(vgdisplay 查看vg的名称和路径)
      sudo vgdisplay
      sudo vgextend vgubuntu /dev/vda3
---4. 扩展逻辑卷lv  (lvdisplay 查看lv 逻辑卷的路径和名称)
    sudo lvdisplay
    sudo lvextend -l +100%FREE /dev/vgubuntu/root
    或者:
    sudo lvextend -L +30G /dev/vgubuntu/root
---5.接着用 dh –f,发现实际容量并没有变化,因为系统还识别刚添加进磁盘的文件系统,所以还需对文件系统进行扩容。
    df -h
---6. 扩展相应分区,重新设置大:
  ---对于ext4分区
  sudo resize2fs /dev/vgubuntu/root
  ---对于xfs分区
  sudo xfs_growfs /dev/vgubuntu/root
---8.再次df -h 查看扩容后磁盘空间

---开始操作
hellojesson@hmops-harbor:~$ df -h
Filesystem                 Size  Used Avail Use% Mounted on
udev                       3.9G     0  3.9G   0% /dev
tmpfs                      795M  1.5M  794M   1% /run
/dev/mapper/vgubuntu-root   48G  8.2G   37G  19% /

hellojesson@hmops-harbor:~$ lsblk
NAME                MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT 
vda                 252:0    0    80G  0 disk 
├─vda1              252:1    0   512M  0 part /boot/efi
├─vda2              252:2    0     1K  0 part 
└─vda5              252:5    0  49.5G  0 part 
  ├─vgubuntu-root   253:0    0  48.5G  0 lvm  /
  └─vgubuntu-swap_1 253:1    0   976M  0 lvm  [SWAP]
hellojesson@hmops-harbor:~$ sudo fdisk -l
Disk /dev/vda: 80 GiB, 85899345920 bytes, 167772160 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
Disklabel type: dos
Disk identifier: 0x0c2cdc77

Device     Boot   Start       End   Sectors  Size Id Type
/dev/vda1  *       2048   1050623   1048576  512M  b W95 FAT32
/dev/vda2       1052670 104855551 103802882 49.5G  5 Extended
/dev/vda5       1052672 104855551 103802880 49.5G 8e Linux LVM

Disk /dev/mapper/vgubuntu-root: 48.51 GiB, 52072284160 bytes, 101703680 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

Disk /dev/mapper/vgubuntu-swap_1: 976 MiB, 1023410176 bytes, 1998848 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

---注意:上边这行记录信息 /dev/vda5       1052672 104855551 103802880 49.5G 8e Linux LVM
---确认当前/dev/vda分区表类型及信息
hellojesson@hmops-harbor:~$ sudo parted /dev/vda print
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 85.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  538MB   537MB   primary   fat32        boot
 2      539MB   53.7GB  53.1GB  extended
 5      539MB   53.7GB  53.1GB  logical                lvm

---为什么要确认分区类型,是主分区还是逻辑分区
--因为我们要在/dev/vda基础上库容,增加类似/dev/vda5一样的LVM分区,因此新增的分区类型尽量跟/dev/vda5保持一致
---但是发现添加逻辑分区会报错:
--Select (default p): l
Adding logical partition 6
No free sectors available.
---最终创建分区的时候还是选择的主分区

---fdisk /dev/vda开始进行分区--创建新分区 
hellojesson@hmops-harbor:~$ sudo fdisk /dev/vda
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): p  
Disk /dev/vda: 80 GiB, 85899345920 bytes, 167772160 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
Disklabel type: dos
Disk identifier: 0x0c2cdc77

Device     Boot   Start       End   Sectors  Size Id Type
/dev/vda1  *       2048   1050623   1048576  512M  b W95 FAT32
/dev/vda2       1052670 104855551 103802882 49.5G  5 Extended
/dev/vda5       1052672 104855551 103802880 49.5G 8e Linux LVM

Command (m for help): n
Partition type
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): p
Partition number (3,4, default 3): 
First sector (104855552-167772159, default 104855552): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (104855552-167772159, default 167772159): 

Created a new partition 3 of type 'Linux' and of size 30 GiB.
Command (m for help): t
Partition number (1-3,5, default 5): 3
Hex code (type L to list all codes): 8e

Changed type of partition 'Linux' to 'Linux LVM'.
Command (m for help): w
The partition table has been altered.
Syncing disks.

---查看变化
---可以看到新增了:/dev/vda3  104855552 167772159  62916608   30G 8e Linux LVM
hellojesson@hmops-harbor:~$ sudo fdisk -l
Disk /dev/vda: 80 GiB, 85899345920 bytes, 167772160 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
Disklabel type: dos
Disk identifier: 0x0c2cdc77

Device     Boot     Start       End   Sectors  Size Id Type
/dev/vda1  *         2048   1050623   1048576  512M  b W95 FAT32
/dev/vda2         1052670 104855551 103802882 49.5G  5 Extended
/dev/vda3       104855552 167772159  62916608   30G 8e Linux LVM
/dev/vda5         1052672 104855551 103802880 49.5G 8e Linux LVM

Partition table entries are not in disk order.

Disk /dev/mapper/vgubuntu-root: 48.51 GiB, 52072284160 bytes, 101703680 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

Disk /dev/mapper/vgubuntu-swap_1: 976 MiB, 1023410176 bytes, 1998848 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

---查看/dev/vda最新分区情况 和 磁盘使用情况
hellojesson@hmops-harbor:~$ sudo parted /dev/vda print
[sudo] password for hellojesson: 
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 85.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  538MB   537MB   primary   fat32        boot
 2      539MB   53.7GB  53.1GB  extended
 5      539MB   53.7GB  53.1GB  logical                lvm
 3      53.7GB  85.9GB  32.2GB  primary                lvm

hellojesson@hmops-harbor:~$ df -h
Filesystem                 Size  Used Avail Use% Mounted on
udev                       3.9G     0  3.9G   0% /dev
tmpfs                      795M  1.5M  794M   1% /run
/dev/mapper/vgubuntu-root   48G  8.2G   37G  19% /

---查看当前系统LVM信息:
---查看当前物理卷
hellojesson@hmops-harbor:~$ sudo pvs
  PV         VG       Fmt  Attr PSize   PFree 
  /dev/vda5  vgubuntu lvm2 a--  <49.50g 48.00m

hellojesson@hmops-harbor:~$ sudo pvdisplay 
  --- Physical volume ---
  PV Name               /dev/vda5
  VG Name               vgubuntu
  PV Size               <49.50 GiB / not usable 1.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              12671
  Free PE               12
  Allocated PE          12659
  PV UUID               lllydW-SJew-09DA-DjK5-Kd6W-muil-Mp3par

---查看当前卷组
hellojesson@hmops-harbor:~$ sudo vgscan
  Found volume group "vgubuntu" using metadata type lvm2

hellojesson@hmops-harbor:~$ sudo vgdisplay 
  --- Volume group ---
  VG Name               vgubuntu
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <49.50 GiB
  PE Size               4.00 MiB
  Total PE              12671
  Alloc PE / Size       12659 / <49.45 GiB
  Free  PE / Size       12 / 48.00 MiB
  VG UUID               mfjnJC-niXC-sPqN-KLJ9-1enQ-7NeV-SdBfjv

---查看当前逻辑卷
hellojesson@hmops-harbor:~$ sudo lvdisplay 
  --- Logical volume ---
  LV Path                /dev/vgubuntu/root
  LV Name                root
  VG Name                vgubuntu
  LV UUID                0EbIm5-0OEe-27pP-l2UQ-1Hdq-vrB6-jWTCxj
  LV Write Access        read/write
  LV Creation host, time ubuntu, 2023-03-06 17:46:14 +0800
  LV Status              available
  # open                 1
  LV Size                <48.50 GiB
  Current LE             12415
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

---开始LVM扩容:创建物理卷、加入卷组、扩展逻辑卷
---创建物理卷/dev/vda3
sudo pvcreate /dev/vda3
hellojesson@hmops-harbor:~$ sudo pvcreate /dev/vda3
  Physical volume "/dev/vda3" successfully created.

---查看
sudo pvs
sudo pvdisplay
hellojesson@hmops-harbor:~$ sudo pvs
  PV         VG       Fmt  Attr PSize   PFree 
  /dev/vda3           lvm2 ---   30.00g 30.00g
  /dev/vda5  vgubuntu lvm2 a--  <49.50g 48.00m
hellojesson@hmops-harbor:~$ sudo pvdisplay         
  --- Physical volume ---
  PV Name               /dev/vda5
  VG Name               vgubuntu
  PV Size               <49.50 GiB / not usable 1.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              12671
  Free PE               12
  Allocated PE          12659
  PV UUID               lllydW-SJew-09DA-DjK5-Kd6W-muil-Mp3par
   
  "/dev/vda3" is a new physical volume of "30.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/vda3
  VG Name               
  PV Size               30.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               6f6TLj-oL0U-zPKt-0Nev-U63P-Gk7E-fWccBn

---vgextend扩展当前卷组,把物理卷/dev/vda3加入进去
hellojesson@hmops-harbor:~$ sudo vgextend vgubuntu /dev/vda3
  Volume group "vgubuntu" successfully extended

--vgdisplay查看扩展后的vgdisplay 
---注意扩展前后VG Size变化: <49.50 GiB 变成了 <79.50 GiB
---扩展前
hellojesson@hmops-harbor:~$ sudo vgdisplay
  --- Volume group ---
  VG Name               vgubuntu
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <49.50 GiB
  PE Size               4.00 MiB
  Total PE              12671
  Alloc PE / Size       12659 / <49.45 GiB
  Free  PE / Size       12 / 48.00 MiB
  VG UUID               mfjnJC-niXC-sPqN-KLJ9-1enQ-7NeV-SdBfjv

---扩展后
hellojesson@hmops-harbor:~$ sudo vgdisplay
  --- Volume group ---
  VG Name               vgubuntu
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               <79.50 GiB
  PE Size               4.00 MiB
  Total PE              20351
  Alloc PE / Size       12659 / <49.45 GiB
  Free  PE / Size       7692 / <30.05 GiB
  VG UUID               mfjnJC-niXC-sPqN-KLJ9-1enQ-7NeV-SdBfjv

---lvextend扩展逻辑卷:sudo lvextend -l +100%FREE /dev/vgubuntu/root
hellojesson@hmops-harbor:~$ sudo lvextend -l +100%FREE /dev/vgubuntu/root
  Size of logical volume vgubuntu/root changed from <48.50 GiB (12415 extents) to 78.54 GiB (20107 extents).
  Logical volume vgubuntu/root successfully resized.

---lvdisplay查看逻辑卷 ---已经变成了78.54 GiB
hellojesson@hmops-harbor:~$ sudo lvdisplay
  --- Logical volume ---
  LV Path                /dev/vgubuntu/root
  LV Name                root
  VG Name                vgubuntu
  LV UUID                0EbIm5-0OEe-27pP-l2UQ-1Hdq-vrB6-jWTCxj
  LV Write Access        read/write
  LV Creation host, time ubuntu, 2023-03-06 17:46:14 +0800
  LV Status              available
  # open                 1
  LV Size                78.54 GiB
  Current LE             20107
  Segments               3
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

---扩展相应分区,重新设置大:
hellojesson@hmops-harbor:~$ df -lhT
Filesystem                Type      Size  Used Avail Use% Mounted on
udev                      devtmpfs  3.9G     0  3.9G   0% /dev
tmpfs                     tmpfs     795M  1.5M  794M   1% /run
/dev/mapper/vgubuntu-root ext4       48G  8.2G   37G  19% /

---这里因为我们/dev/mapper/vgubuntu-root文件系统是ext4类型,我们用resize2fs命令来扩大文件系统。
---对于ext4类型的分区 
resize2fs /dev/vgubuntu/root
---对于xfs类型的分区
xfs_growfs /dev/vgubuntu/root

hellojesson@hmops-harbor:~$ sudo resize2fs /dev/mapper/vgubuntu-root
resize2fs 1.45.5 (07-Jan-2020)
Filesystem at /dev/mapper/vgubuntu-root is mounted on /; on-line resizing required
old_desc_blocks = 7, new_desc_blocks = 10
The filesystem on /dev/mapper/vgubuntu-root is now 20589568 (4k) blocks long.

---补充:
---可能小伙伴们有疑问:df -h挂载的文件系统是/dev/mapper/vgubuntu-root,那为什么扩展的是/dev/vgubuntu/root,
---这是因为,我们扩展的是逻辑卷,这里逻辑卷对应的名称是/dev/vgubuntu/root,所以需要扩展/dev/vgubuntu/root,至于挂载的文件系统“/dev/mapper/vgubuntu-root”起的这个名字,
---他们是映射关联关系,对应的是同一个分区,因此用下边任一个命令都是可以的:
sudo resize2fs /dev/mapper/vgubuntu-root
或者:
sudo resize2fs /dev/vgubuntu/root

---OK,我们看下系统当前的磁盘空间 Size 78G--扩容成功
hellojesson@hmops-harbor:~$ df -h
Filesystem                 Size  Used Avail Use% Mounted on
udev                       3.9G     0  3.9G   0% /dev
tmpfs                      795M  1.5M  794M   1% /run
/dev/mapper/vgubuntu-root   78G  8.2G   66G  12% /

---其他命令
hellojesson@hmops-harbor:~$ more /etc/fstab 
# /etc/fstab: static file system information.
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).

# <file system> <mount point>   <type>  <options>       <dump>  <pass>
/dev/mapper/vgubuntu-root /               ext4    errors=remount-ro 0       1

hellojesson@hmops-harbor:~$ blkid
/dev/mapper/vgubuntu-swap_1: UUID="c4abaa8a-2cee-4873-be2b-fd21eedae086" TYPE="swap"
/dev/mapper/vgubuntu-root: UUID="eea2f6da-237e-49b8-9fcc-a24c82529218" TYPE="ext4"

---参考:

virsh常用命令:https://www.cnblogs.com/Trezeguet/p/16771718.html
https://blog.csdn.net/Thewei666/article/details/129034951
https://blog.csdn.net/weixin_65690979/article/details/126135876
https://developer.aliyun.com/article/943716
https://blog.csdn.net/hjxloveqsx/article/details/120361799

 

posted @ 2023-03-10 11:00  hello-Jesson  阅读(672)  评论(0编辑  收藏  举报