手动给KVM虚拟机添加虚拟磁盘并扩展LVM文件系统容量

819 阅读5分钟
  • 查看虚拟机
root@vHead-172.16.131.125 ~# virsh list
 Id    Name                           State
----------------------------------------------------
 7     k8s_node1                      running

root@vHead-172.16.131.125 ~#
  • 查看虚拟机已有磁盘
root@vHead-172.16.131.125 ~# virsh domblklist 7
Target     Source
------------------------------------------------
vda        /image/image.qcow2
hdc        -

root@vHead-172.16.131.125 ~#
  • 创建虚拟磁盘文件(qcow2)

创建一个80G大小的虚拟磁盘为例

qemu-img create -f qcow2 node1_data.qcow2 80G
  • 查看创建的虚拟磁盘文件
root@vHead-172.16.131.125 /image# qemu-img info node1_data.qcow2
image: node1_data.qcow2
file format: qcow2
virtual size: 80G (85899345920 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
root@vHead-172.16.131.125 /image#
  • 将虚拟磁盘添加到虚拟机当中(attach)
virsh attach-disk k8s_node1 node1_data.qcow2 vdb --cache none --subdriver qcow2 --persistent
  • 再次查看虚拟机磁盘信息
root@vHead-172.16.131.125 /image# virsh domblklist 7
Target     Source
------------------------------------------------
vda        /image/image.qcow2
vdb        /image/node1_data.qcow2
hdc        -

root@vHead-172.16.131.125 /image#
  • 进入到虚拟机当中查看磁盘信息
[root@k8s-node1 ~]# lsblk
NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0            11:0    1 1024M  0 rom
vda           252:0    0   50G  0 disk
├─vda1        252:1    0    1G  0 part /boot
└─vda2        252:2    0   49G  0 part
  ├─rhel-root 253:0    0   47G  0 lvm  /
  └─rhel-swap 253:1    0    2G  0 lvm
vdb           252:16   0   80G  0 disk
[root@k8s-node1 ~]#

发现虚拟机里面已经多了一块80G大小的磁盘(vdb)

  • 查看虚拟机当中当前文件系统容量
[root@k8s-node1 ~]# df -Th /
Filesystem            Type  Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root xfs    47G   31G   17G  66% /
[root@k8s-node1 ~]#
  • 查看当前LV大小 (小于47G)
[root@k8s-node1 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/rhel/swap
  LV Name                swap
  VG Name                rhel
  LV UUID                YX63U1-53aJ-kMpC-f43q-ghND-MlwG-cyDQ7H
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2018-06-13 11:42:54 +0800
  LV Status              available
  # open                 0
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

  --- Logical volume ---
  LV Path                /dev/rhel/root
  LV Name                root
  VG Name                rhel
  LV UUID                lbHA32-rdhb-KJUG-tyzO-4COC-9n6r-4h4Slz
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2018-06-13 11:42:54 +0800
  LV Status              available
  # open                 1
  LV Size                <47.00 GiB
  Current LE             12031
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

[root@k8s-node1 ~]#
  • 查看当前VG大小 (小于49G)
[root@k8s-node1 ~]# vgdisplay
  --- Volume group ---
  VG Name               rhel
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <49.00 GiB
  PE Size               4.00 MiB
  Total PE              12543
  Alloc PE / Size       12543 / <49.00 GiB
  Free  PE / Size       0 / 0
  VG UUID               cW9c2V-vr4E-TyTF-WOhy-oCJ7-xXlc-rjLRxT

[root@k8s-node1 ~]#
  • 通过新添加的磁盘创建PV
[root@k8s-node1 ~]# lsblk
NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0            11:0    1 1024M  0 rom
vda           252:0    0   50G  0 disk
├─vda1        252:1    0    1G  0 part /boot
└─vda2        252:2    0   49G  0 part
  ├─rhel-root 253:0    0   47G  0 lvm  /
  └─rhel-swap 253:1    0    2G  0 lvm
vdb           252:16   0   80G  0 disk
[root@k8s-node1 ~]# pvcreate /dev/vdb
  Physical volume "/dev/vdb" successfully created.
[root@k8s-node1 ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/vda2
  VG Name               rhel
  PV Size               <49.00 GiB / not usable 192.00 KiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              12543
  Free PE               0
  Allocated PE          12543
  PV UUID               hY6OKe-Of3E-GPNj-UhzE-VKHw-XquG-s3Dl4o

  "/dev/vdb" is a new physical volume of "80.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/vdb
  VG Name
  PV Size               80.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               mQW0ih-HGvO-dsiu-Gjd3-isfW-4rYc-plKJpC

[root@k8s-node1 ~]#
  • 将新创建的PV /dev/vdb添加到VG rhel当中
[root@k8s-node1 ~]# vgextend rhel /dev/vdb
  Volume group "rhel" successfully extended
[root@k8s-node1 ~]#
  • 添加PV后再次查看VG rhel的大小 (128.99G, PV添加成功)
[root@k8s-node1 ~]# vgdisplay
  --- Volume group ---
  VG Name               rhel
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  6
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               128.99 GiB
  PE Size               4.00 MiB
  Total PE              33022
  Alloc PE / Size       12543 / <49.00 GiB
  Free  PE / Size       20479 / <80.00 GiB
  VG UUID               cW9c2V-vr4E-TyTF-WOhy-oCJ7-xXlc-rjLRxT

[root@k8s-node1 ~]#
  • 扩容LV /dev/rhel/root
lvextend -L +80G /dev/rhel/root
  • 查看扩容后的LV /dev/rhel/root (120G, 扩容成功)
[root@k8s-node1 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/rhel/swap
  LV Name                swap
  VG Name                rhel
  LV UUID                YX63U1-53aJ-kMpC-f43q-ghND-MlwG-cyDQ7H
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2018-06-13 11:42:54 +0800
  LV Status              available
  # open                 0
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

  --- Logical volume ---
  LV Path                /dev/rhel/root
  LV Name                root
  VG Name                rhel
  LV UUID                lbHA32-rdhb-KJUG-tyzO-4COC-9n6r-4h4Slz
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2018-06-13 11:42:54 +0800
  LV Status              available
  # open                 1
  LV Size                120.00 GiB
  Current LE             30720
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

[root@k8s-node1 ~]#
  • 文件系统扩展 (ZFS文件系统)
[root@k8s-node1 ~]# xfs_growfs /dev/rhel/root
meta-data=/dev/mapper/rhel-root  isize=512    agcount=19, agsize=1113856 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=20971520, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 12319744 to 31457280
[root@k8s-node1 ~]#
  • 再次查看虚拟机文件系统容量
[root@k8s-node1 ~]# df -Th /
Filesystem            Type  Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root xfs   120G   33G   88G  27% /
[root@k8s-node1 ~]#