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