天天实时:43-KVM虚拟化-利用libvirt服务进行KVM虚拟机管理

2023-01-01 19:23:58 来源:51CTO博客

半虚拟化驱动virtio的工作原理

为了提高内存、硬盘、网络的性能,需要支持半虚拟化

virtio 是一种 I/O 半虚拟化解决方案,是一套通用 I/O 设备虚拟化的程序,是对半虚拟化 Hypervisor 中的一组通用 I/O 设备的抽象,提供了一套上层应用与各 Hypervisor 虚拟化设备(KVM,Xen,VMware等)之间的通信框架和编程接口,减少跨平台所带来的兼容性问题,大大提高驱动程序开发效率,Windows 系统需要单独安装virtio驱 动,Linux系统自带virtio驱动


【资料图】


libvirt 架构

如果libvirtd服务意外关闭,将导致相关工具,如:virt-manager等无法和虚拟机连接,但虚拟机仍会正常运行

[root@ubuntu2204 ~]#virsh list --all Id   Name                State------------------------------------ 2    centos7-mooreyxia   running -    rocky8              shut off[root@ubuntu2204 ~]#systemctl status libvirtd● libvirtd.service - Virtualization daemon     Loaded: loaded (/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)     Active: active (running) since Sat 2022-12-31 17:53:28 CST; 17h agoTriggeredBy: ● libvirtd-ro.socket             ● libvirtd.socket             ● libvirtd-admin.socket       Docs: man:libvirtd(8)             https://libvirt.org   Main PID: 871 (libvirtd)      Tasks: 23 (limit: 32768)     Memory: 72.6M        CPU: 9.258s     CGroup: /system.slice/libvirtd.service             ├─871 /usr/sbin/libvirtd             ├─987 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/lib/libvirt/libvirt_leaseshelper             └─988 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/lib/libvirt/libvirt_leaseshelperDec 31 17:53:30 ubuntu2204.wang.org dnsmasq[987]: using nameserver 127.0.0.53#53Dec 31 17:53:30 ubuntu2204.wang.org dnsmasq[987]: read /etc/hosts - 7 addressesDec 31 17:53:30 ubuntu2204.wang.org dnsmasq[987]: read /var/lib/libvirt/dnsmasq/default.addnhosts - 0 addressesDec 31 17:53:30 ubuntu2204.wang.org dnsmasq-dhcp[987]: read /var/lib/libvirt/dnsmasq/default.hostsfileJan 01 11:11:55 ubuntu2204.wang.org dnsmasq-dhcp[987]: DHCPDISCOVER(virbr0) 192.168.122.23 52:54:00:18:b7:d2Jan 01 11:11:55 ubuntu2204.wang.org dnsmasq-dhcp[987]: DHCPOFFER(virbr0) 192.168.122.23 52:54:00:18:b7:d2Jan 01 11:11:55 ubuntu2204.wang.org dnsmasq-dhcp[987]: DHCPREQUEST(virbr0) 192.168.122.23 52:54:00:18:b7:d2Jan 01 11:11:55 ubuntu2204.wang.org dnsmasq-dhcp[987]: DHCPACK(virbr0) 192.168.122.23 52:54:00:18:b7:d2Jan 01 11:18:35 ubuntu2204.wang.org dnsmasq-dhcp[987]: DHCPREQUEST(virbr0) 192.168.122.23 52:54:00:18:b7:d2Jan 01 11:18:35 ubuntu2204.wang.org dnsmasq-dhcp[987]: DHCPACK(virbr0) 192.168.122.23 52:54:00:18:b7:d2#停止libvirtd服务,socket还在运行,systemctl会自动激活socket,所以libvirtd会自动重启[root@ubuntu2204 ~]#systemctl stop libvirtdWarning: Stopping libvirtd.service, but it can still be activated by:  libvirtd-ro.socket  libvirtd.socket  libvirtd-admin.socket[root@ubuntu2204 ~]#systemctl status libvirtd-ro.socket● libvirtd-ro.socket - Libvirt local read-only socket     Loaded: loaded (/lib/systemd/system/libvirtd-ro.socket; enabled; vendor preset: enabled)     Active: active (listening) since Sat 2022-12-31 17:53:24 CST; 17h ago   Triggers: ● libvirtd.service     Listen: /run/libvirt/libvirt-sock-ro (Stream)     CGroup: /system.slice/libvirtd-ro.socketDec 31 17:53:24 ubuntu2204.wang.org systemd[1]: Listening on Libvirt local read-only socket.[root@ubuntu2204 ~]#systemctl status libvirtd○ libvirtd.service - Virtualization daemon     Loaded: loaded (/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)     Active: inactive (dead) since Sun 2023-01-01 11:27:32 CST; 1min 32s agoTriggeredBy: ● libvirtd-ro.socket             ● libvirtd.socket             ● libvirtd-admin.socket       Docs: man:libvirtd(8)             https://libvirt.org    Process: 871 ExecStart=/usr/sbin/libvirtd $LIBVIRTD_ARGS (code=exited, status=0/SUCCESS)   Main PID: 871 (code=exited, status=0/SUCCESS)      Tasks: 2 (limit: 32768)     Memory: 57.1M        CPU: 9.370s     CGroup: /system.slice/libvirtd.service             ├─987 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/lib/libvirt/libvirt_leaseshelper             └─988 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/lib/libvirt/libvirt_leaseshelperJan 01 11:11:55 ubuntu2204.wang.org dnsmasq-dhcp[987]: DHCPREQUEST(virbr0) 192.168.122.23 52:54:00:18:b7:d2Jan 01 11:11:55 ubuntu2204.wang.org dnsmasq-dhcp[987]: DHCPACK(virbr0) 192.168.122.23 52:54:00:18:b7:d2Jan 01 11:18:35 ubuntu2204.wang.org dnsmasq-dhcp[987]: DHCPREQUEST(virbr0) 192.168.122.23 52:54:00:18:b7:d2Jan 01 11:18:35 ubuntu2204.wang.org dnsmasq-dhcp[987]: DHCPACK(virbr0) 192.168.122.23 52:54:00:18:b7:d2Jan 01 11:27:32 ubuntu2204.wang.org systemd[1]: Stopping Virtualization daemon...Jan 01 11:27:32 ubuntu2204.wang.org systemd[1]: libvirtd.service: Deactivated successfully.Jan 01 11:27:32 ubuntu2204.wang.org systemd[1]: libvirtd.service: Unit process 987 (dnsmasq) remains running after unit stopped.Jan 01 11:27:32 ubuntu2204.wang.org systemd[1]: libvirtd.service: Unit process 988 (dnsmasq) remains running after unit stopped.Jan 01 11:27:32 ubuntu2204.wang.org systemd[1]: Stopped Virtualization daemon.Jan 01 11:27:32 ubuntu2204.wang.org systemd[1]: libvirtd.service: Consumed 9.370s CPU time.#彻底停掉libvirt服务,发现虚拟机还在运行,只是连接虚拟机的窗口失效[root@ubuntu2204 ~]#systemctl stop libvirtd libvirtd.socket libvirtdadmin.socket libvirtd-ro.socketFailed to stop libvirtdadmin.socket: Unit libvirtdadmin.socket not loaded.[root@ubuntu2204 ~]#virsh list --allerror: failed to connect to the hypervisorerror: Failed to connect socket to "/var/run/libvirt/libvirt-sock": Connection refused[root@ubuntu2204 ~]#ps aux |grep kvmroot        1247  0.0  0.0      0     0 ?        I    10:52   0:02 [kworker/5:0-kvm-irqfd-cleanup]libvirt+    1575  5.8  3.2 3853876 532360 ?      Sl   11:17   0:55 /usr/bin/qemu-system-x86_64 -name guest=centos7-mooreyxia,debug-threads=on -S -object {"qom-type":"secret","id":"masterKey0","format":"raw","file":"/var/lib/libvirt/qemu/domain-2-centos7-mooreyxia/master-key.aes"} -machine pc-q35-6.2,usb=off,vmport=off,dump-guest-core=off,memory-backend=pc.ram -accel kvm -cpu host,migratable=on -m 2048 -object {"qom-type":"memory-backend-ram","id":"pc.ram","size":2147483648} -overcommit mem-lock=off -smp 2,sockets=2,cores=1,threads=1 -uuid 3492dccc-468d-4fe9-8ec8-b4a251dccf00 -no-user-config -nodefaults -chardev socket,id=charmonitor,fd=33,server=on,wait=off -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc,driftfix=slew -global kvm-pit.lost_tick_policy=delay -no-hpet -no-shutdown -global ICH9-LPC.disable_s3=1 -global ICH9-LPC.disable_s4=1 -boot strict=on -device pcie-root-port,port=16,chassis=1,id=pci.1,bus=pcie.0,multifunction=on,addr=0x2 -device pcie-root-port,port=17,chassis=2,id=pci.2,bus=pcie.0,addr=0x2.0x1 -device pcie-root-port,port=18,chassis=3,id=pci.3,bus=pcie.0,addr=0x2.0x2 -device pcie-root-port,port=19,chassis=4,id=pci.4,bus=pcie.0,addr=0x2.0x3 -device pcie-root-port,port=20,chassis=5,id=pci.5,bus=pcie.0,addr=0x2.0x4 -device pcie-root-port,port=21,chassis=6,id=pci.6,bus=pcie.0,addr=0x2.0x5 -device pcie-root-port,port=22,chassis=7,id=pci.7,bus=pcie.0,addr=0x2.0x6 -device pcie-root-port,port=23,chassis=8,id=pci.8,bus=pcie.0,addr=0x2.0x7 -device pcie-root-port,port=24,chassis=9,id=pci.9,bus=pcie.0,multifunction=on,addr=0x3 -device pcie-root-port,port=25,chassis=10,id=pci.10,bus=pcie.0,addr=0x3.0x1 -device pcie-root-port,port=26,chassis=11,id=pci.11,bus=pcie.0,addr=0x3.0x2 -device pcie-root-port,port=27,chassis=12,id=pci.12,bus=pcie.0,addr=0x3.0x3 -device pcie-root-port,port=28,chassis=13,id=pci.13,bus=pcie.0,addr=0x3.0x4 -device pcie-root-port,port=29,chassis=14,id=pci.14,bus=pcie.0,addr=0x3.0x5 -device qemu-xhci,p2=15,p3=15,id=usb,bus=pci.2,addr=0x0 -device virtio-serial-pci,id=virtio-serial0,bus=pci.3,addr=0x0 -blockdev {"driver":"file","filename":"/var/lib/libvirt/images/centos7-mooreyxia.qcow2","node-name":"libvirt-2-storage","auto-read-only":true,"discard":"unmap"} -blockdev {"node-name":"libvirt-2-format","read-only":false,"discard":"unmap","driver":"qcow2","file":"libvirt-2-storage","backing":null} -device virtio-blk-pci,bus=pci.4,addr=0x0,drive=libvirt-2-format,id=virtio-disk0,bootindex=1 -device ide-cd,bus=ide.0,id=sata0-0-0 -netdev tap,fd=34,id=hostnet0,vhost=on,vhostfd=36 -device virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:18:b7:d2,bus=pci.1,addr=0x0 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -chardev socket,id=charchannel0,fd=32,server=on,wait=off -device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=org.qemu.guest_agent.0 -chardev spicevmc,id=charchannel1,name=vdagent -device virtserialport,bus=virtio-serial0.0,nr=2,chardev=charchannel1,id=channel1,name=com.redhat.spice.0 -device usb-tablet,id=input0,bus=usb.0,port=1 -audiodev {"id":"audio1","driver":"spice"} -spice port=5900,addr=127.0.0.1,disable-ticketing=on,image-compression=off,seamless-migration=on -device qxl-vga,id=video0,ram_size=67108864,vram_size=67108864,vram64_size_mb=0,vgamem_mb=16,max_outputs=1,bus=pcie.0,addr=0x1 -device ich9-intel-hda,id=sound0,bus=pcie.0,addr=0x1b -device hda-duplex,id=sound0-codec0,bus=sound0.0,cad=0,audiodev=audio1 -chardev spicevmc,id=charredir0,name=usbredir -device usb-redir,chardev=charredir0,id=redir0,bus=usb.0,port=2 -chardev spicevmc,id=charredir1,name=usbredir -device usb-redir,chardev=charredir1,id=redir1,bus=usb.0,port=3 -device virtio-balloon-pci,id=balloon0,bus=pci.5,addr=0x0 -object {"qom-type":"rng-random","id":"objrng0","filename":"/dev/urandom"} -device virtio-rng-pci,rng=objrng0,id=rng0,bus=pci.6,addr=0x0 -sandbox on,obsolete=deny,elevateprivileges=deny,spawn=deny,resourcecontrol=deny -msg timestamp=onroot        1581  0.0  0.0      0     0 ?        S    11:17   0:00 [kvm-nx-lpage-re]root        1586  0.0  0.0      0     0 ?        S    11:17   0:00 [kvm-pit/1575]root        1781  0.0  0.0   9696  2404 pts/1    S+   11:33   0:00 grep --color=auto kvm#重启libvirtd服务,就可以继续连接kvm进行管理[root@ubuntu2204 ~]#systemctl start libvirtd [root@ubuntu2204 ~]#virsh list --all Id   Name                State------------------------------------ 2    centos7-mooreyxia   running -    rocky8              shut off

