xref: /openbmc/qemu/tests/vm/openbsd (revision 11b8920ed2093848f79f93d106afe8a69a61a523)
1c88ee46cSPhilippe Mathieu-Daudé#!/usr/bin/env python3
2fdfaa332SFam Zheng#
3fdfaa332SFam Zheng# OpenBSD VM image
4fdfaa332SFam Zheng#
512745eaaSGerd Hoffmann# Copyright 2017-2019 Red Hat Inc.
6fdfaa332SFam Zheng#
7fdfaa332SFam Zheng# Authors:
8fdfaa332SFam Zheng#  Fam Zheng <famz@redhat.com>
912745eaaSGerd Hoffmann#  Gerd Hoffmann <kraxel@redhat.com>
10fdfaa332SFam Zheng#
11fdfaa332SFam Zheng# This code is licensed under the GPL version 2 or later.  See
12fdfaa332SFam Zheng# the COPYING file in the top-level directory.
13fdfaa332SFam Zheng#
14fdfaa332SFam Zheng
15fdfaa332SFam Zhengimport os
16fdfaa332SFam Zhengimport sys
1712745eaaSGerd Hoffmannimport socket
18fdfaa332SFam Zhengimport subprocess
19fdfaa332SFam Zhengimport basevm
20fdfaa332SFam Zheng
21fdfaa332SFam Zhengclass OpenBSDVM(basevm.BaseVM):
22fdfaa332SFam Zheng    name = "openbsd"
2331719c37SPhilippe Mathieu-Daudé    arch = "x86_64"
2412745eaaSGerd Hoffmann
25*d8fe5b47SBrad Smith    link = "https://cdn.openbsd.org/pub/OpenBSD/7.6/amd64/install76.iso"
26*d8fe5b47SBrad Smith    csum = "60cba8cb391b50bba8fa10fc768bd0529636f5345d82133c93e22c798d8e5269"
2712745eaaSGerd Hoffmann    size = "20G"
2812745eaaSGerd Hoffmann    pkgs = [
2912745eaaSGerd Hoffmann        # tools
3005c223ceSThomas Huth        "dtc",
3112745eaaSGerd Hoffmann        "git",
3212745eaaSGerd Hoffmann        "pkgconf",
3312745eaaSGerd Hoffmann        "bzip2", "xz",
34345d7053SPaolo Bonzini        "ninja",
3512745eaaSGerd Hoffmann
3612745eaaSGerd Hoffmann        # gnu tools
3712745eaaSGerd Hoffmann        "bash",
3812745eaaSGerd Hoffmann        "gmake",
3912745eaaSGerd Hoffmann        "gsed",
4045716765SBrad Smith        "gettext-tools",
4112745eaaSGerd Hoffmann
4212745eaaSGerd Hoffmann        # libs: usb
4345716765SBrad Smith        "libusb1--",
4412745eaaSGerd Hoffmann
4512745eaaSGerd Hoffmann        # libs: crypto
4612745eaaSGerd Hoffmann        "gnutls",
4712745eaaSGerd Hoffmann
4812745eaaSGerd Hoffmann        # libs: images
4912745eaaSGerd Hoffmann        "jpeg",
5012745eaaSGerd Hoffmann        "png",
5112745eaaSGerd Hoffmann
5212745eaaSGerd Hoffmann        # libs: ui
5328e7e95eSThomas Huth        "capstone",
5412745eaaSGerd Hoffmann        "sdl2",
5512745eaaSGerd Hoffmann        "gtk+3",
5612745eaaSGerd Hoffmann        "libxkbcommon",
573a678481SJuan Quintela
583a678481SJuan Quintela        # libs: migration
593a678481SJuan Quintela        "zstd",
60690d43a8SBrad Smith
61690d43a8SBrad Smith        # libs: networking
62690d43a8SBrad Smith        "libslirp",
6312745eaaSGerd Hoffmann    ]
6412745eaaSGerd Hoffmann
65fdfaa332SFam Zheng    BUILD_SCRIPT = """
66fdfaa332SFam Zheng        set -e;
6712745eaaSGerd Hoffmann        rm -rf /home/qemu/qemu-test.*
6812745eaaSGerd Hoffmann        cd $(mktemp -d /home/qemu/qemu-test.XXXXXX);
6912745eaaSGerd Hoffmann        mkdir src build; cd src;
70fdfaa332SFam Zheng        tar -xf /dev/rsd1c;
7105c223ceSThomas Huth        cd ../build;
7205c223ceSThomas Huth        ../src/configure --cc=cc  --extra-cflags=-I/usr/local/include \
7305c223ceSThomas Huth                         --extra-ldflags=-L/usr/local/lib {configure_opts};
7412745eaaSGerd Hoffmann        gmake --output-sync -j{jobs} {target} {verbose};
75fdfaa332SFam Zheng    """
7612745eaaSGerd Hoffmann    poweroff = "halt -p"
77fdfaa332SFam Zheng
78fdfaa332SFam Zheng    def build_image(self, img):
7912745eaaSGerd Hoffmann        self.print_step("Downloading install iso")
8012745eaaSGerd Hoffmann        cimg = self._download_with_cache(self.link, sha256sum=self.csum)
81fdfaa332SFam Zheng        img_tmp = img + ".tmp"
8212745eaaSGerd Hoffmann        iso = img + ".install.iso"
8312745eaaSGerd Hoffmann
8412745eaaSGerd Hoffmann        self.print_step("Preparing iso and disk image")
8512745eaaSGerd Hoffmann        subprocess.check_call(["cp", "-f", cimg, iso])
861e48931cSWainer dos Santos Moschetta        self.exec_qemu_img("create", "-f", "qcow2", img_tmp, self.size)
8712745eaaSGerd Hoffmann
8812745eaaSGerd Hoffmann        self.print_step("Booting installer")
8912745eaaSGerd Hoffmann        self.boot(img_tmp, extra_args = [
9012745eaaSGerd Hoffmann            "-machine", "graphics=off",
9150a06452SGerd Hoffmann            "-device", "VGA",
9212745eaaSGerd Hoffmann            "-cdrom", iso
9312745eaaSGerd Hoffmann        ])
9412745eaaSGerd Hoffmann        self.console_init()
9512745eaaSGerd Hoffmann        self.console_wait_send("boot>", "set tty com0\n")
9612745eaaSGerd Hoffmann        self.console_wait_send("boot>", "\n")
9712745eaaSGerd Hoffmann
9812745eaaSGerd Hoffmann        # pre-install configuration
9912745eaaSGerd Hoffmann        self.console_wait_send("(I)nstall",               "i\n")
10012745eaaSGerd Hoffmann        self.console_wait_send("Terminal type",           "xterm\n")
10112745eaaSGerd Hoffmann        self.console_wait_send("System hostname",         "openbsd\n")
1028467ac75SAlex Bennée        self.console_wait_send("Network interface to configure", "vio0\n")
103b31b3fd0SRichard Henderson        self.console_wait_send("IPv4 address",            "autoconf\n")
10412745eaaSGerd Hoffmann        self.console_wait_send("IPv6 address",            "none\n")
1058467ac75SAlex Bennée        self.console_wait_send("Network interface to configure", "done\n")
10612745eaaSGerd Hoffmann        self.console_wait("Password for root account")
107df001680SRobert Foley        self.console_send("%s\n" % self._config["root_pass"])
10812745eaaSGerd Hoffmann        self.console_wait("Password for root account")
109df001680SRobert Foley        self.console_send("%s\n" % self._config["root_pass"])
11012745eaaSGerd Hoffmann        self.console_wait_send("Start sshd(8)",           "yes\n")
1113b67f43cSDaniel P. Berrangé        self.console_wait_send("X Window System",         "no\n")
11212745eaaSGerd Hoffmann        self.console_wait_send("console to com0",         "\n")
11312745eaaSGerd Hoffmann        self.console_wait_send("Which speed",             "\n")
11412745eaaSGerd Hoffmann
11512745eaaSGerd Hoffmann        self.console_wait("Setup a user")
116df001680SRobert Foley        self.console_send("%s\n" % self._config["guest_user"])
11712745eaaSGerd Hoffmann        self.console_wait("Full name")
118df001680SRobert Foley        self.console_send("%s\n" % self._config["guest_user"])
11912745eaaSGerd Hoffmann        self.console_wait("Password")
120df001680SRobert Foley        self.console_send("%s\n" % self._config["guest_pass"])
12112745eaaSGerd Hoffmann        self.console_wait("Password")
122df001680SRobert Foley        self.console_send("%s\n" % self._config["guest_pass"])
12312745eaaSGerd Hoffmann
12412745eaaSGerd Hoffmann        self.console_wait_send("Allow root ssh login",    "yes\n")
12512745eaaSGerd Hoffmann        self.console_wait_send("timezone",                "UTC\n")
12612745eaaSGerd Hoffmann        self.console_wait_send("root disk",               "\n")
1275e279f38SBrad Smith        self.console_wait_send("Encrypt the root disk with a (p)assphrase", "no\n")
12812745eaaSGerd Hoffmann        self.console_wait_send("(W)hole disk",            "\n")
12985b98348SDaniel P. Berrangé        self.console_wait_send("(A)uto layout",           "c\n")
13085b98348SDaniel P. Berrangé
13185b98348SDaniel P. Berrangé        # 4000 MB / as /dev/sd0a, at start of disk
13285b98348SDaniel P. Berrangé        self.console_wait_send("sd0>", "a a\n")
13385b98348SDaniel P. Berrangé        self.console_wait_send("offset:", "\n")
13485b98348SDaniel P. Berrangé        self.console_wait_send("size:", "4000M\n")
13585b98348SDaniel P. Berrangé        self.console_wait_send("FS type", "4.2BSD\n")
13685b98348SDaniel P. Berrangé        self.console_wait_send("mount point:", "/\n")
13785b98348SDaniel P. Berrangé
13885b98348SDaniel P. Berrangé        # 256 MB swap as /dev/sd0b
13985b98348SDaniel P. Berrangé        self.console_wait_send("sd0*>", "a b\n")
14085b98348SDaniel P. Berrangé        self.console_wait_send("offset:", "\n")
14185b98348SDaniel P. Berrangé        self.console_wait_send("size:", "256M\n")
14285b98348SDaniel P. Berrangé        self.console_wait_send("FS type", "swap\n")
14385b98348SDaniel P. Berrangé
14485b98348SDaniel P. Berrangé        # All remaining space for /home as /dev/sd0d
14585b98348SDaniel P. Berrangé        # NB, 'c' isn't allowed to be used.
14685b98348SDaniel P. Berrangé        self.console_wait_send("sd0*>", "a d\n")
14785b98348SDaniel P. Berrangé        self.console_wait_send("offset:", "\n")
14885b98348SDaniel P. Berrangé        self.console_wait_send("size:", "\n")
14985b98348SDaniel P. Berrangé        self.console_wait_send("FS type", "4.2BSD\n")
15085b98348SDaniel P. Berrangé        self.console_wait_send("mount point:", "/home\n")
15185b98348SDaniel P. Berrangé
15285b98348SDaniel P. Berrangé        self.console_wait_send("sd0*>", "q\n")
15385b98348SDaniel P. Berrangé        self.console_wait_send("Write new label?:", "y\n")
15485b98348SDaniel P. Berrangé
15512745eaaSGerd Hoffmann        self.console_wait_send("Location of sets",        "cd0\n")
15612745eaaSGerd Hoffmann        self.console_wait_send("Pathname to the sets",    "\n")
15712745eaaSGerd Hoffmann        self.console_wait_send("Set name(s)",             "\n")
15812745eaaSGerd Hoffmann        self.console_wait_send("without verification",    "yes\n")
15912745eaaSGerd Hoffmann
16012745eaaSGerd Hoffmann        self.print_step("Installation started now, this will take a while")
16112745eaaSGerd Hoffmann        self.console_wait_send("Location of sets",        "done\n")
16212745eaaSGerd Hoffmann
16312745eaaSGerd Hoffmann        self.console_wait("successfully completed")
16412745eaaSGerd Hoffmann        self.print_step("Installation finished, rebooting")
16512745eaaSGerd Hoffmann        self.console_wait_send("(R)eboot",                "reboot\n")
16612745eaaSGerd Hoffmann
16712745eaaSGerd Hoffmann        # setup qemu user
16812745eaaSGerd Hoffmann        prompt = "$"
169df001680SRobert Foley        self.console_ssh_init(prompt, self._config["guest_user"],
170df001680SRobert Foley                                      self._config["guest_pass"])
17112745eaaSGerd Hoffmann        self.console_wait_send(prompt, "exit\n")
17212745eaaSGerd Hoffmann
17312745eaaSGerd Hoffmann        # setup root user
17412745eaaSGerd Hoffmann        prompt = "openbsd#"
175df001680SRobert Foley        self.console_ssh_init(prompt, "root", self._config["root_pass"])
17612745eaaSGerd Hoffmann        self.console_sshd_config(prompt)
17712745eaaSGerd Hoffmann
17812745eaaSGerd Hoffmann        # setup virtio-blk #1 (tarfile)
17912745eaaSGerd Hoffmann        self.console_wait(prompt)
18012745eaaSGerd Hoffmann        self.console_send("echo 'chmod 666 /dev/rsd1c' >> /etc/rc.local\n")
18112745eaaSGerd Hoffmann
18212745eaaSGerd Hoffmann        # enable w+x for /home
18312745eaaSGerd Hoffmann        self.console_wait(prompt)
18412745eaaSGerd Hoffmann        self.console_send("sed -i -e '/home/s/rw,/rw,wxallowed,/' /etc/fstab\n")
18512745eaaSGerd Hoffmann
18612745eaaSGerd Hoffmann        # tweak datasize limit
18712745eaaSGerd Hoffmann        self.console_wait(prompt)
18812745eaaSGerd Hoffmann        self.console_send("sed -i -e 's/\\(datasize[^=]*\\)=[^:]*/\\1=infinity/' /etc/login.conf\n")
18912745eaaSGerd Hoffmann
19012745eaaSGerd Hoffmann        # use http (be proxy cache friendly)
19112745eaaSGerd Hoffmann        self.console_wait(prompt)
19212745eaaSGerd Hoffmann        self.console_send("sed -i -e 's/https/http/' /etc/installurl\n")
19312745eaaSGerd Hoffmann
19412745eaaSGerd Hoffmann        self.print_step("Configuration finished, rebooting")
19512745eaaSGerd Hoffmann        self.console_wait_send(prompt, "reboot\n")
19612745eaaSGerd Hoffmann        self.console_wait("login:")
19712745eaaSGerd Hoffmann        self.wait_ssh()
19812745eaaSGerd Hoffmann
19912745eaaSGerd Hoffmann        self.print_step("Installing packages")
20012745eaaSGerd Hoffmann        self.ssh_root_check("pkg_add %s\n" % " ".join(self.pkgs))
20112745eaaSGerd Hoffmann
20212745eaaSGerd Hoffmann        # shutdown
20312745eaaSGerd Hoffmann        self.ssh_root(self.poweroff)
20412745eaaSGerd Hoffmann        self.wait()
20512745eaaSGerd Hoffmann
20612745eaaSGerd Hoffmann        if os.path.exists(img):
20712745eaaSGerd Hoffmann            os.remove(img)
208fdfaa332SFam Zheng        os.rename(img_tmp, img)
20912745eaaSGerd Hoffmann        os.remove(iso)
21012745eaaSGerd Hoffmann        self.print_step("All done")
211fdfaa332SFam Zheng
212fdfaa332SFam Zhengif __name__ == "__main__":
213fdfaa332SFam Zheng    sys.exit(basevm.main(OpenBSDVM))
214