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 31*b4358ed4SThomas Huth link = "https://download.freebsd.org/releases/CI-IMAGES/14.1-RELEASE/amd64/Latest/FreeBSD-14.1-RELEASE-amd64-BASIC-CI.raw.xz" 32*b4358ed4SThomas Huth csum = "202fe27a05427f0a86d3ebb97712745186f2776ccc4f70d95466dd99a0238ba5" 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; 41c73272f5SThomas Huth cd ../build; 42*b4358ed4SThomas Huth ../src/configure --extra-ldflags=-L/usr/local/lib \ 43c73272f5SThomas Huth --extra-cflags=-I/usr/local/include {configure_opts}; 445c2ec9b6SAlex Bennée gmake --output-sync -j{jobs} {target} {verbose}; 45111e30c0SFam Zheng """ 46111e30c0SFam Zheng 47111e30c0SFam Zheng def build_image(self, img): 48ec6fb1c8SThomas Huth self.print_step("Downloading disk image") 49918fb8faSGerd Hoffmann cimg = self._download_with_cache(self.link, sha256sum=self.csum) 50ec6fb1c8SThomas Huth tmp_raw = img + ".tmp.raw" 51ec6fb1c8SThomas Huth tmp_raw_xz = tmp_raw + ".xz" 52ec6fb1c8SThomas Huth img_tmp = img + ".tmp.qcow2" 53918fb8faSGerd Hoffmann 54ec6fb1c8SThomas Huth self.print_step("Preparing disk image") 55ec6fb1c8SThomas Huth subprocess.check_call(["cp", "-f", cimg, tmp_raw_xz]) 56ec6fb1c8SThomas Huth subprocess.check_call(["xz", "-dvf", tmp_raw_xz]) 57ec6fb1c8SThomas Huth self.exec_qemu_img("convert", "-O", "qcow2", tmp_raw, img_tmp) 58ec6fb1c8SThomas Huth self.exec_qemu_img("resize", img_tmp, self.size) 59ec6fb1c8SThomas Huth os.remove(tmp_raw) 60918fb8faSGerd Hoffmann 61ec6fb1c8SThomas Huth self.print_step("Preparing disk image") 62918fb8faSGerd Hoffmann self.boot(img_tmp, extra_args = [ 63918fb8faSGerd Hoffmann "-machine", "graphics=off", 64ec6fb1c8SThomas Huth "-vga", "none" 65918fb8faSGerd Hoffmann ]) 66918fb8faSGerd Hoffmann self.console_init() 67ec6fb1c8SThomas Huth self.console_wait_send("login:", "root\n") 68ec6fb1c8SThomas Huth self.console_wait_send("~ #", "service growfs onestart\n") 69918fb8faSGerd Hoffmann 70ec6fb1c8SThomas Huth # root user 71ec6fb1c8SThomas Huth self.console_wait_send("~ #", "passwd\n") 72918fb8faSGerd Hoffmann self.console_wait("New Password:") 73df001680SRobert Foley self.console_send("%s\n" % self._config["root_pass"]) 74918fb8faSGerd Hoffmann self.console_wait("Retype New Password:") 75df001680SRobert Foley self.console_send("%s\n" % self._config["root_pass"]) 76918fb8faSGerd Hoffmann 77918fb8faSGerd Hoffmann # qemu user 78ec6fb1c8SThomas Huth self.console_wait_send("~ #", "adduser\n") 79918fb8faSGerd Hoffmann self.console_wait("Username") 80df001680SRobert Foley self.console_send("%s\n" % self._config["guest_user"]) 81918fb8faSGerd Hoffmann self.console_wait("Full name") 82df001680SRobert Foley self.console_send("%s\n" % self._config["guest_user"]) 83918fb8faSGerd Hoffmann self.console_wait_send("Uid", "\n") 84918fb8faSGerd Hoffmann self.console_wait_send("Login group", "\n") 85918fb8faSGerd Hoffmann self.console_wait_send("Login group", "\n") 86918fb8faSGerd Hoffmann self.console_wait_send("Login class", "\n") 87918fb8faSGerd Hoffmann self.console_wait_send("Shell", "\n") 88918fb8faSGerd Hoffmann self.console_wait_send("Home directory", "\n") 89918fb8faSGerd Hoffmann self.console_wait_send("Home directory perm", "\n") 90918fb8faSGerd Hoffmann self.console_wait_send("Use password", "\n") 91918fb8faSGerd Hoffmann self.console_wait_send("Use an empty password", "\n") 92918fb8faSGerd Hoffmann self.console_wait_send("Use a random password", "\n") 93918fb8faSGerd Hoffmann self.console_wait("Enter password:") 94df001680SRobert Foley self.console_send("%s\n" % self._config["guest_pass"]) 95918fb8faSGerd Hoffmann self.console_wait("Enter password again:") 96df001680SRobert Foley self.console_send("%s\n" % self._config["guest_pass"]) 97918fb8faSGerd Hoffmann self.console_wait_send("Lock out", "\n") 98918fb8faSGerd Hoffmann self.console_wait_send("OK", "yes\n") 99918fb8faSGerd Hoffmann self.console_wait_send("Add another user", "no\n") 100ec6fb1c8SThomas Huth self.console_wait_send("~ #", "exit\n") 101918fb8faSGerd Hoffmann 102918fb8faSGerd Hoffmann # setup qemu user 103918fb8faSGerd Hoffmann prompt = "$" 104df001680SRobert Foley self.console_ssh_init(prompt, self._config["guest_user"], self._config["guest_pass"]) 105918fb8faSGerd Hoffmann self.console_wait_send(prompt, "exit\n") 106918fb8faSGerd Hoffmann 107918fb8faSGerd Hoffmann # setup root user 108918fb8faSGerd Hoffmann prompt = "root@freebsd:~ #" 109df001680SRobert Foley self.console_ssh_init(prompt, "root", self._config["root_pass"]) 110918fb8faSGerd Hoffmann self.console_sshd_config(prompt) 111fceffd6bSIlya Leoshkevich self.console_wait_send(prompt, "service sshd reload\n") 112918fb8faSGerd Hoffmann 113918fb8faSGerd Hoffmann # setup virtio-blk #1 (tarfile) 114918fb8faSGerd Hoffmann self.console_wait(prompt) 115918fb8faSGerd Hoffmann self.console_send("echo 'chmod 666 /dev/vtbd1' >> /etc/rc.local\n") 116918fb8faSGerd Hoffmann 117ca2a07f6SPhilippe Mathieu-Daudé pkgs = self.get_qemu_packages_from_lcitool_json() 118918fb8faSGerd Hoffmann self.print_step("Installing packages") 119ca2a07f6SPhilippe Mathieu-Daudé self.ssh_root_check("pkg install -y %s\n" % " ".join(pkgs)) 120918fb8faSGerd Hoffmann 121918fb8faSGerd Hoffmann # shutdown 122918fb8faSGerd Hoffmann self.ssh_root(self.poweroff) 123918fb8faSGerd Hoffmann self.wait() 124918fb8faSGerd Hoffmann 125918fb8faSGerd Hoffmann if os.path.exists(img): 126918fb8faSGerd Hoffmann os.remove(img) 127111e30c0SFam Zheng os.rename(img_tmp, img) 128918fb8faSGerd Hoffmann self.print_step("All done") 129111e30c0SFam Zheng 130111e30c0SFam Zhengif __name__ == "__main__": 131262fd273SWarner Losh sys.exit(basevm.main(FreeBSDVM, config=FREEBSD_CONFIG)) 132