virt-manager 管理虚拟机

virt-manager是一个图形化工具,主要功能:

定义和创建虚拟机硬件管理性能监视控制台在线和离线迁移虚拟机的保存和恢复、暂停和继续、关闭和启动

virsh 命令行工具

virsh是使用libvirt managementAPI构建的管理工具,相比virt-manager可以提高效率

#virsh 主要功能[root@ubuntu2204 ~]#virsh --helpvirsh [options]... []virsh [options]...  [args...]options:-c | --connect=URI hypervisor connection URI-d | --debug=NUM debug level [0-4]-e | --escape  set escape sequence for console-h | --help this help-k | --keepalive-interval=NUMkeepalive interval in seconds, 0 for disable-K | --keepalive-count=NUMnumber of possible missed keepalive messages-l | --log=FILE output logging to file-q | --quiet quiet mode-r | --readonly connect readonly-t | --timing print timing information-v short version-V long version--version[=TYPE] version, TYPE is short or long (default short)....Domain Management (help keyword "domain"):Domain Monitoring (help keyword "monitor"):Host and Hypervisor (help keyword "host"):Interface (help keyword "interface"):Network Filter (help keyword "filter"):Networking (help keyword "network"):Node Device (help keyword "nodedev"):Secret (help keyword "secret"):Snapshot (help keyword "snapshot"):Storage Pool (help keyword "pool"):Storage Volume (help keyword "volume"):Virsh itself (help keyword "virsh"):#virsh 子命令说明help #打印基本帮助信息attach-device #使用XML文件中的设备定义在虚拟机中添加设备attach-disk #在虚拟机中附加新磁盘设备attach-interface #在虚拟机中附加新网络接口create #从 XML 配置文件生成虚拟机并启动新虚拟机define #为虚拟机输出XML配置文件destroy #强制虚拟机停止detach-device #从虚拟机中分离设备,使用同样的XML 描述作为命令attach-devicedetach-disk #从虚拟机中分离磁盘设备detach-interface #从虚拟机中分离网络接口domblkstat #显示正在运行的虚拟机的块设备统计domid #显示虚拟机IDdomifstat #显示正在运行的虚拟机的网络接口统计dominfo #显示虚拟机信息domname #显示虚拟机名称domstate #显示虚以机状态domuuid #显示虚拟机UUIDdumpxml #输出虚拟机 XML配置文件list #列出所有虚拟机migrate #将虚拟机迁移到另一台主机中nodeinfo #有关管理程序的输出信息quit #退出这个互动终端reboot #重新启动虚拟机restore #恢复以前保存在文件中的虚拟机resume #恢复暂停的虚拟机save #将虚拟机当前状态保存到某个文件中setmaxmem #为管理程序设定内存上限setmem #为虚拟机设定分配的内存setvcpus #修改为虚拟机分配的虚拟CPU数目shutdown #关闭某个虚拟机start #启动未激活的虚拟机suspend #暂停虚拟机undefine #删除与虚拟机关联的所有文件vepuinfo #显示虚以机的虚拟CPU信息vcpupin #控制虚拟机的虚拟CPU亲和性version #显示virsh版本#查看子命令 list 命令用法[root@ubuntu2204 ~]#virsh help list  NAME    list - list domains  SYNOPSIS    list [--inactive] [--all] [--transient] [--persistent] [--with-snapshot] [--without-snapshot] [--with-checkpoint] [--without-checkpoint] [--state-running] [--state-paused] [--state-shutoff] [--state-other] [--autostart] [--no-autostart] [--with-managed-save] [--without-managed-save] [--uuid] [--name] [--id] [--table] [--managed-save] [--title]  DESCRIPTION    Returns list of domains.  OPTIONS    --inactive       list inactive domains    --all            list inactive & active domains    --transient      list transient domains    --persistent     list persistent domains    --with-snapshot  list domains with existing snapshot    --without-snapshot  list domains without a snapshot    --with-checkpoint  list domains with existing checkpoint    --without-checkpoint  list domains without a checkpoint    --state-running  list domains in running state    --state-paused   list domains in paused state    --state-shutoff  list domains in shutoff state    --state-other    list domains in other states    --autostart      list domains with autostart enabled    --no-autostart   list domains with autostart disabled    --with-managed-save  list domains with managed save state    --without-managed-save  list domains without managed save    --uuid           list uuid"s only    --name           list domain names only    --id             list domain IDs only    --table          list table (default)    --managed-save   mark inactive domains with managed save state    --title          show domain title例:[root@ubuntu2204 ~]#virsh list --all Id   Name                State------------------------------------ 2    centos7-mooreyxia   running -    rocky8              shut off[root@ubuntu2204 ~]#virsh start rocky8Domain "rocky8" started[root@ubuntu2204 ~]#virsh list --all Id   Name                State----------------------------------- 2    centos7-mooreyxia   running 3    rocky8              running[root@ubuntu2204 ~]#virsh shutdown 3Domain "3" is being shutdown#列出开机状态虚拟机的UUID和名称[root@ubuntu2204 ~]#virsh list --uuid --name3492dccc-468d-4fe9-8ec8-b4a251dccf00 centos7-mooreyxia3a984e57-70f4-428d-8f30-3127dd4e9be2 rocky8

