🧘

Debian11 で Xen

2023/02/23に公開

環境について

Debian11の環境で、Xen のインストール、仮想マシンの作成をしてみます。

$ cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

必要パッケージの導入

# apt install --no-install-recommends xen-system-amd64 qemu-system-x86

dom0 の設定

dom0 のメモリ容量の自動伸縮を禁止します。

# cp -pi /etc/xen/xl.conf /etc/xen/xl.conf.000
# vi /etc/xen/xl.conf
# diff -u /etc/xen/xl.conf /etc/xen/xl.conf.000
--- /etc/xen/xl.conf    2023-02-11 17:17:42.300000000 -0500
+++ /etc/xen/xl.conf.000        2021-10-24 15:05:31.000000000 -0400
@@ -8,7 +8,6 @@
 # free memory to create a domain.  "auto" means only balloon if dom0
 # starts with all the host's memory.
 #autoballoon="auto"
-autoballoon=0

 # full path of the lockfile used by xl during domain creation
 #lockfile="/var/lock/xl"

dom0 で利用するメモリ容量を設定します。

# cp -pi /etc/default/grub /etc/default/grub.000
# vi /etc/default/grub
# diff -u /etc/default/grub /etc/default/grub.000
--- /etc/default/grub   2023-02-11 17:19:47.868000000 -0500
+++ /etc/default/grub.000       2023-02-05 00:49:51.956000000 -0500
@@ -30,6 +30,3 @@

 # Uncomment to get a beep at grub start
 #GRUB_INIT_TUNE="480 440 1"
-
-# Xen boot parameters for all Xen boots
-GRUB_CMDLINE_XEN="dom0_mem=1024M,max:1024M"

GRUBメニューの設定を反映します。

# update-grub
Including Xen overrides from /etc/default/grub.d/xen.cfg
WARNING: GRUB_DEFAULT changed to boot into Xen by default!
         Edit /etc/default/grub.d/xen.cfg to avoid this warning.
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.10.0-21-amd64
Found initrd image: /boot/initrd.img-5.10.0-21-amd64
Found linux image: /boot/vmlinuz-5.10.0-20-amd64
Found initrd image: /boot/initrd.img-5.10.0-20-amd64
Found linux image: /boot/vmlinuz-5.10.0-21-amd64
Found initrd image: /boot/initrd.img-5.10.0-21-amd64
Found linux image: /boot/vmlinuz-5.10.0-20-amd64
Found initrd image: /boot/initrd.img-5.10.0-20-amd64
Found linux image: /boot/vmlinuz-5.10.0-21-amd64
Found initrd image: /boot/initrd.img-5.10.0-21-amd64
Found linux image: /boot/vmlinuz-5.10.0-20-amd64
Found initrd image: /boot/initrd.img-5.10.0-20-amd64
Warning: os-prober will be executed to detect other bootable partitions.
Its output will be used to detect bootable binaries on them and create new boot entries.
done

OSを再起動します。

# shutdown -r now

確認

dom0 として動いていることを確認します。

# xl list
Name                                        ID   Mem VCPUs      State   Time(s)
Domain-0                                     0  1024     1     r-----       5.4

