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