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