使用virsh建立虛擬機器
阿新 • • 發佈:2020-09-22
環境:CentOS 16.04 圖形介面
1、首先將防火牆關閉,否則後面使用VNC Viewer無法連線
1 systemctl stop firewalld.service #關閉防火牆 2 systemctl disable firewalld.service #關閉開機自啟
2、編寫xml配置檔案(以centos7-0.xml為例)
1 <domain type='kvm'> 2 <name>centos7-0</name> #虛擬機器名稱 3 <memory unit='KiB'>2048576</memory> #虛擬機器記憶體大小4 <currentMemory unit='KiB'>2048576</currentMemory> #虛擬機器在開機時分配的記憶體大小 5 <vcpu placement='static'>2</vcpu> #cpu個數 6 <os> 7 <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type> 8 <boot dev='cdrom'/> #啟動方式 cdrom為從光碟啟動(ISO映象),後面會再次修改啟動方式9 </os> 10 <features> 11 <acpi/> 12 <apic/> 13 </features> 14 <cpu mode='custom' match='exact'> 15 <model fallback='allow'>Nehalem</model> 16 </cpu> 17 <clock offset='localtime'/> 18 <on_poweroff>destroy</on_poweroff> 19<on_reboot>restart</on_reboot> 20 <on_crash>restart</on_crash> 21 <devices> 22 <emulator>/usr/libexec/qemu-kvm</emulator> 23 <disk type='file' device='disk'> 24 <driver name='qemu' type='qcow2'/> 25 <source file='/opt/image/centos7-0.qcow2'/> #虛擬硬碟 26 <target dev='hda' bus='ide'/> 27 </disk> 28 <disk type='file' device='cdrom'> 29 <source file='/mnt/ISO/CentOS-7-x86_64-DVD-1810.iso'/> #ISO映象路徑 30 <target dev='hdb' bus='ide'/> 31 </disk> 32 <interface type='bridge'> #網路模式為橋接 33 <source network='virbr0' bridge='virbr0'/> #此處要和自己虛擬交換機的配置對應 34 </interface> 35 <input type='tablet' bus='usb'/> 36 <input type='mouse' bus='ps2'/> 37 <input type='keyboard' bus='ps2'/> 38 <graphics type='vnc' port='5905' autopart='no' listen='0.0.0.0' keymap='en-us'/> #5905為埠號 39 </devices> 40 </domain>
關於虛擬硬碟的建立
qemu-img create -f qcow2 /opt/image/centos7-0.qcow2 20G #此處的路徑與上方xml檔案中的硬碟路徑一致
3、建立虛擬機器
virsh define centos7-0.xml #定義虛擬機器 virsh start centos7-0 #啟動虛擬機器
檢視是否啟動
virsh list --all
4、連線VNC Viewer ->宿主機IP:埠號
5、連線以後進入安裝系統介面,安裝完成以後,要將虛擬機器的開機方式改為從硬碟啟動(否則將一直重複系統安裝引導)
virsh shutdown centos7-0 #將虛擬機器關機 // 關機後先檢視一下虛擬機器的狀態,如果還是執行態,則進行強制關機 // virsh destroy centos7-0 virsh undefine centos7-0 #取消定義
然後重新編輯xml檔案
重新建立虛擬機器
virsh define centos7-0.xml virsh start centos7-0