Install OpenBSD 8.0 into a QEMU base disk
The base disk is the OpenBSD that every task starts from. It is installed once, by hand, configured once, and never booted again except to update it. This page creates the empty disk, walks through the installer and prepares the system for the work the next pages do over SSH. The steps were run on OpenBSD 8.0 amd64 with software emulation, so the times quoted are those of a machine without KVM.
Create the disk
All the files of the virtual machine live in one folder outside the repository. The image of the previous page makes the disk, and --user makes the file belong to the user instead of root:
cd ~/.local/share/aredel/openbsd
mkdir -p vm
docker run --rm --user "$(id -u):$(id -g)" -v "$PWD:/w" -w /w --entrypoint qemu-img openbsd-vm create -f qcow2 vm/base.qcow2 10G
Formatting 'vm/base.qcow2', fmt=qcow2 cluster_size=65536 extended_l2=off compression_type=zlib size=10737418240 lazy_refcounts=off refcount_bits=16
The file is sparse: 10 GiB is the size the guest sees, and the file itself grows only as the system writes to it. The finished system took 3.0 GiB.
Start the installer
For the install QEMU is started directly, not through the start script, because it needs the second disk with install80.img and a boot order. The installer image is attached read-only. -nographic puts the console of the machine into the terminal; to leave QEMU, press Ctrl-A and then X.
docker run --rm -it --name obsd-install -v "$PWD:/w" -w /w --entrypoint qemu-system-x86_64 openbsd-vm -machine q35 -accel tcg -m 1024 -smp 2 -nographic -drive file=vm/base.qcow2,if=none,id=hd0,format=qcow2 -device virtio-blk-pci,drive=hd0,bootindex=2 -drive file=8.0/install80.img,if=none,id=inst,format=raw,readonly=on -device virtio-blk-pci,drive=inst,bootindex=1 -netdev user,id=n0 -device virtio-net-pci,netdev=n0
With KVM (page 1) add --device /dev/kvm after docker run and write -accel kvm -cpu host instead of -accel tcg.
The boot loader asks for its command on the serial line first. The installer prints on the screen of the virtual machine unless it is told to use the serial port, so type these two lines at the boot> prompt:
set tty com0
boot
Answer the questions
The installer asks in this order. The text in square brackets is its default, and an empty answer, written as Enter, takes it.
- (I)nstall, (U)pgrade, (A)utoinstall or (S)hell? — i
- Terminal type? — Enter
- System hostname? — obsd-lab
- Network interface to configure? — Enter for vio0, then Enter for the IPv4 and IPv6 answers (autoconf), then Enter at the second interface question (done). The virtual network of QEMU gives the guest the address 10.0.2.15, a name server and a route out.
- Password for root account? — a password, twice. Nothing but the console uses it.
- Start sshd(8) by default? — Enter (yes)
- Do you expect to run the X Window System? — no
- Change the default console to com0? — Enter (yes), and Enter for the speed. The log of the container then shows the console of the guest.
- Setup a user? — worker, Enter for the full name, a password twice
- Allow root ssh login? — Enter (no)
- What timezone are you in? — UTC
- Which disk is the root disk? — sd0. The 10 GB disk is the one the installer lists first; the installer image is sd1.
- Encrypt the root disk? — Enter (no); Use (W)hole disk — Enter; Use (A)uto layout — Enter
- Which disk do you wish to initialize? — Enter (done)
- Location of sets? — Enter (http), Enter for no proxy, cdn.openbsd.org for the server, Enter for the directory pub/OpenBSD/8.0/amd64
- Set name(s)? — type -x*, Enter, then -game*, Enter, then done, Enter. The two lines remove the X Window System and the games; everything else, with the compiler and the manual pages, stays.
- Location of sets? — Enter (done)
- Exit to (S)hell, (H)alt or (R)eboot? — h
The sets come from the mirror over the network, which is why the install image of the virtual machine needs no special files. The whole install took 28 minutes with software emulation: 13 for the download and the unpacking of the sets, and the rest for the kernel link and the final configuration. When the installer prints The operating system has halted, press Ctrl-A and X.
Make a key for the lab
The worker container will log in with a key, so the machine needs the public half. A lab key pair is made once and kept outside the repository, with the private half readable only by you:
mkdir -p ~/.config/aredel/openbsd
ssh-keygen -t ed25519 -N '' -C obsd-lab -f ~/.config/aredel/openbsd/lab_key
cat ~/.config/aredel/openbsd/lab_key.pub
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA... obsd-lab
The key has no passphrase, because a program has to use it. It opens only this virtual machine, which has no port on the host.
Boot the new system and configure it
Boot the disk through the start script of the previous page. It takes about five minutes before the login: line appears under software emulation, and the console of the guest is the output of the container:
docker run --rm -it -v "$PWD/vm:/vm" -e VM_DISK=/vm/base.qcow2 openbsd-vm
Log in as root with the password of the installer, then run these commands in the virtual machine. The first one makes the directory of the user, and the key in the second one is the line printed above, in single quotes:
mkdir -p /home/worker/.ssh
echo 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA... obsd-lab' > /home/worker/.ssh/authorized_keys
chown -R worker:worker /home/worker/.ssh && chmod 700 /home/worker/.ssh && chmod 600 /home/worker/.ssh/authorized_keys
The next line lets worker run commands as root with doas, without a password, which a program needs to run pfctl and sshd -t. It is acceptable only because the machine is a throw-away lab with no other users:
echo 'permit nopass worker' > /etc/doas.conf
Then close the two password doors of the SSH server. PasswordAuthentication no alone leaves the keyboard-interactive method, which OpenBSD also checks against the password, so both are switched off. The second command reads the file back:
echo 'PasswordAuthentication no' >> /etc/ssh/sshd_config
echo 'KbdInteractiveAuthentication no' >> /etc/ssh/sshd_config
sshd -t && echo sshd-config-ok
sshd-config-ok
Install the checker of shell scripts. The package manager is already pointed at the mirror by the installer, and the package pulls three libraries with it:
pkg_add shellcheck
shellcheck-0.11.0p0: ok
The other checkers are in the base system: ksh -n for syntax, mandoc -Tlint for manual pages, pfctl -nf for pf.conf and sshd -t -f for sshd_config. Security patches of the release are separate and optional for a lab machine. The list shows which are available, and syspatch without an option installs them:
syspatch -c
001_ppp
002_nsd
003_aesgmac
...
Switch the machine off with halt -p; the container ends when the guest powers down.
halt -p
Two things seen on the way
- Two processors crashed the guest. With -smp 2 and software emulation the guest stopped in halt -p with kernel: protection fault trap in mtx_enter; with one processor the same disk powered off cleanly. That is why the start script gives the machine one processor without KVM and two with it. The install itself ran with two.
- The boot is slow without KVM. It took about 5 minutes to the login line on this laptop. A task that restarts the machine for every round pays that every time, so the machine stays up for the whole task.
Related
- Overlay disks, Compose and SSH from the worker: the next step.
- Build the QEMU container image: the container that boots this disk.
- OpenBSD course: the lessons the machine is built for.
Article author: Klavdiya Kondakova