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