@查看虛擬機
virsh list 查看運行的虛擬機
virsh list --all 查看所有定義了的虛擬機,包括沒有運行的
說明:在所有需要名稱作爲參數的virsh命令中,可以用ID號代替虛擬機的名稱,但ID號是不固定的,每次啓動虛擬機會改變,並且只有運行中的虛擬機有ID。
ps -ef | grep -v grep | grep qemu 查看當前Qemu進程。
說明:每個虛擬機都是一個Qemu進程,當虛擬機遇到問題無法關閉時,可以通過kill的方式幹掉它。
@virt-manager打開管理控制檯
@virsh命令創建、啓動、關機、銷燬虛擬機
(定義) virsh define <vmname>.xml -> 如果xml文件不在當前路徑下,xml文件要寫全路徑
(啓動) virsh start <vmname> -> <vmname>爲虛擬機xml配置文件中虛擬機的名字<name>vm001</name>
(停止) virsh shutdown <vmname> -> 此方法爲正常關機方法,需要一段才能關機
(下電) virsh destroy <vmname> -> 此方法爲暴力下電,虛擬機立即關閉
(刪除) virsh undefine <vmname> -> 關閉了的虛擬機,只是不在運行狀態而已,通過virsh undefine xxx就能從virsh列表裏面將其刪除,undefine命令不會刪除鏡像文件和xml文件。運行狀態的虛擬機是不能刪除的。
(臨時起虛擬機) virsh create <vmname>.xml -> 此方法爲方便開發調試等臨時需求,不會持久化,虛擬機關機後就消失了,不推薦生產系統使用。
(設置虛擬機跟隨系統自啓) virsh autostart <vmname>
(關閉虛擬機自啓) virsh autostart --disable <vmname>
[例] virsh相關命令舉例
1)virsh list 查看運行的虛擬機
[root@localhost ~]# virsh list
Id Name State
----------------------------------------------------
3 vm001 running
8 vm002 running
9 vm003 running
10 vm004 running
[root@localhost ~]#
2)virsh list --all 查看所有定義了的虛擬機
[root@localhost ~]# virsh list --all
Id Name State
----------------------------------------------------
3 vm001 running
8 vm002 running
9 vm003 running
10 vm004 running
[root@localhost ~]#
3)virsh shutdown <vmname> 正常關機方法,需要一段時間才能關機
[root@localhost ~]# virsh shutdown vm002
Domain vm002 is being shutdown
[root@localhost ~]# virsh list --all
Id Name State
----------------------------------------------------
3 vm001 running
9 vm003 running
10 vm004 running
- vm002 shut off
[root@localhost ~]# virsh list
Id Name State
----------------------------------------------------
3 vm001 running
9 vm003 running
10 vm004 running
[root@localhost ~]#
4)virsh start <vmname> 啓動虛擬機,其中vmname是xml配置文件中虛擬機的名字<name>vm001</name>
[root@localhost ~]# virsh start vm002
Domain vm002 started
[root@localhost ~]# virsh list
Id Name State
----------------------------------------------------
3 vm001 running
9 vm003 running
10 vm004 running
11 vm002 running
[root@localhost ~]#
5)virsh destroy <vmname> 暴力下電,虛擬機立即關閉
[root@localhost ~]# virsh destroy vm002
Domain vm002 destroyed
[root@localhost ~]# virsh list
Id Name State
----------------------------------------------------
3 vm001 running
9 vm003 running
10 vm004 running
[root@localhost ~]# virsh list --all
Id Name State
----------------------------------------------------
3 vm001 running
9 vm003 running
10 vm004 running
- vm002 shut off
[root@localhost ~]# virsh start vm002
Domain vm002 started
[root@localhost ~]#
5)關閉了的虛擬機,只是不在運行狀態而已,通過virsh undefine <vmname> 將其從virsh列表中刪除
說明:undefine命令不會刪除鏡像文件和xml文件,不會刪除運行狀態的虛擬機
[root@localhost ~]# virsh undefine vm002
Domain vm002 has been undefined
[root@localhost ~]# virsh list --all
Id Name State
----------------------------------------------------
3 vm001 running
9 vm003 running
10 vm004 running
12 vm002 running
[root@localhost ~]# virsh list
Id Name State
----------------------------------------------------
3 vm001 running
9 vm003 running
10 vm004 running
12 vm002 running
[root@localhost ~]# virsh shutdown vm002
Domain vm002 is being shutdown
[root@localhost ~]# virsh undefine vm002
error: failed to get domain 'vm002'
error: Domain not found: no domain with matching name 'vm002'
[root@localhost ~]# virsh list --all
Id Name State
----------------------------------------------------
3 vm001 running
9 vm003 running
10 vm004 running
[root@localhost ~]#
6)virsh create <vmname>.xml 該方法創建虛擬機不會持久化,關機後就消失了,不推薦生產系統使用
[root@localhost ~]# virsh create vm002.xml
Domain vm002 created from vm002.xml
[root@localhost ~]# virsh list --all
Id Name State
----------------------------------------------------
3 vm001 running
9 vm003 running
10 vm004 running
13 vm002 running
[root@localhost ~]# virsh shutdown vm002.xml
error: failed to get domain 'vm002.xml'
error: Domain not found: no domain with matching name 'vm002.xml'
[root@localhost ~]# virsh destroy vm002.xml
error: failed to get domain 'vm002.xml'
error: Domain not found: no domain with matching name 'vm002.xml'
[root@localhost ~]# virsh list --all
Id Name State
----------------------------------------------------
3 vm001 running
9 vm003 running
10 vm004 running
13 vm002 running
[root@localhost ~]# virsh undefine vm002
error: Failed to undefine domain vm002
error: Requested operation is not valid: cannot undefine transient domain
[root@localhost ~]# ssh [email protected]
The authenticity of host '10.123.123.123 (10.123.123.123)' can't be established.
ECDSA key fingerprint is SHA256:N9O+aFQrSPZvdkYyuLBXY9d169fXKYBEDZm3TXYVeSM.
ECDSA key fingerprint is MD5:04:92:1a:95:9c:96:00:11:42:12:33:94:6f:b8:a3:e4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.123.123.123' (ECDSA) to the list of known hosts.
[email protected]'s password:
Last login: Tue Jun 16 11:22:17 2020
[root@vm002 ~]# shutdown -h now
Connection to 10.123.123.123 closed by remote host.
Connection to 10.123.123.123 closed.
[root@localhost ~]#
[root@localhost ~]# virsh list --all
Id Name State
----------------------------------------------------
3 vm001 running
9 vm003 running
10 vm004 running
[root@localhost ~]#
7)virsh define <vmname>.xml 創建虛擬機,如果xml文件不在當前路徑下,要寫全路徑
[root@localhost ~]# virsh define vm002.xml
Domain vm002 defined from vm002.xml
[root@localhost ~]# virsh list --all
Id Name State
----------------------------------------------------
3 vm001 running
9 vm003 running
10 vm004 running
- vm002 shut off
[root@localhost ~]# virsh start vm002
Domain vm002 started
[root@localhost ~]# virsh list --all
Id Name State
----------------------------------------------------
3 vm001 running
9 vm003 running
10 vm004 running
14 vm002 running
[root@localhost ~]#
@KVM刪除虛擬機具體步驟
virsh list 查看要刪除的虛擬機
virsh destroy <vm to del> 強制停止虛擬機
virsh undefine <vm to del> 刪除虛擬機
updatedb 更新當前文件
locate <vm to del> 查找包含要刪除虛擬機的所有內容
rm -rf xxx 刪除相關虛擬機的一切內容
updatedb 更新當前文件
locate <vm to del> 確保要刪除虛擬機的相關文件徹底刪除
@虛擬機XML文件
<!-- kvm guest 定義開始 -->
<domain type='kvm'>
<!-- guest的short name,由字母和數字組成,不能包含空格 -->
<name>vm001</name>
<!-- uuid,由命令行工具 uuidgen生成 -->
<uuid>8ef72578-ca8a-4bd9-a6c4-ec4d5af7e770</uuid>
<!-- 在不reboot guest的情況下,guset可以使用的最大內存,以KB爲單位 -->
<memory unit='KiB'>24576000</memory>
<!-- uguest啓動時內存,可以通過virsh setmem來調整內存,但不能大於最大可使用內存。 -->
<currentMemory unit='KiB'>24576000</currentMemory>
<!-- 分配的虛擬cpu -->
<vcpu placement='static'>4</vcpu>
<!-- 關於OS,架構: i686、x86_64; machine: 宿主機的操作系統
boot:指定啓動設備,可以重複多行,指定不同的值,作爲一個啓動設備列表。 -->
<os>
<type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
<boot dev='hd'/>
</os>
<!-- 處理器特性 -->
<features>
<acpi/>
<apic/>
</features>
<cpu mode='host-model' check='partial'>
<model fallback='allow'/>
</cpu>
<clock offset='utc'>
<timer name='rtc' tickpolicy='catchup'/>
<timer name='pit' tickpolicy='delay'/>
<timer name='hpet' present='no'/>
</clock>
<!-- 定義了在kvm環境中power off、reboot、crash時的默認動作分別爲destroy和restart。
其它允許的動作包括: preserve,rename-restart。 -->
<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>
<!-- 用於kvm存儲的文件。使用qemu-img命令創建該文件,kvm image的默認目錄爲:/var/lib/libvirt/images/-->
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/var/lib/libvirt/images/vm001.qcow2'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
</disk>
<controller type='usb' index='0' model='ich9-ehci1'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x7'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci1'>
<master startport='0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0' multifunction='on'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci2'>
<master startport='2'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x1'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci3'>
<master startport='4'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x2'/>
</controller>
<controller type='pci' index='0' model='pci-root'/>
<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='0x07' function='0x0'/>
</controller>
<!-- 使用網橋類型,確保每個kvm guest的mac地址唯一 -->
<interface type='bridge'>
<mac address='00:50:56:00:00:0b'/>
<source bridge='br0'/>
<model type='e1000'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<interface type='bridge'>
<mac address='52:54:00:7d:af:4c'/>
<source bridge='br0'/>
<model type='e1000'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</interface>
<serial type='pty'>
<target type='isa-serial' port='0'>
<model name='isa-serial'/>
</target>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<channel type='spicevmc'>
<target type='virtio' name='com.redhat.spice.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'/>
<graphics type='spice' autoport='yes'>
<listen type='address'/>
<image compression='off'/>
</graphics>
<sound model='ich6'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</sound>
<video>
<model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1' primary='yes'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<redirdev bus='usb' type='spicevmc'>
<address type='usb' bus='0' port='2'/>
</redirdev>
<redirdev bus='usb' type='spicevmc'>
<address type='usb' bus='0' port='3'/>
</redirdev>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>
</memballoon>
</devices>
</domain>
創建啓動虛擬機後可以找到以下文件
[root@localhost ~]# locate vm001
/etc/libvirt/qemu/vm001.xml
/var/vm001
/var/lib/libvirt/images/vm001.qcow2
/var/lib/libvirt/qemu/domain-9-vm001
/var/lib/libvirt/qemu/channel/target/domain-9-vm001
/var/lib/libvirt/qemu/domain-9-vm001/monitor.sock
/var/log/libvirt/qemu/vm001.log
[root@localhost ~]#
@關於網卡類型
參考:KVM虛擬機網絡配置 Bridge方式,NAT方式 https://blog.csdn.net/hzhsan/article/details/44098537/
ovirt幾種網卡(e1000, rtl8139,virtio)https://blog.csdn.net/gyunling/article/details/104748662/
CentOS 7下的KVM網卡配置爲千兆網卡 https://blog.csdn.net/weixin_33961829/article/details/86265094
@virt-install
參考:virt-install命令---詳解 https://www.cnblogs.com/saryli/p/11827903.html
@virt-clone
參考:CentOS7.1 KVM虛擬化之虛擬機克隆 https://blog.csdn.net/hnhuangyiyang/article/details/50885755
關於kvm虛擬機的克隆方法總結 https://www.cnblogs.com/5201351/p/4461000.html
參考資料:
virsh常用命令 https://blog.csdn.net/jiahaojie1984/article/details/23425695
KVM虛擬機的xml配置文件 https://blog.csdn.net/liukuan73/article/details/46049579