xref: /openbmc/qemu/tests/vm/openbsd (revision 1141159c)
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/7.2/amd64/install72.iso"
26    csum = "0369ef40a3329efcb978c578c7fdc7bda71e502aecec930a74b44160928c91d3"
27    size = "20G"
28    pkgs = [
29        # tools
30        "git",
31        "pkgconf",
32        "bzip2", "xz",
33        "ninja",
34
35        # gnu tools
36        "bash",
37        "gmake",
38        "gsed",
39        "gettext-tools",
40
41        # libs: usb
42        "libusb1--",
43
44        # libs: crypto
45        "gnutls",
46
47        # libs: images
48        "jpeg",
49        "png",
50
51        # libs: ui
52        "capstone",
53        "sdl2",
54        "gtk+3",
55        "libxkbcommon",
56
57        # libs: migration
58        "zstd",
59
60        # libs: networking
61        "libslirp",
62    ]
63
64    BUILD_SCRIPT = """
65        set -e;
66        rm -rf /home/qemu/qemu-test.*
67        cd $(mktemp -d /home/qemu/qemu-test.XXXXXX);
68        mkdir src build; cd src;
69        tar -xf /dev/rsd1c;
70        cd ../build
71        ../src/configure --cc=cc --python=python3 {configure_opts};
72        gmake --output-sync -j{jobs} {target} {verbose};
73    """
74    poweroff = "halt -p"
75
76    def build_image(self, img):
77        self.print_step("Downloading install iso")
78        cimg = self._download_with_cache(self.link, sha256sum=self.csum)
79        img_tmp = img + ".tmp"
80        iso = img + ".install.iso"
81
82        self.print_step("Preparing iso and disk image")
83        subprocess.check_call(["cp", "-f", cimg, iso])
84        self.exec_qemu_img("create", "-f", "qcow2", img_tmp, self.size)
85
86        self.print_step("Booting installer")
87        self.boot(img_tmp, extra_args = [
88            "-machine", "graphics=off",
89            "-device", "VGA",
90            "-cdrom", iso
91        ])
92        self.console_init()
93        self.console_wait_send("boot>", "set tty com0\n")
94        self.console_wait_send("boot>", "\n")
95
96        # pre-install configuration
97        self.console_wait_send("(I)nstall",               "i\n")
98        self.console_wait_send("Terminal type",           "xterm\n")
99        self.console_wait_send("System hostname",         "openbsd\n")
100        self.console_wait_send("Which network interface", "vio0\n")
101        self.console_wait_send("IPv4 address",            "autoconf\n")
102        self.console_wait_send("IPv6 address",            "none\n")
103        self.console_wait_send("Which network interface", "done\n")
104        self.console_wait("Password for root account")
105        self.console_send("%s\n" % self._config["root_pass"])
106        self.console_wait("Password for root account")
107        self.console_send("%s\n" % self._config["root_pass"])
108        self.console_wait_send("Start sshd(8)",           "yes\n")
109        self.console_wait_send("X Window System",         "no\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",           "c\n")
127
128        # 4000 MB / as /dev/sd0a, at start of disk
129        self.console_wait_send("sd0>", "a a\n")
130        self.console_wait_send("offset:", "\n")
131        self.console_wait_send("size:", "4000M\n")
132        self.console_wait_send("FS type", "4.2BSD\n")
133        self.console_wait_send("mount point:", "/\n")
134
135        # 256 MB swap as /dev/sd0b
136        self.console_wait_send("sd0*>", "a b\n")
137        self.console_wait_send("offset:", "\n")
138        self.console_wait_send("size:", "256M\n")
139        self.console_wait_send("FS type", "swap\n")
140
141        # All remaining space for /home as /dev/sd0d
142        # NB, 'c' isn't allowed to be used.
143        self.console_wait_send("sd0*>", "a d\n")
144        self.console_wait_send("offset:", "\n")
145        self.console_wait_send("size:", "\n")
146        self.console_wait_send("FS type", "4.2BSD\n")
147        self.console_wait_send("mount point:", "/home\n")
148
149        self.console_wait_send("sd0*>", "q\n")
150        self.console_wait_send("Write new label?:", "y\n")
151
152        self.console_wait_send("Location of sets",        "cd0\n")
153        self.console_wait_send("Pathname to the sets",    "\n")
154        self.console_wait_send("Set name(s)",             "\n")
155        self.console_wait_send("without verification",    "yes\n")
156
157        self.print_step("Installation started now, this will take a while")
158        self.console_wait_send("Location of sets",        "done\n")
159
160        self.console_wait("successfully completed")
161        self.print_step("Installation finished, rebooting")
162        self.console_wait_send("(R)eboot",                "reboot\n")
163
164        # setup qemu user
165        prompt = "$"
166        self.console_ssh_init(prompt, self._config["guest_user"],
167                                      self._config["guest_pass"])
168        self.console_wait_send(prompt, "exit\n")
169
170        # setup root user
171        prompt = "openbsd#"
172        self.console_ssh_init(prompt, "root", self._config["root_pass"])
173        self.console_sshd_config(prompt)
174
175        # setup virtio-blk #1 (tarfile)
176        self.console_wait(prompt)
177        self.console_send("echo 'chmod 666 /dev/rsd1c' >> /etc/rc.local\n")
178
179        # enable w+x for /home
180        self.console_wait(prompt)
181        self.console_send("sed -i -e '/home/s/rw,/rw,wxallowed,/' /etc/fstab\n")
182
183        # tweak datasize limit
184        self.console_wait(prompt)
185        self.console_send("sed -i -e 's/\\(datasize[^=]*\\)=[^:]*/\\1=infinity/' /etc/login.conf\n")
186
187        # use http (be proxy cache friendly)
188        self.console_wait(prompt)
189        self.console_send("sed -i -e 's/https/http/' /etc/installurl\n")
190
191        self.print_step("Configuration finished, rebooting")
192        self.console_wait_send(prompt, "reboot\n")
193        self.console_wait("login:")
194        self.wait_ssh()
195
196        self.print_step("Installing packages")
197        self.ssh_root_check("pkg_add %s\n" % " ".join(self.pkgs))
198
199        # shutdown
200        self.ssh_root(self.poweroff)
201        self.wait()
202
203        if os.path.exists(img):
204            os.remove(img)
205        os.rename(img_tmp, img)
206        os.remove(iso)
207        self.print_step("All done")
208
209if __name__ == "__main__":
210    sys.exit(basevm.main(OpenBSDVM))
211