# xl info
host                   : debian
release                : 5.10.0-21-amd64
version                : #1 SMP Debian 5.10.162-1 (2023-01-21)
machine                : x86_64
nr_cpus                : 1
max_cpu_id             : 0
nr_nodes               : 1
cores_per_socket       : 1
threads_per_core       : 1
cpu_mhz                : 3200.019
hw_caps                : 078bf3ff:f7f83203:2e500800:000003f7:0000000f:209c01ab:00000000:00000000
virt_caps              : pv hvm hap shadow
total_memory           : 4095
free_memory            : 3021
sharing_freed_memory   : 0
sharing_used_memory    : 0
outstanding_claims     : 0
free_cpus              : 0
xen_major              : 4
xen_minor              : 14
xen_extra              : .5
xen_version            : 4.14.5
xen_caps               : xen-3.0-x86_64 hvm-3.0-x86_32 hvm-3.0-x86_32p hvm-3.0-x86_64
xen_scheduler          : credit2
xen_pagesize           : 4096
platform_params        : virt_start=0xffff800000000000
xen_changeset          :
xen_commandline        : placeholder dom0_mem=1024M,max:1024M
cc_compiler            : x86_64-linux-gnu-gcc (Debian 10.2.1-6) 10.2.1 20210110
cc_compile_by          : pkg-xen-devel
cc_compile_domain      : lists.alioth.debian.org
cc_compile_date        : Fri Nov  4 19:25:46 UTC 2022
build_id               : 888699a351d5cbf2705344205565dd20479f1b14
xend_config_format     : 4

dom0 のメモリ容量が設定した値となっていることを確認します。

# uname -a
Linux debian 5.10.0-21-amd64 #1 SMP Debian 5.10.162-1 (2023-01-21) x86_64 GNU/Linux

# free -m
               total        used        free      shared  buff/cache   available
Mem:             906          70         729           0         106         817
Swap:            974           0         974

仮想ブリッジ

ここでは Open vSwitch の 仮想ブリッジ ovsbr0 を利用することにします。
導入については https://zenn.dev/mnod/articles/c67fe726fb635b を参照。

# ovs-vsctl show
dfb99cb1-056d-4ab4-b28e-a1342190d3f8
    Bridge ovsbr0
        Port ens4
            Interface ens4
        Port ovsbr0
            Interface ovsbr0
                type: internal
    ovs_version: "2.15.0"

# ip addr show ovsbr0
7: ovsbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default qlen 1000
    link/ether e2:2d:b5:1c:26:43 brd ff:ff:ff:ff:ff:ff
    inet 10.2.0.241/24 brd 10.2.0.255 scope global ovsbr0
       valid_lft forever preferred_lft forever
    inet6 fe80::e02d:b5ff:fe1c:2643/64 scope link
       valid_lft forever preferred_lft forever

関連パッケージの導入

# apt install --no-install-recommends xen-tools

使用するブリッジの設定を変更します。

# cp -pi /etc/xen/xl.conf /etc/xen/xl.conf.001
# vi /etc/xen/xl.conf
# diff -u /etc/xen/xl.conf /etc/xen/xl.conf.001
--- /etc/xen/xl.conf    2023-02-11 18:02:21.512037528 -0500
+++ /etc/xen/xl.conf.001        2023-02-11 17:17:42.300000000 -0500
@@ -33,11 +33,9 @@

 # default vif script to use if none is specified in the guest config
 #vif.default.script="vif-bridge"
-vif.default.script="vif-openvswitch"

 # default bridge device to use with vif-bridge hotplug scripts
 #vif.default.bridge="xenbr0"
-vif.default.bridge="ovsbr0"

 # Reserve a claim of memory when launching a guest. This guarantees immediate
 # feedback whether the guest can be launched due to memory exhaustion

利用するネットワークアドレスの情報を設定します。

# cp -pi /etc/xen-tools/xen-tools.conf /etc/xen-tools/xen-tools.conf.000
# vi /etc/xen-tools/xen-tools.conf
# diff -u /etc/xen-tools/xen-tools.conf /etc/xen-tools/xen-tools.conf.000
--- /etc/xen-tools/xen-tools.conf       2023-02-11 18:04:15.536039299 -0500
+++ /etc/xen-tools/xen-tools.conf.000   2020-12-29 06:19:51.000000000 -0500
@@ -162,9 +162,6 @@
 # gateway    = 192.168.1.1
 # netmask    = 255.255.255.0
 # broadcast  = 192.168.1.255
-gateway    = 10.2.0.1
-netmask    = 255.255.255.0
-broadcast  = 10.2.0.255
 #
 # Uncomment this if you wish the images to use DHCP
 #
