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 31e850897eSBrad Smith link = "https://download.freebsd.org/ftp/releases/ISO-IMAGES/12.3/FreeBSD-12.3-RELEASE-amd64-disc1.iso.xz" 32e850897eSBrad Smith csum = "36dd0de50f1fe5f0a88e181e94657656de26fb64254412f74e80e128e8b938b4" 33918fb8faSGerd Hoffmann size = "20G" 34918fb8faSGerd Hoffmann pkgs = [ 35918fb8faSGerd Hoffmann # build tools 36918fb8faSGerd Hoffmann "git", 37918fb8faSGerd Hoffmann "pkgconf", 38918fb8faSGerd Hoffmann "bzip2", 39bc2bf7f3SGerd Hoffmann "python37", 40345d7053SPaolo Bonzini "ninja", 41918fb8faSGerd Hoffmann 42918fb8faSGerd Hoffmann # gnu tools 43918fb8faSGerd Hoffmann "bash", 44918fb8faSGerd Hoffmann "gmake", 45918fb8faSGerd Hoffmann "gsed", 46e8f3bd71SMarc-André Lureau "gettext", 47918fb8faSGerd Hoffmann 48918fb8faSGerd Hoffmann # libs: crypto 49918fb8faSGerd Hoffmann "gnutls", 50918fb8faSGerd Hoffmann 51918fb8faSGerd Hoffmann # libs: images 52918fb8faSGerd Hoffmann "jpeg-turbo", 53918fb8faSGerd Hoffmann "png", 54918fb8faSGerd Hoffmann 55918fb8faSGerd Hoffmann # libs: ui 56918fb8faSGerd Hoffmann "sdl2", 57918fb8faSGerd Hoffmann "gtk3", 58918fb8faSGerd Hoffmann "libxkbcommon", 59918fb8faSGerd Hoffmann 60918fb8faSGerd Hoffmann # libs: opengl 61918fb8faSGerd Hoffmann "libepoxy", 62918fb8faSGerd Hoffmann "mesa-libs", 633a678481SJuan Quintela 643a678481SJuan Quintela # libs: migration 653a678481SJuan Quintela "zstd", 660026be1dSThomas Huth 670026be1dSThomas Huth # libs: networking 680026be1dSThomas Huth "libslirp", 69*8f4bcbcfSBrad Smith 70*8f4bcbcfSBrad Smith # libs: sndio 71*8f4bcbcfSBrad Smith "sndio", 72918fb8faSGerd Hoffmann ] 73918fb8faSGerd Hoffmann 74111e30c0SFam Zheng BUILD_SCRIPT = """ 75111e30c0SFam Zheng set -e; 76918fb8faSGerd Hoffmann rm -rf /home/qemu/qemu-test.* 77918fb8faSGerd Hoffmann cd $(mktemp -d /home/qemu/qemu-test.XXXXXX); 78918fb8faSGerd Hoffmann mkdir src build; cd src; 79111e30c0SFam Zheng tar -xf /dev/vtbd1; 80918fb8faSGerd Hoffmann cd ../build 81e850897eSBrad Smith ../src/configure --python=python3.7 {configure_opts}; 825c2ec9b6SAlex Bennée gmake --output-sync -j{jobs} {target} {verbose}; 83111e30c0SFam Zheng """ 84111e30c0SFam Zheng 85918fb8faSGerd Hoffmann def console_boot_serial(self): 86918fb8faSGerd Hoffmann self.console_wait_send("Autoboot", "3") 87918fb8faSGerd Hoffmann self.console_wait_send("OK", "set console=comconsole\n") 88918fb8faSGerd Hoffmann self.console_wait_send("OK", "boot\n") 89918fb8faSGerd Hoffmann 90111e30c0SFam Zheng def build_image(self, img): 91918fb8faSGerd Hoffmann self.print_step("Downloading install iso") 92918fb8faSGerd Hoffmann cimg = self._download_with_cache(self.link, sha256sum=self.csum) 93111e30c0SFam Zheng img_tmp = img + ".tmp" 94918fb8faSGerd Hoffmann iso = img + ".install.iso" 95918fb8faSGerd Hoffmann iso_xz = iso + ".xz" 96918fb8faSGerd Hoffmann 97918fb8faSGerd Hoffmann self.print_step("Preparing iso and disk image") 98918fb8faSGerd Hoffmann subprocess.check_call(["cp", "-f", cimg, iso_xz]) 99918fb8faSGerd Hoffmann subprocess.check_call(["xz", "-dvf", iso_xz]) 1001e48931cSWainer dos Santos Moschetta self.exec_qemu_img("create", "-f", "qcow2", img_tmp, self.size) 101918fb8faSGerd Hoffmann 102918fb8faSGerd Hoffmann self.print_step("Booting installer") 103918fb8faSGerd Hoffmann self.boot(img_tmp, extra_args = [ 104918fb8faSGerd Hoffmann "-machine", "graphics=off", 10550a06452SGerd Hoffmann "-device", "VGA", 106918fb8faSGerd Hoffmann "-cdrom", iso 107918fb8faSGerd Hoffmann ]) 108918fb8faSGerd Hoffmann self.console_init() 109918fb8faSGerd Hoffmann self.console_boot_serial() 110918fb8faSGerd Hoffmann self.console_wait_send("Console type", "xterm\n") 111918fb8faSGerd Hoffmann 112918fb8faSGerd Hoffmann # pre-install configuration 113918fb8faSGerd Hoffmann self.console_wait_send("Welcome", "\n") 114918fb8faSGerd Hoffmann self.console_wait_send("Keymap Selection", "\n") 115918fb8faSGerd Hoffmann self.console_wait_send("Set Hostname", "freebsd\n") 116918fb8faSGerd Hoffmann self.console_wait_send("Distribution Select", "\n") 117918fb8faSGerd Hoffmann self.console_wait_send("Partitioning", "\n") 118918fb8faSGerd Hoffmann self.console_wait_send("Partition", "\n") 119918fb8faSGerd Hoffmann self.console_wait_send("Scheme", "\n") 120918fb8faSGerd Hoffmann self.console_wait_send("Editor", "f") 121918fb8faSGerd Hoffmann self.console_wait_send("Confirmation", "c") 122918fb8faSGerd Hoffmann 123918fb8faSGerd Hoffmann self.print_step("Installation started now, this will take a while") 124918fb8faSGerd Hoffmann 125918fb8faSGerd Hoffmann # post-install configuration 126918fb8faSGerd Hoffmann self.console_wait("New Password:") 127df001680SRobert Foley self.console_send("%s\n" % self._config["root_pass"]) 128918fb8faSGerd Hoffmann self.console_wait("Retype New Password:") 129df001680SRobert Foley self.console_send("%s\n" % self._config["root_pass"]) 130918fb8faSGerd Hoffmann 131918fb8faSGerd Hoffmann self.console_wait_send("Network Configuration", "\n") 132918fb8faSGerd Hoffmann self.console_wait_send("IPv4", "y") 133918fb8faSGerd Hoffmann self.console_wait_send("DHCP", "y") 134918fb8faSGerd Hoffmann self.console_wait_send("IPv6", "n") 135918fb8faSGerd Hoffmann self.console_wait_send("Resolver", "\n") 136918fb8faSGerd Hoffmann 137262fd273SWarner Losh self.console_wait_send("Time Zone Selector", "0\n") 138918fb8faSGerd Hoffmann self.console_wait_send("Confirmation", "y") 139918fb8faSGerd Hoffmann self.console_wait_send("Time & Date", "\n") 140918fb8faSGerd Hoffmann self.console_wait_send("Time & Date", "\n") 141918fb8faSGerd Hoffmann 142918fb8faSGerd Hoffmann self.console_wait_send("System Configuration", "\n") 143918fb8faSGerd Hoffmann self.console_wait_send("System Hardening", "\n") 144918fb8faSGerd Hoffmann 145918fb8faSGerd Hoffmann # qemu user 146918fb8faSGerd Hoffmann self.console_wait_send("Add User Accounts", "y") 147918fb8faSGerd Hoffmann self.console_wait("Username") 148df001680SRobert Foley self.console_send("%s\n" % self._config["guest_user"]) 149918fb8faSGerd Hoffmann self.console_wait("Full name") 150df001680SRobert Foley self.console_send("%s\n" % self._config["guest_user"]) 151918fb8faSGerd Hoffmann self.console_wait_send("Uid", "\n") 152918fb8faSGerd Hoffmann self.console_wait_send("Login group", "\n") 153918fb8faSGerd Hoffmann self.console_wait_send("Login group", "\n") 154918fb8faSGerd Hoffmann self.console_wait_send("Login class", "\n") 155918fb8faSGerd Hoffmann self.console_wait_send("Shell", "\n") 156918fb8faSGerd Hoffmann self.console_wait_send("Home directory", "\n") 157918fb8faSGerd Hoffmann self.console_wait_send("Home directory perm", "\n") 158918fb8faSGerd Hoffmann self.console_wait_send("Use password", "\n") 159918fb8faSGerd Hoffmann self.console_wait_send("Use an empty password", "\n") 160918fb8faSGerd Hoffmann self.console_wait_send("Use a random password", "\n") 161918fb8faSGerd Hoffmann self.console_wait("Enter password:") 162df001680SRobert Foley self.console_send("%s\n" % self._config["guest_pass"]) 163918fb8faSGerd Hoffmann self.console_wait("Enter password again:") 164df001680SRobert Foley self.console_send("%s\n" % self._config["guest_pass"]) 165918fb8faSGerd Hoffmann self.console_wait_send("Lock out", "\n") 166918fb8faSGerd Hoffmann self.console_wait_send("OK", "yes\n") 167918fb8faSGerd Hoffmann self.console_wait_send("Add another user", "no\n") 168918fb8faSGerd Hoffmann 169918fb8faSGerd Hoffmann self.console_wait_send("Final Configuration", "\n") 170918fb8faSGerd Hoffmann self.console_wait_send("Manual Configuration", "\n") 171918fb8faSGerd Hoffmann self.console_wait_send("Complete", "\n") 172918fb8faSGerd Hoffmann 173918fb8faSGerd Hoffmann self.print_step("Installation finished, rebooting") 174918fb8faSGerd Hoffmann self.console_boot_serial() 175918fb8faSGerd Hoffmann 176918fb8faSGerd Hoffmann # setup qemu user 177918fb8faSGerd Hoffmann prompt = "$" 178df001680SRobert Foley self.console_ssh_init(prompt, self._config["guest_user"], self._config["guest_pass"]) 179918fb8faSGerd Hoffmann self.console_wait_send(prompt, "exit\n") 180918fb8faSGerd Hoffmann 181918fb8faSGerd Hoffmann # setup root user 182918fb8faSGerd Hoffmann prompt = "root@freebsd:~ #" 183df001680SRobert Foley self.console_ssh_init(prompt, "root", self._config["root_pass"]) 184918fb8faSGerd Hoffmann self.console_sshd_config(prompt) 185918fb8faSGerd Hoffmann 186918fb8faSGerd Hoffmann # setup serial console 187918fb8faSGerd Hoffmann self.console_wait(prompt) 188918fb8faSGerd Hoffmann self.console_send("echo 'console=comconsole' >> /boot/loader.conf\n") 189918fb8faSGerd Hoffmann 190918fb8faSGerd Hoffmann # setup boot delay 191918fb8faSGerd Hoffmann self.console_wait(prompt) 192918fb8faSGerd Hoffmann self.console_send("echo 'autoboot_delay=1' >> /boot/loader.conf\n") 193918fb8faSGerd Hoffmann 194918fb8faSGerd Hoffmann # setup virtio-blk #1 (tarfile) 195918fb8faSGerd Hoffmann self.console_wait(prompt) 196918fb8faSGerd Hoffmann self.console_send("echo 'chmod 666 /dev/vtbd1' >> /etc/rc.local\n") 197918fb8faSGerd Hoffmann 198918fb8faSGerd Hoffmann self.print_step("Configuration finished, rebooting") 199918fb8faSGerd Hoffmann self.console_wait_send(prompt, "reboot\n") 200918fb8faSGerd Hoffmann self.console_wait("login:") 201918fb8faSGerd Hoffmann self.wait_ssh() 202918fb8faSGerd Hoffmann 203918fb8faSGerd Hoffmann self.print_step("Installing packages") 204918fb8faSGerd Hoffmann self.ssh_root_check("pkg install -y %s\n" % " ".join(self.pkgs)) 205918fb8faSGerd Hoffmann 206918fb8faSGerd Hoffmann # shutdown 207918fb8faSGerd Hoffmann self.ssh_root(self.poweroff) 208918fb8faSGerd Hoffmann self.console_wait("Uptime:") 209918fb8faSGerd Hoffmann self.wait() 210918fb8faSGerd Hoffmann 211918fb8faSGerd Hoffmann if os.path.exists(img): 212918fb8faSGerd Hoffmann os.remove(img) 213111e30c0SFam Zheng os.rename(img_tmp, img) 214918fb8faSGerd Hoffmann os.remove(iso) 215918fb8faSGerd Hoffmann self.print_step("All done") 216111e30c0SFam Zheng 217111e30c0SFam Zhengif __name__ == "__main__": 218262fd273SWarner Losh sys.exit(basevm.main(FreeBSDVM, config=FREEBSD_CONFIG)) 219