使用virsh对虚拟机进行进阶管理

给虚拟机添加网卡
在宿主机上查看网卡信息,默认只有一块网卡。

[root@localhost qemu]# virsh domiflist vm01
Interface  Type       Source     Model       MAC
-------------------------------------------------------
-          network    default    virtio      52:54:00:dd:70:e5

使用virsh 命令添加网卡,attach-interface后面接上虚拟机的名称,network是网络类型,default是虚拟机的网络源,即给虚拟机添加网络接口。–persistent代表永久。

[root@localhost qemu]# virsh attach-interface vm01 network default --persistent
Interface attached successfully

[root@localhost qemu]# virsh domiflist vm01
Interface  Type       Source     Model       MAC
-------------------------------------------------------
-          network    default    virtio      52:54:00:dd:70:e5
-          network    default    rtl8139     52:54:00:e4:63:93

此时虚拟机并没有启动,所以查询到的网卡信息中interface选项中没有内容。
此时如果要删除网卡,使用detach-interface命令会出现下列内容。需要开启虚拟机才行。

[root@localhost qemu]# virsh detach-interface vm01 --mac 52:54:00:e4:63:93 --type network
error: Failed to detach interface
error: Requested operation is not valid: domain is not running

开启虚拟机,查看ip addr发现新网卡是没有ip地址的,使用dhclient命令重新获取一下ip。
在这里插入图片描述
在宿主机上重新查看domiflist,可以看到interface选项中有对应的信息了。

[root@localhost qemu]# virsh domiflist vm01
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet0      network    default    virtio      52:54:00:dd:70:e5
vnet1      network    default    rtl8139     52:54:00:e4:63:93

再使用detach-interface命令即可删除网卡。

[root@localhost qemu]# virsh detach-interface vm01 --type network --mac 52:54:00:e4:63:93
Interface detached successfully

[root@localhost qemu]# virsh domiflist vm01
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet0      network    default    virtio      52:54:00:dd:70:e5

给虚拟机添加磁盘
当虚拟机存储容量不够时,需要添加磁盘。
通过dd命令生成一个虚拟机磁盘文件。
dd 可从标准输入或文件中读取数据,根据指定的格式来转换数据,再输出到文件、设备或标准输出。
if=文件名:输入文件名,默认为标准输入。即指定源文件。
of=文件名:输出文件名,默认为标准输出。即指定目的文件。
ibs=bytes:一次读入bytes个字节,即指定一个块大小为bytes个字节。
obs=bytes:一次输出bytes个字节,即指定一个块大小为bytes个字节。
bs=bytes:同时设置读入/输出的块大小为bytes个字节。
count=blocks:仅拷贝blocks个块,块大小等于ibs指定的字节数。

[root@localhost ~]# dd if=/dev/zero of=/var/lib/libvirt/images/vm01_1.img bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 0.654528 s, 160 MB/s

也可以使用qemu-img命令生成磁盘文件。
create [-f fmt] [-o options] filename
创建一个格式为fmt大小为size文件名为filename的镜像文件。

[root@localhost ~]# qemu-img create -f qcow2 -o size=100M,preallocation=metadata /var/lib/libvirt/images/vm01_3.img
Formatting '/var/lib/libvirt/images/vm01_3.img', fmt=qcow2 size=209715200 encryption=off cluster_size=65536 lazy_refcounts=off 
[root@localhost ~]# virsh domblklist vm01

此时创建了磁盘文件,但还没有添加到虚拟机中,暂时无法查看到。需要使用attach-disk命令将新生成的磁盘添加到虚拟机中。

[root@localhost ~]# virsh attach-disk vm01 /var/lib/libvirt/images/vm01_1.img vdb
error: Failed to attach disk
error: Requested operation is not valid: domain is not running

[root@localhost ~]# virsh start vm01
Domain vm01 started

[root@localhost ~]# virsh attach-disk vm01 /var/lib/libvirt/images/vm01_1.img vdb
Disk attached successfully

[root@localhost ~]# virsh attach-disk vm01 /var/lib/libvirt/images/vm01_3.img vdc
Disk attached successfully

[root@localhost ~]# virsh domblklist vm01
Target     Source
------------------------------------------------
vda        /var/lib/libvirt/images/vm01.dsk
vdb        /var/lib/libvirt/images/vm01_1.img
vdc        /var/lib/libvirt/images/vm01_3.img

在添加磁盘时,虚拟机必须是要启动,否则添加不成功。添加完成后可以看到虚拟机中多出2块磁盘信息。
进入虚拟机,也可以通过lsblk查看磁盘情况。
在这里插入图片描述
此时磁盘还需要分区、格式化、挂载后才能正常使用。
如果不小心添加错了磁盘,要删除也可以使用命令detach-disk。

[root@localhost ~]# virsh attach-disk vm01 /var/lib/libvirt/images/vm01_3.img vdd
Disk attached successfully

[root@localhost ~]# virsh domblklist vm01
Target     Source
------------------------------------------------
vda        /var/lib/libvirt/images/vm01.dsk
vdb        /var/lib/libvirt/images/vm01_1.img
vdc        /var/lib/libvirt/images/vm01_3.img
vdd        /var/lib/libvirt/images/vm01_3.img

[root@localhost ~]# virsh detach-disk vm01 vdd
Disk detached successfully

