KVM常用命令整理

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/AMimiDou_212/article/details/102484259

一、virt-install 命令 – 创建虚拟机命令

# 默认网络
~]# virt-install  --virt-type kvm --name CentOS-7-x86_64 --ram 1024 --cdrom=/opt/CentOS-7-x86_64-DVD-1511.iso --disk path=/opt/CentOS-7-x86_64.raw  --network network=default --graphics  vnc,listen=0.0.0.0 --noautoconsole

# 桥接网络:
~]# virt-install  --virt-type kvm --name  openstack-middleware1    --ram 4096  --vcpus 4 --cdrom=/usr/local/src/CentOS-7-x86_64-Minimal-1511.iso    --disk path=/var/lib/libvirt/images/CentOS-7-x86_64-GenericCloud-1511-ok.qcow2   --network bridge=br0  --graphics  vnc,listen=0.0.0.0  --noautoconsole

# 安装Windows:
~]# qemu-img create -f qcow2   /var/lib/libvirt/images/Windows-2008_r2-x86_64.qcow2  200G
~]# virt-install --virt-type kvm  --name  Win_2008_r2-x86_64 --ram 3072  --vcpus=2 --os-type=windows --cdrom=/usr/local/src/windows_server_2008_r2.iso --disk path=/var/lib/libvirt/images/Windows-2008_r2-x86_64.qcow2,format=qcow2,bus=virtio   --disk path=/usr/local/src/virtio-win-0.1.141_amd64.vfd,device=floppy     --network bridge=br0,model=virtio  --graphics  vnc,listen=0.0.0.0 --noautoconsole

开始安装......
创建域......                                                                                                                       |    0 B  00:00:01     
域安装仍在进行。您可以重新连接
到控制台以便完成安装进程。

# virsh-install命令帮助:
~]# virt-install  --help
usage: virt-install --name NAME --ram RAM STORAGE INSTALL [options]

使用指定安装介质新建虚拟机。

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  --connect URI         使用 libvirt URI 连接到 hypervisor

通用选项:
  -n NAME, --name NAME  客户端事件名称
  --memory MEMORY       配置虚拟机内存分配。例如:
                        --memory 1024 (in MiB)
                        --memory 512,maxmemory=1024
  --vcpus VCPUS         为虚拟机配置的 vcpus 数。例如:
                        --vcpus 5
                        --vcpus 5,maxcpus=10,cpuset=1-4,6,8
                        --vcpus sockets=2,cores=4,threads=2,
  --cpu CPU             CPU 型号及功能。例如:
                        --cpu coreduo,+x2apic
                        --cpu host
  --metadata METADATA   配置虚拟机元数据。例如:
                        --metadata name=foo,title="My pretty title",uuid=...
                        --metadata description="My nice long description"

