通过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查看

posted @ 2019-11-19 10:47  一尾  阅读(5584)  评论(0编辑  收藏  举报