KVM常用运维指令

0、取虚拟机名字

virsh list | awk '{print $2}'


1、虚拟机配置路径:/etc/libvirt/qemu


2、创建硬盘

qemu-img create /home/kvm/123.img 5G


3、创建虚拟机

virt-install \

--name=Winxp \

--ram 512 \

--vcpus=2 \

-f /home/123.img \

--cdrom /home/acton-systemvm-02062012.qcow2.bz2 \

--graphics vnc,listen=0.0.0.0,port=5980,password='12345678',keymap='en-us' \

--network bridge=cloudbr0  \

--force  --autostart

(name 虚拟机名称,ram 分配内存, vcpus 分配cpu个数, cdrom guest系统文件地址,network 网卡桥接名称)


4、管理虚拟机:virsh

[root@os-node6 ~]# virsh

Welcome to virsh, the virtualization interactive terminal.


Type:  'help' for help with commands

       'quit' to quit


virsh # 


5、显示虚拟机list或者list �Call

(list显示本地活动虚拟机; list �Call显示本地所有的虚拟机(活动的+不活动的) )

virsh # list

 Id    Name                           State

----------------------------------------------------

 9     instance-000000c7              running

 10    instance-0000002e              running

 11    instance-000000a8              running

 12    instance-000000a7              running


virsh # list --all

 Id    Name                           State

----------------------------------------------------

 9     instance-000000c7              running

 10    instance-0000002e              running

 11    instance-000000a8              running

 12    instance-000000a7              running

 -     instance-00000066              shut off


virsh # 


6、网卡列表: virsh domiflist  实例名

virsh # domiflist instance-000000c7

Interface  Type       Source     Model       MAC

-------------------------------------------------------

tapc234bf36-36 bridge     qbrc234bf36-36 virtio      fa:16:3e:dd:7b:49


7、网卡状态:virsh domifstat  实例名  网卡名

virsh # domifstat instance-000000c7 tapc234bf36-36 

tapc234bf36-36 rx_bytes 78257011

tapc234bf36-36 rx_packets 638453

tapc234bf36-36 rx_errs 0

tapc234bf36-36 rx_drop 0

tapc234bf36-36 tx_bytes 1699649

tapc234bf36-36 tx_packets 25784

tapc234bf36-36 tx_errs 0

tapc234bf36-36 tx_drop 0


8、磁盘列表:virsh domblklist 实例名

virsh # domblklist instance-000000c7

Target     Source

------------------------------------------------

vda        volumes/volume-e04786e0-15d0-41f5-8401-7c1693462f29


9、虚拟机快照

 1)、查看此虚拟机xml文件的详细信息

virsh # dumpxml instance-000000c7

