xref: /qemu/tests/vm/freebsd (revision 1fe8ac35)
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            "-machine", "graphics=off",
99            "-device", "VGA",
100            "-cdrom", iso
101        ])
102        self.console_init()
103        self.console_boot_serial()
104        self.console_wait_send("Console type",          "xterm\n")
105
106        # pre-install configuration
107        self.console_wait_send("Welcome",               "\n")
108        self.console_wait_send("Keymap Selection",      "\n")
109        self.console_wait_send("Set Hostname",          "freebsd\n")
110        self.console_wait_send("Distribution Select",   "\n")
111        self.console_wait_send("Partitioning",          "\n")
112        self.console_wait_send("Partition",             "\n")
113        self.console_wait_send("Scheme",                "\n")
114        self.console_wait_send("Editor",                "f")
115        self.console_wait_send("Confirmation",          "c")
116
117        self.print_step("Installation started now, this will take a while")
118
119        # post-install configuration
120        self.console_wait("New Password:")
121        self.console_send("%s\n" % self._config["root_pass"])
122        self.console_wait("Retype New Password:")
123        self.console_send("%s\n" % self._config["root_pass"])
124
125        self.console_wait_send("Network Configuration", "\n")
126        self.console_wait_send("IPv4",                  "y")
127        self.console_wait_send("DHCP",                  "y")
128        self.console_wait_send("IPv6",                  "n")
129        self.console_wait_send("Resolver",              "\n")
130
131        self.console_wait_send("Time Zone Selector",    "0\n")
132        self.console_wait_send("Confirmation",          "y")
133        self.console_wait_send("Time & Date",           "\n")
134        self.console_wait_send("Time & Date",           "\n")
135
136        self.console_wait_send("System Configuration",  "\n")
137        self.console_wait_send("System Hardening",      "\n")
138
139        # qemu user
140        self.console_wait_send("Add User Accounts", "y")
141        self.console_wait("Username")
142        self.console_send("%s\n" % self._config["guest_user"])
143        self.console_wait("Full name")
144        self.console_send("%s\n" % self._config["guest_user"])
145        self.console_wait_send("Uid",                   "\n")
146        self.console_wait_send("Login group",           "\n")
147        self.console_wait_send("Login group",           "\n")
148        self.console_wait_send("Login class",           "\n")
149        self.console_wait_send("Shell",                 "\n")
150        self.console_wait_send("Home directory",        "\n")
151        self.console_wait_send("Home directory perm",   "\n")
152        self.console_wait_send("Use password",          "\n")
153        self.console_wait_send("Use an empty password", "\n")
154        self.console_wait_send("Use a random password", "\n")
155        self.console_wait("Enter password:")
156        self.console_send("%s\n" % self._config["guest_pass"])
157        self.console_wait("Enter password again:")
158        self.console_send("%s\n" % self._config["guest_pass"])
159        self.console_wait_send("Lock out",              "\n")
160        self.console_wait_send("OK",                    "yes\n")
161        self.console_wait_send("Add another user",      "no\n")
162
163        self.console_wait_send("Final Configuration",   "\n")
164        self.console_wait_send("Manual Configuration",  "\n")
165        self.console_wait_send("Complete",              "\n")
166
167        self.print_step("Installation finished, rebooting")
168        self.console_boot_serial()
169
170        # setup qemu user
171        prompt = "$"
172        self.console_ssh_init(prompt, self._config["guest_user"], self._config["guest_pass"])
173        self.console_wait_send(prompt, "exit\n")
174
175        # setup root user
176        prompt = "root@freebsd:~ #"
177        self.console_ssh_init(prompt, "root", self._config["root_pass"])
178        self.console_sshd_config(prompt)
179
180        # setup serial console
181        self.console_wait(prompt)
182        self.console_send("echo 'console=comconsole' >> /boot/loader.conf\n")
183
184        # setup boot delay
185        self.console_wait(prompt)
186        self.console_send("echo 'autoboot_delay=1' >> /boot/loader.conf\n")
187
188        # setup virtio-blk #1 (tarfile)
189        self.console_wait(prompt)
190        self.console_send("echo 'chmod 666 /dev/vtbd1' >> /etc/rc.local\n")
191
192        self.print_step("Configuration finished, rebooting")
193        self.console_wait_send(prompt, "reboot\n")
194        self.console_wait("login:")
195        self.wait_ssh()
196
197        self.print_step("Installing packages")
198        self.ssh_root_check("pkg install -y %s\n" % " ".join(self.pkgs))
199
200        # shutdown
201        self.ssh_root(self.poweroff)
202        self.console_wait("Uptime:")
203        self.wait()
204
205        if os.path.exists(img):
206            os.remove(img)
207        os.rename(img_tmp, img)
208        os.remove(iso)
209        self.print_step("All done")
210
211if __name__ == "__main__":
212    sys.exit(basevm.main(FreeBSDVM, config=FREEBSD_CONFIG))
213