八、KVM虚拟机相关命令

  作者: thtomatic  分类: KVM   评论: [ 0 ] 条  浏览: [ 915 ] 次

两种磁盘文件是可以相互转换的

命令:

qemu-img convert -f raw centos.img -O qcow2 centos.qcow2
参数说明:convert 将磁盘文件转换为指定格式的文件


                    -f 指定需要转换文件的文件格式

                    -O 指定要转换的目标格式

                    转换完成后,将产生一个目标的映像文件,原文件仍保存

回顾:

        创建KVM虚拟机CentOS

        命令:virt-install --name CentOS-7-x86_64 --virt-type kvm --ram 1024 --                cdrom=/opt/CentOS-7.2.iso --disk path=/opt/CentOS-7.2-x86_64.raw --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole

        virsh-install 参数

        --virt-type     指定虚拟机类型(KVM、qemu、xen)

        --name          指定虚拟机的名称

        --raw             指定内存大小

        --cpu             指定CPU的核数(默认1)

        --cdrom         指定镜像

        --disk             指定磁盘路径(既上文创建的虚拟磁盘)

        --network      指定网络类型

        --graphics      配置虚拟机像是设置比如VNC

        --noautoconsole    不要自动尝试连接到客户端控制台

查看创建的虚拟机

    查看运行的虚拟机(仅是运行中)

    命令:vish list 

查看所有的虚拟机(运行和关闭)

命令:virsh list --all

1.png


virsh命令参数

注:这里所说的域名值在创建虚拟机时的命令 --name 后面的

查看运行的虚拟机

virsh list

查看所有的虚拟机(关闭和运行的虚拟机)

virsh list --all

关闭虚拟机(强制关机)

virsh destroy+域名

这种方式的关闭,是一种删除的方式,只是在virsh list中删除了该虚拟机

挂起虚拟机

virsh suspend + 域名

恢复挂起的虚拟机

virsh resume +域名

子机随宿主机启动而启动(小鸡随母鸡的启动而启动)

virsh autostart +域名

取消自动启动

virsh autostart --disable +域名

删除创建的虚拟机

virsh undefine +域名

启动虚拟机

virsh start +域名

关闭虚拟机

virsh shutdown +域名

编辑虚拟机配置文件(使用方法与vi编辑器一样)

virsh edit +域名



查看虚拟机监听的端口

命令:netstat -anlp | grep 5900 

一个虚拟机占用一个端口,从5900端口开始。第一个运行的虚拟机使用5900,第二个使用5901,第三个使用5902.......

使用VNC连接需要指明端口


虚拟机配置文件

虚拟机配置文件,就是xml文件所在目录是:etc/libvirt/qume/

2.png

配置文件:

<!--
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 CentOS-7-x86-64
or other application using the libvirt API.
-->

<domain type='kvm'>
  <name>CentOS-7-x86-64</name>
  <uuid>b9e050f4-d568-41ef-9a1d-f54bdf02b450</uuid>
  <memory unit='KiB'>1048576</memory>
  <currentMemory unit='KiB'>1048576</currentMemory>
  <vcpu placement='static'>1</vcpu>
  <os>
    <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
  </features>
  <cpu mode='custom' match='exact'>
    <model fallback='allow'>Nehalem</model>
  </cpu>
  <clock offset='utc'>
    <timer name='rtc' tickpolicy='catchup'/>
    <timer name='pit' tickpolicy='delay'/>
    <timer name='hpet' present='no'/>
  </clock>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <pm>
    <suspend-to-mem enabled='no'/>
    <suspend-to-disk enabled='no'/>
  </pm>
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/opt/CentOS-7.2-x86_64.raw'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
    </disk>
    <disk type='block' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <target dev='hda' bus='ide'/>
      <readonly/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>
    <controller type='usb' index='0' model='ich9-ehci1'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x7'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci1'>
      <master startport='0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0' multifunction='on'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci2'>
      <master startport='2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x1'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci3'>
      <master startport='4'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' 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='0x04' function='0x0'/>
    </controller>
    <interface type='network'>
      <mac address='52:54:00:bb:23:a7'/>
      <source network='default'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <serial type='pty'>
      <target port='0'/>
    </serial>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
    <channel type='unix'>
      <source mode='bind' path='/var/lib/libvirt/qemu/channel/target/domain-CentOS-7-x86-64/org.qemu.guest_agent.0'/>
      <target type='virtio' name='org.qemu.guest_agent.0'/>
      <address type='virtio-serial' controller='0' bus='0' port='1'/>
    </channel>
    <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'>
      <listen type='address' address='0.0.0.0'/>
    </graphics>
    <video>
      <model type='cirrus' vram='16384' heads='1'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <memballoon model='virtio'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
    </memballoon>
  </devices>
</domain>




版权所有:《thtomatic》 => 《八、KVM虚拟机相关命令
本文地址:https://ask.mykeji.net/kvm/kvm_8.html
除非注明,文章均为 《简单记录》 原创,欢迎转载!转载请注明本文地址,谢谢。


发表评论:

    13.30ms