通過virsh console進入虛擬機


 virsh創建虛擬機

1、virsh啟動一個虛擬機。

#!/bin/bash
#以下信息是必須編輯的,確保信息不與其他資源沖突
uuid="0586b67a-92b2-4751-ae2d-9d11ca2a9322"
name="lyq-vm"
image_path="/home/liuyq/CentOS6U5_GLOBAL_64bit_75G_20180129_APP.qcow2"
mac="fa:16:3f:11:3d:ff"
bridge_name="lyq-br"
#以下信息默認值,可不編輯
user="root"
new_password="aaaaaa"
image_type="qcow2"
work_dir="./"
if [ -f ${name}.xml ];then
    echo "file ${name}.xml already exist"
    exit 1
fi
cat > $name.xml <<EOF
<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
  virsh edit instance-0000037d
or other application using the libvirt API.
-->

<domain type='kvm'>
    <name>$name</name>
  <uuid>$uuid</uuid>
  <memory unit='KiB'>4194304</memory>
  <currentMemory unit='KiB'>4194304</currentMemory>
  <vcpu placement='static' cpuset='0-7,16-23'>4</vcpu>
  <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'>
    <model fallback='allow'/>
    <topology sockets='4' cores='1' threads='1'/>
  </cpu>
  <clock offset='utc'>
    <timer name='pit' tickpolicy='delay'/>
    <timer name='rtc' tickpolicy='catchup'/>
    <timer name='hpet' present='no'/>
  </clock>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>destroy</on_crash>
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='$image_type' cache='none'/>
      <source file='$image_path'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </disk>
    <controller type='usb' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' 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>
    <channel type='unix'>
      <source mode='bind' path='/var/lib/libvirt/qemu/org.qemu.guest_agent.0'/>
      <target type='virtio' name='org.qemu.guest_agent.0'/>
    </channel>
    <interface type='bridge'>
      <mac address='$mac'/>
      <source bridge='$bridge_name'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <input type='tablet' bus='usb'/>
    <input type='mouse' bus='ps2'/>
    <input type='keyboard' bus='ps2'/>
    <graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0' keymap='en-us'>
      <listen type='address' address='0.0.0.0'/>
    </graphics>
    <memballoon model='virtio'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
      <stats period='10'/>
    </memballoon>
  </devices>
</domain>
EOF
virsh define ${work_dir}${name}.xml
virsh start $name
virsh vncdisplay $name
#可通過vnc viewer來查看虛擬機是否已經啟動
while :
do
echo "waiting vm start,make sure vm is running y/n?"
read vm_status
if [ $vm_status != "y" ];then
    echo "sleep 10s,wait vm start"
    sleep 10
else
    virsh set-user-password $name $user $new_password
    if [ $? -ne 0 ];then
        result="failed"
    else
        result="succeed"
    fi
    break
fi
done
echo -e "==========\nqemu-guest-agent test ${result}\n=========="
echo "clean up the resource y/n?"
read clean
if [ $clean == "y" ];then
    rm -f ${name}.xml
    virsh destroy $name
    sleep 1
    virsh undefine $name
fi

執行腳本test_qga.sh

 

2、virsh vncdisplay <vm_ID>

3、vnc登錄到vm里面,執行
#systemctl start serial-getty@ttyS0.service
#systemctl enable serial-getty@ttyS0.service

4、返回宿主機,進入cd /etc/libvirt/qemu目錄,備份<vm_name>.xml為<vm_name>.xml.bak

5、
virsh destroy <vm_name>

6、
virsh undefine <vm_name>

7、進入cd /etc/libvirt/qemu目錄,
編輯<vm_name>.xml.bak,在<devices> </devices>之間加入

<console type='pty'>
<target type='serial' port='0'/>
</console>

mv <vm_name>.xml.bak為<vm_name>.xml

8、在/etc/libvirt/qemu目錄,執行
virsh define <vm_name>.xml

9、

virsh start <vm_name>

10、

virsh console <vm_name>

 

virsh創建/關閉/停止虛擬機常用的幾條指令

定義:virsh definexxx.xml xxx為xml文件所在的路徑及文件名稱,在當前目錄下則不寫路徑

啟動:virsh start xyz xyz為虛擬機xml配置文件中虛擬機的名字<name>rhel6.2_2</name>

停止:virsh shutdownxyz 此方法為正常關機方法,需要一段才能關機

下電:virsh destroy xyz 此方法為暴力下電,虛擬機立即關閉

刪除:virsh undefinexxx 關閉了的虛擬機,只是不在運行狀態而已,通過virsh undefine xxx就能從virsh列表里面(virsh list查看當前系統中的虛擬機列表,詳見第2.4節)將其刪除,undefine命令不會刪除鏡像文件和xml文件。運行狀態的虛擬機是不能刪除的。

臨時起虛擬機:virsh create xxx.xml 此方法為方便開發調試等臨時需求,不會持久化,虛擬機關機后就消失了,不推薦生產系統使用。

查看VNC端口:virshvncdisplay xx 查看VNC端口,其中xx可通過virsh list查看


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM