libvirt(virsh命令总结)

virsh回车进入交互式界面:

  version

  pwd

  hostname  显示本节点主机名

  nodeinfo    显示节点信息

  list --all   显示所有云主机

  7种状态:

    running   运行中

    idel     空闲,未运行

    paused   暂停状态

    shutdown 关闭

    crashed  虚拟机崩溃

    dying   垂死状态,但是又没有完全关闭或崩溃

    shutdown <domain>  

    destroy    <domain>  强制关闭虚拟机(相当于直接拨电源)

    start   <domain>  启动虚拟机

    edit  <domain>     编辑该虚拟机的xml文件

    dommemstat <domain> 获取domain的内存状态

    suspend  <domain>      挂起一个正在运行的虚拟机,该虚拟机仍占资源;

    resume  <domain>    从挂起状态恢复一下虚拟机

    vcpuinfo <domain>    显示一些虚拟机的vcpu的信息

    vncdisplay  <domain>    显示vnc监听地址和端口

快照相关:

  snapshot-create <domain> xmlfile   给domain创建一个snapshot,详细内容保存在xmlfile中

  snapshot-current <domain>      显示一个domain的当前的snapshot

  snapshot-list <domain>         显示一个domain的所有的snapshot

  snapshot-revert <domain> snapshot       恢复一个domian到以前的snapshot

  snapshot-delete <domain> snapshot --children   删除一个domain的snapshot

 如何用libvirt远程管理虚拟机?

  要用libvirt连接到超级管理程序,我们需要一个URI,这个URI配合virsh和virt-viewer命令使用,后面可以跟一些可选项,virt-viewer可以调用一些链接参数,例如:virsh -c qemu:///system 当链接到远程机器时,可以定义几种使用的协议:ssh,tcp,tls。当链接到远程机器时,需要使用远程主机的用户和主机名进行链接,如果没有定义链接用户,则会使用本机环境的$USER的用户进行链接,当连接到qemu hypervisor时,接受两种链接类型:system可以有所有的访问权限,session有限制的访问。例如:

  使用full access链接至本机的qemu hypervisor,前面的-c 是为了执行后面的list命令(--connect)

  virsh -c  qemu:///system list

  使用full access链接至远程主机的qemu hypervisor,每次都要输入ssh密码,改成ssh无密码登陆就不需要输入密码了,直接显示结果。

  virsh -c qemu+ssh://tux@mercur/system   直接进入交互virsh模式

  virsh -c qemu+ssh://10.1.1.8/system list   直接显示list后的结果

  其余连接格式如下:

  qemu:///session                      (local access to per-user instance)
  qemu+unix:///session                 (local access to per-user instance)
  qemu:///system                       (local access to system instance)
  qemu+unix:///system                  (local access to system instance)
  qemu://example.com/system            (remote access, TLS/x509)  
  qemu+tcp://example.com/system        (remote access, SASl/Kerberos)
  qemu+ssh:///system   (remote access, SSH tunnelled)

使用libvirt创建kvm虚拟机:
  1、制作虚拟机镜像
    qemu-img create -f qcow2 test.qcow2 10G //格式,名字,大小
  2、下载并复制iso镜像到指定目录(在第3步中创建xml文件中指定)
  3、创建安装配置文件,demo.xml如下,可以根据自己需求更改

 1 <domain type='kvm'>
 2         <name>test_ubuntu</name> //虚拟机名称
 3         <memory>1048576</memory> //最大内存,单位k
 4         <currentMemory>1048576</currentMemory> //可用内存,单位k
 5         <vcpu>8</vcpu> //虚拟cpu个数
 6         <os>
 7           <type arch='x86_64' machine='pc'>hvm</type>
 8           <boot dev='cdrom'/> //光盘启动
 9        </os>
10        <features>
11          <acpi/>
12          <apic/>
13          <pae/>
14        </features>
15        <clock offset='localtime'/>
16        <on_poweroff>destroy</on_poweroff>
17        <on_reboot>restart</on_reboot>
18        <on_crash>destroy</on_crash>
19        <devices>
20          <emulator>/usr/libexec/qemu-kvm</emulator>
21          <disk type='file' device='disk'>
22           <driver name='qemu' type='qcow2'/>
23            <source file='/var/lib/libvirt/images/test.qcow2'/> //目的镜像路径
24            <target dev='hda' bus='ide'/>
25          </disk>
26          <disk type='file' device='cdrom'>
27            <source file='/var/lib/libvirt/images/ubuntu.iso'/> //光盘镜像路径
28            <target dev='hdb' bus='ide'/>
29          </disk>
30         <interface type='bridge'> //虚拟机网络连接方式
31           <source bridge='kvmbr0'/> //当前主机网桥的名称
32           <mac address="00:16:3e:5d:aa:a8"/> //为虚拟机分配mac地址,务必唯一,否则dhcp获得同样ip,引起冲突
33         </interface>
34         <input type='mouse' bus='ps2'/>
35          <graphics type='vnc' port='-1' autoport='yes' listen = '0.0.0.0' keymap='en-us'/>//vnc方式登录,端口号自动分配,自动加1,可以通过virsh vncdisplay来查询
36        </devices>
37      </domain>

  virsh define  demo.xml    //创建虚拟机

  virsh  start    test_ubuntu   //启动虚拟机

  virsh  vncdisplay   test_ubuntu   ////查看虚拟机的vnc端口, 然后就可以通过vnc登录来完成虚拟机的安装

 

 

posted on 2015-01-13 20:18  阳台  阅读(10158)  评论(0编辑  收藏  举报

导航