virsh 命令行工具

virsh 是使用libvirt managementAPI构建的管理工具,相比virt-manager可以提高效率
virsh 的名称的含义是virtualization shell

1、两种工作模式

  • 交互模式
  • 非交互模式
图片[1]-virsh 命令行工具-李佳程的个人主页
图片[2]-virsh 命令行工具-李佳程的个人主页

2、virsh 主要功能

# virsh help
 Domain Management (help keyword 'domain'):
 Domain Monitoring (help keyword 'monitor'):
 Host and Hypervisor (help keyword 'host'):
 Interface (help keyword 'interface'):
 Network Filter (help keyword 'filter'):
 Networking (help keyword 'network'):
 Node Device (help keyword 'nodedev'):
 Secret (help keyword 'secret'):
 Snapshot (help keyword 'snapshot'):
 Storage Pool (help keyword 'pool'):
 Storage Volume (help keyword 'volume'):
 Virsh itself (help keyword 'virsh'):

3、virsh 命令格式

# virsh --help

virsh [options]... [<command_string>]
virsh [options]... <command> [args...]

 options:
    -c | --connect=URI     hypervisor connection URI
    -d | --debug=NUM       debug level [0-4]
    -e | --escape <char>    set escape sequence for console
    -h | --help             this help
    -k | --keepalive-interval=NUM
                           keepalive interval in seconds, 0 for disable
    -K | --keepalive-count=NUM
                           number of possible missed keepalive messages
    -l | --log=FILE         output logging to file
    -q | --quiet           quiet mode
    -r | --readonly         connect readonly
    -t | --timing           print timing information
    -v                     short version
    -V                     long version
         --version[=TYPE]   version, TYPE is short or long (default short)

4、virsh 子命令说明

help                            #打印基本帮助信息
attach-device                   #使用XML文件中的设备定义在虚拟机中添加设备
attach-disk                     #在虚拟机中附加新磁盘设备
attach-interface                #在虚拟机中附加新网络接口
create                          #从 XML 配置文件生成虚拟机并启动新虚拟机
define                          #为虚拟机输出XML配置文件
destroy #强制虚拟机停止
detach-device                   #从虚拟机中分离设备,使用同样的XML 描述作为命令
attach-device
detach-disk                     #从虚拟机中分离磁盘设备
detach-interface                #从虚拟机中分离网络接口
domblkstat                      #显示正在运行的虚拟机的块设备统计
domid                           #显示虚拟机ID
domifstat                       #显示正在运行的虚拟机的网络接口统计
dominfo                         #显示虚拟机信息
domname                         #显示虚拟机名称
domstate                        #显示虚以机状态
domuuid                         #显示虚拟机UUID
dumpxml                         #输出虚拟机 XML配置文件
list                            #列出所有虚拟机
migrate                         #将虚拟机迁移到另一台主机中
nodeinfo                        #有关管理程序的输出信息
quit                            #退出这个互动终端
reboot                          #重新启动虚拟机
restore                         #恢复以前保存在文件中的虚拟机
resume                          #恢复暂停的虚拟机
save                            #将虚拟机当前状态保存到某个文件中
setmaxmem                       #为管理程序设定内存上限
setmem                          #为虚拟机设定分配的内存
setvcpus                        #修改为虚拟机分配的虚拟CPU数目
shutdown                        #关闭某个虚拟机
start                           #启动未激活的虚拟机
suspend                         #暂停虚拟机
undefine                        #删除与虚拟机关联的所有文件
vepuinfo                        #显示虚以机的虚拟CPU信息
vcpupin                         #控制虚拟机的虚拟CPU亲和性
version                         #显示virsh版本

5、查看子命令帮助

virsh help COMMAND

查看子命令 list 命令用法

[root@kvm ~]# virsh help list
  NAME
    list - list domains

  SYNOPSIS
    list [--inactive] [--all] [--transient] [--persistent] [--with-snapshot] [--without-snapshot] [--state-running] [--state-paused] [--state-shutoff] [--state-other] [--autostart] [--no-autostart] [--with-managed-save] [--without-managed-save] [--uuid] [--name] [--table] [--managed-save] [--title]

  DESCRIPTION
    Returns list of domains.

  OPTIONS
    --inactive       list inactive domains
    --all            list inactive & active domains
    --transient      list transient domains
    --persistent     list persistent domains
    --with-snapshot  list domains with existing snapshot
    --without-snapshot  list domains without a snapshot
    --state-running  list domains in running state
    --state-paused   list domains in paused state
    --state-shutoff  list domains in shutoff state
    --state-other    list domains in other states
    --autostart      list domains with autostart enabled
    --no-autostart   list domains with autostart disabled
    --with-managed-save  list domains with managed save state
    --without-managed-save  list domains without managed save
    --uuid           list uuid's only
    --name           list domain names only
    --table          list table (default)
    --managed-save   mark inactive domains with managed save state
    --title          show domain title

