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 23111e30c0SFam Zhengclass FreeBSDVM(basevm.BaseVM): 24111e30c0SFam Zheng name = "freebsd" 2531719c37SPhilippe Mathieu-Daudé arch = "x86_64" 26918fb8faSGerd Hoffmann 27610bd2cfSGerd Hoffmann link = "https://download.freebsd.org/ftp/releases/ISO-IMAGES/12.1/FreeBSD-12.1-RELEASE-amd64-disc1.iso.xz" 28610bd2cfSGerd Hoffmann csum = "7394c3f60a1e236e7bd3a05809cf43ae39a3b8e5d42d782004cf2f26b1cfcd88" 29918fb8faSGerd Hoffmann size = "20G" 30918fb8faSGerd Hoffmann pkgs = [ 31918fb8faSGerd Hoffmann # build tools 32918fb8faSGerd Hoffmann "git", 33918fb8faSGerd Hoffmann "pkgconf", 34918fb8faSGerd Hoffmann "bzip2", 35bc2bf7f3SGerd Hoffmann "python37", 36918fb8faSGerd Hoffmann 37918fb8faSGerd Hoffmann # gnu tools 38918fb8faSGerd Hoffmann "bash", 39918fb8faSGerd Hoffmann "gmake", 40918fb8faSGerd Hoffmann "gsed", 41918fb8faSGerd Hoffmann 42918fb8faSGerd Hoffmann # libs: crypto 43918fb8faSGerd Hoffmann "gnutls", 44918fb8faSGerd Hoffmann 45918fb8faSGerd Hoffmann # libs: images 46918fb8faSGerd Hoffmann "jpeg-turbo", 47918fb8faSGerd Hoffmann "png", 48918fb8faSGerd Hoffmann 49918fb8faSGerd Hoffmann # libs: ui 50918fb8faSGerd Hoffmann "sdl2", 51918fb8faSGerd Hoffmann "gtk3", 52918fb8faSGerd Hoffmann "libxkbcommon", 53918fb8faSGerd Hoffmann 54918fb8faSGerd Hoffmann # libs: opengl 55918fb8faSGerd Hoffmann "libepoxy", 56918fb8faSGerd Hoffmann "mesa-libs", 573a678481SJuan Quintela 583a678481SJuan Quintela # libs: migration 593a678481SJuan Quintela "zstd", 60918fb8faSGerd Hoffmann ] 61918fb8faSGerd Hoffmann 62111e30c0SFam Zheng BUILD_SCRIPT = """ 63111e30c0SFam Zheng set -e; 64918fb8faSGerd Hoffmann rm -rf /home/qemu/qemu-test.* 65918fb8faSGerd Hoffmann cd $(mktemp -d /home/qemu/qemu-test.XXXXXX); 66918fb8faSGerd Hoffmann mkdir src build; cd src; 67111e30c0SFam Zheng tar -xf /dev/vtbd1; 68918fb8faSGerd Hoffmann cd ../build 69bc2bf7f3SGerd Hoffmann ../src/configure --python=python3.7 {configure_opts}; 705c2ec9b6SAlex Bennée gmake --output-sync -j{jobs} {target} {verbose}; 71111e30c0SFam Zheng """ 72111e30c0SFam Zheng 73918fb8faSGerd Hoffmann def console_boot_serial(self): 74918fb8faSGerd Hoffmann self.console_wait_send("Autoboot", "3") 75918fb8faSGerd Hoffmann self.console_wait_send("OK", "set console=comconsole\n") 76918fb8faSGerd Hoffmann self.console_wait_send("OK", "boot\n") 77918fb8faSGerd Hoffmann 78111e30c0SFam Zheng def build_image(self, img): 79918fb8faSGerd Hoffmann self.print_step("Downloading install iso") 80918fb8faSGerd Hoffmann cimg = self._download_with_cache(self.link, sha256sum=self.csum) 81111e30c0SFam Zheng img_tmp = img + ".tmp" 82918fb8faSGerd Hoffmann iso = img + ".install.iso" 83918fb8faSGerd Hoffmann iso_xz = iso + ".xz" 84918fb8faSGerd Hoffmann 85918fb8faSGerd Hoffmann self.print_step("Preparing iso and disk image") 86918fb8faSGerd Hoffmann subprocess.check_call(["cp", "-f", cimg, iso_xz]) 87918fb8faSGerd Hoffmann subprocess.check_call(["xz", "-dvf", iso_xz]) 881e48931cSWainer dos Santos Moschetta self.exec_qemu_img("create", "-f", "qcow2", img_tmp, self.size) 89918fb8faSGerd Hoffmann 90918fb8faSGerd Hoffmann self.print_step("Booting installer") 91918fb8faSGerd Hoffmann self.boot(img_tmp, extra_args = [ 92918fb8faSGerd Hoffmann "-bios", "pc-bios/bios-256k.bin", 93918fb8faSGerd Hoffmann "-machine", "graphics=off", 9450a06452SGerd Hoffmann "-device", "VGA", 95918fb8faSGerd Hoffmann "-cdrom", iso 96918fb8faSGerd Hoffmann ]) 97918fb8faSGerd Hoffmann self.console_init() 98918fb8faSGerd Hoffmann self.console_boot_serial() 99918fb8faSGerd Hoffmann self.console_wait_send("Console type", "xterm\n") 100918fb8faSGerd Hoffmann 101918fb8faSGerd Hoffmann # pre-install configuration 102918fb8faSGerd Hoffmann self.console_wait_send("Welcome", "\n") 103918fb8faSGerd Hoffmann self.console_wait_send("Keymap Selection", "\n") 104918fb8faSGerd Hoffmann self.console_wait_send("Set Hostname", "freebsd\n") 105918fb8faSGerd Hoffmann self.console_wait_send("Distribution Select", "\n") 106918fb8faSGerd Hoffmann self.console_wait_send("Partitioning", "\n") 107918fb8faSGerd Hoffmann self.console_wait_send("Partition", "\n") 108918fb8faSGerd Hoffmann self.console_wait_send("Scheme", "\n") 109918fb8faSGerd Hoffmann self.console_wait_send("Editor", "f") 110918fb8faSGerd Hoffmann self.console_wait_send("Confirmation", "c") 111918fb8faSGerd Hoffmann 112918fb8faSGerd Hoffmann self.print_step("Installation started now, this will take a while") 113918fb8faSGerd Hoffmann 114918fb8faSGerd Hoffmann # post-install configuration 115918fb8faSGerd Hoffmann self.console_wait("New Password:") 116*df001680SRobert Foley self.console_send("%s\n" % self._config["root_pass"]) 117918fb8faSGerd Hoffmann self.console_wait("Retype New Password:") 118*df001680SRobert Foley self.console_send("%s\n" % self._config["root_pass"]) 119918fb8faSGerd Hoffmann 120918fb8faSGerd Hoffmann self.console_wait_send("Network Configuration", "\n") 121918fb8faSGerd Hoffmann self.console_wait_send("IPv4", "y") 122918fb8faSGerd Hoffmann self.console_wait_send("DHCP", "y") 123918fb8faSGerd Hoffmann self.console_wait_send("IPv6", "n") 124918fb8faSGerd Hoffmann self.console_wait_send("Resolver", "\n") 125918fb8faSGerd Hoffmann 126918fb8faSGerd Hoffmann self.console_wait_send("Time Zone Selector", "a\n") 127918fb8faSGerd Hoffmann self.console_wait_send("Confirmation", "y") 128918fb8faSGerd Hoffmann self.console_wait_send("Time & Date", "\n") 129918fb8faSGerd Hoffmann self.console_wait_send("Time & Date", "\n") 130918fb8faSGerd Hoffmann 131918fb8faSGerd Hoffmann self.console_wait_send("System Configuration", "\n") 132918fb8faSGerd Hoffmann self.console_wait_send("System Hardening", "\n") 133918fb8faSGerd Hoffmann 134918fb8faSGerd Hoffmann # qemu user 135918fb8faSGerd Hoffmann self.console_wait_send("Add User Accounts", "y") 136918fb8faSGerd Hoffmann self.console_wait("Username") 137*df001680SRobert Foley self.console_send("%s\n" % self._config["guest_user"]) 138918fb8faSGerd Hoffmann self.console_wait("Full name") 139*df001680SRobert Foley self.console_send("%s\n" % self._config["guest_user"]) 140918fb8faSGerd Hoffmann self.console_wait_send("Uid", "\n") 141918fb8faSGerd Hoffmann self.console_wait_send("Login group", "\n") 142918fb8faSGerd Hoffmann self.console_wait_send("Login group", "\n") 143918fb8faSGerd Hoffmann self.console_wait_send("Login class", "\n") 144918fb8faSGerd Hoffmann self.console_wait_send("Shell", "\n") 145918fb8faSGerd Hoffmann self.console_wait_send("Home directory", "\n") 146918fb8faSGerd Hoffmann self.console_wait_send("Home directory perm", "\n") 147918fb8faSGerd Hoffmann self.console_wait_send("Use password", "\n") 148918fb8faSGerd Hoffmann self.console_wait_send("Use an empty password", "\n") 149918fb8faSGerd Hoffmann self.console_wait_send("Use a random password", "\n") 150918fb8faSGerd Hoffmann self.console_wait("Enter password:") 151*df001680SRobert Foley self.console_send("%s\n" % self._config["guest_pass"]) 152918fb8faSGerd Hoffmann self.console_wait("Enter password again:") 153*df001680SRobert Foley self.console_send("%s\n" % self._config["guest_pass"]) 154918fb8faSGerd Hoffmann self.console_wait_send("Lock out", "\n") 155918fb8faSGerd Hoffmann self.console_wait_send("OK", "yes\n") 156918fb8faSGerd Hoffmann self.console_wait_send("Add another user", "no\n") 157918fb8faSGerd Hoffmann 158918fb8faSGerd Hoffmann self.console_wait_send("Final Configuration", "\n") 159918fb8faSGerd Hoffmann self.console_wait_send("Manual Configuration", "\n") 160918fb8faSGerd Hoffmann self.console_wait_send("Complete", "\n") 161918fb8faSGerd Hoffmann 162918fb8faSGerd Hoffmann self.print_step("Installation finished, rebooting") 163918fb8faSGerd Hoffmann self.console_boot_serial() 164918fb8faSGerd Hoffmann 165918fb8faSGerd Hoffmann # setup qemu user 166918fb8faSGerd Hoffmann prompt = "$" 167*df001680SRobert Foley self.console_ssh_init(prompt, self._config["guest_user"], self._config["guest_pass"]) 168918fb8faSGerd Hoffmann self.console_wait_send(prompt, "exit\n") 169918fb8faSGerd Hoffmann 170918fb8faSGerd Hoffmann # setup root user 171918fb8faSGerd Hoffmann prompt = "root@freebsd:~ #" 172*df001680SRobert Foley self.console_ssh_init(prompt, "root", self._config["root_pass"]) 173918fb8faSGerd Hoffmann self.console_sshd_config(prompt) 174918fb8faSGerd Hoffmann 175918fb8faSGerd Hoffmann # setup serial console 176918fb8faSGerd Hoffmann self.console_wait(prompt) 177918fb8faSGerd Hoffmann self.console_send("echo 'console=comconsole' >> /boot/loader.conf\n") 178918fb8faSGerd Hoffmann 179918fb8faSGerd Hoffmann # setup boot delay 180918fb8faSGerd Hoffmann self.console_wait(prompt) 181918fb8faSGerd Hoffmann self.console_send("echo 'autoboot_delay=1' >> /boot/loader.conf\n") 182918fb8faSGerd Hoffmann 183918fb8faSGerd Hoffmann # setup virtio-blk #1 (tarfile) 184918fb8faSGerd Hoffmann self.console_wait(prompt) 185918fb8faSGerd Hoffmann self.console_send("echo 'chmod 666 /dev/vtbd1' >> /etc/rc.local\n") 186918fb8faSGerd Hoffmann 187918fb8faSGerd Hoffmann self.print_step("Configuration finished, rebooting") 188918fb8faSGerd Hoffmann self.console_wait_send(prompt, "reboot\n") 189918fb8faSGerd Hoffmann self.console_wait("login:") 190918fb8faSGerd Hoffmann self.wait_ssh() 191918fb8faSGerd Hoffmann 192918fb8faSGerd Hoffmann self.print_step("Installing packages") 193918fb8faSGerd Hoffmann self.ssh_root_check("pkg install -y %s\n" % " ".join(self.pkgs)) 194918fb8faSGerd Hoffmann 195918fb8faSGerd Hoffmann # shutdown 196918fb8faSGerd Hoffmann self.ssh_root(self.poweroff) 197918fb8faSGerd Hoffmann self.console_wait("Uptime:") 198918fb8faSGerd Hoffmann self.wait() 199918fb8faSGerd Hoffmann 200918fb8faSGerd Hoffmann if os.path.exists(img): 201918fb8faSGerd Hoffmann os.remove(img) 202111e30c0SFam Zheng os.rename(img_tmp, img) 203918fb8faSGerd Hoffmann os.remove(iso) 204918fb8faSGerd Hoffmann self.print_step("All done") 205111e30c0SFam Zheng 206111e30c0SFam Zhengif __name__ == "__main__": 207111e30c0SFam Zheng sys.exit(basevm.main(FreeBSDVM)) 208