From Fedora Project Wiki

本文将解决如何在 Fedora 中创建并管理虚拟机。关于如何使用 Fedora 作为一个虚拟机,请参看 使用虚拟机安装 Fedora。关于 Fedora 中其他的虚拟化技术,请参看 专门页面


在 Fedora 上使用虚拟化

Fedora 使用 libvirt 系列工具作为其虚拟化解决方案。默认情况下,Feodra 将使用 Qemu 来运行虚拟用户实例。

关于其他平台虚拟化的信息,请参看 http://virt.kernelnewbies.org/TechComparison

Qemu 能够在软件层面模拟虚拟主机,或在硬件支持情况下使用 KVM 提供更加快速而全面的虚拟化。

对于其他可用的虚拟化产品和软件,本文并未涉及。

为虚拟 guests 安装配置 Fedora

本节包括在您的系统上配置 libvirt 。成功完成本节后您将可以创建虚拟 guest 操作系统。

系统要求

Fedora 上虚拟化基本系统要求是:

  • 至少为每个 guest 准备 600MB 的硬盘。一个最小化的命令行 fedora 系统需要 600MB 硬盘。标准 fedora 桌面则要求至少 3GB 的硬盘空间。
  • 至少为每个 guest 准备 256MB 的 RAM 再加上主机基本系统本身 256MB RAM。建议为每个现代操作系统 guest 分配至少 756MB 内存。 一个好的原则是考虑正常情况下操作系统需要多少内存就分配多少给虚拟 guest

KVM 要求 CPU 有虚拟化扩展功能,近些年生产的很多 CPU 都具有该类扩展功能。这些扩展功能被称为 Intel VT 或 AMD-V 。使用如下命令检查您的 CPU 是否支持虚拟化:

$ egrep '^flags.*(vmx|svm)' /proc/cpuinfo 

如果没有任何输出,则说明您的系统不支持相关扩展功能。您仍然可以使用 QEMU/KVM ,但是虚拟将只能使用软件虚拟化(想当慢)。

安装虚拟化软件包

当安装 Fedora 时,可以通过勾选安装基本组中的虚拟化组以安装虚拟化软件包。

在已经完成 Fedora 安装的系统中, QEMU、KVM和其他一些虚拟化工具的安装可以通过运行如下命令安装虚拟化组:

su -c "yum install @virtualization"

该命令将安装 qemu-kvmpython-virtinstqemuvirt-managervirt-viewer 以及所有需要的依赖软件包。

su -c "systemctl start libvirtd"

确认所有 kvm 内核模块已正常加载:

$ lsmod | grep kvm
kvm_amd                55563  0 
kvm                   419458  1 kvm_amd

如果该命令没有列出 kvm_intel 或 kvm_amd, 则说明 KVM 没有正常配置。参看 确保系统正常使用 KVM 以获得解决问题的建议。

网络支持

默认情况下, livert 将为你主机上的 guest 创建一个私人网络。该私人网络将使用 192.168.x.x 子网,其并不能被主机所在网络中主机直接连接,但虚拟 guest 可以使用主机作为网关并通过它连到外网。如果您需要提供让其他计算机连接的服务,您可以使用 iptables DNAT 规则通过特定端口转发,或者您也可以配置一个桥接环境。

更多关于如何配置桥接网络的信息,请参看 libvirt 网络配置页面


创建一个 Fedora guest

Fedora guest 支持使用 anaconda 安装。安装过程可以通过 virt-viewer 程序使用命令行或者使用 virt-manager 图形接口启动。

使用 virt-install 创建 guest

virt-install 是创建虚拟 guest 的一个基于命令行的工具。使用 --prompt 参数运行 virt-install 可以启用一个交互安装过程。

su -c "/usr/bin/virt-install --prompt"

在创建新 guest 时需要解决以下问题。

  1. 设置虚拟机的名字,作为标签以区别不同的 guest 系统。该标签用于 virsh 命令和 virt-manager(虚拟机管理器)。
  2. 应该分配多少 RAM ?是要分配给您的 guest 实例的 RAM 大小(以兆字节为单位,如:256)。需要注意的是,不建议分配低于 256 兆字节。
  3. 要使用的磁盘路径。本地路径和及作为 guest 磁盘镜像的文件名(如: /var/lib/libvirt/images/name.img)。这将作为您的 guest 磁盘镜像输出。最好使用默认路径 /var/lib/libvirt/images/ 。
  4. 可用磁盘大小(吉字节)。为该 guest 设置虚拟磁盘大小(仅在要使用镜像不存在时出现)。对于默认安装 4.0 吉字节比较合理。
  5. 安装源(CD-ROM/ISO 或网络地址)。这是 anaconda 要使用的 Fedora 安装树路径。支持 NFS、FTP以及 HTTP 地址。例如:
    • nfs:my.nfs.server.com:/path/to/test2/tree/
    • http://my.http.server.com/path/to/tree/
    • ftp://my.ftp.server.com/path/to/tree


这些选项可以作为命令行参数,详细情况可以使用 virt-install --help 获得。

virt-install 可以使用 kickstart 文件,如: virt-install -x ks=kickstart-file-name.ks.