案例:查看虚拟机UUID,通过UUID启动关闭虚拟机

[root@ubuntu2204 ~]#virsh list --all Id   Name                State----------------------------------- 2    centos7-mooreyxia   running 3    rocky8              running[root@ubuntu2204 ~]#virsh domuuid 33a984e57-70f4-428d-8f30-3127dd4e9be2[root@ubuntu2204 ~]#virsh destroy 3a984e57-70f4-428d-8f30-3127dd4e9be2Domain "3a984e57-70f4-428d-8f30-3127dd4e9be2" destroyed[root@ubuntu2204 ~]#virsh list --all Id   Name                State------------------------------------ 2    centos7-mooreyxia   running -    rocky8              shut off

案例:暂停和恢复虚拟机

[root@ubuntu2204 ~]#virsh list Id   Name                State----------------------------------- 2    centos7-mooreyxia   running[root@ubuntu2204 ~]#virsh suspend centos7-mooreyxiaDomain "centos7-mooreyxia" suspended[root@ubuntu2204 ~]#virsh list Id   Name                State---------------------------------- 2    centos7-mooreyxia   paused[root@ubuntu2204 ~]#ps aux|grep kvmroot        1247  0.0  0.0      0     0 ?        I    10:52   0:02 [kworker/5:0-kvm-irqfd-cleanup]libvirt+    1575  2.3  3.3 3862072 550556 ?      Sl   11:17   1:13 /usr/bin/qemu-system-x86_64 -name guest=centos7-mooreyxia,debug-threads=on -S -object {"qom-type":"secret","id":"masterKey0","format":"raw","file":"/var/lib/libvirt/qemu/domain-2-centos7-mooreyxia/master-key.aes"} -machine pc-q35-6.2,usb=off,vmport=off,dump-guest-core=off,memory-backend=pc.ram -accel kvm -cpu host,migratable=on -m 2048 -object {"qom-type":"memory-backend-ram","id":"pc.ram","size":2147483648} -overcommit mem-lock=off -smp 2,sockets=2,cores=1,threads=1 -uuid 3492dccc-468d-4fe9-8ec8-b4a251dccf00 -no-user-config -nodefaults -chardev socket,id=charmonitor,fd=33,server=on,wait=off -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc,driftfix=slew -global kvm-pit.lost_tick_policy=delay -no-hpet -no-shutdown -global ICH9-LPC.disable_s3=1 -global ICH9-LPC.disable_s4=1 -boot strict=on -device pcie-root-port,port=16,chassis=1,id=pci.1,bus=pcie.0,multifunction=on,addr=0x2 -device pcie-root-port,port=17,chassis=2,id=pci.2,bus=pcie.0,addr=0x2.0x1 -device pcie-root-port,port=18,chassis=3,id=pci.3,bus=pcie.0,addr=0x2.0x2 -device pcie-root-port,port=19,chassis=4,id=pci.4,bus=pcie.0,addr=0x2.0x3 -device pcie-root-port,port=20,chassis=5,id=pci.5,bus=pcie.0,addr=0x2.0x4 -device pcie-root-port,port=21,chassis=6,id=pci.6,bus=pcie.0,addr=0x2.0x5 -device pcie-root-port,port=22,chassis=7,id=pci.7,bus=pcie.0,addr=0x2.0x6 -device pcie-root-port,port=23,chassis=8,id=pci.8,bus=pcie.0,addr=0x2.0x7 -device pcie-root-port,port=24,chassis=9,id=pci.9,bus=pcie.0,multifunction=on,addr=0x3 -device pcie-root-port,port=25,chassis=10,id=pci.10,bus=pcie.0,addr=0x3.0x1 -device pcie-root-port,port=26,chassis=11,id=pci.11,bus=pcie.0,addr=0x3.0x2 -device pcie-root-port,port=27,chassis=12,id=pci.12,bus=pcie.0,addr=0x3.0x3 -device pcie-root-port,port=28,chassis=13,id=pci.13,bus=pcie.0,addr=0x3.0x4 -device pcie-root-port,port=29,chassis=14,id=pci.14,bus=pcie.0,addr=0x3.0x5 -device qemu-xhci,p2=15,p3=15,id=usb,bus=pci.2,addr=0x0 -device virtio-serial-pci,id=virtio-serial0,bus=pci.3,addr=0x0 -blockdev {"driver":"file","filename":"/var/lib/libvirt/images/centos7-mooreyxia.qcow2","node-name":"libvirt-2-storage","auto-read-only":true,"discard":"unmap"} -blockdev {"node-name":"libvirt-2-format","read-only":false,"discard":"unmap","driver":"qcow2","file":"libvirt-2-storage","backing":null} -device virtio-blk-pci,bus=pci.4,addr=0x0,drive=libvirt-2-format,id=virtio-disk0,bootindex=1 -device ide-cd,bus=ide.0,id=sata0-0-0 -netdev tap,fd=34,id=hostnet0,vhost=on,vhostfd=36 -device virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:18:b7:d2,bus=pci.1,addr=0x0 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -chardev socket,id=charchannel0,fd=32,server=on,wait=off -device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=org.qemu.guest_agent.0 -chardev spicevmc,id=charchannel1,name=vdagent -device virtserialport,bus=virtio-serial0.0,nr=2,chardev=charchannel1,id=channel1,name=com.redhat.spice.0 -device usb-tablet,id=input0,bus=usb.0,port=1 -audiodev {"id":"audio1","driver":"spice"} -spice port=5900,addr=127.0.0.1,disable-ticketing=on,image-compression=off,seamless-migration=on -device qxl-vga,id=video0,ram_size=67108864,vram_size=67108864,vram64_size_mb=0,vgamem_mb=16,max_outputs=1,bus=pcie.0,addr=0x1 -device ich9-intel-hda,id=sound0,bus=pcie.0,addr=0x1b -device hda-duplex,id=sound0-codec0,bus=sound0.0,cad=0,audiodev=audio1 -chardev spicevmc,id=charredir0,name=usbredir -device usb-redir,chardev=charredir0,id=redir0,bus=usb.0,port=2 -chardev spicevmc,id=charredir1,name=usbredir -device usb-redir,chardev=charredir1,id=redir1,bus=usb.0,port=3 -device virtio-balloon-pci,id=balloon0,bus=pci.5,addr=0x0 -object {"qom-type":"rng-random","id":"objrng0","filename":"/dev/urandom"} -device virtio-rng-pci,rng=objrng0,id=rng0,bus=pci.6,addr=0x0 -sandbox on,obsolete=deny,elevateprivileges=deny,spawn=deny,resourcecontrol=deny -msg timestamp=onroot        1581  0.0  0.0      0     0 ?        S    11:17   0:00 [kvm-nx-lpage-re]root        1586  0.0  0.0      0     0 ?        S    11:17   0:00 [kvm-pit/1575]root        2244  0.0  0.0   9696  2268 pts/1    S+   12:09   0:00 grep --color=auto kvm[root@ubuntu2204 ~]#virsh resume centos7-mooreyxiaDomain "centos7-mooreyxia" resumed[root@ubuntu2204 ~]#virsh list Id   Name                State----------------------------------- 2    centos7-mooreyxia   running

