Virtualization – Getting Started

Jan Kuparinen Version F34 onwards Last review: 2020-11-28 Needs Review!

Fedora uses the libvirt family of tools as its virtualization solution.

ハードウェア仮想化サポートの有効化

このセクションでは、システム上での libvirt のセットアップについて説明します。libvirt をセットアップすると、仮想化ゲスト OS (仮想マシンとも呼ばれます) を作成できるようになります。

システム要件

Fedora で仮想化を実行するには、以下が必要です:

  • ゲストごとに少なくとも 600 MB のハードディスクストレージ。最小限のコマンドライン Fedora システムには 600 MB のストレージが必要です。標準的な Fedora デスクトップゲストは少なくとも 3 GB の容量を必要とします。

  • ゲストごとに少なくとも 256 MB の RAM と、ベースオペレーティングシステム用に 256 MB の RAM。最新のオペレーティングシステムの各ゲストには、少なくとも 756 MB を推奨します。これを見積もる良い方法は、通常のオペレーティングシステムに必要なメモリ量を考え、その量を仮想化ゲストに割り当てることです。

KVM には、ほとんどのコンシューマー向け CPU に搭載されている仮想化拡張機能を備えた CPU が必要です。これらの拡張機能は、Intel VT または AMD-V と呼ばれます。CPU がサポートされているかどうかを確認するには、以下のコマンドを実行します:

$ grep -E '^flags.*(vmx|svm)' /proc/cpuinfo

このコマンドを実行しても何も表示されない場合は、システムが該当する仮想化拡張機能をサポートしていません。その場合でも QEMU/KVM を使用できますが、エミュレータはソフトウェア仮想化にフォールバックし、はるかに遅くなります。

仮想化ソフトウェアのインストール

Fedoraのインストーラーでインストールする際、 Base Group 内の Virtualization を選択することで、仮想化用のパッケージをインストールできます。

Fedoraが既にインストールされている場合は、コマンドラインを使用してVirtualizationパッケージグループにある仮想化ツールをインストールすることができます。このグループに属するパッケージを表示するには、以下を実行します:

$ dnf group info virtualization

Group: Virtualization
 Description: These packages provide a graphical virtualization environment.
 Mandatory Packages:
   virt-install
 Default Packages:
   libvirt-daemon-config-network
   libvirt-daemon-kvm
   qemu-kvm
   virt-manager
   virt-viewer
 Optional Packages:
   libguestfs-tools
   python3-libguestfs
   virt-top
  1. Virtualizationグループから必須パッケージおよびデフォルトのパッケージをインストールするには、以下のコマンドを実行します:

    # sudo dnf install @virtualization

    もしくは、必須、デフォルト、オプションのパッケージをインストールする場合、以下を実行します:

    # sudo dnf group install --with-optional virtualization
  2. インストール完了後、 libvirtd サービスを開始します:

    # sudo systemctl start libvirtd

    ブート時に本サービスを自動的にスタートするには、以下を実行します:

    # sudo systemctl enable libvirtd
  3. KVMのカーネルモジュールが適切に読み込まれているかを確認するには、以下を実行します:

    $ lsmod | grep kvm
    kvm_amd               114688  0
    kvm                   831488  1 kvm_amd

    このコマンドで kvm_intelkvm_amd が表示されたら、正しくKVMが設定されていることになります。

ネットワークのサポート

デフォルトでは、libvirtはゲスト用のプライベートネットワークをホストマシン上に作成します。このネットワークはサブネットが192.168.x.xに存在し、ホストマシンが属するネットワークからは直接アクセスできません。しかし、ゲストはホストマシンをゲートウェイとして経由することで外部に接続できます。もしゲスト上で実行されるサービスをホストマシンのネットワークからアクセスできるようにするには、iptablesのDNATルールでポート転送するか、ブリッジ接続します。

ブリッジネットワークの設定方法については libvirtのネットワークセットアップのページ を参照してください。

仮想マシンの作成

Anaconda を使用した Fedora ゲストのインストールがサポートされています。この方法でインストールするには、コマンドライン上で virt-install プログラムを使うか、GUIの virt-manager を使用します。

virt-installによるゲスト作成

