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