1#!/usr/bin/env python 2# 3# FreeBSD VM image 4# 5# Copyright 2017-2019 Red Hat Inc. 6# 7# Authors: 8# Fam Zheng <famz@redhat.com> 9# Gerd Hoffmann <kraxel@redhat.com> 10# 11# This code is licensed under the GPL version 2 or later. See 12# the COPYING file in the top-level directory. 13# 14 15import os 16import re 17import sys 18import time 19import socket 20import subprocess 21import basevm 22 23class FreeBSDVM(basevm.BaseVM): 24 name = "freebsd" 25 arch = "x86_64" 26 27 link = "https://download.freebsd.org/ftp/releases/ISO-IMAGES/12.0/FreeBSD-12.0-RELEASE-amd64-disc1.iso.xz" 28 csum = "1d40015bea89d05b8bd13e2ed80c40b522a9ec1abd8e7c8b80954fb485fb99db" 29 size = "20G" 30 pkgs = [ 31 # build tools 32 "git", 33 "pkgconf", 34 "bzip2", 35 36 # gnu tools 37 "bash", 38 "gmake", 39 "gsed", 40 "flex", "bison", 41 42 # libs: crypto 43 "gnutls", 44 45 # libs: images 46 "jpeg-turbo", 47 "png", 48 49 # libs: ui 50 "sdl2", 51 "gtk3", 52 "libxkbcommon", 53 54 # libs: opengl 55 "libepoxy", 56 "mesa-libs", 57 ] 58 59 BUILD_SCRIPT = """ 60 set -e; 61 rm -rf /home/qemu/qemu-test.* 62 cd $(mktemp -d /home/qemu/qemu-test.XXXXXX); 63 mkdir src build; cd src; 64 tar -xf /dev/vtbd1; 65 cd ../build 66 ../src/configure --python=python3.6 {configure_opts}; 67 gmake --output-sync -j{jobs} {target} {verbose}; 68 """ 69 70 def console_boot_serial(self): 71 self.console_wait_send("Autoboot", "3") 72 self.console_wait_send("OK", "set console=comconsole\n") 73 self.console_wait_send("OK", "boot\n") 74 75 def build_image(self, img): 76 self.print_step("Downloading install iso") 77 cimg = self._download_with_cache(self.link, sha256sum=self.csum) 78 img_tmp = img + ".tmp" 79 iso = img + ".install.iso" 80 iso_xz = iso + ".xz" 81 82 self.print_step("Preparing iso and disk image") 83 subprocess.check_call(["cp", "-f", cimg, iso_xz]) 84 subprocess.check_call(["xz", "-dvf", iso_xz]) 85 self.exec_qemu_img("create", "-f", "qcow2", img_tmp, self.size) 86 87 self.print_step("Booting installer") 88 self.boot(img_tmp, extra_args = [ 89 "-bios", "pc-bios/bios-256k.bin", 90 "-machine", "graphics=off", 91 "-cdrom", iso 92 ]) 93 self.console_init() 94 self.console_boot_serial() 95 self.console_wait_send("Console type", "xterm\n") 96 97 # pre-install configuration 98 self.console_wait_send("Welcome", "\n") 99 self.console_wait_send("Keymap Selection", "\n") 100 self.console_wait_send("Set Hostname", "freebsd\n") 101 self.console_wait_send("Distribution Select", "\n") 102 self.console_wait_send("Partitioning", "\n") 103 self.console_wait_send("Partition", "\n") 104 self.console_wait_send("Scheme", "\n") 105 self.console_wait_send("Editor", "f") 106 self.console_wait_send("Confirmation", "c") 107 108 self.print_step("Installation started now, this will take a while") 109 110 # post-install configuration 111 self.console_wait("New Password:") 112 self.console_send("%s\n" % self.ROOT_PASS) 113 self.console_wait("Retype New Password:") 114 self.console_send("%s\n" % self.ROOT_PASS) 115 116 self.console_wait_send("Network Configuration", "\n") 117 self.console_wait_send("IPv4", "y") 118 self.console_wait_send("DHCP", "y") 119 self.console_wait_send("IPv6", "n") 120 self.console_wait_send("Resolver", "\n") 121 122 self.console_wait_send("Time Zone Selector", "a\n") 123 self.console_wait_send("Confirmation", "y") 124 self.console_wait_send("Time & Date", "\n") 125 self.console_wait_send("Time & Date", "\n") 126 127 self.console_wait_send("System Configuration", "\n") 128 self.console_wait_send("System Hardening", "\n") 129 130 # qemu user 131 self.console_wait_send("Add User Accounts", "y") 132 self.console_wait("Username") 133 self.console_send("%s\n" % self.GUEST_USER) 134 self.console_wait("Full name") 135 self.console_send("%s\n" % self.GUEST_USER) 136 self.console_wait_send("Uid", "\n") 137 self.console_wait_send("Login group", "\n") 138 self.console_wait_send("Login group", "\n") 139 self.console_wait_send("Login class", "\n") 140 self.console_wait_send("Shell", "\n") 141 self.console_wait_send("Home directory", "\n") 142 self.console_wait_send("Home directory perm", "\n") 143 self.console_wait_send("Use password", "\n") 144 self.console_wait_send("Use an empty password", "\n") 145 self.console_wait_send("Use a random password", "\n") 146 self.console_wait("Enter password:") 147 self.console_send("%s\n" % self.GUEST_PASS) 148 self.console_wait("Enter password again:") 149 self.console_send("%s\n" % self.GUEST_PASS) 150 self.console_wait_send("Lock out", "\n") 151 self.console_wait_send("OK", "yes\n") 152 self.console_wait_send("Add another user", "no\n") 153 154 self.console_wait_send("Final Configuration", "\n") 155 self.console_wait_send("Manual Configuration", "\n") 156 self.console_wait_send("Complete", "\n") 157 158 self.print_step("Installation finished, rebooting") 159 self.console_boot_serial() 160 161 # setup qemu user 162 prompt = "$" 163 self.console_ssh_init(prompt, self.GUEST_USER, self.GUEST_PASS) 164 self.console_wait_send(prompt, "exit\n") 165 166 # setup root user 167 prompt = "root@freebsd:~ #" 168 self.console_ssh_init(prompt, "root", self.ROOT_PASS) 169 self.console_sshd_config(prompt) 170 171 # setup serial console 172 self.console_wait(prompt) 173 self.console_send("echo 'console=comconsole' >> /boot/loader.conf\n") 174 175 # setup boot delay 176 self.console_wait(prompt) 177 self.console_send("echo 'autoboot_delay=1' >> /boot/loader.conf\n") 178 179 # setup virtio-blk #1 (tarfile) 180 self.console_wait(prompt) 181 self.console_send("echo 'chmod 666 /dev/vtbd1' >> /etc/rc.local\n") 182 183 self.print_step("Configuration finished, rebooting") 184 self.console_wait_send(prompt, "reboot\n") 185 self.console_wait("login:") 186 self.wait_ssh() 187 188 self.print_step("Installing packages") 189 self.ssh_root_check("pkg install -y %s\n" % " ".join(self.pkgs)) 190 191 # shutdown 192 self.ssh_root(self.poweroff) 193 self.console_wait("Uptime:") 194 self.wait() 195 196 if os.path.exists(img): 197 os.remove(img) 198 os.rename(img_tmp, img) 199 os.remove(iso) 200 self.print_step("All done") 201 202if __name__ == "__main__": 203 sys.exit(basevm.main(FreeBSDVM)) 204