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*e850897eSBrad Smith link = "https://download.freebsd.org/ftp/releases/ISO-IMAGES/12.3/FreeBSD-12.3-RELEASE-amd64-disc1.iso.xz" 32*e850897eSBrad 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", 66918fb8faSGerd Hoffmann ] 67918fb8faSGerd Hoffmann 68111e30c0SFam Zheng BUILD_SCRIPT = """ 69111e30c0SFam Zheng set -e; 70918fb8faSGerd Hoffmann rm -rf /home/qemu/qemu-test.* 71918fb8faSGerd Hoffmann cd $(mktemp -d /home/qemu/qemu-test.XXXXXX); 72918fb8faSGerd Hoffmann mkdir src build; cd src; 73111e30c0SFam Zheng tar -xf /dev/vtbd1; 74918fb8faSGerd Hoffmann cd ../build 75*e850897eSBrad Smith ../src/configure --python=python3.7 {configure_opts}; 765c2ec9b6SAlex Bennée gmake --output-sync -j{jobs} {target} {verbose}; 77111e30c0SFam Zheng """ 78111e30c0SFam Zheng 79918fb8faSGerd Hoffmann def console_boot_serial(self): 80918fb8faSGerd Hoffmann self.console_wait_send("Autoboot", "3") 81918fb8faSGerd Hoffmann self.console_wait_send("OK", "set console=comconsole\n") 82918fb8faSGerd Hoffmann self.console_wait_send("OK", "boot\n") 83918fb8faSGerd Hoffmann 84111e30c0SFam Zheng def build_image(self, img): 85918fb8faSGerd Hoffmann self.print_step("Downloading install iso") 86918fb8faSGerd Hoffmann cimg = self._download_with_cache(self.link, sha256sum=self.csum) 87111e30c0SFam Zheng img_tmp = img + ".tmp" 88918fb8faSGerd Hoffmann iso = img + ".install.iso" 89918fb8faSGerd Hoffmann iso_xz = iso + ".xz" 90918fb8faSGerd Hoffmann 91918fb8faSGerd Hoffmann self.print_step("Preparing iso and disk image") 92918fb8faSGerd Hoffmann subprocess.check_call(["cp", "-f", cimg, iso_xz]) 93918fb8faSGerd Hoffmann subprocess.check_call(["xz", "-dvf", iso_xz]) 941e48931cSWainer dos Santos Moschetta self.exec_qemu_img("create", "-f", "qcow2", img_tmp, self.size) 95918fb8faSGerd Hoffmann 96918fb8faSGerd Hoffmann self.print_step("Booting installer") 97918fb8faSGerd Hoffmann self.boot(img_tmp, extra_args = [ 98918fb8faSGerd Hoffmann "-bios", "pc-bios/bios-256k.bin", 99918fb8faSGerd Hoffmann "-machine", "graphics=off", 10050a06452SGerd Hoffmann "-device", "VGA", 101918fb8faSGerd Hoffmann "-cdrom", iso 102918fb8faSGerd Hoffmann ]) 103918fb8faSGerd Hoffmann self.console_init() 104918fb8faSGerd Hoffmann self.console_boot_serial() 105918fb8faSGerd Hoffmann self.console_wait_send("Console type", "xterm\n") 106918fb8faSGerd Hoffmann 107918fb8faSGerd Hoffmann # pre-install configuration 108918fb8faSGerd Hoffmann self.console_wait_send("Welcome", "\n") 109918fb8faSGerd Hoffmann self.console_wait_send("Keymap Selection", "\n") 110918fb8faSGerd Hoffmann self.console_wait_send("Set Hostname", "freebsd\n") 111918fb8faSGerd Hoffmann self.console_wait_send("Distribution Select", "\n") 112918fb8faSGerd Hoffmann self.console_wait_send("Partitioning", "\n") 113918fb8faSGerd Hoffmann self.console_wait_send("Partition", "\n") 114918fb8faSGerd Hoffmann self.console_wait_send("Scheme", "\n") 115918fb8faSGerd Hoffmann self.console_wait_send("Editor", "f") 116918fb8faSGerd Hoffmann self.console_wait_send("Confirmation", "c") 117918fb8faSGerd Hoffmann 118918fb8faSGerd Hoffmann self.print_step("Installation started now, this will take a while") 119918fb8faSGerd Hoffmann 120918fb8faSGerd Hoffmann # post-install configuration 121918fb8faSGerd Hoffmann self.console_wait("New Password:") 122df001680SRobert Foley self.console_send("%s\n" % self._config["root_pass"]) 123918fb8faSGerd Hoffmann self.console_wait("Retype New Password:") 124df001680SRobert Foley self.console_send("%s\n" % self._config["root_pass"]) 125918fb8faSGerd Hoffmann 126918fb8faSGerd Hoffmann self.console_wait_send("Network Configuration", "\n") 127918fb8faSGerd Hoffmann self.console_wait_send("IPv4", "y") 128918fb8faSGerd Hoffmann self.console_wait_send("DHCP", "y") 129918fb8faSGerd Hoffmann self.console_wait_send("IPv6", "n") 130918fb8faSGerd Hoffmann self.console_wait_send("Resolver", "\n") 131918fb8faSGerd Hoffmann 132262fd273SWarner Losh self.console_wait_send("Time Zone Selector", "0\n") 133918fb8faSGerd Hoffmann self.console_wait_send("Confirmation", "y") 134918fb8faSGerd Hoffmann self.console_wait_send("Time & Date", "\n") 135918fb8faSGerd Hoffmann self.console_wait_send("Time & Date", "\n") 136918fb8faSGerd Hoffmann 137918fb8faSGerd Hoffmann self.console_wait_send("System Configuration", "\n") 138918fb8faSGerd Hoffmann self.console_wait_send("System Hardening", "\n") 139918fb8faSGerd Hoffmann 140918fb8faSGerd Hoffmann # qemu user 141918fb8faSGerd Hoffmann self.console_wait_send("Add User Accounts", "y") 142918fb8faSGerd Hoffmann self.console_wait("Username") 143df001680SRobert Foley self.console_send("%s\n" % self._config["guest_user"]) 144918fb8faSGerd Hoffmann self.console_wait("Full name") 145df001680SRobert Foley self.console_send("%s\n" % self._config["guest_user"]) 146918fb8faSGerd Hoffmann self.console_wait_send("Uid", "\n") 147918fb8faSGerd Hoffmann self.console_wait_send("Login group", "\n") 148918fb8faSGerd Hoffmann self.console_wait_send("Login group", "\n") 149918fb8faSGerd Hoffmann self.console_wait_send("Login class", "\n") 150918fb8faSGerd Hoffmann self.console_wait_send("Shell", "\n") 151918fb8faSGerd Hoffmann self.console_wait_send("Home directory", "\n") 152918fb8faSGerd Hoffmann self.console_wait_send("Home directory perm", "\n") 153918fb8faSGerd Hoffmann self.console_wait_send("Use password", "\n") 154918fb8faSGerd Hoffmann self.console_wait_send("Use an empty password", "\n") 155918fb8faSGerd Hoffmann self.console_wait_send("Use a random password", "\n") 156918fb8faSGerd Hoffmann self.console_wait("Enter password:") 157df001680SRobert Foley self.console_send("%s\n" % self._config["guest_pass"]) 158918fb8faSGerd Hoffmann self.console_wait("Enter password again:") 159df001680SRobert Foley self.console_send("%s\n" % self._config["guest_pass"]) 160918fb8faSGerd Hoffmann self.console_wait_send("Lock out", "\n") 161918fb8faSGerd Hoffmann self.console_wait_send("OK", "yes\n") 162918fb8faSGerd Hoffmann self.console_wait_send("Add another user", "no\n") 163918fb8faSGerd Hoffmann 164918fb8faSGerd Hoffmann self.console_wait_send("Final Configuration", "\n") 165918fb8faSGerd Hoffmann self.console_wait_send("Manual Configuration", "\n") 166918fb8faSGerd Hoffmann self.console_wait_send("Complete", "\n") 167918fb8faSGerd Hoffmann 168918fb8faSGerd Hoffmann self.print_step("Installation finished, rebooting") 169918fb8faSGerd Hoffmann self.console_boot_serial() 170918fb8faSGerd Hoffmann 171918fb8faSGerd Hoffmann # setup qemu user 172918fb8faSGerd Hoffmann prompt = "$" 173df001680SRobert Foley self.console_ssh_init(prompt, self._config["guest_user"], self._config["guest_pass"]) 174918fb8faSGerd Hoffmann self.console_wait_send(prompt, "exit\n") 175918fb8faSGerd Hoffmann 176918fb8faSGerd Hoffmann # setup root user 177918fb8faSGerd Hoffmann prompt = "root@freebsd:~ #" 178df001680SRobert Foley self.console_ssh_init(prompt, "root", self._config["root_pass"]) 179918fb8faSGerd Hoffmann self.console_sshd_config(prompt) 180918fb8faSGerd Hoffmann 181918fb8faSGerd Hoffmann # setup serial console 182918fb8faSGerd Hoffmann self.console_wait(prompt) 183918fb8faSGerd Hoffmann self.console_send("echo 'console=comconsole' >> /boot/loader.conf\n") 184918fb8faSGerd Hoffmann 185918fb8faSGerd Hoffmann # setup boot delay 186918fb8faSGerd Hoffmann self.console_wait(prompt) 187918fb8faSGerd Hoffmann self.console_send("echo 'autoboot_delay=1' >> /boot/loader.conf\n") 188918fb8faSGerd Hoffmann 189918fb8faSGerd Hoffmann # setup virtio-blk #1 (tarfile) 190918fb8faSGerd Hoffmann self.console_wait(prompt) 191918fb8faSGerd Hoffmann self.console_send("echo 'chmod 666 /dev/vtbd1' >> /etc/rc.local\n") 192918fb8faSGerd Hoffmann 193918fb8faSGerd Hoffmann self.print_step("Configuration finished, rebooting") 194918fb8faSGerd Hoffmann self.console_wait_send(prompt, "reboot\n") 195918fb8faSGerd Hoffmann self.console_wait("login:") 196918fb8faSGerd Hoffmann self.wait_ssh() 197918fb8faSGerd Hoffmann 198918fb8faSGerd Hoffmann self.print_step("Installing packages") 199918fb8faSGerd Hoffmann self.ssh_root_check("pkg install -y %s\n" % " ".join(self.pkgs)) 200918fb8faSGerd Hoffmann 201918fb8faSGerd Hoffmann # shutdown 202918fb8faSGerd Hoffmann self.ssh_root(self.poweroff) 203918fb8faSGerd Hoffmann self.console_wait("Uptime:") 204918fb8faSGerd Hoffmann self.wait() 205918fb8faSGerd Hoffmann 206918fb8faSGerd Hoffmann if os.path.exists(img): 207918fb8faSGerd Hoffmann os.remove(img) 208111e30c0SFam Zheng os.rename(img_tmp, img) 209918fb8faSGerd Hoffmann os.remove(iso) 210918fb8faSGerd Hoffmann self.print_step("All done") 211111e30c0SFam Zheng 212111e30c0SFam Zhengif __name__ == "__main__": 213262fd273SWarner Losh sys.exit(basevm.main(FreeBSDVM, config=FREEBSD_CONFIG)) 214