labunix's blog

labunixのラボUnix

Raspberry Pi 4B(Ubuntu 20.04)にKVMを導入する。

■Raspberry Pi 4B(Ubuntu 20.04)にKVMを導入する。

$ awk -F: '/Model|Hardware/{print $NF}' /proc/cpuinfo 
 BCM2835
 Raspberry Pi 4 Model B Rev 1.4

$ lsb_release -d
Description:	Ubuntu 20.04.1 LTS

$ uname -r -m
5.4.0-1018-raspi aarch64

■あと4.5年ほどの通常サポート期間のOS。

$ w3m -dump "https://www.ubuntulinux.jp/ubuntu" | grep -A 2 -B 3 20.04
┌─────────────────┬──────────┬──────────────┬─────────────────────────────┐
│コードネーム     │バージョン│リリース日    │サポート期限                 │
├─────────────────┼──────────┼──────────────┼─────────────────────────────┤
│Focal Fossa      │20.04 LTS │2020423日 │20254(通常)              │
│                 │          │              │20304(ESM)               │
├─────────────────┼──────────┼──────────────┼─────────────────────────────┤

■KVMが有効になっていることを確認

$ dmesg | grep kvm
[    0.897886] kvm [1]: IPA Size Limit: 44bits
[    0.898809] kvm [1]: vgic interrupt IRQ1
[    0.899022] kvm [1]: Hyp mode initialized successfully

■aarch64向けのqemuを探す

$ apt-cache search ^qemu.*aarch64
qemu-efi-aarch64 - UEFI firmware for 64-bit ARM virtual machines
qemu-system-arm - QEMU full system emulation binaries (arm)

■aarch64 arm用でqemu-system-aarch64を指定すると、qemu-system-armとしてインストールされる。

$ apt-cache show qemu-system-arm | lsec ^Desc.*en
Description-en: QEMU full system emulation binaries (arm)
 QEMU is a fast processor emulator: currently the package supports
 ARM emulation. By using dynamic translation it achieves
 reasonable speed while being easy to port on new host CPUs.
 .
 This package provides the full system emulation binaries to emulate
 the following arm hardware:  aarch64 arm.
 .
 In system emulation mode QEMU emulates a full system, including a processor
 and various peripherals.  It enables easier testing and debugging of system
 code.  It can also be used to provide virtual hosting of several virtual
 machines on a single server.
Description-en: QEMU full system emulation binaries (arm)
 QEMU is a fast processor emulator: currently the package supports
 ARM emulation. By using dynamic translation it achieves
 reasonable speed while being easy to port on new host CPUs.
 .
 This package provides the full system emulation binaries to emulate
 the following arm hardware:  aarch64 arm.
 .
 In system emulation mode QEMU emulates a full system, including a processor
 and various peripherals.  It enables easier testing and debugging of system
 code.  It can also be used to provide virtual hosting of several virtual
 machines on a single server.
■qemuのインストール

$ sudo apt-get install -y qemu-system-aarch64

$ dpkg -l | awk '$1 ~ /ii/&&$2 ~/qemu/{printf "%-35s %-40s %-10s\n",$2,$3,$4}'
ipxe-qemu                           1.0.0+git-20190109.133f4c4-0ubuntu3.2    all       
ipxe-qemu-256k-compat-efi-roms      1.0.0+git-20150424.a25a16d-0ubuntu4      all       
qemu-block-extra:arm64              1:4.2-3ubuntu6.6                         arm64     
qemu-efi-aarch64                    0~20191122.bd85bf54-2ubuntu3             all       
qemu-efi-arm                        0~20191122.bd85bf54-2ubuntu3             all       
qemu-system-arm                     1:4.2-3ubuntu6.6                         arm64     
qemu-system-common                  1:4.2-3ubuntu6.6                         arm64     
qemu-system-data                    1:4.2-3ubuntu6.6                         all       
qemu-system-gui:arm64               1:4.2-3ubuntu6.6                         arm64     
qemu-utils                          1:4.2-3ubuntu6.6                         arm64     

■QEMUのバージョン確認

$ ls -l /usr/bin/qemu*
-rwxr-xr-x 1 root root  1974648  915 23:02 /usr/bin/qemu-img
-rwxr-xr-x 1 root root  1933896  915 23:02 /usr/bin/qemu-io
-rwxr-xr-x 1 root root  1930248  915 23:02 /usr/bin/qemu-nbd
-rwxr-xr-x 1 root root   722328  915 23:02 /usr/bin/qemu-pr-helper
-rwxr-xr-x 1 root root 18305048  915 23:02 /usr/bin/qemu-system-aarch64
-rwxr-xr-x 1 root root 17239584  915 23:02 /usr/bin/qemu-system-arm

$ qemu-system-aarch64 -version
QEMU emulator version 4.2.1 (Debian 1:4.2-3ubuntu6.6)
Copyright (c) 2003-2019 Fabrice Bellard and the QEMU Project developers

■aspberry Pi 4BのCPUはCortex-A72なので対応していることを確認

$ sudo dmesg | awk '/[Cc]ortex/'
[    0.897682] hw perfevents: enabled with armv8_cortex_a72 PMU driver, 7 counters available

$ qemu-system-aarch64 -cpu help -machine virt | awk '/:|cortex-a72/'
Available CPUs:
  cortex-a72

■KVM用のブリッジネットワーク準備

$ sudo apt-get install -y bridge-utils

$ brctl show

■virt-managerを探してインストール
 libvirtグループに自動追加されたユーザはvirshコマンドが使える。

$ apt-cache search libvirt0;apt-cache search ^virt-manager 
libvirt0 - library for interfacing with different virtualization systems
virt-manager - 仮想マシン管理用デスクトップアプリケーション

$ sudo apt-get install -y libvirt0 virt-manager

$ grep libvirt /etc/group
libvirt:x:138:ubuntu,labunix
libvirt-qemu:x:64055:libvirt-qemu
libvirt-dnsmasq:x:139:

$ virsh list
 Id   Name   State
--------------------

$ sudo systemctl list-units --type=service | grep libvirt
  libvirt-guests.service                                   loaded active exited  Suspend/Resume Running libvirt Guests                                        
  libvirtd.service                                         loaded active running Virtualization daemon                                                        
■システムの再起動

$ sudo shutdown -r now && exit

■起動後のブリッジネットワーク。
 仮想マシンマネージャが起動したら、あとはお好きなように。

$ brctl show
bridge name	bridge id		STP enabled	interfaces
virbr0		8000.5254005d3558	yes		virbr0-nic