xref: /openbmc/qemu/tests/vm/freebsd (revision 278f064e)
1#!/usr/bin/env python3
2#
3# FreeBSD 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 re
17import sys
18import time
19import socket
20import subprocess
21import basevm
22
23FREEBSD_CONFIG = {
24    'cpu'	: "max,sse4.2=off",
25}
26
27class FreeBSDVM(basevm.BaseVM):
28    name = "freebsd"
29    arch = "x86_64"
30
31    link = "https://download.freebsd.org/ftp/releases/ISO-IMAGES/12.2/FreeBSD-12.2-RELEASE-amd64-disc1.iso.xz"
32    csum = "a4530246cafbf1dd42a9bd3ea441ca9a78a6a0cd070278cbdf63f3a6f803ecae"
33    size = "20G"
34    pkgs = [
35        # build tools
36        "git",
37        "pkgconf",
38        "bzip2",
39        "python37",
40        "ninja",
41
42        # gnu tools
43        "bash",
44        "gmake",
45        "gsed",
46        "gettext",
47
48        # libs: crypto
49        "gnutls",
50
51        # libs: images
52        "jpeg-turbo",
53        "png",
54
55        # libs: ui
56        "sdl2",
57        "gtk3",
58        "libxkbcommon",
59
60        # libs: opengl
61        "libepoxy",
62        "mesa-libs",
63
64        # libs: migration
65        "zstd",
66    ]
67
68    # TODO: Enable gnutls again once FreeBSD's libtasn1 got fixed
69    # See: https://gitlab.com/gnutls/libtasn1/-/merge_requests/71
70    BUILD_SCRIPT = """
71        set -e;
72        rm -rf /home/qemu/qemu-test.*
73        cd $(mktemp -d /home/qemu/qemu-test.XXXXXX);
74        mkdir src build; cd src;
75        tar -xf /dev/vtbd1;
76        cd ../build
77        ../src/configure --python=python3.7 --disable-gnutls {configure_opts};
78        gmake --output-sync -j{jobs} {target} {verbose};
79    """
80
81    def console_boot_serial(self):
82        self.console_wait_send("Autoboot", "3")
83        self.console_wait_send("OK", "set console=comconsole\n")
84        self.console_wait_send("OK", "boot\n")
85
86    def build_image(self, img):
87        self.print_step("Downloading install iso")
88        cimg = self._download_with_cache(self.link, sha256sum=self.csum)
89        img_tmp = img + ".tmp"
90        iso = img + ".install.iso"
91        iso_xz = iso + ".xz"
92
93        self.print_step("Preparing iso and disk image")
94        subprocess.check_call(["cp", "-f", cimg, iso_xz])
95        subprocess.check_call(["xz", "-dvf", iso_xz])
96        self.exec_qemu_img("create", "-f", "qcow2", img_tmp, self.size)
97
98        self.print_step("Booting installer")
99        self.boot(img_tmp, extra_args = [
100            "-bios", "pc-bios/bios-256k.bin",
101            "-machine", "graphics=off",
102            "-device", "VGA",
103            "-cdrom", iso
104        ])
105        self.console_init()
106        self.console_boot_serial()
107        self.console_wait_send("Console type",          "xterm\n")
108
109        # pre-install configuration
110        self.console_wait_send("Welcome",               "\n")
111        self.console_wait_send("Keymap Selection",      "\n")
112        self.console_wait_send("Set Hostname",          "freebsd\n")
113        self.console_wait_send("Distribution Select",   "\n")
114        self.console_wait_send("Partitioning",          "\n")
115        self.console_wait_send("Partition",             "\n")
116        self.console_wait_send("Scheme",                "\n")
117        self.console_wait_send("Editor",                "f")
118        self.console_wait_send("Confirmation",          "c")
119
120        self.print_step("Installation started now, this will take a while")
121
122        # post-install configuration
123        self.console_wait("New Password:")
124        self.console_send("%s\n" % self._config["root_pass"])
125        self.console_wait("Retype New Password:")
126        self.console_send("%s\n" % self._config["root_pass"])
127
128        self.console_wait_send("Network Configuration", "\n")
129        self.console_wait_send("IPv4",                  "y")
130        self.console_wait_send("DHCP",                  "y")
131        self.console_wait_send("IPv6",                  "n")
132        self.console_wait_send("Resolver",              "\n")
133
134        self.console_wait_send("Time Zone Selector",    "0\n")
135        self.console_wait_send("Confirmation",          "y")
136        self.console_wait_send("Time & Date",           "\n")
137        self.console_wait_send("Time & Date",           "\n")
138
139        self.console_wait_send("System Configuration",  "\n")
140        self.console_wait_send("System Hardening",      "\n")
141
142        # qemu user
143        self.console_wait_send("Add User Accounts", "y")
144        self.console_wait("Username")
145        self.console_send("%s\n" % self._config["guest_user"])
146        self.console_wait("Full name")
147        self.console_send("%s\n" % self._config["guest_user"])
148        self.console_wait_send("Uid",                   "\n")
149        self.console_wait_send("Login group",           "\n")
150        self.console_wait_send("Login group",           "\n")
151        self.console_wait_send("Login class",           "\n")
152        self.console_wait_send("Shell",                 "\n")
153        self.console_wait_send("Home directory",        "\n")
154        self.console_wait_send("Home directory perm",   "\n")
155        self.console_wait_send("Use password",          "\n")
156        self.console_wait_send("Use an empty password", "\n")
157        self.console_wait_send("Use a random password", "\n")
158        self.console_wait("Enter password:")
159        self.console_send("%s\n" % self._config["guest_pass"])
160        self.console_wait("Enter password again:")
161        self.console_send("%s\n" % self._config["guest_pass"])
162        self.console_wait_send("Lock out",              "\n")
163        self.console_wait_send("OK",                    "yes\n")
164        self.console_wait_send("Add another user",      "no\n")
165
166        self.console_wait_send("Final Configuration",   "\n")
167        self.console_wait_send("Manual Configuration",  "\n")
168        self.console_wait_send("Complete",              "\n")
169
170        self.print_step("Installation finished, rebooting")
171        self.console_boot_serial()
172
173        # setup qemu user
174        prompt = "$"
175        self.console_ssh_init(prompt, self._config["guest_user"], self._config["guest_pass"])
176        self.console_wait_send(prompt, "exit\n")
177
178        # setup root user
179        prompt = "root@freebsd:~ #"
180        self.console_ssh_init(prompt, "root", self._config["root_pass"])
181        self.console_sshd_config(prompt)
182
183        # setup serial console
184        self.console_wait(prompt)
185        self.console_send("echo 'console=comconsole' >> /boot/loader.conf\n")
186
187        # setup boot delay
188        self.console_wait(prompt)
189        self.console_send("echo 'autoboot_delay=1' >> /boot/loader.conf\n")
190
191        # setup virtio-blk #1 (tarfile)
192        self.console_wait(prompt)
193        self.console_send("echo 'chmod 666 /dev/vtbd1' >> /etc/rc.local\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 install -y %s\n" % " ".join(self.pkgs))
202
203        # shutdown
204        self.ssh_root(self.poweroff)
205        self.console_wait("Uptime:")
206        self.wait()
207
208        if os.path.exists(img):
209            os.remove(img)
210        os.rename(img_tmp, img)
211        os.remove(iso)
212        self.print_step("All done")
213
214if __name__ == "__main__":
215    sys.exit(basevm.main(FreeBSDVM, config=FREEBSD_CONFIG))
216