如果启用了图形界面,会打开一个 VNC 窗口并启用图形安装器。若未启用图形界面,将会使用文本安装器进行 fedora 安装。

使用 virt-manager 创建 guest

可以通过选择"应用-->系统工具"菜单启动 GUI 虚拟机管理器,也可以运行如下命令启动:

su -c "virt-manager"

If you encounter an error along the lines of "Failed to contact configuration server; some possible causes are that you need to enable TCP/IP networking for ORBit, or you have stale NFS locks due to a system crash", trying running virt-manager not as root (without the su -c). The GUI will prompt for the root password.


  1. Open a connection to a hypervisor by choosing File-->Add connection...
  2. Choose "qemu" for KVM, or "Xen" for Xen.
  3. Choose "local" or select a method to connect to a remote hypervisor
  4. After a connection is opened, click the new icon next to the hypervisor, or right click on the active hypervisor and select "New" (Note - the new icon is going to be improved to make it easier to see)
  5. A wizard will present the same questions as appear with the virt-install command-line utility (see descriptions above). The wizard assumes that a graphical installation is desired and does not prompt for this option.
  6. On the last page of the wizard there is a "Finish" button. When this is clicked, the guest OS is provisioned. After a few moments a VNC window should appear. Proceed with the installation as normal.

Remote management

The following remote management options are available:

  • (easiest) If using non-root users via SSH, then setup instructions are at: http://wiki.libvirt.org/page/SSHSetup
  • If using root for access via SSH, then create SSH keys for root, and use ssh-agent and ssh-add before launching virt-manager.
  • To use TLS, set up a local certificate authority and issue x509 certs to all servers and clients. For information on configuring this option, refer to http://wiki.libvirt.org/page/TLSSetup.

Guest system administration

When the installation of the guest operating system is complete, it can be managed using the GUI virt-manager program or on the command line using virsh.

Managing guests with virt-manager

Start the Virtual Machine Manager. Virtual Machine Manager is in the "Applications-->System Tools" menu, or execute:

su -c "virt-manager"

{1} If you are not root, you will be prompted to enter the root password. ChooseRun unprivileged to operate in a read-only non-root mode.

  • Choose the host you wish to manage and click "Connect" in the "Open Connection" dialog window.
  • The list of virtual machines is displayed in the main window. Guests that are running will display a ">" icon. Guests that are not running will be greyed out.
  • To manage a particular guest, double click on it, or right click and select "Open".
  • A new window for the guest will open that will allow you to use its console, see information about its virtual hardware and start/stop/pause it.

For further information about virt-manager consult the project website

Bugs in the virt-manager tool should be reported in BugZilla against the 'virt-manager' component

Managing guests with virsh

The virsh command line utility that allows you to manage virtual machines. Guests can be managed on the command line with the virsh utility. The virsh utility is built around the libvirt management APIl:

  • virsh has a stable set of commands whose syntax and semantics are preserved across updates to the underlying virtualization platform.
  • virsh can be used as an unprivileged user for read-only operations (e.g. listing domains, listing domain statistics).
  • virsh can manage domains running under Xen, Qemu/KVM, esx or other backends with no perceptible difference to the user
一个有效地址可以使用 "-c" 参数传递给 virsh 来连接到远程 libvirtd 实例。详情请参看 http://libvirt.org/uri.html

如下命令启动虚拟机:

su -c "virsh create <name of virtual machine>"

要列出当前运行的虚拟机,执行:

su -c "virsh list"

列出所有虚拟机(不管是否运行):

su -c "virsh list --all"

正常关闭 guest

su -c "virsh shutdown <virtual machine (name | id | uuid)>"

强制关闭 guest

su -c "virsh destroy <virtual machine (name | id | uuid)>"

保存虚拟机快照到文件:

su -c "virsh save <virtual machine (name | id | uuid)> <filename>"

从快照恢复虚拟机:

su -c "virsh restore <filename>"

导出虚拟机配置文件:

su -c "virsh dumpxml <virtual machine (name | id | uuid)"

列出全部 virsh 可用命令:

su -c "virsh help"

也可以查看手册: man 1 virsh

关于 virsh 工具的 bug 可以在 BugZilla 上 'libvirt' 部分报告。

Other virtualization options

QEMU/KVM without Libvirt

QEMU/KVM can be invoked directly without libvirt, however you won't be able to use tools such as virt-manager, virt-install, or virsh. Plain QEMU (without KVM) can also virtualize other processor architectures like ARM or PowerPC. See How to use qemu

Xen

Fedora can run as a Xen Guest OS, but using Fedora as a Xen Host is currently not supported. There is an experimental repo available at http://myoung.fedorapeople.org/dom0

OpenStack

OpenStack consists of a number services for running IaaS clouds. They are the Object Store (Swift), Compute (Nova) and Image (Glance) services. It is a Fedora 16 feature.

OpenNebula

OpenNebula is an Open Source Toolkit for Data Center Virtualization.

oVirt

The oVirt project is an open virtualization project providing a feature-rich, end to end, server virtualization management system with advanced capabilities for hosts and guests, including high availability, live migration, storage management, system scheduler, and more.


Troubleshooting, bug reporting, and known issues

For a list of known unresolved issues, as well as troubleshooting tips, please see How to debug virtualization problems