<domain type='kvm' id='9'>

  <name>instance-000000c7</name>

  <uuid>a62dc36e-7619-474d-8abe-ff38391f36e4</uuid>

  <memory unit='KiB'>8192000</memory>

  <currentMemory unit='KiB'>8192000</currentMemory>

  <vcpu placement='static'>8</vcpu>

  <sysinfo type='smbios'>

    <system>

      <entry name='manufacturer'>Red Hat Inc.</entry>

      <entry name='product'>OpenStack Nova</entry>

      <entry name='version'>2013.2.3-1.el6</entry>

      <entry name='serial'>44454c4c-5700-1033-804e-b1c04f485931</entry>

      <entry name='uuid'>a62dc36e-7619-474d-8abe-ff38391f36e4</entry>

    </system>

  </sysinfo>

  <os>

    <type arch='x86_64' machine='rhel6.5.0'>hvm</type>

    <boot dev='hd'/>

    <smbios mode='sysinfo'/>

  </os>

  <features>

    <acpi/>

    <apic/>

  </features>

  <cpu mode='host-model'>

    <model fallback='allow'/>

  </cpu>

  <clock offset='utc'>

    <timer name='pit' tickpolicy='delay'/>

    <timer name='rtc' tickpolicy='catchup'/>

  </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='network' device='disk'>

      <driver name='qemu' type='raw' cache='none'/>

      <auth username='compute'>

        <secret type='ceph' uuid='0071bd6f-849c-433a-8051-2e553df49aea'/>

      </auth>

      <source protocol='rbd' name='volumes/volume-e04786e0-15d0-41f5-8401-7c1693462f29'>

        <host name='10.40.10.66' port='6789'/>

        <host name='10.40.10.68' port='6789'/>

        <host name='10.40.10.69' port='6789'/>

      </source>

      <target dev='vda' bus='virtio'/>

      <serial>e04786e0-15d0-41f5-8401-7c1693462f29</serial>

      <alias name='virtio-disk0'/>

      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>

    </disk>

    <controller type='usb' index='0'>

      <alias name='usb0'/>

      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>

    </controller>

    <interface type='bridge'>

      <mac address='fa:16:3e:dd:7b:49'/>

      <source bridge='qbrc234bf36-36'/>

      <target dev='tapc234bf36-36'/>

      <model type='virtio'/>

      <filterref filter='nova-instance-instance-000000c7-fa163edd7b49'/>

      <alias name='net0'/>

      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>

    </interface>

    <serial type='file'>

      <source path='/var/lib/nova/instances/a62dc36e-7619-474d-8abe-ff38391f36e4/console.log'/>

      <target port='0'/>

      <alias name='serial0'/>

    </serial>

    <serial type='pty'>

      <source path='/dev/pts/0'/>

      <target port='1'/>

      <alias name='serial1'/>

    </serial>

    <console type='file'>

      <source path='/var/lib/nova/instances/a62dc36e-7619-474d-8abe-ff38391f36e4/console.log'/>

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

      <alias name='serial0'/>

    </console>

    <input type='tablet' bus='usb'>

      <alias name='input0'/>

    </input>

    <input type='mouse' bus='ps2'/>

    <graphics type='vnc' port='5900' autoport='yes' listen='0.0.0.0' keymap='en-us'>

      <listen type='address' address='0.0.0.0'/>

    </graphics>

    <video>

      <model type='cirrus' vram='9216' heads='1'/>

      <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='0x05' function='0x0'/>

    </memballoon>

  </devices>

  <seclabel type='none'/>

</domain>


virsh # 


2)、查看快照列表: virsh snapshot-list 实例名

virsh # snapshot-list instance-000000c7

 Name                 Creation Time             State

------------------------------------------------------------

3)、创建快照: 首先需要关闭虚拟机,然后按照下面的命令进行快照。最后恢复快照的时候先关机在恢复

virsh # snapshot-create-as --domain instance-000000c7 --name instance-000000c7-snap --description "instance-000000c7-snap"


4)、查看快照配置: virsh snapshot-current instance-000000c7

virsh # snapshot-current instance-000000c7 

5)、恢复快照: virsh snapshot-revert instance-000000c7  instance-000000c7-snap


6)、删除快照: virsh snapshot-delete instance-000000c7  instance-000000c7-snap



10、把raw格式的镜像转换为qcow2(raw格式的镜像不支持快照)

qemu-img convert -f raw -O qcow2 100.img 111.img


11、虚拟机状态:domstate 实例名

virsh # domstate instance-000000c7

running


12、启动虚拟机start

virsh # start instance-000000c7


13、查看虚拟机的详细信息

virsh # dominfo instance-000000c7

Id:             9

Name:           instance-000000c7

UUID:           a62dc36e-7619-474d-8abe-ff38391f36e4

OS Type:        hvm

State:          running

CPU(s):         8

CPU time:       580.2s

Max memory:     8192000 KiB

Used memory:    8192000 KiB

Persistent:     yes

Autostart:      disable

Managed save:   no

Security model: none

Security DOI:   0


14、自启设置:autostart 实例名  (在宿主机重启后,虚拟机默认自己是不启动的必须手动开启)

virsh # autostart instance-000000c7


