xref: /openbmc/qemu/tests/vm/openbsd (revision b492a4b8)
1#!/usr/bin/env python3
2#
3# OpenBSD VM image
4#
5# Copyright 2017-2019 Red Hat Inc.
6#
7# Authors:
8#  Fam Zheng <famz@redhat.com>
9#  Gerd Hoffmann <kraxel@redhat.com>
10#
11# This code is licensed under the GPL version 2 or later.  See
12# the COPYING file in the top-level directory.
13#
14
15import os
16import sys
17import socket
18import subprocess
19import basevm
20
21class OpenBSDVM(basevm.BaseVM):
22    name = "openbsd"
23    arch = "x86_64"
24
25    link = "https://cdn.openbsd.org/pub/OpenBSD/6.8/amd64/install68.iso"
26    csum = "47e291fcc2d0c1a8ae0b66329f040b33af755b6adbd21739e20bb5ad56f62b6c"
27    size = "20G"
28    pkgs = [
29        # tools
30        "git",
31        "pkgconf",
32        "bzip2", "xz",
33        "py3-setuptools",
34        "ninja",
35
36        # gnu tools
37        "bash",
38        "gmake",
39        "gsed",
40        "gettext-tools",
41
42        # libs: usb
43        "libusb1--",
44
45        # libs: crypto
46        "gnutls",
47
48        # libs: images
49        "jpeg",
50        "png",
51
52	# libs: ui
53        "sdl2",
54        "gtk+3",
55        "libxkbcommon",
56
57        # libs: migration
58        "zstd",
59    ]
60
61    BUILD_SCRIPT = """
62        set -e;
63        rm -rf /home/qemu/qemu-test.*
64        cd $(mktemp -d /home/qemu/qemu-test.XXXXXX);
65        mkdir src build; cd src;
66        tar -xf /dev/rsd1c;
67        cd ../build
68        ../src/configure --cc=cc --python=python3 {configure_opts};
69        gmake --output-sync -j{jobs} {target} {verbose};
70    """
71    poweroff = "halt -p"
72
73    def build_image(self, img):
74        self.print_step("Downloading install iso")
75        cimg = self._download_with_cache(self.link, sha256sum=self.csum)
76        img_tmp = img + ".tmp"
77        iso = img + ".install.iso"
78
79        self.print_step("Preparing iso and disk image")
80        subprocess.check_call(["cp", "-f", cimg, iso])
81        self.exec_qemu_img("create", "-f", "qcow2", img_tmp, self.size)
82
83        self.print_step("Booting installer")
84        self.boot(img_tmp, extra_args = [
85            "-bios", "pc-bios/bios-256k.bin",
86            "-machine", "graphics=off",
87            "-device", "VGA",
88            "-cdrom", iso
89        ])
90        self.console_init()
91        self.console_wait_send("boot>", "set tty com0\n")
92        self.console_wait_send("boot>", "\n")
93
94        # pre-install configuration
95        self.console_wait_send("(I)nstall",               "i\n")
96        self.console_wait_send("Terminal type",           "xterm\n")
97        self.console_wait_send("System hostname",         "openbsd\n")
98        self.console_wait_send("Which network interface", "vio0\n")
99        self.console_wait_send("IPv4 address",            "dhcp\n")
100        self.console_wait_send("IPv6 address",            "none\n")
101        self.console_wait_send("Which network interface", "done\n")
102        self.console_wait_send("DNS domain name",         "localnet\n")
103        self.console_wait("Password for root account")
104        self.console_send("%s\n" % self._config["root_pass"])
105        self.console_wait("Password for root account")
106        self.console_send("%s\n" % self._config["root_pass"])
107        self.console_wait_send("Start sshd(8)",           "yes\n")
108        self.console_wait_send("X Window System",         "\n")
109        self.console_wait_send("xenodm",                  "\n")
110        self.console_wait_send("console to com0",         "\n")
111        self.console_wait_send("Which speed",             "\n")
112
113        self.console_wait("Setup a user")
114        self.console_send("%s\n" % self._config["guest_user"])
115        self.console_wait("Full name")
116        self.console_send("%s\n" % self._config["guest_user"])
117        self.console_wait("Password")
118        self.console_send("%s\n" % self._config["guest_pass"])
119        self.console_wait("Password")
120        self.console_send("%s\n" % self._config["guest_pass"])
121
122        self.console_wait_send("Allow root ssh login",    "yes\n")
123        self.console_wait_send("timezone",                "UTC\n")
124        self.console_wait_send("root disk",               "\n")
125        self.console_wait_send("(W)hole disk",            "\n")
126        self.console_wait_send("(A)uto layout",           "\n")
127        self.console_wait_send("Location of sets",        "cd0\n")
128        self.console_wait_send("Pathname to the sets",    "\n")
129        self.console_wait_send("Set name(s)",             "\n")
130        self.console_wait_send("without verification",    "yes\n")
131
132        self.print_step("Installation started now, this will take a while")
133        self.console_wait_send("Location of sets",        "done\n")
134
135        self.console_wait("successfully completed")
136        self.print_step("Installation finished, rebooting")
137        self.console_wait_send("(R)eboot",                "reboot\n")
138
139        # setup qemu user
140        prompt = "$"
141        self.console_ssh_init(prompt, self._config["guest_user"],
142                                      self._config["guest_pass"])
143        self.console_wait_send(prompt, "exit\n")
144
145        # setup root user
146        prompt = "openbsd#"
147        self.console_ssh_init(prompt, "root", self._config["root_pass"])
148        self.console_sshd_config(prompt)
149
150        # setup virtio-blk #1 (tarfile)
151        self.console_wait(prompt)
152        self.console_send("echo 'chmod 666 /dev/rsd1c' >> /etc/rc.local\n")
153
154        # enable w+x for /home
155        self.console_wait(prompt)
156        self.console_send("sed -i -e '/home/s/rw,/rw,wxallowed,/' /etc/fstab\n")
157
158        # tweak datasize limit
159        self.console_wait(prompt)
160        self.console_send("sed -i -e 's/\\(datasize[^=]*\\)=[^:]*/\\1=infinity/' /etc/login.conf\n")
161
162        # use http (be proxy cache friendly)
163        self.console_wait(prompt)
164        self.console_send("sed -i -e 's/https/http/' /etc/installurl\n")
165
166        self.print_step("Configuration finished, rebooting")
167        self.console_wait_send(prompt, "reboot\n")
168        self.console_wait("login:")
169        self.wait_ssh()
170
171        self.print_step("Installing packages")
172        self.ssh_root_check("pkg_add %s\n" % " ".join(self.pkgs))
173
174        # shutdown
175        self.ssh_root(self.poweroff)
176        self.wait()
177
178        if os.path.exists(img):
179            os.remove(img)
180        os.rename(img_tmp, img)
181        os.remove(iso)
182        self.print_step("All done")
183
184if __name__ == "__main__":
185    sys.exit(basevm.main(OpenBSDVM))
186