xref: /openbmc/qemu/tests/vm/freebsd (revision d447a624)
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
35    BUILD_SCRIPT = """
36        set -e;
37        rm -rf /home/qemu/qemu-test.*
38        cd $(mktemp -d /home/qemu/qemu-test.XXXXXX);
39        mkdir src build; cd src;
40        tar -xf /dev/vtbd1;
41        cd ../build
42        ../src/configure --python=python3.9 {configure_opts};
43        gmake --output-sync -j{jobs} {target} {verbose};
44    """
45
46    def build_image(self, img):
47        self.print_step("Downloading disk image")
48        cimg = self._download_with_cache(self.link, sha256sum=self.csum)
49        tmp_raw = img + ".tmp.raw"
50        tmp_raw_xz = tmp_raw + ".xz"
51        img_tmp = img + ".tmp.qcow2"
52
53        self.print_step("Preparing disk image")
54        subprocess.check_call(["cp", "-f", cimg, tmp_raw_xz])
55        subprocess.check_call(["xz", "-dvf", tmp_raw_xz])
56        self.exec_qemu_img("convert", "-O", "qcow2", tmp_raw, img_tmp)
57        self.exec_qemu_img("resize", img_tmp, self.size)
58        os.remove(tmp_raw)
59
60        self.print_step("Preparing disk image")
61        self.boot(img_tmp, extra_args = [
62            "-machine", "graphics=off",
63            "-vga", "none"
64        ])
65        self.console_init()
66        self.console_wait_send("login:", "root\n")
67        self.console_wait_send("~ #", "service growfs onestart\n")
68
69        # root user
70        self.console_wait_send("~ #", "passwd\n")
71        self.console_wait("New Password:")
72        self.console_send("%s\n" % self._config["root_pass"])
73        self.console_wait("Retype New Password:")
74        self.console_send("%s\n" % self._config["root_pass"])
75
76        # qemu user
77        self.console_wait_send("~ #", "adduser\n")
78        self.console_wait("Username")
79        self.console_send("%s\n" % self._config["guest_user"])
80        self.console_wait("Full name")
81        self.console_send("%s\n" % self._config["guest_user"])
82        self.console_wait_send("Uid",                   "\n")
83        self.console_wait_send("Login group",           "\n")
84        self.console_wait_send("Login group",           "\n")
85        self.console_wait_send("Login class",           "\n")
86        self.console_wait_send("Shell",                 "\n")
87        self.console_wait_send("Home directory",        "\n")
88        self.console_wait_send("Home directory perm",   "\n")
89        self.console_wait_send("Use password",          "\n")
90        self.console_wait_send("Use an empty password", "\n")
91        self.console_wait_send("Use a random password", "\n")
92        self.console_wait("Enter password:")
93        self.console_send("%s\n" % self._config["guest_pass"])
94        self.console_wait("Enter password again:")
95        self.console_send("%s\n" % self._config["guest_pass"])
96        self.console_wait_send("Lock out",              "\n")
97        self.console_wait_send("OK",                    "yes\n")
98        self.console_wait_send("Add another user",      "no\n")
99        self.console_wait_send("~ #", "exit\n")
100
101        # setup qemu user
102        prompt = "$"
103        self.console_ssh_init(prompt, self._config["guest_user"], self._config["guest_pass"])
104        self.console_wait_send(prompt, "exit\n")
105
106        # setup root user
107        prompt = "root@freebsd:~ #"
108        self.console_ssh_init(prompt, "root", self._config["root_pass"])
109        self.console_sshd_config(prompt)
110
111        # setup virtio-blk #1 (tarfile)
112        self.console_wait(prompt)
113        self.console_send("echo 'chmod 666 /dev/vtbd1' >> /etc/rc.local\n")
114
115        pkgs = self.get_qemu_packages_from_lcitool_json()
116        self.print_step("Installing packages")
117        self.ssh_root_check("pkg install -y %s\n" % " ".join(pkgs))
118
119        # shutdown
120        self.ssh_root(self.poweroff)
121        self.wait()
122
123        if os.path.exists(img):
124            os.remove(img)
125        os.rename(img_tmp, img)
126        self.print_step("All done")
127
128if __name__ == "__main__":
129    sys.exit(basevm.main(FreeBSDVM, config=FREEBSD_CONFIG))
130