virt-install は仮想化ゲストを作成するためのコマンドラインツールです。 virt-install --help でコマンドラインヘルプの表示、 man 1 virt-install でマニュアルの閲覧が可能です。

virt-installコマンドを使用する前に、まずインストールしたいバージョンのFedoraのISOをダウンロードする必要があります。 https://fedoraproject.org にて最新のFedoraイメージがダウンロードできます。このISOはFedoraのインストール時にのみ必要であり、その後はストレージの容量を空けるため削除しても問題ありません。

以下の例ではFedora Workstationをインストールします。

VMのスペックの決定

利用可能なリソースに応じて、RAM、vCPU、ディスクサイズのパラメーターを調整します。

  • ストレージ:bashシェルでディスクサイズを簡単に確かめるには、 df(1) ユーティリティを使用します:

    # df -h
  • メモリ:シェルでfree(1)を使うことで、利用可能なメモリを確認できます:

    # free -m
  • VCPU: lscpu(1) を使うことでプロセッサの情報を確認できます:

    # lscpu

VMにリソースを割り当てる際、該当バージョンのFedoraの最低必要スペックに注意するとともに、自身の用途も勘案してください。Fedora 42の最低必要スペックについては リリースノート を参照してください。

VM用ストレージの作成

The libvirt default storage pool is located at `/var/lib/libvirt/images - which is the parent file path we use in this example. For individuals who are lacking enough storage in that path, you can simply mount a new disk or partition to that directory path (from the BASH shell, type man 1 mount) or select a new path. In the example virt-install command below, the disk did not exist prior to running virt-install. When the specified disk is not pre-existing, you must specify the size so virt-install can create a disk for you. If your disk already exists, you can safely remove the ,size=20 parameter from the disk argument.

You have several disk storage options for your VM. While it’s outside the scope of this article to discuss these in detail, the following are a few common options. These examples use 20G as the upper limit for disk size, but you can adjust this size to fit your needs.

繰り返しますが、以下に示す virt-install の例で size パラメータを指定する場合、以下に示す例のオプションを使用して手動でストレージを割り当てる必要はありません。

Raw File (Non-Sparse)

To create a fully allocated (non-sparse) raw file:

# sudo dd if=/dev/zero of=/var/lib/libvirt/images/guest.img bs=1M count=20480

you can also use fallocate(1):

# sudo fallocate -l 20480M /var/lib/libvirt/images/guest.img
Raw File (Sparse)

To create a dynamically allocated (sparse) raw file:

# sudo rm -f /var/lib/libvirt/images/guest.img
# sudo truncate --size=20480M /var/lib/libvirt/images/guest.img
QCOW2 To create a new qcow2-formatted disk separately, you can use qemu-img (the example below specifies a disk size of 20G):
# sudo qemu-img create -f qcow2 /var/lib/libvirt/images/guest.qcow2 20480

More information about libvirt storage options can be found at https://libvirt.org/storage.html.

Finally, run the virt-install command using the following format (adjusting parameters as needed):

# sudo virt-install --name Fedora42 \
--description 'Fedora 42 Workstation' \
--ram 4096 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/Fedora-Workstation-42/Fedora-Workstation-42-20180518.0.x86_64.qcow2,size=20 \
--os-type linux \
--os-variant fedora42 \
--network bridge=virbr0 \
--graphics vnc,listen=127.0.0.1,port=5901 \
--cdrom /var/lib/libvirt/images/Fedora-Workstation-42/Fedora-Workstation-Live-x86-64-42-1.1.iso \
--noautoconsole

Note: For the graphics parameter, we’re setting the vnc listener to localhost because it’s more secure to tunnel your VNC connection through SSH so that you don’t expose VNC to everyone with access to the network.

virt-install can use kickstart files, for example, virt-install -x ks=kickstart-file-name.ks.

If graphics were enabled, a VNC window will open and present the graphical installer. If graphics were not enabled, a text installer will appear. Proceed with the Fedora installation.

Creating a guest with virt-manager

  1. Start Virtual Machine Manager by navigating to Applications  System Tools, or by running the following command:

    # sudo virt-manager
  2. Open a connection to a hypervisor by navigating to File  Add connection.

  3. Choose qemu for KVM, or Xen for Xen.

  4. Choose local or select a method to connect to a remote hypervisor.

  5. After a connection is opened, click the new icon next to the hypervisor, or right-click on the active hypervisor and select New.

  6. Configure the virtual machine following the steps in the New VM wizard.

  7. Click Finish at the end of the wizard to provision the guest operating system. After a few moments a VNC window will appear. Proceed with the Fedora installation.

仮想マシン管理

ゲストオペレーティングシステムのインストールが完了したら、virt-manager プログラムを使うか、または virsh を使ってコマンドラインから管理することができます。

virt-manager によるゲストの管理

  1. Start the Virtual Machine Manager by navigating to menu:[Applications]System Tools, or run:

    # virt-manager

    root でない場合、root パスワードを入力するようプロンプトが表示されます。

  2. Choose the host you wish to manage and click Connect in the Open Connection dialog window.

  3. 仮想マシンのリストがメインウィンドウに表示されます。実行中のゲストには「>」アイコンが表示されます。実行されていないゲストはグレーアウトされます。

  4. To manage a particular guest, double click on it, or right click and select Open.

  5. ゲストの新しいウィンドウが開き、コンソールを使用したり、仮想ハードウェアの情報を見たり、起動、停止、一時停止したりできます。

virt-manager の詳細については、 RedHat virt-manager guide を参照してください。

virt-manager ツールのバグは、 Bugzillavirt-manager コンポーネントに対して報告してください。

Managing guests with virsh

The virsh command-line utility allows you to manage virtual machines on the command line. The virsh utility is built around the libvirt management API:

  • 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 back-ends with no perceptible difference to the user.

To start a virtual machine:

# virsh create <name of virtual machine>

To list the virtual machines currently running:

# virsh list

To list all virtual machines, running or not:

# virsh list --all

To gracefully power off a guest:

# virsh shutdown <virtual machine (name | id | uuid)>

To non gracefully power off a guest:

# virsh destroy <virtual machine (name | id | uuid)>

To save a snapshot of the machine to a file:

# virsh save <virtual machine (name | id | uuid)> <filename>

To restore a previously saved snapshot:

# virsh restore <filename>

To export the configuration file of a virtual machine:

# virsh dumpxml <virtual machine (name | id | uuid)

For a complete list of commands available for use with virsh:

# virsh help

Or consult the manual page: man virsh.

Bugs in the virsh tool should be reported in Bugzilla against the libvirt component.

Remote management

The following remote management options are available:

  • If using non-root users via SSH, see the setup instructions in https://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, see https://wiki.libvirt.org/page/TLSSetup.

その他の仮想化オプション

libvirt を使用しない QEMU/KVM

QEMU/KVM は libvirt なしで直接起動できますが、virt-managervirt-installvirsh などのツールは使えません。(KVMなしの)プレーンなQEMUは、ARMやPowerPCのような他のプロセッサアーキテクチャも仮想化できます。

Xen

Fedora は Xen ゲストオペレーティングシステムとして実行することができ、Xen ホストとしても使用できます(後者は Fedora 16 以降で有効です。以前のバージョンの Fedora を Xen ホストとして使用するには、https://myoung.fedorapeople.org/dom0 にある実験的なリポジトリをチェックしてください)。 Fedora Xen ホストのインストールとセットアップ方法については、Xen Project wiki の Fedora Host Installation ページを参照してください。

OpenStack

OpenStackは、IaaS(Infrastructure as a Service)クラウドを実行するための多くのサービスで構成されており、オブジェクト・ストア(Swift)、コンピュート(Nova)、イメージ(Glance)の各サービスがあります。

OpenNebula

OpenNebula はデータセンター仮想化のためのオープンソースのツールキットです。

oVirt

oVirtプロジェクトはオープンな仮想化プロジェクトで、高可用性、ライブマイグレーション、ストレージ管理、システムスケジューラなど、ホストとゲストのための高度な機能を備えたエンドツーエンドのサーバー仮想化管理システムを提供しています。

トラブルシューティングと既知の問題

First take a look at the well-known common issues. Replace the version number by the version you are actually using.

For troubleshooting tips, see Virtualization – How to Debug Issues