使用 virsh 管理虚拟机

2,304 阅读5分钟
原文链接: shangzongyu.com

virsh 是一个使用 libvirt 开发的一个命令行工具,可以用户在命令行控制虚拟机的一些行为,包括创建虚拟机、删除虚拟机、创建网络、删除网络等。

接下来介绍怎么使用 virsh 来控制虚拟机。

一、准备工作

  • 装有 Ubuntu16.04 的物理机,请参考 1.1 实体机准备
  • 如果没有物理机,请参看 1.2 虚拟机准备

1.1 实体机准备

  1. 安装操作系统
  2. 系统安装完成后,需要安装相应的软件,但是在此之前,需要把安装源准备下,因为官网源比较慢,我们添加清华的源, 打开 /etc/apt/sources.list, 添加一下内容进去:

    # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
    
    # 预发布软件源,不建议启用
    # deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse
    

    之后执行:

    sudo apt-get update
    
  3. 安装所需的相应的软件:

    • 必须软件:
      • 安装 kvm/qemu: sudo apt-get install -y kvm qemu
      • 安装 libvirt 库: sudo apt-get install -y libvirt-bin libvirt-dev
    • 可选软件:
      • 网桥管理工具: sudo apt-get install -y bridge-utils (如果使用了网桥,则需安装)
      • vnc 虚拟机查看工具: sudo apt-get install -y vncviewer vnc4server (当然可使用其他 vnc 工具)

1.2 虚拟机准备

  1. 下载虚拟机软件(VmWare/Virtuabox/Parallels 等都可以) ,但是必须开启 CPU 虚拟化。因为我自己的电脑是 Mac 系统,我使用 Parallels Desktop (这个应该是 MAC 下最好的虚拟机软件了,个人想法而已)
  2. 下载 Ubuntu 16.04镜像镜像,并安装系统。或者使用 Parallels Desktop可以直接下载你所需要的系统,Parallels Desktop 下载完成后,会自己安装系统,系统有一个默认用户 parallels, 密码在第一次启动的时候需要设置。

剩下就和 1.1 实体机准备 一样了,安装 virsh 所需的各个软件和库。

1.3 检查 CPU 是否支持虚拟化

$egrep -o '(vmx|svm)' /proc/cpuinfo
vmx
vmx

如果有数据,说明 CPU 支持虚拟化(现在的大部分 CPU 都支持虚拟化,除非你的 CPU 真的太老了),如果没有,则需要开启,如果是物理机则在 BIOS 界面开启这个功能,如果是在虚拟机上,则在虚拟机上开启 CPU 的嵌套虚拟化功能。例如在 Parallels Desktop 软件配置如下:

vmx_sv

二、使用 virsh 创建 KVM 虚拟机

环境安装好后,我们接下来就正式开始。

2.1 制作虚拟机镜像

$ sudo qemu-img create -f qcow2 /var/lib/libvirt/images/tiny-core.qcow2 1G

这样就创建了一个大小为 1G 的镜像

镜像是什么东西?我们在镜像上启动一个虚拟机,这个 1G 的镜像就相当于这个虚拟机对应的磁盘空间。
备注:qcow2 支持动态扩张, 来获得一个动态扩张的镜像。

2.2 ISO 准备

www.tinycorelinux.net 下载一个 ISO 文件(这个系统比较小,但是麻雀虽小五脏俱全,Linux 的基本功能都有)。

把下载的 ISO 文件将所有镜像及配置文件放到 /var/lib/libvirt/images/ 目录下。

注意:有些系统因为 SELinux 的原因,限定了 QEMU 的访问,所以,可以根据自己需求调整,默认放在 /var/lib/libvirt/images/ 下。

2.3 创建虚拟机配置文件

创建名为 tiny-core.xml 如下,可以根据自己需求更改。

内容如下:

<domain type="kvm">
<name>tiny-core</name>          <!--虚拟机名称-->
<memory unit="MiB">200</memory> <!--最大内存,单位M-->
<currentMemory unit="MiB">100</currentMemory>  <!--可用内存,单位M-->
<vcpu>2</vcpu>  <!--虚拟cpu个数-->
<os>
  <type arch="x86_64" machine="pc">hvm</type>
  <boot dev="hd" />    <!--硬盘启动-->
  <boot dev="cdrom" /> <!--光盘启动-->
</os>
<features>
  <acpi />
  <apic />
  <pae />
</features>
<clock offset="localtime" />
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<devices>
  <emulator>/bin/qemu-kvm</emulator>
  <disk type="file" device="disk">
      <driver name="qemu" type="qcow2" />
      <source file="/var/lib/libvirt/images/tiny-core.qcow2" /> <!--目的镜像路径-->
      <target dev="hda" bus="ide" />
  </disk>
  <disk type="file" device="cdrom">
      <source file="/var/lib/libvirt/images/TinyCore-current.iso" /> <!--光盘镜像路径 -->
      <target dev="hdb" bus="ide" />
  </disk>
  <input type="mouse" bus="ps2" />
  <graphics type="vnc" port="-1" autoport="yes" listen="0.0.0.0" keymap="en-us" />   <!--vnc方式登录,端口号自动分配,自动加1,可以通过virsh vncdisplay来查询-->
</devices>
</domain>
注:libvirt xml 配置文件: libvirt(包括virsh)使用 xml 文件对虚拟机进行配置,其中包括虚拟机名称、分配内存、vcpu等多种信息。定义、创建虚拟机等操作都需要 xml 配置文件的参与。

三、定义、启动、创建虚拟机

这里以上述 tiny-core 配置文件为例

3.1 定义虚拟机

