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