xref: /qemu/tests/vm/freebsd (revision a976a99a)
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        # libs: networking
68        "libslirp",
69    ]
70
71    BUILD_SCRIPT = """
72        set -e;
73        rm -rf /home/qemu/qemu-test.*
74        cd $(mktemp -d /home/qemu/qemu-test.XXXXXX);
75        mkdir src build; cd src;
76        tar -xf /dev/vtbd1;
77        cd ../build
78        ../src/configure --python=python3.7 {configure_opts};
79        gmake --output-sync -j{jobs} {target} {verbose};
80    """
81
82    def console_boot_serial(self):
83        self.console_wait_send("Autoboot", "3")
84        self.console_wait_send("OK", "set console=comconsole\n")
85        self.console_wait_send("OK", "boot\n")
86
87    def build_image(self, img):
88        self.print_step("Downloading install iso")
89        cimg = self._download_with_cache(self.link, sha256sum=self.csum)
90        img_tmp = img + ".tmp"
91        iso = img + ".install.iso"
92        iso_xz = iso + ".xz"
93
94        self.print_step("Preparing iso and disk image")
95        subprocess.check_call(["cp", "-f", cimg, iso_xz])
96        subprocess.check_call(["xz", "-dvf", iso_xz])
97        self.exec_qemu_img("create", "-f", "qcow2", img_tmp, self.size)
98
99        self.print_step("Booting installer")
100        self.boot(img_tmp, extra_args = [
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