案例:配置虚拟机开机自动启动

在virt-manager图形界面中可以设置宿主机启动时虚拟机自动开启

xml是这个虚拟机的所有配置文本

查看虚拟机配置

#未勾选开机启动时[root@ubuntu2204 ~]#virsh list --all Id   Name                State------------------------------------ 2    centos7-mooreyxia   running -    rocky8              shut off#每个虚拟机配置都存放在/etc/libvirt/qemu目录下的xml文件中[root@ubuntu2204 ~]#ll /etc/libvirt/qemu/total 28drwxr-xr-x 3 root root 4096 Dec 31 00:18 ./drwxr-xr-x 7 root root 4096 Dec 29 20:24 ../-rw------- 1 root root 7673 Dec 29 21:36 centos7-mooreyxia.xmldrwxr-xr-x 3 root root 4096 Dec 29 19:10 networks/-rw------- 1 root root 7078 Dec 30 22:44 rocky8.xml#查看XML配置文件[root@ubuntu2204 ~]#cat /etc/libvirt/qemu/centos7-mooreyxia.xml   centos7-mooreyxia  3492dccc-468d-4fe9-8ec8-b4a251dccf00                    2097152  2097152  2      hvm                                          destroy  restart  destroy                  /usr/bin/qemu-system-x86_64                            
...

