kvm管理虚拟机(代码片段)

author author     2022-10-23     127

关键词:

笔记内容:KVM管理虚拟机
笔记日期:2018-03-08

  • 28.9 克隆虚拟机
  • 28.10 快照管理
  • 28.11 磁盘格式
  • 28.12 磁盘扩容(raw)
  • 28.13 磁盘扩容(qcow2)
  • 28.14 调整cpu内存和网卡
  • 28.15 迁移虚拟机

在上一篇中,我们介绍了如何在 CentOS7上安装并配置KVM,以及通过KVM安装CentOS系统。本篇则是介绍在KVM里安装完虚拟机后,如何使用KVM来管理虚拟机。

克隆虚拟机

克隆虚拟机是虚拟机软件必须得有的功能,这样能省去我们安装系统及环境的时间,如果使用过VMware、VirtualBox等虚拟机软件的话,都知道有这个功能,KVM也不例外,只不过我们没有用图形化界面,是命令行形式的而已。

要克隆某个虚拟机,首先这个虚拟机需要为关闭状态:

[[email protected] ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 1     study01                        running

You have new mail in /var/spool/mail/root
[[email protected] ~]# virsh shutdown study01
Domain study01 is being shutdown
[[email protected] ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     study01                        shut off

[[email protected] ~]# 

然后执行以下命令进行克隆:

[[email protected] ~]# virt-clone  --original study01 --name study02 --file /kvm_data/study02.img
Allocating ‘study02.img‘                                                                          |  10 GB  00:00:42     

Clone ‘study02‘ created successfully.  # 克隆成功
[[email protected] ~]# ls /kvm_data/
lost+found  study01.img  study02.img
[[email protected] ~]# ls /etc/libvirt/qemu/
networks  study01.xml  study02.xml
[[email protected] ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     study01                        shut off
 -     study02                        shut off

[[email protected] ~]# virsh start study02
Domain study02 started

[[email protected] ~]#

命令说明:

  • --original指定克隆源虚拟机,也就是克隆哪一个虚拟机
  • --name指定克隆后的虚拟机的名称
  • --file指定克隆后的虚拟机的虚拟磁盘文件所存放的目录及名称

快照管理

同样的创建快照也是虚拟机软件的一个很重要的功能,快照能够很方便的帮我们把虚拟机恢复到一个正常的状态下。我们来看看命令行下如何使用KVM创建虚拟机的快照:

[[email protected] ~]# virsh snapshot-create study01  # 创建study01的快照
Domain snapshot 1520519047 created 
[[email protected] ~]#

注:raw格式的虚拟磁盘不支持做快照,qcow2则支持

查看study01.img的信息,同时会查看到快照列表:

[[email protected] ~]# qemu-img info /kvm_data/study01.img 
image: /kvm_data/study01.img
file format: qcow2  # qcow2是一种虚拟磁盘的格式
virtual size: 10G (10737418240 bytes)  # 虚拟磁盘的总大小
disk size: 1.1G  # 虚拟磁盘目前所占用的物理磁盘大小
cluster_size: 65536
Snapshot list:  # 快照列表
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         1520519047                0 2018-03-08 22:24:07   00:00:00.000
Format specific information:
    compat: 1.1
    lazy refcounts: true
[[email protected] ~]# 

使用以下命令可以列出某个虚拟机下的所有快照:

[[email protected] ~]# virsh snapshot-list study01
 Name                 Creation Time             State
------------------------------------------------------------
 1520519047           2018-03-08 22:24:07 +0800 shutoff

[[email protected] ~]# 

查看某个虚拟机当前快照版本的详细信息,也就是最近的一个快照版本的信息:

[[email protected] ~]# virsh snapshot-current study01
<domainsnapshot>
  <name>1520519047</name>
  <state>shutoff</state>
  <creationTime>1520519047</creationTime>
  <memory snapshot=‘no‘/>
  <disks>
    <disk name=‘vda‘ snapshot=‘internal‘/>
    <disk name=‘hda‘ snapshot=‘no‘/>
  </disks>
  <domain type=‘kvm‘>
    <name>study01</name>
    <uuid>eeedcd47-1546-4e5f-ab2a-f62deb0838cf</uuid>
    <memory unit=‘KiB‘>1048576</memory>
    <currentMemory unit=‘KiB‘>524288</currentMemory>
    <vcpu placement=‘static‘ current=‘1‘>2</vcpu>
    <os>
      <type arch=‘x86_64‘ machine=‘pc-i440fx-rhel7.0.0‘>hvm</type>
      <boot dev=‘hd‘/>
    </os>
    <features>
      <acpi/>
      <apic/>
    </features>
    <cpu mode=‘custom‘ match=‘exact‘ check=‘partial‘>
      <model fallback=‘allow‘>Haswell-noTSX</model>
    </cpu>
    <clock offset=‘utc‘>
      <timer name=‘rtc‘ tickpolicy=‘catchup‘/>
      <timer name=‘pit‘ tickpolicy=‘delay‘/>
      <timer name=‘hpet‘ present=‘no‘/>
    </clock>
    <on_poweroff>destroy</on_poweroff>
    <on_reboot>restart</on_reboot>
    <on_crash>destroy</on_crash>
    <pm>
      <suspend-to-mem enabled=‘no‘/>
      <suspend-to-disk enabled=‘no‘/>
    </pm>
    <devices>
      <emulator>/usr/libexec/qemu-kvm</emulator>
      <disk type=‘file‘ device=‘disk‘>
        <driver name=‘qemu‘ type=‘qcow2‘/>
        <source file=‘/kvm_data/study01.img‘/>
        <target dev=‘vda‘ bus=‘virtio‘/>
        <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x06‘ function=‘0x0‘/>
      </disk>
      <disk type=‘file‘ device=‘cdrom‘>
        <driver name=‘qemu‘ type=‘raw‘/>
        <target dev=‘hda‘ bus=‘ide‘/>
        <readonly/>
        <address type=‘drive‘ controller=‘0‘ bus=‘0‘ target=‘0‘ unit=‘0‘/>
      </disk>
      <controller type=‘usb‘ index=‘0‘ model=‘ich9-ehci1‘>
        <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x04‘ function=‘0x7‘/>
      </controller>
      <controller type=‘usb‘ index=‘0‘ model=‘ich9-uhci1‘>
        <master startport=‘0‘/>
        <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x04‘ function=‘0x0‘ multifunction=‘on‘/>
      </controller>
      <controller type=‘usb‘ index=‘0‘ model=‘ich9-uhci2‘>
        <master startport=‘2‘/>
        <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x04‘ function=‘0x1‘/>
      </controller>
      <controller type=‘usb‘ index=‘0‘ model=‘ich9-uhci3‘>
        <master startport=‘4‘/>
        <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x04‘ function=‘0x2‘/>
      </controller>
      <controller type=‘ide‘ index=‘0‘>
        <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x01‘ function=‘0x1‘/>
      </controller>
      <controller type=‘virtio-serial‘ index=‘0‘>
        <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x05‘ function=‘0x0‘/>
      </controller>
      <controller type=‘pci‘ index=‘0‘ model=‘pci-root‘/>
      <interface type=‘bridge‘>
        <mac address=‘52:54:00:65:d3:3f‘/>
        <source bridge=‘br0‘/>
        <model type=‘virtio‘/>
        <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x03‘ function=‘0x0‘/>
      </interface>
      <serial type=‘pty‘>
        <target type=‘isa-serial‘ port=‘0‘/>
      </serial>
      <console type=‘pty‘>
        <target type=‘serial‘ port=‘0‘/>
      </console>
      <channel type=‘unix‘>
        <target type=‘virtio‘ name=‘org.qemu.guest_agent.0‘/>
        <address type=‘virtio-serial‘ controller=‘0‘ bus=‘0‘ port=‘1‘/>
      </channel>
      <input type=‘tablet‘ bus=‘usb‘>
        <address type=‘usb‘ bus=‘0‘ port=‘1‘/>
      </input>
      <input type=‘mouse‘ bus=‘ps2‘/>
      <input type=‘keyboard‘ bus=‘ps2‘/>
      <memballoon model=‘virtio‘>
        <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x07‘ function=‘0x0‘/>
      </memballoon>
    </devices>
  </domain>
</domainsnapshot>

[[email protected] ~]# 

查看所有快照的配置文件:

[[email protected] ~]# virsh snapshot-create study01
Domain snapshot 1520520513 created
[[email protected] ~]# ls /var/lib/libvirt/qemu/snapshot/study01/ # 快照的配置文件都存放在这个目录下
1520519047.xml  1520520513.xml
[[email protected] ~]# 

通过快照的TAG,将虚拟机恢复到指定快照:

[[email protected] ~]# virsh snapshot-revert study01 1520519047

删除某个虚拟机下的某个快照:

[[email protected] ~]# virsh snapshot-delete study01 1520519047
Domain snapshot 1520519047 deleted

[[email protected] ~]# 

磁盘格式

虚拟磁盘常用格式有raw和qcow2,以上我们也提到了raw格式的虚拟磁盘不支持创建快照,qcow2格式的虚拟磁盘才支持创建快照。

查看虚拟磁盘格式的命令:

[[email protected] ~]# qemu-img info /kvm_data/study01.img 
image: /kvm_data/study01.img
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 1.1G
cluster_size: 65536
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
2         1520520513                0 2018-03-08 22:48:33   00:00:00.000
Format specific information:
    compat: 1.1
    lazy refcounts: true
[[email protected] ~]# 

创建一个2G大小的raw格式的虚拟磁盘,-f选项用于指定虚拟磁盘的格式:

[[email protected] ~]# qemu-img create -f raw /kvm_data/study01_2.img 2G
Formatting ‘/kvm_data/study01_2.img‘, fmt=raw size=2147483648 
[[email protected] ~]# qemu-img info /kvm_data/study01_2.img 
image: /kvm_data/study01_2.img
file format: raw
virtual size: 2.0G (2147483648 bytes)
disk size: 0
[[email protected] ~]# 

我们可以把raw格式的磁盘转换为qcow2格式,-O选项指定转换的目标格式:

[[email protected] ~]# qemu-img convert -O qcow2 /kvm_data/study01_2.img /kvm_data/study01_2.qcow2
[[email protected] ~]# ls /kvm_data/study01_2*
/kvm_data/study01_2.img  /kvm_data/study01_2.qcow2
[[email protected] ~]# qemu-img info /kvm_data/study01_2.qcow2 
image: /kvm_data/study01_2.qcow2
file format: qcow2
virtual size: 2.0G (2147483648 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
[[email protected] ~]# 

如上,与其说是转换,不如说是从raw格式的磁盘复制了一份格式为qcow2磁盘出来,因为原本的磁盘并不会发生任何的变化,而是从这个raw格式磁盘的基础上复制出来了一个新的qcow2格式的磁盘。

转换后用ls -lh查看磁盘文件的大小,可以看到qcow2格式的磁盘文件比较小,raw格式的磁盘文件大小和我们指定空间大小一样是2G:

[[email protected] ~]# cd /kvm_data/
[[email protected] /kvm_data]# ls -lh
total 2.3G
drwx------ 2 root root  16K Mar  7 20:56 lost+found
-rw-r--r-- 1 root root 2.0G Mar  8 23:02 study01_2.img
-rw-r--r-- 1 root root 193K Mar  8 23:05 study01_2.qcow2
-rw------- 1 root root  11G Mar  8 22:51 study01.img
-rw------- 1 qemu qemu 1.2G Mar  8 23:11 study02.img
[[email protected] /kvm_data]# 

注:raw格式的磁盘性能比qcow2要好,但是raw格式的磁盘无法制作快照。

使用du -sh命令查看则不一样了,raw格式的磁盘文件大小是0,而qcow2格式的磁盘文件大小则是和我们使用ls -lh查看的一样:

[[email protected] /kvm_data]# du -sh *
16K lost+found
0   study01_2.img
196K    study01_2.qcow2
1.2G    study01.img
1.2G    study02.img
[[email protected] /kvm_data]# 

这就是raw格式的虚拟磁盘文件和qcow2格式的虚拟磁盘文件在物理磁盘存储上的区别。

下面我们来将study02虚拟机的虚拟磁盘转换成raw格式的虚拟磁盘,并进行启动:

[[email protected] /kvm_data]# virsh list
 Id    Name                           State
----------------------------------------------------
 2     study02                        running

[[email protected] /kvm_data]# virsh shutdown study02
Domain study02 is being shutdown

[[email protected] /kvm_data]# qemu-img convert -O raw /kvm_data/study02.img /kvm_data/study02_3.raw
[[email protected] /kvm_data]# ls -l
total 3531020
drwx------ 2 root root       16384 Mar  7 20:56 lost+found
-rw-r--r-- 1 root root  2147483648 Mar  8 23:02 study01_2.img
-rw-r--r-- 1 root root      197120 Mar  8 23:05 study01_2.qcow2
-rw------- 1 root root 10739515904 Mar  8 22:51 study01.img
-rw-r--r-- 1 root root 10737418240 Mar  8 23:45 study02_3.raw  # 此时的属组属主为root
-rw------- 1 root root  1209008128 Mar  8 23:38 study02.img
[[email protected] /kvm_data]# virsh edit study02  # 更改格式和文件路径,修改如下内容即可
<driver name=‘qemu‘ type=‘raw‘/>
<source file=‘/kvm_data/study02_3.raw‘/>
[[email protected] /kvm_data]# virsh start study02
Domain study02 started

[[email protected] /kvm_data]# ls -l  # 可以发现启动状态的虚拟机的磁盘文件的属组属主会发生改变
total 3531020
drwx------ 2 root root       16384 Mar  7 20:56 lost+found
-rw-r--r-- 1 root root  2147483648 Mar  8 23:02 study01_2.img
-rw-r--r-- 1 root root      197120 Mar  8 23:05 study01_2.qcow2
-rw------- 1 root root 10739515904 Mar  8 22:51 study01.img
-rw-r--r-- 1 qemu qemu 10737418240 Mar  8 23:45 study02_3.raw   # 此时的属组属主为qemu
-rw------- 1 root root  1209008128 Mar  8 23:38 study02.img
[[email protected] /kvm_data]# 

此时如果要给study02制作快照就会报如下错误:

[[email protected] /kvm_data]# virsh snapshot-create study02
error: unsupported configuration: internal snapshot for disk vda unsupported for storage type raw

[[email protected] /kvm_data]# 

磁盘扩容(raw)

以上介绍了虚拟磁盘格式的,以及如何转换虚拟磁盘格式,下面我们来看看如何进行磁盘扩容:

[[email protected] ~]# ls -lh /kvm_data/study02_3.raw 
-rw-r--r-- 1 qemu qemu 10G Mar  9 01:00 /kvm_data/study02_3.raw
[[email protected] ~]# qemu-img resize /kvm_data/study02_3.raw +2G
Image resized.
[[email protected] ~]# ls -lh /kvm_data/study02_3.raw 
-rw-r--r-- 1 qemu qemu 12G Mar  9 01:00 /kvm_data/study02_3.raw
[[email protected] ~]# 

如上可以看到,原本的10G变成12G了,但是此时进入虚拟机中会发现磁盘依旧是10G:

[[email protected] ~]# virsh console study02
[[email protected] ~]# fdisk -l

磁盘 /dev/vda:10.7 GB, 10737418240 字节,20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x00003436

   设备 Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048     3123199     1048576   82  Linux swap / Solaris
/dev/vda3         3123200    20971519     8924160   83  Linux
[[email protected] ~]# 

这是因为还没有生效,需要重启虚拟机才能生效,但是注意这个重启要在宿主机上使用virsh命令进行重启,如下:

[[email protected] ~]# virsh destroy study02
Domain study02 destroyed

[[email protected] ~]# virsh start study02
Domain study02 started

[[email protected] ~]# 

重启完成之后再查看就生效了:

[[email protected] ~]# virsh console study02
[[email protected] ~]# fdisk -l

磁盘 /dev/vda:12.9 GB, 12884901888 字节,25165824 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x00003436

   设备 Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048     3123199     1048576   82  Linux swap / Solaris
/dev/vda3         3123200    20971519     8924160   83  Linux
[[email protected] ~]# 

然后还需要把这2G的空间分区才可以使用,因为我们安装这个系统的时候选择的是标准分区,所以无法像LVM分区那样直接扩容逻辑卷:

[[email protected] ~]# fdisk /dev/vda
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。

命令(输入 m 获取帮助):n  # 新建分区
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): p  # 选择新建主分区
已选择分区 4
起始 扇区 (20971520-25165823,默认为 20971520):  # 默认即可,直接按回车
将使用默认值 20971520
Last 扇区, +扇区 or +sizeK,M,G (20971520-25165823,默认为 25165823): # 默认即可,直接按回车
将使用默认值 25165823
分区 4 已设置为 Linux 类型,大小设为 2 GiB

命令(输入 m 获取帮助):p  # 再次查看磁盘信息

磁盘 /dev/vda:12.9 GB, 12884901888 字节,25165824 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x00003436

   设备 Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048     3123199     1048576   82  Linux swap / Solaris
/dev/vda3         3123200    20971519     8924160   83  Linux
/dev/vda4        20971520    25165823     2097152   83  Linux  # 可以看到多了一个/dev/vda4分区

命令(输入 m 获取帮助):w  # 保存退出

除了对已有磁盘扩容外,还可以额外增加磁盘,宿主机下执行以下命令:

[[email protected] ~]# qemu-img create -f raw /kvm_data/study02_2.raw 5G
Formatting ‘/kvm_data/study02_2.raw‘, fmt=raw size=5368709120 
[[email protected] ~]# qemu-img info /kvm_data/study02_2.raw
image: /kvm_data/study02_2.raw
file format: raw
virtual size: 5.0G (5368709120 bytes)
disk size: 0
[[email protected] ~]# virsh edit study02  # 在原本的<disk>标签下增加以下内容
    <disk type=‘file‘ device=‘disk‘>
      <driver name=‘qemu‘ type=‘raw‘/>
      <source file=‘/kvm_data/study02_2.raw‘/>
      <target dev=‘vdb‘ bus=‘virtio‘/>
      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x08‘ function=‘0x0‘/>
    </disk>
[[email protected] ~]# virsh destroy study02
Domain study02 destroyed

[[email protected] ~]# virsh start study02
Domain study02 started

[[email protected] ~]# 

重启完成后进入虚拟机,查看新增的虚拟磁盘是否生效了:

[[email protected] ~]# fdisk -l

磁盘 /dev/vda:12.9 GB, 12884901888 字节,25165824 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x00003436

   设备 Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048     3123199     1048576   82  Linux swap / Solaris
/dev/vda3         3123200    20971519     8924160   83  Linux

磁盘 /dev/vdb:5368 MB, 5368709120 字节,10485760 个扇区  # 新增的虚拟磁盘
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

[[email protected] ~]# 

如上,可以看到新增了一块5G的虚拟磁盘,这时我们只需要对该磁盘进行分区、格式化、挂载后就可以使用了。


磁盘扩容(qcow2)

qcow2格式的虚拟磁盘的扩容方式和raw格式的是一样的,只不过需要注意的是,如果虚拟磁盘保存了快照,则无法扩容会报错,如下:

[[email protected] ~]# qemu-img info /kvm_data/study01.img
image: /kvm_data/study01.img
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 1.1G
cluster_size: 65536
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
2         1520520513                0 2018-03-08 22:48:33   00:00:00.000
Format specific information:
    compat: 1.1
    lazy refcounts: true
[[email protected] ~]# qemu-img resize /kvm_data/study01.img +2G  # 会报如下错误
qemu-img: Can‘t resize an image which has snapshots
qemu-img: This image does not support resize
[[email protected] ~]# 

需要把快照删除后才能进行扩容:

[[email protected] ~]# virsh snapshot-delete study01 1520520513
Domain snapshot 1520520513 deleted

[[email protected] ~]# qemu-img resize /kvm_data/study01.img +2G
Image resized.
[[email protected] ~]# 

剩下的操作和上面演示的raw是一样的,这里就不再演示了。

如果不希望删除快照的话,就只能是创建一块新的虚拟磁盘,操作方式也是和raw一样的。

建议不管是raw格式的还是qcow2格式的虚拟磁盘需要扩容,最好是使用新增硬盘的方式进行扩容,而不是在原有的磁盘上进行扩容,因为新增磁盘相对独立与已有的磁盘,误操作时也不容易对已有的磁盘造成影响。


调整cpu内存和网卡

介绍完了如何扩容磁盘,我们再来看看如何调整cpu、内存以及网卡,首先查看虚拟机的配置:

[[email protected] ~]# virsh dominfo study01
Id:             -
Name:           study01
UUID:           eeedcd47-1546-4e5f-ab2a-f62deb0838cf
OS Type:        hvm
State:          shut off
CPU(s):         1
Max memory:     1048576 KiB
Used memory:    524288 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: none
Security DOI:   0

[[email protected] ~]# 

例如我现在要对该虚拟机的内存以及CPU进行调整,编辑该虚拟机的配置文件即可:

[[email protected] ~]# virsh edit study01  # 更改以下标签的值即可
<memory unit=‘KiB‘>2048576</memory>  # 内存的最大大小
<currentMemory unit=‘KiB‘>624288</currentMemory>  # 目前使用的内存大小
<vcpu placement=‘static‘ current=‘2‘>4</vcpu>  # CPU的核心数量,current的值为当前使用的核心数量,标签的值则为最大可使用的核心数量

更改完配置文件之后,如果虚拟机是开机状态的话需要重启,关机状态的话则直接启动即可生效:

[[email protected] ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 5     study02                        running
 -     study01                        shut off

[[email protected] ~]# virsh start study01
Domain study01 started

[[email protected] ~]# 

此时再查看study01虚拟机的配置,会发现Max memory、Used memory以及CPU(s)参数的值发生了改变:

[[email protected] ~]# virsh dominfo study01
Id:             6
Name:           study01
UUID:           eeedcd47-1546-4e5f-ab2a-f62deb0838cf
OS Type:        hvm
State:          running
CPU(s):         2
CPU time:       26.7s
Max memory:     2049024 KiB
Used memory:    624288 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: none
Security DOI:   0

[[email protected] ~]# 

这样就完成了内存和CPU的调整,以上我们是通过编辑配置文件来对内存和CPU的资源进行调整,需要重启虚拟机之后才能生效。不过好在KVM是支持动态调整的,但前提是有设置最大的资源大小,例如以上的例子中,我们将内存的最大大小设置了两个G左右,那么我们就可以在这两个G以内动态的调整内存的大小,CPU也是同理。如下示例:

[[email protected] ~]# virsh setmem study01 800m  # 动态调整内存大小为800M

[[email protected] ~]# virsh setvcpus study01 3  # 动态调整CPU核心数量为3核

[[email protected] ~]# virsh dominfo study01  # 可以看到无需重启也生效了
Id:             6
Name:           study01
UUID:           eeedcd47-1546-4e5f-ab2a-f62deb0838cf
OS Type:        hvm
State:          running
CPU(s):         3
CPU time:       31.2s
Max memory:     2049024 KiB
Used memory:    819200 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: none
Security DOI:   0

# 想要动态调整的配置永久生效,则需要把当前配置写入到配置文件里去
[[email protected] ~]# virsh dumpxml study01 > /etc/libvirt/qemu/study01.xml  

注:内存资源可以动态的调大调小,而CPU资源只能动态地调大不能调小。

以上介绍了如何调整内存以及CPU资源,下面我们来看看如何增加网卡,首先查看一下网卡:

[[email protected] ~]# virsh domiflist study01
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet1      bridge     br0        virtio      52:54:00:65:d3:3f

[[email protected] ~]# 

如上,可以看到目前该虚拟机只有一个网卡。

然后我们来增加一块新的网卡,将该网卡设置为NAT网络模式(因为virbr0类似vmware的vmnet8):

[[email protected] ~]# virsh attach-interface study01 --type bridge  --source virbr0
Interface attached successfully

[[email protected] ~]# virsh domiflist study01
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet1      bridge     br0        virtio      52:54:00:65:d3:3f
vnet2      bridge     virbr0     rtl8139     52:54:00:a4:60:b0

[[email protected] ~]# 

这条命令末尾如果写的是 --source br0 ,则是设置网络模式为桥接,和VMware里的桥接模式一样。--source是用于指定桥接到哪一块网卡上。例如我们也可以桥接到br0上:

[[email protected] ~]# virsh attach-interface study01 --type bridge  --source br0
Interface attached successfully

[[email protected] ~]# virsh domiflist study01
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet1      bridge     br0        virtio      52:54:00:65:d3:3f
vnet2      bridge     virbr0     rtl8139     52:54:00:a4:60:b0
vnet3      bridge     br0        rtl8139     52:54:00:e0:6c:da

# 同样的想要动态调整的配置永久生效,则需要把当前配置写入到配置文件里去
[[email protected] ~]# virsh dumpxml study01 > /etc/libvirt/qemu/study01.xml  

迁移虚拟机

以上我们介绍完了如何调整资源的大小,下面我们来看看如何迁移虚拟机。迁移虚拟机就是把当前宿主机里的虚拟机迁移到另一台宿主机上去。

首先关闭需要迁移的虚拟机:

[[email protected] ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 5     study02                        running
 6     study01                        running

[[email protected] ~]# virsh shutdown study01
Domain study01 is being shutdown

[[email protected] ~]# 

然后通过rsync命令将配置文件拷贝到另一台机器上:

[[email protected] ~]# rsync -av /etc/libvirt/qemu/study01.xml 192.168.77.128:/etc/libvirt/qemu/study01.xml 

接着将虚拟机的磁盘文件也拷贝到另一台机器上:

[[email protected] ~]# virsh domblklist study01  # 查看虚拟机的磁盘文件路径
Target     Source
------------------------------------------------
vda        /kvm_data/study01.img
hda        -

[[email protected] ~]# rsync -av /kvm_data/study01.img  192.168.77.128:/kvm_data/study01.img

最后就是在另一台机器上根据实际情况修改虚拟机的配置文件,例如虚拟机的名称要对得上,虚拟磁盘文件路径要对得上等等,因为我这里两台机器的环境都是一样的,所以就不需要修改配置文件了,直接定义新虚拟机即可。以下这条命令在另一台机器上执行:

[[email protected] ~]# virsh define /etc/libvirt/qemu/study01.xml

此时就可以看到新迁移的虚拟机了:

[[email protected] ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     study01                        shut off

[[email protected] ~]# 

kvm管理命令(代码片段)

...次主要介绍了KVM虚拟机的安装,这次主要介绍KVM虚拟机的管理。这一次主要是KVM的一些操作命令,比如虚拟机的开启,删除,恢复,挂起以及重新定义虚拟机。相关命令开关机#virshlist//查看虚拟机状态#virshlist--all//查看所有虚拟... 查看详情

使用kvm命令集管理虚拟机(代码片段)

kvm基本功能管理查看命令帮助vrish-h虚拟机配置文件存放目录ls/etc/libvirt/qemu查看虚拟机状态virshlist//查看已经开启的虚拟机virshlist--all//查看处于任何状态的虚拟机虚拟机开关机virshshutdownchen//关闭虚拟机实例chencirshstartchen//开启虚... 查看详情

kvm管理虚拟机命令(代码片段)

KVM基本功能管理virsh-h查看命令帮助kvm的配置存放路径cd/etc/libvirt/qemu下有.xml的文件查看虚拟机的状态virshlist--all虚拟机关机与开启virshshutdownc1//关机virshstartc1//开机强行关闭虚拟virshdestroycc1强行关闭电源状态导出虚拟机配置virshdump... 查看详情

kvm命令集管理虚拟机(代码片段)

使用KVM命令集管理虚拟机查看虚拟机状态[[email protected]~]#virshlist--allId名称状态----------------------------------------------------2c01running虚拟机的关机与开机[[email protected]~]#virshshutdownc01域c01被关闭[[email  查看详情

kvm命令集管理虚拟机(代码片段)

KVM基本功能管理1、查看命令帮助virsh-h2、KVM的配置文件存放目录cd/etc/libvirt/qemu//目录下有.xml的文件3、查看虚拟机状态virshlist--all4、虚拟机关机与开机virshshutdownc1//关机virshstartc1//开机5、强制虚拟机关闭电源virshdestroyc16、导出虚... 查看详情

kvm虚拟化之基本命令管理虚拟机(代码片段)

KVM基本功能管理查看KVM的配置文件存放目录(c01.xml是虚拟机系统实例的配置文件)[[email protected]~]#ls/etc/libvirt/qemuc01.xmlnetworks查看查看虚拟机的状态[[email protected]~]#virshlist--allId名称状态--------------------------------------- 查看详情

实操干货kvm命令管理虚拟机与性能优化(代码片段)

KVM命令管理虚拟机1、KVM基本功能管理简要介绍KVM中经常使用的基本功能管理命令(CentOS7.4是宿主机中安装的虚拟机);1)、virsh-h#查看命令帮助2)、ls/etc/libvirt/qemu#查看KVM的配置文件存放目录(虚拟机系统实例配置文件的文件... 查看详情

kvm虚拟机管理(代码片段)

...virt-manager创建虚拟机1>打开图像化界面,打开虚拟系统管理器  2>新建虚拟机 3>选择本地介质安装此处需要先将镜像文件导入图形化服务器的/var/lib/libvirt.images/目录下[root@localhostimages]#cd/var/lib/libvirt/images/[root@local... 查看详情

linux系统下kvm虚拟机的基本管理和操作(代码片段)

Linux系统下KVM虚拟机的基本管理和操作一、检查本地环境1.检查系统版本2.检查防火墙状态3.检查selinux3.检查libvirtd服务状态4.检查kvm安装结果5.检查kvm虚拟机状态6.检查virsh版本二、virsh常用命令1.列出虚拟机2.虚拟机开关机操作3.删... 查看详情

kvm虚拟机管理——虚拟机克隆(代码片段)

1.概述2.部署基本操作系统虚拟机3.配置虚拟机3.1修改/etc/sysconfig/network3.2删除/etc/sysconfig/network-scripts/ifcfg-ethx和主机有关信息3.3删除网卡编号规则文件3.4编辑/etc/yum.repo.d/3.5配置console到node99无密码访问3.6配置sshd配置文件3.7配置virshc... 查看详情

kvm虚拟机管理和配置(代码片段)

4.KVM虚拟机管理和配置4.1KVM虚拟机管理命令#virshlist列出正在运⾏的虚拟机[root@localhostopt]#virshlistIdNameState----------------------------------------------------1web02running2web01running#virshlist--all列出所有虚拟机(关闭和运⾏的虚拟机)[root@localhostop... 查看详情

案例:使用kvm命令集管理虚拟机(代码片段)

一、KVM基本功能管理1、查看命令帮助virsh-h或者virsh--help2、查看KVM配置文件存放目录ls/etc/libvirt/qemu3、查看虚拟机状态virshlist--all4、虚拟机关机与开机virshshutdownc7-1//关机virshstartc7-1//开机强制关闭关闭电源系统virshdestroyc7-15、通过... 查看详情

记录kvm虚拟机常用操作管理命令(代码片段)

环境说明centos7中的KVMNAT方式是kvm安装后的默认方式。它支持主机与虚拟机的互访,同时也支持虚拟机访问互联网,但不支持外界访问虚拟机。检查当前的网络设置#virshnet-list--alldefault是宿主机安装虚拟机支持模块的时候自动安装... 查看详情

kvm虚拟机扩展磁盘空间(代码片段)

...式的磁盘加入到KVM虚拟机,然后通过虚拟机系统lvm逻辑卷管理方式进行管理,扩展磁盘空间。(2) 直接通过dd命令扩展现有虚拟机磁盘大小,扩展之后,原磁盘大小增大,进入虚拟机系统后,直接通过fdis 查看详情

kvm网络虚拟化管理(代码片段)

1.LinuxBridge网桥管理  一个网桥上添加多个虚拟机,虚拟机之间是可以相互通信的的,同时虚拟机也都可以通外网。 kvm的网桥管理可以通过brctl命令[[email protected]~]#brctl.......#桥addbr<bridge>addbridgedelbr<bridge>deletebrid... 查看详情

kvm虚拟机的日常管理与配置(代码片段)

1.查看KVM虚拟机配置文件及运行状态(1)KVM虚拟机默认配置文件位置:/etc/libvirt/qemu/autostart目录是配置kvm虚拟机开机自启动目录。(2)virsh命令帮助#virsh-help或直接virsh命令和,再执行子命令。如下所示。[[email protected]~]#virsh欢迎使... 查看详情

kvm虚拟机硬盘配置管理(代码片段)

新建raw格式虚拟盘[[email protected]~]#qemu-imgcreate-fraw/data/raw/disk02.raw5GFormatting'/data/raw/disk02.raw',fmt=rawsize=5368709120[[email protected]~]#qemu-imginfo/data/raw/disk02.rawim 查看详情

kvm网络虚拟化管理(代码片段)

1.LinuxBridge网桥管理  一个网桥上添加多个虚拟机,虚拟机之间是可以相互通信的的,同时虚拟机也都可以通外网。 kvm的网桥管理可以通过brctl命令[root@localhost~]#brctl.......#桥addbr<bridge>addbridgedelbr<bridge>deletebridge#端... 查看详情