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