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