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