xref: /openbmc/qemu/tests/vm/freebsd (revision ffe98631)
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.4/FreeBSD-12.4-RELEASE-amd64-disc1.iso.xz"
32    csum = "1dcf6446e31bf3f81b582e9aba3319a258c29a937a2af6138ee4b181ed719a87"
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        # libs: networking
68        "libslirp",
69
70        # libs: sndio
71        "sndio",
72    ]
73
74    BUILD_SCRIPT = """
75        set -e;
76        rm -rf /home/qemu/qemu-test.*
77        cd $(mktemp -d /home/qemu/qemu-test.XXXXXX);
78        mkdir src build; cd src;
79        tar -xf /dev/vtbd1;
80        cd ../build
81        ../src/configure --python=python3.7 {configure_opts};
82        gmake --output-sync -j{jobs} {target} {verbose};
83    """
84
85    def console_boot_serial(self):
86        self.console_wait_send("Autoboot", "3")
87        self.console_wait_send("OK", "set console=comconsole\n")
88        self.console_wait_send("OK", "boot\n")
89
90    def build_image(self, img):
91        self.print_step("Downloading install iso")
92        cimg = self._download_with_cache(self.link, sha256sum=self.csum)
93        img_tmp = img + ".tmp"
94        iso = img + ".install.iso"
95        iso_xz = iso + ".xz"
96
97        self.print_step("Preparing iso and disk image")
98        subprocess.check_call(["cp", "-f", cimg, iso_xz])
99        subprocess.check_call(["xz", "-dvf", iso_xz])
100        self.exec_qemu_img("create", "-f", "qcow2", img_tmp, self.size)
101
102        self.print_step("Booting installer")
103        self.boot(img_tmp, extra_args = [
104            "-machine", "graphics=off",
105            "-device", "VGA",
106            "-cdrom", iso
107        ])
108        self.console_init()
109        self.console_boot_serial()
110        self.console_wait_send("Console type",          "xterm\n")
111
112        # pre-install configuration
113        self.console_wait_send("Welcome",               "\n")
114        self.console_wait_send("Keymap Selection",      "\n")
115        self.console_wait_send("Set Hostname",          "freebsd\n")
116        self.console_wait_send("Distribution Select",   "\n")
117        self.console_wait_send("Partitioning",          "\n")
118        self.console_wait_send("Partition",             "\n")
119        self.console_wait_send("Scheme",                "\n")
120        self.console_wait_send("Editor",                "f")
121        self.console_wait_send("Confirmation",          "c")
122
123        self.print_step("Installation started now, this will take a while")
124
125        # post-install configuration
126        self.console_wait("New Password:")
127        self.console_send("%s\n" % self._config["root_pass"])
128        self.console_wait("Retype New Password:")
129        self.console_send("%s\n" % self._config["root_pass"])
130
131        self.console_wait_send("Network Configuration", "\n")
132        self.console_wait_send("IPv4",                  "y")
133        self.console_wait_send("DHCP",                  "y")
134        self.console_wait_send("IPv6",                  "n")
135        self.console_wait_send("Resolver",              "\n")
136
137        self.console_wait_send("Time Zone Selector",    "0\n")
138        self.console_wait_send("Confirmation",          "y")
139        self.console_wait_send("Time & Date",           "\n")
140        self.console_wait_send("Time & Date",           "\n")
141
142        self.console_wait_send("System Configuration",  "\n")
143        self.console_wait_send("System Hardening",      "\n")
144
145        # qemu user
146        self.console_wait_send("Add User Accounts", "y")
147        self.console_wait("Username")
148        self.console_send("%s\n" % self._config["guest_user"])
149        self.console_wait("Full name")
150        self.console_send("%s\n" % self._config["guest_user"])
151        self.console_wait_send("Uid",                   "\n")
152        self.console_wait_send("Login group",           "\n")
153        self.console_wait_send("Login group",           "\n")
154        self.console_wait_send("Login class",           "\n")
155        self.console_wait_send("Shell",                 "\n")
156        self.console_wait_send("Home directory",        "\n")
157        self.console_wait_send("Home directory perm",   "\n")
158        self.console_wait_send("Use password",          "\n")
159        self.console_wait_send("Use an empty password", "\n")
160        self.console_wait_send("Use a random password", "\n")
161        self.console_wait("Enter password:")
162        self.console_send("%s\n" % self._config["guest_pass"])
163        self.console_wait("Enter password again:")
164        self.console_send("%s\n" % self._config["guest_pass"])
165        self.console_wait_send("Lock out",              "\n")
166        self.console_wait_send("OK",                    "yes\n")
167        self.console_wait_send("Add another user",      "no\n")
168
169        self.console_wait_send("Final Configuration",   "\n")
170        self.console_wait_send("Manual Configuration",  "\n")
171        self.console_wait_send("Complete",              "\n")
172
173        self.print_step("Installation finished, rebooting")
174        self.console_boot_serial()
175
176        # setup qemu user
177        prompt = "$"
178        self.console_ssh_init(prompt, self._config["guest_user"], self._config["guest_pass"])
179        self.console_wait_send(prompt, "exit\n")
180
181        # setup root user
182        prompt = "root@freebsd:~ #"
183        self.console_ssh_init(prompt, "root", self._config["root_pass"])
184        self.console_sshd_config(prompt)
185
186        # setup serial console
187        self.console_wait(prompt)
188        self.console_send("echo 'console=comconsole' >> /boot/loader.conf\n")
189
190        # setup boot delay
191        self.console_wait(prompt)
192        self.console_send("echo 'autoboot_delay=1' >> /boot/loader.conf\n")
193
194        # setup virtio-blk #1 (tarfile)
195        self.console_wait(prompt)
196        self.console_send("echo 'chmod 666 /dev/vtbd1' >> /etc/rc.local\n")
197
198        self.print_step("Configuration finished, rebooting")
199        self.console_wait_send(prompt, "reboot\n")
200        self.console_wait("login:")
201        self.wait_ssh()
202
203        self.print_step("Installing packages")
204        self.ssh_root_check("pkg install -y %s\n" % " ".join(self.pkgs))
205
206        # shutdown
207        self.ssh_root(self.poweroff)
208        self.console_wait("Uptime:")
209        self.wait()
210
211        if os.path.exists(img):
212            os.remove(img)
213        os.rename(img_tmp, img)
214        os.remove(iso)
215        self.print_step("All done")
216
217if __name__ == "__main__":
218    sys.exit(basevm.main(FreeBSDVM, config=FREEBSD_CONFIG))
219