KVM 虚拟机调整 qcow2 格式硬盘大小

需求

kvm114 虚拟机 根分区需要扩展 5G 大小

2种解决方案

1、通过 resize 命令扩展境像大小,然后进入虚拟机分区, 再创建物理卷,加入卷组,扩展逻辑卷

2、通过 create 命令 创建一块新的 qcow2 磁盘,并加入虚拟机配置里面,然后进入虚拟机分区, 再创建物理卷,加入卷组,扩展逻辑卷

第一种方案

1、查看 kvm114 虚拟机的大小是 10G

[root@vm71 vms]# qemu-img info /home/vms/kvm114.qcow2

# 显示
image: kvm114.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 1.3G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true

2、关闭虚拟机

[root@vm71 vms]# virsh shutdown kvm114

3、通过 resize 调整大小

[root@vm71 vms]# qemu-img resize kvm114.qcow2 +5G

# 显示
Image resized.

# 查看
qemu-img info kvm114.qcow2

# 结果已扩大至 15G
image: kvm114.qcow2
file format: qcow2
virtual size: 15G (16106127360 bytes)
disk size: 1.3G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true

4、启动虚拟机

[root@vm71 vms]# virsh start kvm114

5、查看虚拟机硬盘使用空间

# 发现增加的 5G 没有显示
[root@kvm114 ~]# df -h
Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/cl-root  8.0G  1.1G  7.0G  13% /
devtmpfs             486M     0  486M   0% /dev
tmpfs                497M     0  497M   0% /dev/shm
tmpfs                497M  6.5M  490M   2% /run
tmpfs                497M     0  497M   0% /sys/fs/cgroup
/dev/vda1           1014M  138M  877M  14% /boot
tmpfs                100M     0  100M   0% /run/user/0

6、查看分区

[root@kvm114 ~]# fdisk -l

# /dev/vda 硬盘已扩大了
Disk /dev/vda: 16.1 GB, 16106127360 bytes, 31457280 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 label type: dos
Disk identifier: 0x000b41fc

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200    20971519     9436160   8e  Linux LVM

Disk /dev/mapper/cl-root: 8585 MB, 8585740288 bytes, 16769024 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/cl-swap: 1073 MB, 1073741824 bytes, 2097152 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

7、对新加的空间分区

[root@kvm114 ~]# fdisk /dev/vda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


# 输入 p 查看分区
Command (m for help): p

Disk /dev/vda: 16.1 GB, 16106127360 bytes, 31457280 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 label type: dos
Disk identifier: 0x000b41fc

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200    20971519     9436160   8e  Linux LVM

# 输入 n 创建一个分区
Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended

# 输入 p 选择主分区
Select (default p): p

# 输入分区号,默认回车
Partition number (3,4, default 3): 3

# 输入 起始扇区,默认回车
First sector (20971520-31457279, default 20971520): 
Using default value 20971520

# 输入 结尾扇区,默认回车
Last sector, +sectors or +size{K,M,G} (20971520-31457279, default 31457279): 
Using default value 31457279
Partition 3 of type Linux and of size 5 GiB is set

# 调整分区格式
Command (m for help): t

# 选择哪个分区,默认回车
Partition number (1-3, default 3): 3

# 输入 8e,为lvm分区
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!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

8、查看分区

# 重载分区表
[root@kvm114 ~]# partprobe

# 查看分区
[root@kvm114 ~]# fdisk -l

# 新加的 vda3 分区已显示
Disk /dev/vda: 16.1 GB, 16106127360 bytes, 31457280 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 label type: dos
Disk identifier: 0x000b41fc

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200    20971519     9436160   8e  Linux LVM
/dev/vda3        20971520    31457279     5242880   8e  Linux LVM

Disk /dev/mapper/cl-root: 8585 MB, 8585740288 bytes, 16769024 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/cl-swap: 1073 MB, 1073741824 bytes, 2097152 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

9、创建物理卷,加入卷组,扩展逻辑卷

# 将物理分区初始化为物理卷,以便LVM使用
[root@kvm114 ~]# pvcreate /dev/vda3
  Physical volume "/dev/vda3" successfully created.

# 查看物理卷
[root@kvm114 ~]# pvs
  PV         VG Fmt  Attr PSize PFree
  /dev/vda2  cl lvm2 a--  9.00g    0 
  /dev/vda3     lvm2 ---  5.00g 5.00g

# 把 vda3 加入卷组
[root@kvm114 ~]# vgextend cl /dev/vda3
  Volume group "cl" successfully extended

# 查看 卷组大小,显示可用为 5G
[root@kvm114 ~]# vgs
  VG #PV #LV #SN Attr   VSize  VFree
  cl   2   2   0 wz--n- 13.99g 5.00g

# 扩展 cl-root 逻辑卷
[root@kvm114 ~]# lvextend -l +100%FREE /dev/mapper/cl-root
  Size of logical volume cl/root changed ...to 12.99 GiB (3326 extents).
  Logical volume cl/root successfully resized.

# 同步文件系统
[root@kvm114 ~]# xfs_growfs /dev/mapper/cl-root

10、查看分区使用情况

# cl-root 已由 8G 扩大至 13G 
[root@kvm114 ~]# df -h
Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/cl-root   13G  1.1G   12G   8% /
devtmpfs             486M     0  486M   0% /dev
tmpfs                497M     0  497M   0% /dev/shm
tmpfs                497M  6.6M  490M   2% /run
tmpfs                497M     0  497M   0% /sys/fs/cgroup
/dev/vda1           1014M  138M  877M  14% /boot
tmpfs                100M     0  100M   0% /run/user/0

第二种方案

1、创建镜像

# 创建一个新的境像
[root@vm71 vms]# qemu-img create -f qcow2 kvm114_add.qcow2 5G

# 查看镜像文件

[root@vm71 vms]# ll
-rw-r--r--. 1 root root      197120 3月  24 11:10 kvm114_add.qcow2
-rw-------. 1 qemu qemu 10739777536 3月  24 11:09 kvm114.qcow2

2、编辑 kvm114 配置文件

# 关闭虚拟机
[root@vm71 vms]# virsh shutdown kvm114

# 编辑配置文件
[root@vm71 vms]# virsh edit kvm114

# 新增

    
    
    
    

3、进入虚拟机 分区

[root@kvm114 ~]# fdisk /dev/vdb

# 具体过程见上面

4、创建物理卷,加入卷组,扩展逻辑卷

# 创建物理卷
[root@kvm114 ~]# pvcreate /dev/vdb1

# 具体过程见上面

 

你可能感兴趣的:(KVM)