xref: /openbmc/qemu/tests/vm/freebsd (revision ca2a07f6c3630d5d39af24651987256a705ac500)
1c88ee46cSPhilippe Mathieu-Daudé#!/usr/bin/env python3
2111e30c0SFam Zheng#
3111e30c0SFam Zheng# FreeBSD VM image
4111e30c0SFam Zheng#
5918fb8faSGerd Hoffmann# Copyright 2017-2019 Red Hat Inc.
6111e30c0SFam Zheng#
7111e30c0SFam Zheng# Authors:
8111e30c0SFam Zheng#  Fam Zheng <famz@redhat.com>
9918fb8faSGerd Hoffmann#  Gerd Hoffmann <kraxel@redhat.com>
10111e30c0SFam Zheng#
11111e30c0SFam Zheng# This code is licensed under the GPL version 2 or later.  See
12111e30c0SFam Zheng# the COPYING file in the top-level directory.
13111e30c0SFam Zheng#
14111e30c0SFam Zheng
15111e30c0SFam Zhengimport os
16918fb8faSGerd Hoffmannimport re
17111e30c0SFam Zhengimport sys
18918fb8faSGerd Hoffmannimport time
19918fb8faSGerd Hoffmannimport socket
20111e30c0SFam Zhengimport subprocess
21111e30c0SFam Zhengimport basevm
22111e30c0SFam Zheng
23262fd273SWarner LoshFREEBSD_CONFIG = {
24262fd273SWarner Losh    'cpu'	: "max,sse4.2=off",
25262fd273SWarner Losh}
26262fd273SWarner Losh
27111e30c0SFam Zhengclass FreeBSDVM(basevm.BaseVM):
28111e30c0SFam Zheng    name = "freebsd"
2931719c37SPhilippe Mathieu-Daudé    arch = "x86_64"
30918fb8faSGerd Hoffmann
31ec6fb1c8SThomas Huth    link = "https://download.freebsd.org/releases/CI-IMAGES/13.2-RELEASE/amd64/Latest/FreeBSD-13.2-RELEASE-amd64-BASIC-CI.raw.xz"
32ec6fb1c8SThomas Huth    csum = "a4fb3b6c7b75dd4d58fb0d75e4caf72844bffe0ca00e66459c028b198ffb3c0e"
33918fb8faSGerd Hoffmann    size = "20G"
34918fb8faSGerd Hoffmann
35111e30c0SFam Zheng    BUILD_SCRIPT = """
36111e30c0SFam Zheng        set -e;
37918fb8faSGerd Hoffmann        rm -rf /home/qemu/qemu-test.*
38918fb8faSGerd Hoffmann        cd $(mktemp -d /home/qemu/qemu-test.XXXXXX);
39918fb8faSGerd Hoffmann        mkdir src build; cd src;
40111e30c0SFam Zheng        tar -xf /dev/vtbd1;
41918fb8faSGerd Hoffmann        cd ../build
42ec6fb1c8SThomas Huth        ../src/configure --python=python3.9 {configure_opts};
435c2ec9b6SAlex Bennée        gmake --output-sync -j{jobs} {target} {verbose};
44111e30c0SFam Zheng    """
45111e30c0SFam Zheng
46111e30c0SFam Zheng    def build_image(self, img):
47ec6fb1c8SThomas Huth        self.print_step("Downloading disk image")
48918fb8faSGerd Hoffmann        cimg = self._download_with_cache(self.link, sha256sum=self.csum)
49ec6fb1c8SThomas Huth        tmp_raw = img + ".tmp.raw"
50ec6fb1c8SThomas Huth        tmp_raw_xz = tmp_raw + ".xz"
51ec6fb1c8SThomas Huth        img_tmp = img + ".tmp.qcow2"
52918fb8faSGerd Hoffmann
53ec6fb1c8SThomas Huth        self.print_step("Preparing disk image")
54ec6fb1c8SThomas Huth        subprocess.check_call(["cp", "-f", cimg, tmp_raw_xz])
55ec6fb1c8SThomas Huth        subprocess.check_call(["xz", "-dvf", tmp_raw_xz])
56ec6fb1c8SThomas Huth        self.exec_qemu_img("convert", "-O", "qcow2", tmp_raw, img_tmp)
57ec6fb1c8SThomas Huth        self.exec_qemu_img("resize", img_tmp, self.size)
58ec6fb1c8SThomas Huth        os.remove(tmp_raw)
59918fb8faSGerd Hoffmann
60ec6fb1c8SThomas Huth        self.print_step("Preparing disk image")
61918fb8faSGerd Hoffmann        self.boot(img_tmp, extra_args = [
62918fb8faSGerd Hoffmann            "-machine", "graphics=off",
63ec6fb1c8SThomas Huth            "-vga", "none"
64918fb8faSGerd Hoffmann        ])
65918fb8faSGerd Hoffmann        self.console_init()
66ec6fb1c8SThomas Huth        self.console_wait_send("login:", "root\n")
67ec6fb1c8SThomas Huth        self.console_wait_send("~ #", "service growfs onestart\n")
68918fb8faSGerd Hoffmann
69ec6fb1c8SThomas Huth        # root user
70ec6fb1c8SThomas Huth        self.console_wait_send("~ #", "passwd\n")
71918fb8faSGerd Hoffmann        self.console_wait("New Password:")
72df001680SRobert Foley        self.console_send("%s\n" % self._config["root_pass"])
73918fb8faSGerd Hoffmann        self.console_wait("Retype New Password:")
74df001680SRobert Foley        self.console_send("%s\n" % self._config["root_pass"])
75918fb8faSGerd Hoffmann
76918fb8faSGerd Hoffmann        # qemu user
77ec6fb1c8SThomas Huth        self.console_wait_send("~ #", "adduser\n")
78918fb8faSGerd Hoffmann        self.console_wait("Username")
79df001680SRobert Foley        self.console_send("%s\n" % self._config["guest_user"])
80918fb8faSGerd Hoffmann        self.console_wait("Full name")
81df001680SRobert Foley        self.console_send("%s\n" % self._config["guest_user"])
82918fb8faSGerd Hoffmann        self.console_wait_send("Uid",                   "\n")
83918fb8faSGerd Hoffmann        self.console_wait_send("Login group",           "\n")
84918fb8faSGerd Hoffmann        self.console_wait_send("Login group",           "\n")
85918fb8faSGerd Hoffmann        self.console_wait_send("Login class",           "\n")
86918fb8faSGerd Hoffmann        self.console_wait_send("Shell",                 "\n")
87918fb8faSGerd Hoffmann        self.console_wait_send("Home directory",        "\n")
88918fb8faSGerd Hoffmann        self.console_wait_send("Home directory perm",   "\n")
89918fb8faSGerd Hoffmann        self.console_wait_send("Use password",          "\n")
90918fb8faSGerd Hoffmann        self.console_wait_send("Use an empty password", "\n")
91918fb8faSGerd Hoffmann        self.console_wait_send("Use a random password", "\n")
92918fb8faSGerd Hoffmann        self.console_wait("Enter password:")
93df001680SRobert Foley        self.console_send("%s\n" % self._config["guest_pass"])
94918fb8faSGerd Hoffmann        self.console_wait("Enter password again:")
95df001680SRobert Foley        self.console_send("%s\n" % self._config["guest_pass"])
96918fb8faSGerd Hoffmann        self.console_wait_send("Lock out",              "\n")
97918fb8faSGerd Hoffmann        self.console_wait_send("OK",                    "yes\n")
98918fb8faSGerd Hoffmann        self.console_wait_send("Add another user",      "no\n")
99ec6fb1c8SThomas Huth        self.console_wait_send("~ #", "exit\n")
100918fb8faSGerd Hoffmann
101918fb8faSGerd Hoffmann        # setup qemu user
102918fb8faSGerd Hoffmann        prompt = "$"
103df001680SRobert Foley        self.console_ssh_init(prompt, self._config["guest_user"], self._config["guest_pass"])
104918fb8faSGerd Hoffmann        self.console_wait_send(prompt, "exit\n")
105918fb8faSGerd Hoffmann
106918fb8faSGerd Hoffmann        # setup root user
107918fb8faSGerd Hoffmann        prompt = "root@freebsd:~ #"
108df001680SRobert Foley        self.console_ssh_init(prompt, "root", self._config["root_pass"])
109918fb8faSGerd Hoffmann        self.console_sshd_config(prompt)
110918fb8faSGerd Hoffmann
111918fb8faSGerd Hoffmann        # setup virtio-blk #1 (tarfile)
112918fb8faSGerd Hoffmann        self.console_wait(prompt)
113918fb8faSGerd Hoffmann        self.console_send("echo 'chmod 666 /dev/vtbd1' >> /etc/rc.local\n")
114918fb8faSGerd Hoffmann
115*ca2a07f6SPhilippe Mathieu-Daudé        pkgs = self.get_qemu_packages_from_lcitool_json()
116918fb8faSGerd Hoffmann        self.print_step("Installing packages")
117*ca2a07f6SPhilippe Mathieu-Daudé        self.ssh_root_check("pkg install -y %s\n" % " ".join(pkgs))
118918fb8faSGerd Hoffmann
119918fb8faSGerd Hoffmann        # shutdown
120918fb8faSGerd Hoffmann        self.ssh_root(self.poweroff)
121918fb8faSGerd Hoffmann        self.wait()
122918fb8faSGerd Hoffmann
123918fb8faSGerd Hoffmann        if os.path.exists(img):
124918fb8faSGerd Hoffmann            os.remove(img)
125111e30c0SFam Zheng        os.rename(img_tmp, img)
126918fb8faSGerd Hoffmann        self.print_step("All done")
127111e30c0SFam Zheng
128111e30c0SFam Zhengif __name__ == "__main__":
129262fd273SWarner Losh    sys.exit(basevm.main(FreeBSDVM, config=FREEBSD_CONFIG))
130