15、关闭虚拟机shutdown 实例名

virsh # shutdown instance-000000c7


16、重启虚拟机reboot 实例名

virsh # reboot instance-000000c7


17、把虚拟机强制关闭电源

virsh # destroy instance-000000c7


18、从 XML 定义一个虚拟机

virsh # define /var/lib/nova/instances/081b57d0-1abf-40de-a16d-7cea951ba9f8/libvirt.xml 


19、移除虚拟机:undefine 实例名

virsh # undefine instance-00000066

(使用undefine的前提是,虚拟机是关闭的,那我们怎么关闭虚拟机呢,可以使用destroy,确切的说这个操作就是一脚将服务器的电源踹掉。为什么不用shutdown呢,可能是个bug,apci这个设备不好好工作。)


20、挂起一个正在运行的虚拟机,该虚拟机仍旧占用资

virsh # suspend instance-00000066


21、从挂起状态恢复一个虚拟机: 

virsh # resume instance-00000066


22、输出客户端 XML 配置文件

virsh # dumpxml instance-00000066

<domain type='kvm'>

  <name>instance-00000066</name>

  <uuid>48756d82-71aa-44b1-8d92-7d8ee8b60f44</uuid>

  <memory unit='KiB'>8192000</memory>

  <currentMemory unit='KiB'>8192000</currentMemory>

  <vcpu placement='static'>4</vcpu>

  <sysinfo type='smbios'>

    <system>

      <entry name='manufacturer'>Red Hat Inc.</entry>

      <entry name='product'>OpenStack Nova</entry>

      <entry name='version'>2013.2.3-1.el6</entry>

      <entry name='serial'>44454c4c-5700-1033-804e-b1c04f485931</entry>

      <entry name='uuid'>48756d82-71aa-44b1-8d92-7d8ee8b60f44</entry>

    </system>

  </sysinfo>

  <os>

    <type arch='x86_64' machine='rhel6.5.0'>hvm</type>

    <boot dev='hd'/>

    <smbios mode='sysinfo'/>

  </os>

  <features>

    <acpi/>

    <apic/>

  </features>

  <cpu mode='host-model'>

    <model fallback='allow'/>

  </cpu>

  <clock offset='utc'>

    <timer name='pit' tickpolicy='delay'/>

    <timer name='rtc' tickpolicy='catchup'/>

  </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='network' device='disk'>

      <driver name='qemu' type='raw' cache='none'/>

      <auth username='compute'>

        <secret type='ceph' uuid='0071bd6f-849c-433a-8051-2e553df49aea'/>

      </auth>

      <source protocol='rbd' name='volumes/volume-86bfeb91-55a1-4e10-9712-edb717ed0dd0'>

        <host name='10.40.10.64' port='6789'/>

        <host name='10.40.10.68' port='6789'/>

        <host name='10.40.10.69' port='6789'/>

      </source>

      <target dev='vda' bus='virtio'/>

      <serial>86bfeb91-55a1-4e10-9712-edb717ed0dd0</serial>

      <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>

    <interface type='bridge'>

      <mac address='fa:16:3e:b1:64:e8'/>

      <source bridge='qbr22fec319-4f'/>

      <target dev='tap22fec319-4f'/>

      <model type='virtio'/>

      <filterref filter='nova-instance-instance-00000066-fa163eb164e8'/>

      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>

    </interface>

    <serial type='file'>

      <source path='/var/lib/nova/instances/48756d82-71aa-44b1-8d92-7d8ee8b60f44/console.log'/>

      <target port='0'/>

    </serial>

    <serial type='pty'>

      <target port='1'/>

    </serial>

    <console type='file'>

      <source path='/var/lib/nova/instances/48756d82-71aa-44b1-8d92-7d8ee8b60f44/console.log'/>

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

    </console>

    <input type='tablet' bus='usb'/>

    <input type='mouse' 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>

    <video>

      <model type='cirrus' vram='9216' 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='0x05' function='0x0'/>

    </memballoon>

  </devices>

