xref: /openbmc/qemu/tests/vm/freebsd (revision c7b64948)
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/releases/CI-IMAGES/13.2-RELEASE/amd64/Latest/FreeBSD-13.2-RELEASE-amd64-BASIC-CI.raw.xz"
32    csum = "a4fb3b6c7b75dd4d58fb0d75e4caf72844bffe0ca00e66459c028b198ffb3c0e"
33    size = "20G"
34    pkgs = [
35        # build tools
36        "git",
37        "pkgconf",
38        "bzip2",
39        "python39",
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        # libs: sndio
71        "sndio",
72    ]
73
74    BUILD_SCRIPT = """
75        set -e;
76        rm -rf /home/qemu/qemu-test.*
77        cd $(mktemp -d /home/qemu/qemu-test.XXXXXX);
78        mkdir src build; cd src;
79        tar -xf /dev/vtbd1;
80        cd ../build
81        ../src/configure --python=python3.9 {configure_opts};
82        gmake --output-sync -j{jobs} {target} {verbose};
83    """
84
85    def build_image(self, img):
86        self.print_step("Downloading disk image")
87        cimg = self._download_with_cache(self.link, sha256sum=self.csum)
88        tmp_raw = img + ".tmp.raw"
89        tmp_raw_xz = tmp_raw + ".xz"
90        img_tmp = img + ".tmp.qcow2"
91
92        self.print_step("Preparing disk image")
93        subprocess.check_call(["cp", "-f", cimg, tmp_raw_xz])
94        subprocess.check_call(["xz", "-dvf", tmp_raw_xz])
95        self.exec_qemu_img("convert", "-O", "qcow2", tmp_raw, img_tmp)
96        self.exec_qemu_img("resize", img_tmp, self.size)
97        os.remove(tmp_raw)
98
99        self.print_step("Preparing disk image")
100        self.boot(img_tmp, extra_args = [
101            "-machine", "graphics=off",
102            "-vga", "none"
103        ])
104        self.console_init()
105        self.console_wait_send("login:", "root\n")
106        self.console_wait_send("~ #", "service growfs onestart\n")
107
108        # root user
109        self.console_wait_send("~ #", "passwd\n")
110        self.console_wait("New Password:")
111        self.console_send("%s\n" % self._config["root_pass"])
112        self.console_wait("Retype New Password:")
113        self.console_send("%s\n" % self._config["root_pass"])
114
115        # qemu user
116        self.console_wait_send("~ #", "adduser\n")
117        self.console_wait("Username")
118        self.console_send("%s\n" % self._config["guest_user"])
119        self.console_wait("Full name")
120        self.console_send("%s\n" % self._config["guest_user"])
121        self.console_wait_send("Uid",                   "\n")
122        self.console_wait_send("Login group",           "\n")
123        self.console_wait_send("Login group",           "\n")
124        self.console_wait_send("Login class",           "\n")
125        self.console_wait_send("Shell",                 "\n")
126        self.console_wait_send("Home directory",        "\n")
127        self.console_wait_send("Home directory perm",   "\n")
128        self.console_wait_send("Use password",          "\n")
129        self.console_wait_send("Use an empty password", "\n")
130        self.console_wait_send("Use a random password", "\n")
131        self.console_wait("Enter password:")
132        self.console_send("%s\n" % self._config["guest_pass"])
133        self.console_wait("Enter password again:")
134        self.console_send("%s\n" % self._config["guest_pass"])
135        self.console_wait_send("Lock out",              "\n")
136        self.console_wait_send("OK",                    "yes\n")
137        self.console_wait_send("Add another user",      "no\n")
138        self.console_wait_send("~ #", "exit\n")
139
140        # setup qemu user
141        prompt = "$"
142        self.console_ssh_init(prompt, self._config["guest_user"], self._config["guest_pass"])
143        self.console_wait_send(prompt, "exit\n")
144
145        # setup root user
146        prompt = "root@freebsd:~ #"
147        self.console_ssh_init(prompt, "root", self._config["root_pass"])
148        self.console_sshd_config(prompt)
149
150        # setup virtio-blk #1 (tarfile)
151        self.console_wait(prompt)
152        self.console_send("echo 'chmod 666 /dev/vtbd1' >> /etc/rc.local\n")
153
154        self.print_step("Installing packages")
155        self.ssh_root_check("pkg install -y %s\n" % " ".join(self.pkgs))
156
157        # shutdown
158        self.ssh_root(self.poweroff)
159        self.wait()
160
161        if os.path.exists(img):
162            os.remove(img)
163        os.rename(img_tmp, img)
164        self.print_step("All done")
165
166if __name__ == "__main__":
167    sys.exit(basevm.main(FreeBSDVM, config=FREEBSD_CONFIG))
168