qemu の概要
qemu は Linux
上における仮想化ソリューションを提供します。 仮想化拡張 (Intel VT または AMD-V) を持つ x86
ハードウェア上にて動作します。
注記
LFS や依存パッケージが本ブックに示す最新安定バージョンでなかった場合には、BLFS
開発版においては、パッケージのビルドや処理実行が適切に行われないことがあります。
パッケージ情報
-
ダウンロード (HTTP): https://download.qemu.org/qemu-8.2.0.tar.xz
-
ダウンロード MD5 sum: 302be965cf5e54e7f8b4bd7f1248029d
-
ダウンロードサイズ: 124 MB
-
必要ディスク容量: 2.1 GB (563 MB installed)
-
概算ビルド時間: 1.4 SBU (add 0.8 SBU for tests, both using
parallelism=4)
Qemu の依存パッケージ
必須
GLib-2.78.3, Pixman-0.42.2
推奨
alsa-lib-1.2.10, Libslirp-4.7.0, SDL2-2.28.5
任意
pipewire-1.0.0 or PulseAudio-16.1 (can be used instead of
alsa-lib), BlueZ-5.71, cURL-8.5.0,
Cyrus SASL-2.1.28, Fuse-3.16.2, GnuTLS-3.8.2, GTK+-3.24.38,
keyutils-1.6.1, libaio-0.3.113,
libusb-1.0.26, libgcrypt-1.10.3, libjpeg-turbo-3.0.1, libseccomp-2.5.5, libssh2-1.11.0, libpng-1.6.40,
libtasn1-4.19.0, Linux-PAM-1.5.3,
LZO-2.10, Nettle-3.9.1, Mesa-23.3.1, VTE-0.74.2,
capstone, ceph, daxctl, JACK, glusterfs, libbpf,
libcacard,
libcap-ng,
libdw,
libfdt,
libiscsi, libnfs,
libpmem,
libssh,
libu2f-emu,
lzfse,
netmap, numactl, rdma-core, SELinux, snappy,
spice, usbredir, and
VDE
Optional (Runtime)
elogind-252.9
Optional (for building the documentation)
sphinx_rtd_theme-2.0.0
注記
This optional dependencies list is not comprehensive. See the
output of ./configure
--help for a more complete list.
編集ノート: https://wiki.linuxfromscratch.org/blfs/wiki/qemu
qemu のインストール
The udev rule of LFS only allows the root
user, the users owning a local login
session supported by the optional runtime dependency elogind-252.9 , or the users in the
kvm
group to use the KVM device. As
the root
user, add any
non-root
users that might use the
KVM device either without elogind-252.9 installed or remotely (via
a SSH connection) to the kvm
group:
usermod -a -G kvm <username>
以下のコマンドを実行して qemu をビルドします。
注記
Qemu is capable of running many targets. The build process is
also capable of building multiple targets at one time in a comma
delimited list assigned to --target-list
. Run ./configure --help to get a
complete list of available targets.
if [ $(uname -m) = i686 ]; then
QEMU_ARCH=i386-softmmu
else
QEMU_ARCH=x86_64-softmmu
fi
mkdir -vp build &&
cd build &&
../configure --prefix=/usr \
--sysconfdir=/etc \
--localstatedir=/var \
--target-list=$QEMU_ARCH \
--audio-drv-list=alsa \
--disable-pa \
--enable-slirp \
--docdir=/usr/share/doc/qemu-8.2.0 &&
unset QEMU_ARCH &&
make
qemu uses ninja as a subprocess when
building. To run the tests, issue: ninja test. Two tests,
bios-tables-test and migration-test, are known to fail.
root
ユーザーになって以下を実行します。
make install
Change the permissions and ownership of a helper script, which is
needed when using the 「bridge」 network device (see below). Again as
the root
user, issue:
注記
You need to add any users who might use the 「bridge」 network device
into the kvm
group even if
elogind-252.9 is installed.
chgrp kvm /usr/libexec/qemu-bridge-helper &&
chmod 4750 /usr/libexec/qemu-bridge-helper
注記
For convenience you may want to create a symbolic link to run the
installed program. For instance (as the root
user):
ln -sv qemu-system-`uname -m` /usr/bin/qemu
Using Qemu
Since using qemu means using a virtual computer, the steps to set
up the virtual machine are in close analogy with those to set up a
real computer. You'll need to decide about CPU, memory, disk, USB
devices, network card(s), screen size, etc. Once the 「hardware」 is decided,
you'll have for example to choose how to connect the machine to
internet, and/or to install an OS. In the following, we show basic
ways of performing those steps. But qemu is much more than this,
and it is strongly advised to read the qemu documentation in
/usr/share/doc/qemu-8.2.0/qemu-doc.html
.
注記
It is standard practice to name the computer running qemu
「host」 and
the emulated machine running under qemu the 「guest」. We'll use
those notations in the following.
注記
The following instructions assume the optional symbolic link,
qemu
, has been created.
Additionally, qemu
should be run in a graphical environment. But it is possible to
use qemu 「headless」 or through SSH. See the
documentation for the various possibilities.
Disk
A virtual disk may be set up in the following way:
VDISK_SIZE=50G
VDISK_FILENAME=vdisk.img
qemu-img create -f qcow2 $VDISK_FILENAME $VDISK_SIZE
The virtual disk size and filename should be adjusted as desired.
The actual size of the file will be less than specified, but will
expand as needed, so it is safe to put a high value.
Operating System
To install an operating system, download an iso image from your
preferred Linux distribution. For the purposes of this example,
we'll use Fedora-16-x86_64-Live-LXDE.iso
in the current
directory. Run the following:
qemu -enable-kvm \
-drive file=$VDISK_FILENAME \
-cdrom Fedora-16-x86_64-Live-LXDE.iso \
-boot d \
-m 1G
Follow the normal installation procedures for the chosen
distribution. The -boot
option specifies the boot order of drives as a string of drive
letters. Valid drive letters are: a, b (floppy 1 and 2), c (first
hard disk), d (first CD-ROM). The -m
option is the amount of memory to
use for the virtual machine. The choice depends on the load of the
host. Modern distributions should be comfortable with 1GB. The
-enable-kvm
option allows
hardware acceleration. Without this switch, the emulation is much
slower.
Defining the virtual hardware
The virtual machine hardware is defined by the qemu command line.
An example command is given below:
qemu -enable-kvm \
-smp 4 \
-cpu host \
-m 1G \
-drive file=$VDISK_FILENAME \
-cdrom grub-img.iso \
-boot order=c,once=d,menu=on \
-net nic,netdev=net0 \
-netdev user,id=net0 \
-device ac97 \
-vga std \
-serial mon:stdio \
-name "fedora-16"
Meaning of the command line options
-enable-kvm
: enable full
KVM virtualization support. On some hardware, it may be necessary
to add the undocumented -machine
smm=off
option in order to enable KVM.
-smp <N>
: enable
symmetric multiprocessing with <N> CPUs.
-cpu <model>
:
simulate CPU <model>. the list of supported models can be
obtained with -cpu help
.
-drive
file=<filename>
: defines a virtual disk whose
image is stored in <filename>
.
-cdrom grub-img.iso
:
defines an iso formatted file to use as a cdrom. Here we use a grub
rescue disk, which may turn handy when something goes wrong at boot
time.
-boot
order=c,once=d,menu=on
: defines the boot order for the
virtual BIOS.
-net
nic,netdev=<netid>
: defines a network card
connected to the network device with id <netid>.
-netdev
user,id=<netid>
: defines the network 「user」 device. This is a
virtual local network with addresses 10.0.2.0/24, where the host
has address 10.0.2.2 and acts as a gateway to internet, and with a
name server at address 10.0.2.3, and an smb server at address
10.0.2.4. A builtin DHCP server can allocate addresses between
10.0.2.15 and 10.0.2.31.
-soundhw <model>
:
defines the soundcard model. The list may be obtained with
-soundhw help
.
-vga <type>
: defines
the type of VGA card to emulate. For -vga std
, if you are building a Linux
kernel for the guest, it's recommended to enable CONFIG_DRM_BOCHS
(as a part of the kernel or a
kernel module) to drive all the features of the emulated VGA card,
and CONFIG_FB
to display the Linux
console on it. The other <type>
values are not tested by the editors and may require additional
dependencies.
-serial mon:stdio
: sends
the serial port of the guest (/dev/ttyS0
on linux guests), multiplexed with the
qemu monitor, to the standard input and output of the qemu process.
-name <name>
: sets
the name of the guest. This name is displayed in the guest window
caption. It may be useful if you run several guests at the same
time.
-drive
if=pflash,format=raw,readonly=on,file=/usr/share/qemu/edk2-x86_64-code.fd
:
Load a pre-built EDK2 UEFI firmware, instead of the default PC
BIOS. Use this option if you want to boot the guest OS with UEFI.
-drive file=<filename>,if=virtio
:
Provide Virtio interface to the guest kernel for accessing the disk
image, instead of simulating a real disk hardware. This can improve
disk I/O performance, but it requires a Virtio driver in guest
kernel. Use it instead of a plain -drive
if the guest kernel supports Virtio. To
build a Linux kernel with Virtio support for the guest, use
make defconfig && make
kvm_guest.config to create an initial kernel
configuration with the Virtio drives enabled, then make your
customization. And, if the guest kernel is Linux, the virtual disks
using Virtio interface will be named vdx
in the devtmpfs, instead of sdx
.
-net
nic,netdev=net0,model=virtio-net-pci
: Provide Virtio
interface to the guest kernel for accessing the network interface,
instead of simulating a real network interface card. This can
improve network I/O performance, but it requires a Virtio driver in
guest kernel. Use it instead of a plain -net
if the guest kernel supports Virtio.
Controlling the Emulated Display
To set the resolution of the emulated display for a Xorg server
running in the guest Linux system, read 「画面設定のチューニング」.
Networking
The above solution for networking allows the guest to access the
local network through the host (and possibly to access internet
through the local routers), but the converse is not true. Not even
the host can access the guest, unless port forwarding is enabled.
And in the case several guests are running, they cannot communicate
with each other. Other network devices can be used for this
purpose. For example, there is the 「socket」 device, which
allows several guests to share a common virtual network. In the
following, we describe in more details how to set up the
「bridge」
device, which allows the guests to appear as if connected to the
local network. All the commands below should be run as the
root
user.
Set up bridging with bridge-utils-1.7.1. Only the physical
interface(s) should be set up at boot. The virtual interface(s)
will be added as needed when qemu is started.
Set up a required configuration file:
install -vdm 755 /etc/qemu &&
echo allow br0 > /etc/qemu/bridge.conf
In the qemu command line above, replace the switch -netdev user,...
with -netdev bridge,...
.