[root@localhost ~]# virsh domblklist vm01
Target     Source
------------------------------------------------
vda        /var/lib/libvirt/images/vm01.dsk
vdb        /var/lib/libvirt/images/vm01_1.img
vdc        /var/lib/libvirt/images/vm01_3.img


修改CPU和内存
可以通过命令virsh dominfo查看虚拟机CPU和内存信息。

[root@localhost qemu]# virsh dominfo vm01
Id:             2
Name:           vm01
UUID:           23602350-12ce-4289-803d-807f7bf42e44
OS Type:        hvm
State:          running
CPU(s):         1
CPU time:       186.5s
Max memory:     1048576 KiB
Used memory:    1048576 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: selinux
Security DOI:   0
Security label: system_u:system_r:svirt_t:s0:c171,c311 (permissive)

也可以在虚拟机中使用lscpu可以查看CPU信息,free -m 查看内存大小。
在这里插入图片描述
调整虚拟机CPU和内存有三种办法:
1、通过virsh edit命令修改配置文件
此配置文件存放在/etc/libvirt/qemu/vm01.xml中。
在这里插入图片描述
此时,需要关闭虚拟机再启动虚拟机,配置才会生效。

[root@localhost qemu]# virsh edit vm01
Domain vm01 XML configuration edited.

[root@localhost qemu]# virsh shutdown vm01
Domain vm01 is being shutdown

[root@localhost qemu]# virsh start vm01
Domain vm01 started

在这里插入图片描述

2、通过图形方式调整。
在这里插入图片描述
在这里插入图片描述
同样设置变更后,也是需要关机再开启才会生效。
3、通过命令方式来调整
如设置CPU的个数,将最大允许分配的CPU个数设置为2个,这里是修改配置文件,需要重启才会生效。

[root@localhost qemu]# virsh setvcpus vm01 2 --maximum --config

调整CPU在线个数时,需要注意在线个数不能超过最大允许分配的个数。减少CPU个数必须关闭虚拟机再开启虚拟机才会生效,且不能在线减少CPU。

[root@localhost qemu]# virsh setvcpus vm01 2 --live

[root@localhost qemu]# virsh setvcpus vm01 3 --live
error: invalid argument: requested vcpus is greater than max allowable vcpus for the live domain: 3 > 2

[root@localhost qemu]# virsh setvcpus vm01 1 --live
error: unsupported configuration: failed to find appropriate hotpluggable vcpus to reach the desired target vcpu count

[root@localhost qemu]# virsh setvcpus vm01 1 --maximum --config

[root@localhost qemu]# virsh shutdown vm01
Domain vm01 is being shutdown

[root@localhost qemu]# virsh start vm01
Domain vm01 started

[root@localhost qemu]# virsh dominfo vm01
Id:             3
Name:           vm01
UUID:           23602350-12ce-4289-803d-807f7bf42e44
OS Type:        hvm
State:          running
CPU(s):         1
CPU time:       20.6s
Max memory:     1048576 KiB
Used memory:    1048576 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: selinux
Security DOI:   0
Security label: system_u:system_r:svirt_t:s0:c167,c995 (permissive)

设置内存大小也是通过命令virsh setmaxmem修改配置。修改完之后,也是需要关闭再开启虚拟机才能生效。

[root@localhost qemu]# virsh setmaxmem vm01 4096M --config

[root@localhost qemu]# virsh dominfo vm01
Id:             3
Name:           vm01
UUID:           23602350-12ce-4289-803d-807f7bf42e44
OS Type:        hvm
State:          running
CPU(s):         1
CPU time:       30.2s
Max memory:     1048576 KiB
Used memory:    1048576 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: selinux
Security DOI:   0
Security label: system_u:system_r:svirt_t:s0:c167,c995 (permissive)

[root@localhost qemu]# virsh reboot vm01
Domain vm01 is being rebooted

[root@localhost qemu]# virsh dominfo vm01
Id:             3
Name:           vm01
UUID:           23602350-12ce-4289-803d-807f7bf42e44
OS Type:        hvm
State:          running
CPU(s):         1
CPU time:       48.6s
Max memory:     1048576 KiB
Used memory:    1048576 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: selinux
Security DOI:   0
Security label: system_u:system_r:svirt_t:s0:c167,c995 (permissive)

[root@localhost qemu]# virsh shutdown vm01
Domain vm01 is being shutdown

[root@localhost qemu]# virsh start vm01
Domain vm01 started

[root@localhost qemu]# virsh dominfo vm01
Id:             4
Name:           vm01
UUID:           23602350-12ce-4289-803d-807f7bf42e44
OS Type:        hvm
State:          running
CPU(s):         1
CPU time:       7.3s
Max memory:     4194304 KiB
Used memory:    4194304 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: selinux
Security DOI:   0
Security label: system_u:system_r:svirt_t:s0:c323,c960 (permissive)

如果只是调整当前内存大小,使用以下命令则无需重启虚拟机。但是调整当前内存不能调整到比最大内存数还要大。

[root@localhost qemu]# virsh setmem vm01 2048M --current

[root@localhost qemu]# virsh dominfo vm01
Id:             4
Name:           vm01
UUID:           23602350-12ce-4289-803d-807f7bf42e44
OS Type:        hvm
State:          running
CPU(s):         1
CPU time:       105.4s
Max memory:     4194304 KiB
Used memory:    2097152 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: selinux
Security DOI:   0
Security label: system_u:system_r:svirt_t:s0:c323,c960 (permissive)

猜你喜欢

转载自blog.csdn.net/qq_26350199/article/details/114994891