勾选开机启动项看看会发生什么

#设置虚拟机随宿主机启动而自动启动,本质就是在下面目录生成软链接-------------------------------------#图形界面操作可以用命令代替virsh autostart centos7-mooreyxia----------------------------------------#生成了autostart/centos7-mooreyxia.xml 软链接指向虚拟机配置文件[root@ubuntu2204 ~]#ll /etc/libvirt/qemu/total 32drwxr-xr-x 4 root root 4096 Jan  1 12:24 ./drwxr-xr-x 7 root root 4096 Dec 29 20:24 ../drwxr-xr-x 2 root root 4096 Jan  1 12:24 autostart/-rw------- 1 root root 7673 Jan  1 12:24 centos7-mooreyxia.xmldrwxr-xr-x 3 root root 4096 Dec 29 19:10 networks/-rw------- 1 root root 7078 Dec 30 22:44 rocky8.xml[root@ubuntu2204 ~]#ll /etc/libvirt/qemu/autostart/total 8drwxr-xr-x 2 root root 4096 Jan  1 12:24 ./drwxr-xr-x 4 root root 4096 Jan  1 12:24 ../lrwxrwxrwx 1 root root   39 Jan  1 12:24 centos7-mooreyxia.xml -> /etc/libvirt/qemu/centos7-mooreyxia.xml