@@ -178,7 +175,6 @@
 # be copied to guest.
 #
 # nameserver = 192.168.1.1
-nameserver = 192.168.11.249
 #

 #

domU の起動

domU のイメージを作成します。しばらく時間がかかります。

# xen-create-image --hostname=test --pygrub --ip 10.2.0.250 --dir /home/user --memory=1G --maxmem=1G --noswap --size=4G --passwd

General Information
--------------------
Hostname       :  test
Distribution   :  bullseye
Mirror         :  http://deb.debian.org/debian
Partitions     :  /               4G    (ext4)
Image type     :  sparse
Memory size    :  1G
Max mem size   :  1G
Bootloader     :  pygrub

Networking Information
----------------------
IP Address 1   : 10.2.0.250 [MAC: 00:16:3E:DD:03:0D]
Netmask        : 255.255.255.0
Broadcast      : 10.2.0.255
Gateway        : 10.2.0.1
Nameserver     : 192.168.11.249

WARNING
-------
Loopback module not loaded and you're using loopback images
Run the following to load the module:

modprobe loop max_loop=255


Creating partition image: /home/user/domains/test/disk.img
Done

Creating ext4 filesystem on /home/user/domains/test/disk.img
Done
Installation method: debootstrap
Done

Running hooks
Done

No role scripts were specified.  Skipping

Creating Xen configuration file
Done

No role scripts were specified.  Skipping
Setting up root password
New password:
Retype new password:
passwd: password updated successfully
All done


Logfile produced at:
         /var/log/xen-tools/test.log

Installation Summary
---------------------
Hostname        :  test
Distribution    :  bullseye
MAC Address     :  00:16:3E:DD:03:0D
IP Address(es)  :  10.2.0.250
SSH Fingerprint :  SHA256:zsIh0Cp7w0yLEANDVkME+Zyjxa6ToXV/CsAl/kiblxs (DSA)
SSH Fingerprint :  SHA256:a9bK2g7yJOYOA5IxSR8YvNYwJnQ7X86gfa5m4c8QlsQ (ECDSA)
SSH Fingerprint :  SHA256:P1wWY1sqiglUJdJ/Zel4p0VOA21Qa0CqIod1e2O6WuU (ED25519)
SSH Fingerprint :  SHA256:f9aVegQj73N0VtHMPLlghAATZq+uCd/nwh4sC4cUaq0 (RSA)
Root Password   :  N/A

作成された cfg ファイルを確認します。

# egrep -v '^#|^$' /etc/xen/test.cfg
bootloader = 'pygrub'
vcpus       = '1'
memory      = '1024'
maxmem      = '1024'
root        = '/dev/xvda1 ro'
disk        = [
                  'file:/home/user/domains/test/disk.img,xvda1,w',
              ]
name        = 'test'
vif         = [ 'ip=10.2.0.250 ,mac=00:16:3E:DD:03:0D' ]
on_poweroff = 'destroy'
on_reboot   = 'restart'
on_crash    = 'restart'

domU を起動します。

# xl create /etc/xen/test.cfg
Parsing config from /etc/xen/test.cfg

# xl list
Name                                        ID   Mem VCPUs      State   Time(s)
Domain-0                                     0  1024     1     r-----     203.8
test                                         1  1024     1     -b----       4.8

# ls -lhs /home/user/domains/test/disk.img
1.3G -rw------- 1 root root 4.0G Feb 11 18:21 /home/user/domains/test/disk.img

xl console コマンドでコンソールに接続します。コンソールから切断するには “Ctrl + ]”を入力します。

# xl console test

xl shutdown コマンドで domU を停止します。

# xl shutdown test
Shutting down domain 1

# xl list
Name                                        ID   Mem VCPUs      State   Time(s)
Domain-0                                     0  1024     1     r-----     210.4
GitHubで編集を提案

Discussion