BSD界隈四方山話

第23回ハイパーバイザbhyveでOpenBSDを使ってみよう

OpenBSD on bhyve

bhyveを使うとFreeBSD以外のオペレーティングシステムを実行できます。今回はbhyveでOpenBSDを利用する方法を紹介します。OpenBSDはLibreSSLの開発やpf(4)の開発など興味深い取り組みを行っているプロジェクトです。簡単に利用できる環境を用意しておくとなにかと便利です。

今回は次の条件でセットアップを行います。

  • ホスト:FreeBSD 10.2-RELEASE/amd64
  • ゲスト:OpenBSD 5.7/amd64
  • ホストNIC:bge0
  • ゲストNIC:tap0(via bridge0)
  • 仮想CPU:1個
  • 仮想メモリ:2GB
  • 仮想ディスク(32BG⁠⁠:/dev/zvol/z/openbsd-5.7
  • インストーラ: /d/bhyve/install57.iso
  • grub-bhyve設定ファイル:/d/bhyve/openbsd-5.7-device.map

細かい説明はこれまでのbhyve記事を参考にしてください。

ホストのセットアップ

ホストのセットアップを行います。vmmカーネルモジュールを読み込み、bridge0にtap0とbge0を追加します。

図1 ホスト環境をセットアップ
# kldload vmm
# ifconfig tap0 create
# sysctl net.link.tap.up_on_open=1
net.link.tap.up_on_open: 0 -> 1
# ifconfig bridge0 create
# ifconfig bridge0 addm bge0 addm tap0 up

再起動したときも同じコマンドが実行されるように3つの設定ファイルにそれぞれ次のような設定を追加します。

リスト1 /boot/loader.confに追加する設定
vmm_load="YES"
リスト2 /etc/sysctl.confに追加する設定
net.link.tap.up_on_open=1
リスト3 /etc/rc.confに追加する設定
cloned_interfaces="bridge0 tap0"
ifconfig_bridge0="addm bge0 addm tap0"

ゲストのインストール

FreeBSD以外のオペレーティングシステムをbhyveで使う場合、カーネルのローディングにgrub-bhyveを使います。grub-bhyveで使う設定ファイルを次のように用意します。

リスト4 grub-bhyve for OpenBSDのマッピングファイル openbsd-5.7-device.map
(hd0) /dev/zvol/z/openbsd-5.7
(cd0) /d/bhyve/install57.iso

OpenBSD 5.7のインストールイメージファイルをダウンロードするとともに、次のようにZFSを使って仮想ディスクを作成します。

図2 仮想ディスクの作成
# fetch ftp://ftp.kddilabs.jp/OpenBSD/5.7/amd64/install57.iso
install57.iso                                 100% of  218 MB 4525 kBps 00m49s
# zfs create -V 32G -o volmode=dev z/openbsd-5.7

次のようにgrub-bhyveを実行してOpenBSDカーネルを読み込みます。

図3 OpenBSD 5.7カーネルの読み込み
# grub-bhyve -m openbsd-5.7-device.map -r cd0 -M 2G openbsd-5.7

                             GNU GRUB  version 2.00

   Minimal BASH-like line editing is supported. For the first word, TAB
   lists possible command completions. Anywhere else TAB lists possible
   device or file completions.

grub> ls (cd0)
Device cd0: Filesystem type iso9660 - Label \`OpenBSD/amd64   5.7 Install CD' -
Last modification time 2015-03-06 01:10:10 Friday, UUID 2015-03-08-11-10-10-00
- Total size 448444 sectors
grub> ls (cd0)/
5.7/ etc/ TRANS.TBL
grub> ls (cd0)/5.7
amd64/ TRANS.TBL
grub> ls (cd0)/5.7/amd64/
base57.tgz boot.catalog bsd bsd.mp bsd.rd cdboot cdbr comp57.tgz game57.tgz INS
TALL.amd64 man57.tgz SHA256 TRANS.TBL xbase57.tgz xfont57.tgz xserv57.tgz xshar
e57.tgz
grub> kopenbsd -h com0 (cd0)/5.7/amd64/bsd.rd
grub> boot
#
図4 OpenBSD 5.7インストーラの起動
# bhyve \
    -H -P -A \
    -W -c 1 \
    -m 2G \
    -l com1,stdio \
    -s 0:0,hostbridge \
    -s 1:0,lpc \
    -s 2:0,virtio-net,tap0 \
    -s 3,ahci-cd,/d/bhyve/install57.iso \
    -s 4,virtio-blk,/dev/zvol/z/openbsd-5.7 \
    openbsd-5.7

ちょっと長くなりますが、OpenBSD 5.7インストールの様子を次に掲載しておきます。

図5 OpenBSD 5.7インストール
Copyright (c) 1982, 1986, 1989, 1991, 1993
        The Regents of the University of California.  All rights reserved.
Copyright (c) 1995-2015 OpenBSD. All rights reserved.  http://www.OpenBSD.org

OpenBSD 5.7 (RAMDISK_CD) #806: Sun Mar  8 11:08:49 MDT 2015
    deraadt@amd64.openbsd.org:/usr/src/sys/arch/amd64/compile/RAMDISK_CD
real mem = 2130706432 (2032MB)
avail mem = 2072354816 (1976MB)
mainbus0 at root
bios0 at mainbus0: SMBIOS rev. 2.6 @ 0xf101f (9 entries)
bios0: vendor BHYVE version "1.00" date 03/14/2014
acpi0 at bios0: rev 2
acpi0: sleep states S5
acpi0: tables DSDT APIC FACP HPET MCFG
acpimadt0 at acpi0 addr 0xfee00000: PC-AT compat
cpu0 at mainbus0: apid 0 (boot processor)
cpu0: Intel(R) Xeon(R) CPU E3-1220 v3 @ 3.10GHz, 3092.79 MHz
cpu0: FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,MMX,FXSR,SSE,SSE2,SS,HTT,PBE,SSE3,PCLMUL,DTES64,DS-CPL,SSSE3,FMA3,CX16,xTPR,PCID,SSE4.1,SSE4.2,MOVBE,POPCNT,AES,XSAVE,AVX,F16C,RDRAND,HV,NXE,PAGE1GB,LONG,LAHF,ABM,ITSC,FSGSBASE,BMI1,AVX2,BMI2,ERMS,INVPCID
cpu0: 256KB 64b/line 8-way L2 cache
cpu0: apic clock running at 134MHz
ioapic0 at mainbus0: apid 0 pa 0xfec00000, version 11, 24 pins
acpiprt0 at acpi0: bus 0 (PC00)
pci0 at mainbus0 bus 0
pchb0 at pci0 dev 0 function 0 unknown vendor 0x1275 product 0x1275 rev 0x00
"Intel 82371SB ISA" rev 0x00 at pci0 dev 1 function 0 not configured
virtio0 at pci0 dev 2 function 0 "Qumranet Virtio Network" rev 0x00: Virtio Network Device
vio0 at virtio0: address 00:a0:98:9b:e9:28
virtio0: apic 0 int 16
ahci0 at pci0 dev 3 function 0 "Intel 82801H AHCI" rev 0x00: apic 0 int 17, AHCI 1.3
scsibus0 at ahci0: 32 targets
cd0 at scsibus0 targ 0 lun 0: <BHYVE, BHYVE DVD-ROM, 001> ATAPI 5/cdrom removable
virtio1 at pci0 dev 4 function 0 "Qumranet Virtio Storage" rev 0x00: Virtio Block Device
vioblk0 at virtio1
scsibus1 at vioblk0: 2 targets
sd0 at scsibus1 targ 0 lun 0: <VirtIO, Block Device, > SCSI3 0/direct fixed
sd0: 32768MB, 512 bytes/sector, 67108864 sectors
virtio1: apic 0 int 18
isa0 at mainbus0
com0 at isa0 port 0x3f8/8 irq 4: ns16550a, 16 byte fifo
com0: console
com1 at isa0 port 0x2f8/8 irq 3: ns16550a, 16 byte fifo
pckbc0 at isa0 port 0x60/5
pckbd0 at pckbc0 (kbd slot)
pckbc0: using irq 1 for kbd slot
wskbd0 at pckbd0: console keyboard
softraid0 at root
scsibus2 at softraid0: 256 targets
root on rd0a swap on rd0b dump on rd0b
erase ^?, werase ^W, kill ^U, intr ^C, status ^T

Welcome to the OpenBSD/amd64 5.7 installation program.
(I)nstall, (U)pgrade, (A)utoinstall or (S)hell? I
At any prompt except password prompts you can escape to a shell by
typing '!'. Default answers are shown in []'s and are selected by
pressing RETURN.  You can exit this program at any time by pressing
Control-C, but this can leave your system in an inconsistent state.

Terminal type? [vt220] xterm
System hostname? (short form, e.g. 'foo') trybsd.ongs.co.jp

Available network interfaces are: vio0 vlan0.
Which network interface do you wish to configure? (or 'done') [vio0]
IPv4 address for vio0? (or 'dhcp' or 'none') [dhcp]
DHCPDISCOVER on vio0 - interval 1
DHCPOFFER from 192.168.1.1 (00:0d:b9:2c:6c:60)
DHCPREQUEST on vio0 to 255.255.255.255
DHCPACK from 192.168.1.1 (00:0d:b9:2c:6c:60)
bound to 192.168.1.24 -- renewal in 300 seconds.
IPv6 address for vio0? (or 'rtsol' or 'none') [none]
Available network interfaces are: vio0 vlan0.
Which network interface do you wish to configure? (or 'done') [done]
Using DNS domainname ongs.co.jp
Using DNS nameservers at 202.216.224.66 202.216.246.91 202.216.224.67

Password for root account? (will not echo)
Password for root account? (again)
Start sshd(8) by default? [yes]
Start ntpd(8) by default? [no]
Change the default console to com0? [yes]
Available speeds are: 9600 19200 38400 57600 115200.
Which speed should com0 use? (or 'done') [9600] 115200
Setup a user? (enter a lower-case loginname, or 'no') [no]
What timezone are you in? ('?' for list) [Asia/Tokyo] ?
Africa/      Chile/       GB-Eire      Israel       NZ-CHAT      UCT
America/     Cuba         GMT          Jamaica      Navajo       US/
Antarctica/  EET          GMT+0        Japan        PRC          UTC
Arctic/      EST          GMT-0        Kwajalein    PST8PDT      Universal
Asia/        EST5EDT      GMT0         Libya        Pacific/     W-SU
Atlantic/    Egypt        Greenwich    MET          Poland       WET
Australia/   Eire         HST          MST          Portugal     Zulu
Brazil/      Etc/         Hongkong     MST7MDT      ROC          posix/
CET          Europe/      Iceland      Mexico/      ROK          posixrules
CST6CDT      Factory      Indian/      Mideast/     Singapore    right/
Canada/      GB           Iran         NZ           Turkey
What timezone are you in? ('?' for list) [Asia/Tokyo] GMT

Available disks are: sd0.
Which disk is the root disk? ('?' for details) [sd0]
Use DUIDs rather than device names in fstab? [yes]
Disk: sd0       geometry: 4177/255/63 [67108864 Sectors]
Offset: 0       Signature: 0xAA55
            Starting         Ending         LBA Info:
 #: id      C   H   S -      C   H   S [       start:        size ]
-------------------------------------------------------------------------------
 0: 00      0   0   0 -      0   0   0 [           0:           0 ] unused
 1: 00      0   0   0 -      0   0   0 [           0:           0 ] unused
 2: 00      0   0   0 -      0   0   0 [           0:           0 ] unused
*3: A6      0   1   2 -   4176 254  63 [          64:    67103441 ] OpenBSD
Use (W)hole disk, use the (O)penBSD area, or (E)dit the MBR? [OpenBSD]
The auto-allocated layout for sd0 is:
#                size           offset  fstype [fsize bsize  cpg]
  a:             1.0G               64  4.2BSD   2048 16384    1 # /
  b:             2.2G          2097216    swap
  c:            32.0G                0  unused
  d:             2.1G          6783040  4.2BSD   2048 16384    1 # /tmp
  e:             3.3G         11222656  4.2BSD   2048 16384    1 # /var
  f:             2.0G         18201504  4.2BSD   2048 16384    1 # /usr
  g:             1.0G         22395808  4.2BSD   2048 16384    1 # /usr/X11R6
  h:             4.5G         24492960  4.2BSD   2048 16384    1 # /usr/local
  i:             1.5G         33929568  4.2BSD   2048 16384    1 # /usr/src
  j:             2.0G         37075168  4.2BSD   2048 16384    1 # /usr/obj
  k:            12.3G         41269472  4.2BSD   2048 16384    1 # /home
Use (A)uto layout, (E)dit auto layout, or create (C)ustom layout? [a]
/dev/rsd0a: 1024.0MB in 2097152 sectors of 512 bytes
6 cylinder groups of 202.47MB, 12958 blocks, 25984 inodes each
/dev/rsd0k: 12614.2MB in 25833952 sectors of 512 bytes
63 cylinder groups of 202.47MB, 12958 blocks, 25984 inodes each
/dev/rsd0d: 2167.8MB in 4439616 sectors of 512 bytes
11 cylinder groups of 202.47MB, 12958 blocks, 25984 inodes each
/dev/rsd0f: 2048.0MB in 4194304 sectors of 512 bytes
11 cylinder groups of 202.47MB, 12958 blocks, 25984 inodes each
/dev/rsd0g: 1024.0MB in 2097152 sectors of 512 bytes
6 cylinder groups of 202.47MB, 12958 blocks, 25984 inodes each
/dev/rsd0h: 4607.7MB in 9436608 sectors of 512 bytes
23 cylinder groups of 202.47MB, 12958 blocks, 25984 inodes each
/dev/rsd0j: 2048.0MB in 4194304 sectors of 512 bytes
11 cylinder groups of 202.47MB, 12958 blocks, 25984 inodes each
/dev/rsd0i: 1535.9MB in 3145600 sectors of 512 bytes
8 cylinder groups of 202.47MB, 12958 blocks, 25984 inodes each
/dev/rsd0e: 3407.6MB in 6978848 sectors of 512 bytes
17 cylinder groups of 202.47MB, 12958 blocks, 25984 inodes each
/dev/sd0a (5bd0fb19045d5413.a) on /mnt type ffs (rw, asynchronous, local)
/dev/sd0k (5bd0fb19045d5413.k) on /mnt/home type ffs (rw, asynchronous, local, nodev, nosuid)
/dev/sd0d (5bd0fb19045d5413.d) on /mnt/tmp type ffs (rw, asynchronous, local, nodev, nosuid)
/dev/sd0f (5bd0fb19045d5413.f) on /mnt/usr type ffs (rw, asynchronous, local, nodev)
/dev/sd0g (5bd0fb19045d5413.g) on /mnt/usr/X11R6 type ffs (rw, asynchronous, local, nodev)
/dev/sd0h (5bd0fb19045d5413.h) on /mnt/usr/local type ffs (rw, asynchronous, local, nodev)
/dev/sd0j (5bd0fb19045d5413.j) on /mnt/usr/obj type ffs (rw, asynchronous, local, nodev, nosuid)
/dev/sd0i (5bd0fb19045d5413.i) on /mnt/usr/src type ffs (rw, asynchronous, local, nodev, nosuid)
/dev/sd0e (5bd0fb19045d5413.e) on /mnt/var type ffs (rw, asynchronous, local, nodev, nosuid)

Let's install the sets!
Location of sets? (cd disk http or 'done') [cd]
Available CD-ROMs are: cd0.
Which CD-ROM contains the install media? (or 'done') [cd0]
Pathname to the sets? (or 'done') [5.7/amd64]

Select sets by entering a set name, a file name pattern or 'all'. De-select
sets by prepending a '-' to the set name, file name pattern or 'all'. Selected
sets are labelled '[X]'.
    [X] bsd           [X] base57.tgz    [X] game57.tgz    [ ] xfont57.tgz
    [X] bsd.rd        [X] comp57.tgz    [ ] xbase57.tgz   [ ] xserv57.tgz
    [ ] bsd.mp        [X] man57.tgz     [ ] xshare57.tgz
Set name(s)? (or 'abort' or 'done') [done] bsd.mp
    [X] bsd           [X] base57.tgz    [X] game57.tgz    [ ] xfont57.tgz
    [X] bsd.rd        [X] comp57.tgz    [ ] xbase57.tgz   [ ] xserv57.tgz
    [X] bsd.mp        [X] man57.tgz     [ ] xshare57.tgz
Set name(s)? (or 'abort' or 'done') [done]
Directory does not contain SHA256.sig. Continue without verification? [no] yes
Installing bsd          100% |**************************|  9794 KB    00:00
Installing bsd.rd       100% |**************************|  7414 KB    00:00
Installing bsd.mp       100% |**************************|  9833 KB    00:00
Installing base57.tgz   100% |**************************| 56035 KB    00:04
Extracting etc.tgz      100% |**************************|   110 KB    00:00
Installing comp57.tgz   100% |**************************| 50045 KB    00:05
Installing man57.tgz    100% |**************************|  8773 KB    00:01
Installing game57.tgz   100% |**************************|  2724 KB    00:00
Location of sets? (cd disk http or 'done') [done]
Time appears wrong.  Set to 'Mon Oct  5 07:00:41 GMT 2015'? [yes]
Saving configuration files...done.
Making all device nodes...done.

CONGRATULATIONS! Your OpenBSD install has been successfully completed!
To boot the new system, enter 'reboot' at the command prompt.
When you login to your new system the first time, please read your mail
using the 'mail' command.

# reboot
syncing disks... done
rebooting...

インストールが完了したらrebootコマンドを実行して仮想環境を終了します。

OpenBSD 5.7を使ってみよう

以降は次のようにgrub-bhyveとbhyveコマンドを実行することでOpenBSD 5.7が起動できるようになります。

図6 grub-bhyveによるカーネルの読み込み
# grub-bhyve -m openbsd-5.7-device.map -r cd0 -M 2G openbsd-5.7

                             GNU GRUB  version 2.00

   Minimal BASH-like line editing is supported. For the first word, TAB
   lists possible command completions. Anywhere else TAB lists possible
   device or file completions.

grub> kopenbsd -h com0 -r sd0a (hd0,openbsd1)/bsd
grub> boot
#
図7 bhyveによるOpenBSDの実行
# bhyve \
    -H -P -A \
    -W -c 1 \
    -m 2G \
    -l com1,stdio \
    -s 0:0,hostbridge \
    -s 1:0,lpc \
    -s 2:0,virtio-net,tap0 \
    -s 3,virtio-blk,/dev/zvol/z/openbsd-5.7 \
    openbsd-5.7

起動スクリプトを組むか、または次のようにエイリアスを設定しておけば、コマンド一発でOpenBSD環境を起動できるようになります。

リスト5 OpenBSD 5.7 on bhyveのエイリアス
alias bhyve_openbsd_5.7='sudo bhyvectl --destroy --vm=openbsd-5.7; printf "kopenbsd -h com0 -r sd0a (hd0,openbsd1)/bsd\nboot\n" | sudo grub-bhyve -m /d/bhyve/openbsd-5.7-device.map -M 2G openbsd-5.7; sudo bhyve -W -c 1 -m 2G -H -P -A -l com1,stdio -s 0:0,hostbridge -s 1:0,lpc -s 2:0,virtio-net,tap0 -s 3,virtio-blk,/dev/zvol/z/openbsd-5.7 openbsd-5.7; sudo bhyvectl --destroy --vm=openbsd-5.7'

複数の仮想CPUを割り当てたい場合には、インストールの段階でbsd.mpも選択してインストールしておいてください。またその場合、grub-bhyveで読み込むカーネルをbsdからbsd.mpに変更してください。

おすすめ記事

記事・ニュース一覧