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", 36bfea7012SPaolo Bonzini "py37-setuptools", 37*345d7053SPaolo Bonzini "ninja", 38918fb8faSGerd Hoffmann 39918fb8faSGerd Hoffmann # gnu tools 40918fb8faSGerd Hoffmann "bash", 41918fb8faSGerd Hoffmann "gmake", 42918fb8faSGerd Hoffmann "gsed", 43e8f3bd71SMarc-André Lureau "gettext", 44918fb8faSGerd Hoffmann 45918fb8faSGerd Hoffmann # libs: crypto 46918fb8faSGerd Hoffmann "gnutls", 47918fb8faSGerd Hoffmann 48918fb8faSGerd Hoffmann # libs: images 49918fb8faSGerd Hoffmann "jpeg-turbo", 50918fb8faSGerd Hoffmann "png", 51918fb8faSGerd Hoffmann 52918fb8faSGerd Hoffmann # libs: ui 53918fb8faSGerd Hoffmann "sdl2", 54918fb8faSGerd Hoffmann "gtk3", 55918fb8faSGerd Hoffmann "libxkbcommon", 56918fb8faSGerd Hoffmann 57918fb8faSGerd Hoffmann # libs: opengl 58918fb8faSGerd Hoffmann "libepoxy", 59918fb8faSGerd Hoffmann "mesa-libs", 603a678481SJuan Quintela 613a678481SJuan Quintela # libs: migration 623a678481SJuan Quintela "zstd", 63918fb8faSGerd Hoffmann ] 64918fb8faSGerd Hoffmann 65111e30c0SFam Zheng BUILD_SCRIPT = """ 66111e30c0SFam Zheng set -e; 67918fb8faSGerd Hoffmann rm -rf /home/qemu/qemu-test.* 68918fb8faSGerd Hoffmann cd $(mktemp -d /home/qemu/qemu-test.XXXXXX); 69918fb8faSGerd Hoffmann mkdir src build; cd src; 70111e30c0SFam Zheng tar -xf /dev/vtbd1; 71918fb8faSGerd Hoffmann cd ../build 72bc2bf7f3SGerd Hoffmann ../src/configure --python=python3.7 {configure_opts}; 735c2ec9b6SAlex Bennée gmake --output-sync -j{jobs} {target} {verbose}; 74111e30c0SFam Zheng """ 75111e30c0SFam Zheng 76918fb8faSGerd Hoffmann def console_boot_serial(self): 77918fb8faSGerd Hoffmann self.console_wait_send("Autoboot", "3") 78918fb8faSGerd Hoffmann self.console_wait_send("OK", "set console=comconsole\n") 79918fb8faSGerd Hoffmann self.console_wait_send("OK", "boot\n") 80918fb8faSGerd Hoffmann 81111e30c0SFam Zheng def build_image(self, img): 82918fb8faSGerd Hoffmann self.print_step("Downloading install iso") 83918fb8faSGerd Hoffmann cimg = self._download_with_cache(self.link, sha256sum=self.csum) 84111e30c0SFam Zheng img_tmp = img + ".tmp" 85918fb8faSGerd Hoffmann iso = img + ".install.iso" 86918fb8faSGerd Hoffmann iso_xz = iso + ".xz" 87918fb8faSGerd Hoffmann 88918fb8faSGerd Hoffmann self.print_step("Preparing iso and disk image") 89918fb8faSGerd Hoffmann subprocess.check_call(["cp", "-f", cimg, iso_xz]) 90918fb8faSGerd Hoffmann subprocess.check_call(["xz", "-dvf", iso_xz]) 911e48931cSWainer dos Santos Moschetta self.exec_qemu_img("create", "-f", "qcow2", img_tmp, self.size) 92918fb8faSGerd Hoffmann 93918fb8faSGerd Hoffmann self.print_step("Booting installer") 94918fb8faSGerd Hoffmann self.boot(img_tmp, extra_args = [ 95918fb8faSGerd Hoffmann "-bios", "pc-bios/bios-256k.bin", 96918fb8faSGerd Hoffmann "-machine", "graphics=off", 9750a06452SGerd Hoffmann "-device", "VGA", 98918fb8faSGerd Hoffmann "-cdrom", iso 99918fb8faSGerd Hoffmann ]) 100918fb8faSGerd Hoffmann self.console_init() 101918fb8faSGerd Hoffmann self.console_boot_serial() 102918fb8faSGerd Hoffmann self.console_wait_send("Console type", "xterm\n") 103918fb8faSGerd Hoffmann 104918fb8faSGerd Hoffmann # pre-install configuration 105918fb8faSGerd Hoffmann self.console_wait_send("Welcome", "\n") 106918fb8faSGerd Hoffmann self.console_wait_send("Keymap Selection", "\n") 107918fb8faSGerd Hoffmann self.console_wait_send("Set Hostname", "freebsd\n") 108918fb8faSGerd Hoffmann self.console_wait_send("Distribution Select", "\n") 109918fb8faSGerd Hoffmann self.console_wait_send("Partitioning", "\n") 110918fb8faSGerd Hoffmann self.console_wait_send("Partition", "\n") 111918fb8faSGerd Hoffmann self.console_wait_send("Scheme", "\n") 112918fb8faSGerd Hoffmann self.console_wait_send("Editor", "f") 113918fb8faSGerd Hoffmann self.console_wait_send("Confirmation", "c") 114918fb8faSGerd Hoffmann 115918fb8faSGerd Hoffmann self.print_step("Installation started now, this will take a while") 116918fb8faSGerd Hoffmann 117918fb8faSGerd Hoffmann # post-install configuration 118918fb8faSGerd Hoffmann self.console_wait("New Password:") 119df001680SRobert Foley self.console_send("%s\n" % self._config["root_pass"]) 120918fb8faSGerd Hoffmann self.console_wait("Retype New Password:") 121df001680SRobert Foley self.console_send("%s\n" % self._config["root_pass"]) 122918fb8faSGerd Hoffmann 123918fb8faSGerd Hoffmann self.console_wait_send("Network Configuration", "\n") 124918fb8faSGerd Hoffmann self.console_wait_send("IPv4", "y") 125918fb8faSGerd Hoffmann self.console_wait_send("DHCP", "y") 126918fb8faSGerd Hoffmann self.console_wait_send("IPv6", "n") 127918fb8faSGerd Hoffmann self.console_wait_send("Resolver", "\n") 128918fb8faSGerd Hoffmann 129918fb8faSGerd Hoffmann self.console_wait_send("Time Zone Selector", "a\n") 130918fb8faSGerd Hoffmann self.console_wait_send("Confirmation", "y") 131918fb8faSGerd Hoffmann self.console_wait_send("Time & Date", "\n") 132918fb8faSGerd Hoffmann self.console_wait_send("Time & Date", "\n") 133918fb8faSGerd Hoffmann 134918fb8faSGerd Hoffmann self.console_wait_send("System Configuration", "\n") 135918fb8faSGerd Hoffmann self.console_wait_send("System Hardening", "\n") 136918fb8faSGerd Hoffmann 137918fb8faSGerd Hoffmann # qemu user 138918fb8faSGerd Hoffmann self.console_wait_send("Add User Accounts", "y") 139918fb8faSGerd Hoffmann self.console_wait("Username") 140df001680SRobert Foley self.console_send("%s\n" % self._config["guest_user"]) 141918fb8faSGerd Hoffmann self.console_wait("Full name") 142df001680SRobert Foley self.console_send("%s\n" % self._config["guest_user"]) 143918fb8faSGerd Hoffmann self.console_wait_send("Uid", "\n") 144918fb8faSGerd Hoffmann self.console_wait_send("Login group", "\n") 145918fb8faSGerd Hoffmann self.console_wait_send("Login group", "\n") 146918fb8faSGerd Hoffmann self.console_wait_send("Login class", "\n") 147918fb8faSGerd Hoffmann self.console_wait_send("Shell", "\n") 148918fb8faSGerd Hoffmann self.console_wait_send("Home directory", "\n") 149918fb8faSGerd Hoffmann self.console_wait_send("Home directory perm", "\n") 150918fb8faSGerd Hoffmann self.console_wait_send("Use password", "\n") 151918fb8faSGerd Hoffmann self.console_wait_send("Use an empty password", "\n") 152918fb8faSGerd Hoffmann self.console_wait_send("Use a random password", "\n") 153918fb8faSGerd Hoffmann self.console_wait("Enter password:") 154df001680SRobert Foley self.console_send("%s\n" % self._config["guest_pass"]) 155918fb8faSGerd Hoffmann self.console_wait("Enter password again:") 156df001680SRobert Foley self.console_send("%s\n" % self._config["guest_pass"]) 157918fb8faSGerd Hoffmann self.console_wait_send("Lock out", "\n") 158918fb8faSGerd Hoffmann self.console_wait_send("OK", "yes\n") 159918fb8faSGerd Hoffmann self.console_wait_send("Add another user", "no\n") 160918fb8faSGerd Hoffmann 161918fb8faSGerd Hoffmann self.console_wait_send("Final Configuration", "\n") 162918fb8faSGerd Hoffmann self.console_wait_send("Manual Configuration", "\n") 163918fb8faSGerd Hoffmann self.console_wait_send("Complete", "\n") 164918fb8faSGerd Hoffmann 165918fb8faSGerd Hoffmann self.print_step("Installation finished, rebooting") 166918fb8faSGerd Hoffmann self.console_boot_serial() 167918fb8faSGerd Hoffmann 168918fb8faSGerd Hoffmann # setup qemu user 169918fb8faSGerd Hoffmann prompt = "$" 170df001680SRobert Foley self.console_ssh_init(prompt, self._config["guest_user"], self._config["guest_pass"]) 171918fb8faSGerd Hoffmann self.console_wait_send(prompt, "exit\n") 172918fb8faSGerd Hoffmann 173918fb8faSGerd Hoffmann # setup root user 174918fb8faSGerd Hoffmann prompt = "root@freebsd:~ #" 175df001680SRobert Foley self.console_ssh_init(prompt, "root", self._config["root_pass"]) 176918fb8faSGerd Hoffmann self.console_sshd_config(prompt) 177918fb8faSGerd Hoffmann 178918fb8faSGerd Hoffmann # setup serial console 179918fb8faSGerd Hoffmann self.console_wait(prompt) 180918fb8faSGerd Hoffmann self.console_send("echo 'console=comconsole' >> /boot/loader.conf\n") 181918fb8faSGerd Hoffmann 182918fb8faSGerd Hoffmann # setup boot delay 183918fb8faSGerd Hoffmann self.console_wait(prompt) 184918fb8faSGerd Hoffmann self.console_send("echo 'autoboot_delay=1' >> /boot/loader.conf\n") 185918fb8faSGerd Hoffmann 186918fb8faSGerd Hoffmann # setup virtio-blk #1 (tarfile) 187918fb8faSGerd Hoffmann self.console_wait(prompt) 188918fb8faSGerd Hoffmann self.console_send("echo 'chmod 666 /dev/vtbd1' >> /etc/rc.local\n") 189918fb8faSGerd Hoffmann 190918fb8faSGerd Hoffmann self.print_step("Configuration finished, rebooting") 191918fb8faSGerd Hoffmann self.console_wait_send(prompt, "reboot\n") 192918fb8faSGerd Hoffmann self.console_wait("login:") 193918fb8faSGerd Hoffmann self.wait_ssh() 194918fb8faSGerd Hoffmann 195918fb8faSGerd Hoffmann self.print_step("Installing packages") 196918fb8faSGerd Hoffmann self.ssh_root_check("pkg install -y %s\n" % " ".join(self.pkgs)) 197918fb8faSGerd Hoffmann 198918fb8faSGerd Hoffmann # shutdown 199918fb8faSGerd Hoffmann self.ssh_root(self.poweroff) 200918fb8faSGerd Hoffmann self.console_wait("Uptime:") 201918fb8faSGerd Hoffmann self.wait() 202918fb8faSGerd Hoffmann 203918fb8faSGerd Hoffmann if os.path.exists(img): 204918fb8faSGerd Hoffmann os.remove(img) 205111e30c0SFam Zheng os.rename(img_tmp, img) 206918fb8faSGerd Hoffmann os.remove(iso) 207918fb8faSGerd Hoffmann self.print_step("All done") 208111e30c0SFam Zheng 209111e30c0SFam Zhengif __name__ == "__main__": 210111e30c0SFam Zheng sys.exit(basevm.main(FreeBSDVM)) 211