6、启动和关闭虚拟机

# 查看当前启动的虚拟机
[root@kvm ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 9     centos7                        running


# 查看所有虚拟机
[root@kvm ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 9     centos7                        running
 -     Win2008                        shut off

# 启动
[root@kvm ~]# virsh start Win2008
Domain Win2008 started

# 正常关机
[root@kvm ~]# virsh shutdown centos7
Domain centos7 is being shutdown

# 强制关机
[root@kvm ~]# virsh destroy Win2008
Domain Win2008 destroyed

# 列出虚拟机的UUID和名称
[root@kvm ~]# virsh list --uuid --name --all
7b2055a4-0b5a-4536-a694-90488da95f30 centos7
5b6949d2-348d-4109-8cc3-a3b6c48e8673 Win2008 

查看虚拟机UUID,通过UUID启动关闭虚拟机

# 查看虚拟机uuid
[root@kvm ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 9     centos7                        running
 -     Win2008                        shut off

[root@kvm ~]# virsh domuuid 9
7b2055a4-0b5a-4536-a694-90488da95f30

[root@kvm ~]# virsh  destroy 7b2055a4-0b5a-4536-a694-90488da95f30
Domain 7b2055a4-0b5a-4536-a694-90488da95f30 destroyed

[root@kvm ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     centos7                        shut off
 -     Win2008                        shut off

[root@kvm ~]# virsh domuuid Win2008
5b6949d2-348d-4109-8cc3-a3b6c48e8673

[root@kvm ~]# virsh start 5b6949d2-348d-4109-8cc3-a3b6c48e8673
Domain Win2008 started

[root@kvm ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 11    Win2008                        running
 -     centos7                        shut off

7、暂停和恢复虚拟机

[root@kvm ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 11    Win2008                        running

[root@kvm ~]# virsh suspend Win2008
Domain Win2008 suspended

[root@kvm ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 11    Win2008                        paused

# 虚拟机暂停后,宿主机中还存有相关的进程

[root@kvm ~]# virsh  resume Win2008
Domain Win2008 resumed

[root@kvm ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 11    Win2008                        running

8、配置虚拟机开机自动启动

[root@kvm ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 11    Win2008                        running
 -     centos7                        shut off

[root@kvm ~]# virsh autostart Win2008
Domain Win2008 marked as autostarted

[root@kvm ~]# virsh autostart centos7
Domain centos7 marked as autostarted

# 设置虚拟机随宿主机启动而自动启动,本质就是在下面目录生成软链接
[root@kvm ~]# ll /etc/libvirt/qemu/autostart/
total 0
lrwxrwxrwx 1 root root 29 Dec 17 17:29 centos7.xml -> /etc/libvirt/qemu/centos7.xml
lrwxrwxrwx 1 root root 29 Dec 17 15:48 Win2008.xml -> /etc/libvirt/qemu/Win2008.xml

[root@kvm ~]# reboot

[root@kvm ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 1     Win2008                        running
 2     centos7                        running

9、查看虚拟机配置

# 每个虚拟机配置都存放在/etc/libvirt/qemu目录下的xml文件中


[root@kvm ~]# ll /etc/libvirt/qemu
total 16
drwxr-xr-x 2 root root   44 Dec 17 17:29 autostart
-rw------- 1 root root 4275 Dec 17 16:19 centos7.xml
drwx------ 3 root root   42 Dec 17 12:33 networks
-rw------- 1 root root 4300 Dec 17 16:00 Win2008.xml

# 查看指定虚拟机的配置,以下命令相当于查看 /etc/libvirt/qemu/centos8.xml
[root@kvm ~]# virsh dumpxml centos7
<domain type='kvm' id='2'>
  <name>centos7</name>
  <uuid>7b2055a4-0b5a-4536-a694-90488da95f30</uuid>
  <memory unit='KiB'>1048576</memory>
  <currentMemory unit='KiB'>1048576</currentMemory>
  <vcpu placement='static'>1</vcpu>
  <resource>
    <partition>/machine</partition>
  </resource>
  <os>
    <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
    <boot dev='hd'/>
    <bootmenu enable='yes'/>
  </os>
  <features>
    <acpi/>
    <apic/>
  </features>
  <cpu mode='custom' match='exact' check='full'>
    <model fallback='forbid'>Broadwell-IBRS</model>
    <feature policy='require' name='md-clear'/>
    <feature policy='require' name='ssbd'/>
    <feature policy='disable' name='hle'/>
    <feature policy='disable' name='rtm'/>
    <feature policy='require' name='spec-ctrl'/>
    <feature policy='require' name='hypervisor'/>
    <feature policy='require' name='xsaveopt'/>
  </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>destroy</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='qcow2'/>
      <source file='/var/lib/libvirt/images/centos7.qcow2'/>
      <backingStore/>
      <target dev='vda' bus='virtio'/>
      <alias name='virtio-disk0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
    </disk>
    <disk type='file' device='cdrom'>
      <driver name='qemu'/>
      <target dev='hda' bus='ide'/>
      <readonly/>
      <alias name='ide0-0-0'/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>
    <controller type='usb' index='0' model='ich9-ehci1'>
      <alias name='usb'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci1'>
      <alias name='usb'/>
      <master startport='0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci2'>
      <alias name='usb'/>
      <master startport='2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci3'>
      <alias name='usb'/>
      <master startport='4'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pci-root'>
      <alias name='pci.0'/>
    </controller>
    <controller type='ide' index='0'>
      <alias name='ide'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <controller type='virtio-serial' index='0'>
      <alias name='virtio-serial0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </controller>
    <interface type='network'>
      <mac address='52:54:00:a9:ea:8c'/>
      <source network='default' bridge='virbr0'/>
      <target dev='vnet1'/>
      <model type='virtio'/>
      <alias name='net0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <serial type='pty'>
      <source path='/dev/pts/2'/>
      <target type='isa-serial' port='0'>
        <model name='isa-serial'/>
      </target>
      <alias name='serial0'/>
    </serial>
    <console type='pty' tty='/dev/pts/2'>
      <source path='/dev/pts/2'/>
      <target type='serial' port='0'/>
      <alias name='serial0'/>
    </console>
    <channel type='unix'>
      <source mode='bind' path='/var/lib/libvirt/qemu/channel/target/domain-2-centos7/org.qemu.guest_agent.0'/>
      <target type='virtio' name='org.qemu.guest_agent.0' state='connected'/>
      <alias name='channel0'/>
      <address type='virtio-serial' controller='0' bus='0' port='1'/>
    </channel>
    <input type='tablet' bus='usb'>
      <alias name='input0'/>
      <address type='usb' bus='0' port='1'/>
    </input>
    <input type='mouse' bus='ps2'>
      <alias name='input1'/>
    </input>
    <input type='keyboard' bus='ps2'>
      <alias name='input2'/>
    </input>
    <graphics type='vnc' port='5901' autoport='yes' listen='0.0.0.0'>
      <listen type='address' address='0.0.0.0'/>
    </graphics>
    <video>
      <model type='cirrus' vram='16384' heads='1' primary='yes'/>
      <alias name='video0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <memballoon model='virtio'>
      <alias name='balloon0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
    </memballoon>
    <rng model='virtio'>
      <backend model='random'>/dev/urandom</backend>
      <alias name='rng0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
    </rng>
  </devices>
  <seclabel type='dynamic' model='dac' relabel='yes'>
    <label>+107:+107</label>
    <imagelabel>+107:+107</imagelabel>
  </seclabel>
</domain>

10、删除虚拟机配置

# 删除虚拟机配置,但不删除磁盘文件
[root@kvm ~]# virsh undefine centos7
Domain centos7 has been undefined

# 对应虚拟机xml的配置文件被删除
[root@kvm ~]# ll /etc/libvirt/qemu
total 8
drwxr-xr-x 2 root root   25 Dec 17 17:34 autostart
drwx------ 3 root root   42 Dec 17 12:33 networks
-rw------- 1 root root 4300 Dec 17 16:00 Win2008.xml

# 对应的磁盘文件并没有删除
[root@kvm ~]# ll /var/lib/libvirt/images/
total 11235976
-rw-r--r-- 1 qemu qemu 1734017024 Dec 17 17:34 centos7.qcow2
-rw-r--r-- 1 qemu qemu 7624392704 Dec 17 17:34 Windows2008_r2-x86_64.qcow2

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享