# 定义虚拟机,此时虚拟机只被创建了,并没有真正启动。
$ sudo virsh define tiny-core
Domain tiny-core defined from tiny-core.xml

3.1. 定义虚拟机出现问题

  • 提示 error: Cannot check QEMU binary /bin/qemu-kvm: No such file or directory 错误
出错原因是:没有找到 /bin/qemu-kvm 这个文件,对 ubuntu 的应该对应是 /usr/bin/qemu 这个文件,把上面 xml 配置文件对应的那行改为如下就行了 <emulator>/usr/bin/qemu</emulator>
  • ubuntu 通过 sudo apt-get isnstall kvm qemu 后找不到 qemu 命令,其实最后安装的 qemu-system-* 等一堆文件,使用 qemu-system-i386qemu-system-x86_64 指令替换 qemu 指令。如果你想还是使用 qemu 指令,可以建立一条软链接如下命令:
$ sudo ln -s /usr/bin/qemu-system-x86_64 /usr/bin/qemu

3.2 启动虚拟机

# 启动由 tiny-core.xml 定义的、名为 tiny-core 的虚拟机
$ sudo virsh start tiny-core
Domain tiny-core started

# 此时执行virsh list出现如下信息:
$sudo virsh list
 Id    Name                           State
----------------------------------------------------
 4     tiny-core                      running

3.2.1 启动虚拟机出现问题

  • error: unsupported configuration: Domain requires KVM, but it is not available. Check that virtualization is enabled in the host BIOS, and host configuration is setup to load the kvm modules. 错误:

解决方法:

#执行命令
$ sudo virsh start tiny-core
error: Failed to start domain tiny-core
error: unsupported configuration: Domain requires KVM, but it is not available. Check that  virtualization is enabled in the host BIOS, and host configuration is setup to load the kvm modules.

出错原因是,没有开启CPU的虚拟化,开启就可以了,确认一下 CPU 支持硬件虚拟化:

$ egrep -o '(vmx|svm)' /proc/cpuinfo # 如果有结果显示,就是开启了虚拟化
vmx
  • error: Cannot get interface MTU on 'br0': No such device 错误:
# 执行命令
$ sudo virsh start tiny-core
error: Failed to start domain tiny-core
error: Cannot get interface MTU on 'br0': No such device

解决方法:

undefine test_ubuntu, 由于自己是在虚拟机上执行,机网桥是 virbr0 不是 br0 ,因此需要把 tiny-core.xmlbr0 修改为 virbr0

  • 出现权限问题,error: Failed to start domain core
    error: internal error: process exited while connecting to monitor: 2017-11-07T06:12:57.141240Z qemu-kvm: -drive file=/root/TinyCore-current.iso,format=raw,if=none,id=drive-ide0-0-1,readonly=on: Could not open '/root/TinyCore-current.iso': Permission denied

这个由于 selinux 的限制。

解决办法:

  • 把 ISO 文件 TinyCore-current.iso 放到 /var/lib/libvirt/images 目录即可解决。
  • 修改 /etc/libvirt/qemu.conf 配置文件,解注如下:

    user = "root"
    group = "root"
    
  • 出现错误 error: Cannot check QEMU binary /bin/qemu: No such file or directory

出现这个错误是因为 /bin/qemu 命令找不到,Ubuntu 安装 KVM/QEMU 后不是,qemu 的二进制是 qemu-system-x86_64,可以使用 which qemu-system-x86_64 找出它的绝对路径,然后修改 xml 配置文件为 <emulator>/usr/bin/qemu-system-x86_64</emulator>,既可以解决。

四、通过 vnc 查看虚拟机

启动虚拟机后,我们如何查看虚拟机是否正确启动,我们可以使用 vncviewer

  • 首先查看 vnc 端口

    $ sudo virsh vncdisplay tiny-core
    :0
    
  • 然后通过 vnc 查看虚拟机

    $ sudo vncviewer 127.0.0.1:0
    

    效果如下:
    vnc-viewe

五、关闭、销毁、取消定义虚拟机

# 关闭虚拟机, 虚拟机将慢慢关闭,就像平时我们关闭计算机那样。
# 此步骤 与virsh start tiny-core 对应
$ virsh shutdown tiny-core
    
# 取消定义虚拟机,libvirt 取消 tiny-core的定义,系统无法在辨识tiny-core。
# 此步骤与 virsh define tiny-core.xml 对应
$ virsh undefine tiny-core 

# 销毁虚拟机,libvirt 直接销毁 tiny-core,取消 tiny-core 的定义。
# 此步骤之后无法在执行 virsh undefine demo
$ virsh destroy tiny-core

六、命令整理

virsh 也可以直接进入交互模式,直接在终端输入 virsh 即可进入交互模式。

常用:

命令 表示意义
version 版本号
pwd 显示当前目录
hostname 显示本节点主机名
nodeinfo 显示节点信息
list –all 显示所有云主机
destroy 强制关闭虚拟机(相当于直接拨电源)
start 启动虚拟机
edit 编辑该虚拟机的xml文件
dommemstat 获取domain的内存状态
suspend 挂起一个正在运行的虚拟机,该虚拟机仍占资源;
resume 从挂起状态恢复一下虚拟机
vcpuinfo 显示一些虚拟机的vcpu的信息
vncdisplay 显示vnc监听地址和端口
shutdown 关闭虚拟机

快照相关:

命令 表示意义
snapshot-create xmlfile 给domain创建一个snapshot,详细内容保存在xmlfile中
snapshot-current 显示一个domain的当前的snapshot
snapshot-list 显示一个domain的所有的snapshot
snapshot-revert snapshot 恢复一个domian到以前的snapshot
snapshot-delete snapshot –children 删除一个domain的snapshot

推荐阅读