案例:删除虚拟机配置

[root@centos8 ~]#virsh list --allId Name State----------------------------------------------------2 centos8 running- centos7 shut off- centos8-vm2 shut off- centos8-vm3 shut off- centos8-vm4 shut off- Win_2008_r2-x86_64 shut off#删除虚拟机配置,但不删除磁盘文件[root@centos8 ~]#virsh undefine centos8-vm4Domain centos8-vm4 has been undefined[root@centos8 ~]#virsh list --allId Name State----------------------------------------------------2 centos8 running- centos7 shut off- centos8-vm2 shut off- centos8-vm3 shut off- Win_2008_r2-x86_64 shut off#对应虚拟机xml的配置文件被删除[root@centos8 ~]#ll /etc/libvirt/qemu/total 28drwxr-xr-x 2 root root 25 Sep 17 18:54 autostart-rw------- 1 root root 3618 Sep 17 15:14 centos7.xml-rw------- 1 root root 3673 Sep 13 22:43 centos8-vm2.xml-rw------- 1 root root 3601 Sep 13 22:42 centos8-vm3.xml-rw------- 1 root root 6024 Sep 17 15:47 centos8.xmldrwx------ 3 root root 42 Sep 13 19:03 networks-rw------- 1 root root 5369 Sep 17 11:31 Win_2008_r2-x86_64.xml#对应的磁盘文件并没有删除[root@centos8 ~]#ls /var/lib/libvirt/images/centos7.qcow2 centos8.qcow2 centos8-vm2.qcow2 centos8-vm3.qcow2 centos8-vm4.qcow2 Windows-2008_r2-x86_64.qcow2--------------------------------------------------------#删除虚拟机包括磁盘文件[root@ubuntu2004 ~]#virsh undefine k8s-node-05 --remove-all-storageDomain k8s-node-05 has been undefinedVolume "vda"(/var/lib/libvirt/images/k8s-node-05.qcow2) removed.