安装方法选项:
  --cdrom CDROM         光驱安装介质
  -l LOCATION, --location LOCATION
                        安装源(例如:nfs:host:/path、http://host/path
                        ftp://host/path)
  --pxe                 使用 PXE 协议从网络引导
  --import              在磁盘映像中构建虚拟机
  --livecd              将光驱介质视为 Live CD
  -x EXTRA_ARGS, --extra-args EXTRA_ARGS
                        附加到使用 --location 引导的内核的参数
  --initrd-inject INITRD_INJECT
                        使用 --location 为 initrd 的 root
                        添加给定文件
  --os-variant DISTRO_VARIANT
                        在其中安装 OS 变体的虚拟机,比如
                        'fedora18'、'rhel6'、'winxp' 等等。
  --boot BOOT           配置虚拟机引导设置。例如:
                        --boot hd,cdrom,menu=on
                        --boot init=/sbin/init (for containers)
  --idmap IDMAP         为 LXC 容器启用用户名称空间。例如:
                        --idmap uid_start=0,uid_target=1000,uid_count=10

设备选项:
  --disk DISK           使用不同选项指定存储。例如:
                        --disk size=10 (new 10GiB image in default location)
                        --disk /my/existing/disk,cache=none
                        --disk device=cdrom,bus=scsi
                        --disk=?
  -w NETWORK, --network NETWORK
                        配置虚拟机网络接口。例如:
                        --network bridge=mybr0
                        --network network=my_libvirt_virtual_net
                        --network network=mynet,model=virtio,mac=00:11...
                        --network none
                        --network help
  --graphics GRAPHICS   配置虚拟机显示设置。例如:
                        --graphics vnc
                        --graphics spice,port=5901,tlsport=5902
                        --graphics none
                        --graphics vnc,password=foobar,port=5910,keymap=ja
  --controller CONTROLLER
                        配置虚拟机控制程序设备。例如:
                        --controller type=usb,model=ich9-ehci1
  --input INPUT         配置虚拟机输入设备。例如:
                        --input tablet
                        --input keyboard,bus=usb
  --serial SERIAL       配置虚拟机串口设备
  --parallel PARALLEL   配置虚拟机并口设备
  --channel CHANNEL     配置虚拟机沟通频道
  --console CONSOLE     配置虚拟机与主机之间的文本控制台连接
  --hostdev HOSTDEV     将物理 USB/PCI/etc
                        主机设备配置为与虚拟机共享
  --filesystem FILESYSTEM
                        将主机目录传递给虚拟机。例如:
                        --filesystem /my/source/dir,/dir/in/guest
                        --filesystem template_name,/,type=template
  --sound [SOUND]       配置虚拟机声音设备模拟
  --watchdog WATCHDOG   配置虚拟机 watchdog 设备
  --video VIDEO         配置虚拟机视频硬件。
  --smartcard SMARTCARD
                        配置虚拟机智能卡设备。例如:
                        --smartcard mode=passthrough
  --redirdev REDIRDEV   配置虚拟机重定向设备。例如:
                        --redirdev usb,type=tcp,server=192.168.1.1:4000
  --memballoon MEMBALLOON
                        配置虚拟机 memballoon 设备。例如:
                        --memballoon model=virtio
  --tpm TPM             配置虚拟机 TPM 设备。例如:
                        --tpm /dev/tpm
  --rng RNG             配置虚拟机 RNG 设备。例如:
                        --rng /dev/random


虚拟机配置选项:
  --security SECURITY   设定域安全驱动器配置。
  --numatune NUMATUNE   为域进程调整 NUMA 策略。
  --memtune MEMTUNE     为域进程调整内粗策略。
  --blkiotune BLKIOTUNE
                        为域进程调整 blkio 策略。
  --memorybacking MEMORYBACKING
                        为域进程设置内存后备策略。例如:
                        --memorybacking hugepages=on
  --features FEATURES   设置域 <features> XML。例如:
                        --features acpi=off
                        --features apic=on,eoi=on
  --clock CLOCK         设置域 <clock> XML。例如:
                        --clock offset=localtime,rtc_tickpolicy=catchup

  --events EVENTS       配置 VM 生命周期管理策略
  --resource RESOURCE   配置 VM 资源分区(cgroups)

虚拟化平台选项:
  -v, --hvm             客户端应该是一个全虚拟客户端
  -p, --paravirt        这个客户端一个是一个半虚拟客户端
  --container           这台虚拟机需要一个容器客户端
  --virt-type HV_TYPE   要使用的管理程序名称 ( kvm、qemu、xen 等等)

其它选项:
  --autostart           引导主机时自动启动域。
  --wait WAIT           等待安装完成的分钟数。
  --noautoconsole       不要自动尝试连接到客户端控制台
  --noreboot            完成安装后不要引导虚拟机。
  --print-xml [XMLONLY]
                        输出所生成域 XML,而不是创建虚拟机。
  --dry-run             完成安装步骤,但不要创建设备或者定义虚拟机。
  --check CHECK         启用或禁用验证检查。例如:
                        --check path_in_use=off
                        --check all=off
  -q, --quiet           禁止无错误输出
  -d, --debug           输入故障排除信息

使用 '--option=?' 或者 '--option help' 查看可用子选项
有关示例及完整选项语法,请查看 man 手册

二、 虚拟机管理工具libvirt – virsh 命令

在这里插入图片描述
libvirt 是一个免费开源的主流的虚拟化管理工具,有两个进程,一个后台主进程libvirtd,virsh,virt-install等等都是依靠这个守护进程来间接访问qemu-kvm及配置文件。如果我们关闭这个进程,那么virsh,virsh-install,virt-manager就都不能访问了。另外提供 API,openstack 就是通过 libvirt 的 API 管理虚拟机,virt-install 就是通过 libvirt 完成的。即使此服务 down 了虚拟机不受影响,但是无法管理,openstack 挂了虚拟机不受影响。

The virsh program is the main interface for managing virsh guest domains. The program can be used to create, pause, and shutdown domains. It can also be used to list current domains. Libvirt is a C toolkit to interact with the virtualization capabilities of recent versions of Linux (and other OSes). It is free software available under the GNU Lesser General Public License. Virtualization of the Linux Operating System means the ability to run multiple instances of Operating Systems concurrently on a single hardware system where the basic resources are driven by a Linux instance. The library aims at providing a long term stable C API. It currently supports Xen, QEMU, KVM, LXC, OpenVZ, VirtualBox and VMware ESX.

译文:virsh程序是管理virsh访问虚拟主机域的主要接口。该程序可用于创建,暂停和关闭域。它也可以用来列出当前域。 Libvirt是一个C工具包,可与最新版本的Linux(和其他OS)的虚拟化功能进行交互。它是GNU通用公共许可协议下的免费软件。 Linux操作系统的虚拟化意味着能够在单个硬件系统上同时运行多个操作系统实例,而基本资源是由Linux实例驱动的。该库旨在提供长期稳定的C API。它目前支持 Xen,QEMU,KVM,LXC,OpenVZ,VirtualBox和VMware ESX

该工具可以监控、管理虚拟机、host硬件资源、存储、存储池、网络接口以及查看虚拟主机的转台信息等。

配置文件路径 /etc/libvirt/qemu/

在这里插入图片描述

[root@localhost qemu]# pwd
/etc/libvirt/qemu   #KVM 配置文件目录
[root@localhost qemu]# ll -h
total 4.0K
-rw-------. 1 root root 3.9K Oct 10 16:15 CentOS-7.xml  # 虚拟机配置文件
drwx------. 3 root root   42 Oct  9 15:24 networks   #网络配置文件
# 保存了虚拟机的配置 xml 配置文件,不能使用 vim 直接更改虚拟机配置文件,需要使用 virsh edit xxx 修改(生产中不建议热改变cpu 和内存)

virsh 常用命令

[root@localhost qemu]# virsh help
Grouped commands: #下面仅是常用的

 Domain Management (help keyword "domain"): #域管理
    autostart                      autostart a domain  #自动启动虚拟机域
    console                        connect to the guest console
    cpu-stats                      show domain cpu statistics
    create                         create a domain from an XML file
    define                         define (but don't start) a domain from an XML file
    desc                           show or set domain's description or title
    destroy                        destroy (stop) a domain
    detach-device                  detach device from an XML file #分离设备从xml
    detach-device-alias            detach device from an alias
    detach-disk                    detach disk device
    detach-interface               detach network interface
    domdisplay                     domain display connection URI
    domhostname                    print the domain's hostname
    domid                          convert a domain name or UUID to domain id
    domif-setlink                  set link state of a virtual interface
    domiftune                      get/set parameters of a virtual interface
    domjobabort                    abort active domain job
    domjobinfo                     domain job information
    domname                        convert a domain id or UUID to domain name
    domrename                      rename a domain
    dompmsuspend                   suspend a domain gracefully using power management functions
    dompmwakeup                    wakeup a domain from pmsuspended state
    domuuid                        convert a domain name or id to domain UUID
    domxml-from-native             Convert native config to domain XML
    domxml-to-native               Convert domain XML to native config
    dump                           dump the core of a domain to a file for analysis  
    dumpxml                        domain information in XML #dump 导出到xml文件
    edit                           edit XML configuration for a domain #编辑虚拟机配置文件
    event                          Domain Events  #查看虚拟机的事件
    reboot                         reboot a domain  #重启虚拟机
    restore                        restore a domain from a saved state in a file #重新加载虚拟机
    resume                         resume a domain #唤醒虚拟机
    save                           save a domain state to a file  #保存虚拟机
    save-image-define              redefine the XML for a domain's saved state file
    save-image-dumpxml             saved state domain information in XML
    save-image-edit                edit XML for a domain's saved state file
    schedinfo                      show/set scheduler parameters
    set-user-password              set the user password inside the domain #设置密码
    setmaxmem                      change maximum memory limit
    setmem                         change memory allocation
    setvcpus                       change number of virtual CPUs #设置cpu
    shutdown                       gracefully shutdown a domain #关闭虚拟机
    start                          start a (previously defined) inactive domain #启动虚拟机
    suspend                        suspend a domain #暂停虚拟机
    ttyconsole                     tty console 
    undefine                       undefine a domain #彻底删除一个虚拟机
    vcpucount                      domain vcpu counts
    vcpuinfo                       detailed domain vcpu information
    vcpupin                        control or query domain vcpu affinity
    emulatorpin                    control or query domain emulator affinity
    vncdisplay                     vnc display
    guestvcpus                     query or modify state of vcpu in the guest (via agent)
    setvcpu                        attach/detach vcpu or groups of threads
    

 Domain Monitoring (help keyword "monitor"):  #虚拟机监控管理
   
    domblkinfo                     domain block device size information
    domblklist                     list all domain blocks
    domblkstat                     get device block stats for a domain
    domif-getlink                  get link state of a virtual interface
    domifaddr                      Get network interfaces addresses for a running domain
    domiflist                      list all domain virtual interfaces
    domifstat                      get network interface stats for a domain
    dominfo                        domain information
    dommemstat                     get memory statistics for a domain
    domstate                       domain state
    domstats                       get statistics about one or multiple domains
    domtime                        domain time
    list                           list domains

 Networking (help keyword "network"): #网络管理
    net-autostart                  autostart a network
    net-create                     create a network from an XML file
    net-define                     define an inactive persistent virtual network or modify an existing persistent one from an XML file
    net-destroy                    destroy (stop) a network
    net-dhcp-leases                print lease info for a given network
    net-dumpxml                    network information in XML
    net-edit                       edit XML configuration for a network
    net-event                      Network Events
    net-info                       network information
    net-list                       list networks
    net-name                       convert a network UUID to network name
    net-start                      start a (previously defined) inactive network
    net-undefine                   undefine a persistent network
    net-uuid                       convert a network name to network UUID

实例:一个完整的备份删除和恢复的过程

 # 当前所有的虚拟机有两个
~]# virsh  list --all
 Id    Name                           State
----------------------------------------------------
 4     CentOS-7                       running

# 备份
~]# virsh  dumpxml  CentOS-7  > CentOS-7.xml.bak

# 摧毁一个虚拟机
~]# virsh  undefine CentOS-7
域 CentOS-7 已经被取消定义

# 当前只剩一个虚拟机
~]# virsh  list --all 
 Id    名称                         状态
----------------------------------------------------
 2     CentOS-7-x86_64                running

# 从备份的 xml 文件中恢复虚拟机
~]# virsh  define   /root/CentOS-7.xml.bak
定义域 CentOS-7(从 /root/CentOS-7.xml.bak)

# 恢复完成
~]# virsh  list --all
 Id    Name                           State
----------------------------------------------------
 4     CentOS-7                       running

猜你喜欢

转载自blog.csdn.net/AMimiDou_212/article/details/102484259