</domain>


23、用xml文件创建虚拟机

virsh dumpxml node4 >/etc/libvirt/qemu/node6.xml

#导出虚拟机node6的硬件配置信息为/etc/libvirt/qemu/node6.xml



[root@target ~]# vim /etc/libvirt/qemu/node6.xml 

<domain type='kvm' id='20'>    #修改node6的id号

  <name>node6</name>           #虚拟机node6的name

  <uuid>4b7e91eb-6521-c2c6-cc64-c1ba72707fc7</uuid>  #uuid必须修改,否则会和node4的冲突

  <memory>524288</memory>

  <currentMemory>524288</currentMemory>

  <vcpu>1</vcpu>

  <os>

    <type arch='x86_64' machine='rhel5.4.0'>hvm</type>

    <boot dev='network'/>

  </os>

  <features>

    <acpi/>

    <apic/>

    <pae/>

  </features>

  <clock offset='utc'/>

  <on_poweroff>destroy</on_poweroff>

  <on_reboot>restart</on_reboot>

  <on_crash>restart</on_crash>

  <devices>

    <emulator>/usr/libexec/qemu-kvm</emulator>

    <disk type='file' device='disk'>

      <driver name='qemu' type='qcow2' cache='none'/>

      <source file='/virhost/node4.img'/>         #指定新虚拟机的硬盘文件

      <target dev='vda' bus='virtio'/>

    </disk>

    <interface type='bridge'>

      <mac address='54:52:00:69:d5:c7'/>

      <source bridge='br0'/>

      <target dev='vnet0'/>

      <model type='virtio'/>

    </interface>

    <interface type='bridge'>

      <mac address='54:52:00:69:d5:d7'/>

      <source bridge='br0'/>

      <target dev='vnet1'/>

      <model type='virtio'/>

    </interface>

    <serial type='pty'>

      <source path='/dev/pts/4'/>

      <target port='0'/>

    </serial>

    <console type='pty' tty='/dev/pts/4'>

      <source path='/dev/pts/4'/>

      <target port='0'/>

    </console>

    <input type='mouse' bus='ps2'/>

    <graphics type='vnc' port='5900' autoport='yes' keymap='en-us'/>

  </devices>

</domain>


[root@target ~]# virsh define /etc/libvirt/qemu/node6.xml

#使用虚拟描述文档建立虚拟机,可用virsh edit node6修改node6的配置文件


[root@target ~]# virsh start node6

#启动虚拟机


为虚拟机开启vnc

[root@target ~]# virsh edit node4     #编辑node4的配置文件;不建议直接通过vim node4.xml修改。

<graphics type='vnc' port='-1' autoport='yes' listen='127.0.0.1' keymap='en-us'/>

   #port='-1' autoport='yes':port自动分配,监听回环网络(virt-manager管理需要listen='127.0.0.1'),无密码

改为

<graphics type='vnc' port='5904' autoport='no' listen='0.0.0.0' keymap='en-us' passwd='xiaobai'/>

   #固定vnc管理端口5904,不自动分配,vnc密码xiaobai,监听所有网络


远程vnc访问地址:192.168.32.40:5904



24、从 XML 配置文件生成客户端并启动新客户端: create /root/12.xml


25、显示客户端 ID:

virsh # domid instance-000000c7



26、显示客户端的UUID

virsh # domuuid instance-000000c7

a62dc36e-7619-474d-8abe-ff38391f36e4


27、编辑虚拟机配置

virsh # edit instance-00000066 


Domain instance-00000066 XML configuration edited.


28、显示VNC端口号

virsh # list --all

 Id    Name                           State

----------------------------------------------------

 9     instance-000000c7              running

 10    instance-0000002e              running

 11    instance-000000a8              running

 12    instance-000000a7              running

 -     instance-00000066              shut off


virsh # undefine instance-00000066

error: Refusing to undefine while domain managed save image exists


virsh # 

virsh # vncdisplay instance-000000c7 