案例:冷迁移虚拟机

将一个宿主机的虚拟机迁移到另一台宿主机,注意: 不支持Ubuntu和Rocky8宿主机之间迁移

对下面两个文件进行迁移即可

/etc/libvirt/qemu/xxx.xml/var/lib/libvirt/images/xxx.qcow2
#在一台目标宿主机安装相关虚拟化软件[root@ubuntu2004 ~]#apt update[root@ubuntu2004 ~]#apt -y install qemu-kvm virt-manager libvirt-daemon-system#在源宿主机查看虚拟机的相关文件[root@ubuntu2004 ~]#virsh dumpxml --domain rocky8-template...........#或者#查看块设备[root@ubuntu2004 ~]#virsh domblklist rocky8-templateTarget Source------------------------------------------------vda /var/lib/libvirt/images/rocky8-template.qcow2vdb -#复制源宿主机上的虚拟机的两个文件到目标宿主机[root@ubuntu2004 ~]#scp /etc/libvirt/qemu/rocky8-template.xml10.0.0.101:/etc/libvirt/qemu/[root@ubuntu2004 ~]#scp /var/lib/libvirt/images/rocky8-template.qcow210.0.0.101:/var/lib/libvirt/images#在目标宿主机不重启服务无法看到新的虚拟机[root@ubuntu2004 ~]#virsh list --all[root@ubuntu2004 ~]#systemctl restart libvirtd#在目标宿主机重启服务后看到新的虚拟机[root@ubuntu2004 ~]#virsh list --allId Name State----------------------------------rocky8-template shut off

我是moore,新年快乐!!!

标签: 配置文件 网络接口 磁盘文件

上一篇:天天信息:基于双栈服务的启发式测绘
下一篇:JavaScript奇淫技巧:反调试