:0


virsh # vncdisplay instance-0000002e

:1


29、下表提供所有 virsh 命令行选项的快速参考。 

命令  Description 

help 打印基本帮助信息。 

list 列出所有客户端。 

dumpxml 输出客户端 XML 配置文件。 

create 从 XML 配置文件生成客户端并启动新客户端。 

start 启动未激活的客户端。 

destroy 强制客户端停止。 

define 为客户端输出 XML 配置文件。 

domid 显示客户端 ID。 

domuuid 显示客户端 UUID。 

dominfo 显示客户端信息。 

domname 显示客户端名称。 

domstate 显示客户端状态。 

quit 退出这个互动终端。 

reboot 重新启动客户端。 

restore 恢复以前保存在文件中的客户端。 

resume 恢复暂停的客户端。 

save 将客户端当前状态保存到某个文件中。 

shutdown 关闭某个域。 

suspend 暂停客户端。 

undefine 删除与客户端关联的所有文件。 

migrate 将客户端迁移到另一台主机中。 


使用以下 virsh 命令管理客户端及管理程序资源: 


命令 Description 

setmem 为客户端设定分配的内存。 

setmaxmem 为管理程序设定内存上限。 

setvcpus 修改为客户端分配的虚拟 CPU 数目。 

vcpuinfo 显示客户端的虚拟 CPU 信息。 

vcpupin 控制客户端的虚拟 CPU 亲和性。 

domblkstat 显示正在运行的客户端的块设备统计。 

domifstat 显示正在运行的客户端的网络接口统计。 


attach-device 使用 XML 文件中的设备定义在客户端中添加设备。 

attach-disk 在客户端中附加新磁盘设备。 

attach-interface 在客户端中附加新网络接口。 

detach-device 从客户端中分离设备,使用同样的 XML 描述作为命令attach-device。

detach-disk 从客户端中分离磁盘设备。 

detach-interface 从客户端中分离网络接口。


30、存储池和存储卷的管理 

1.创建 KVM主机存储池 

1).创建基于文件夹(目录)的存储池

virsh pool-define-as vmware_pool --type dir --target /virhost/vmware#定义存储池vmware_pool或

virsh pool-create-as --name vmware_pool --type dir --target /virhost/vmware

#创建存储池vmware_pool,类型为文件目录,/virhost/vmware,与pool-define-as结果一样


2).创建基于文件系统的存储池

virsh pool-define-as --name vmware_pool --type fs --source-dev /dev/vg_target/LogVol02 --source-format ext4 --target /virhost/vmware

virsh pool-create-as --name vmware_pool --type fs --source-dev /dev/vg_target/LogVol02 --source-format ext4 --target /virhost/vmware


3).查看存储池信息

virsh pool-info vmware_pool #查看存储域(池)


4).启动存储池

virsh pool-start vmware_pool #启动存储池

virsh pool-list


5)销毁存储域,取消存储池

virsh pool-destroy vmware_pool #销毁存储池

virsh pool-list --all

virsh pool-undefine vmware_pool #取消存储池的定义

virsh pool-list --all


2.创建了存储池后,就可以创建一个卷,这个卷是用来做虚拟机的硬盘

virsh vol-create-as --pool vmware_pool --name node6.img --capacity 10G --allocation 1G --format qcow2#创建卷 node6.img,所在存储池为vmware_pool,容量10G,初始分配1G,文件格式类型qcow2

virsh vol-info /virhost/vmware/node6.img #查看卷信息名称: node6.img类型: 文件容量: 10.00 GB分配: 136.00 KB


3.在存储卷上安装虚拟主机

virt-install --connect qemu:///system \-n node7 \-r 512 \-f /virhost/vmware/node7.img \--vnc \--os-type=linux \--os-variant=rhel6 \--vcpus=1 \--network bridge=br0 \-c /mnt/rhel-server-6.0-x86_64-dvd.iso



你可能感兴趣的:(命令,kvm,openstack,虚拟化)