| 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # OpenBSD 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 | |
| 15 | import os |
| 16 | import sys |
| 17 | import subprocess |
| 18 | import basevm |
| 19 | |
| 20 | class OpenBSDVM(basevm.BaseVM): |
| 21 | name = "openbsd" |
| 22 | arch = "x86_64" |
| 23 | |
| 24 | link = "https://cdn.openbsd.org/pub/OpenBSD/7.8/amd64/install78.iso" |
| 25 | csum = "a228d0a1ef558b4d9ec84c698f0d3ffd13cd38c64149487cba0f1ad873be07b2" |
| 26 | size = "20G" |
| 27 | |
| 28 | BUILD_SCRIPT = """ |
| 29 | set -e; |
| 30 | rm -rf /home/qemu/qemu-test.* |
| 31 | cd $(mktemp -d /home/qemu/qemu-test.XXXXXX); |
| 32 | mkdir src build; cd src; |
| 33 | tar -xf /dev/rsd1c; |
| 34 | cd ../build; |
| 35 | ../src/configure --cc=cc --extra-cflags=-I/usr/local/include \ |
| 36 | --extra-ldflags=-L/usr/local/lib {configure_opts}; |
| 37 | gmake --output-sync -j{jobs} {target} {verbose}; |
| 38 | """ |
| 39 | poweroff = "halt -p" |
| 40 | |
| 41 | def build_image(self, img): |
| 42 | self.print_step("Downloading install iso") |
| 43 | cimg = self._download_with_cache(self.link, sha256sum=self.csum) |
| 44 | img_tmp = img + ".tmp" |
| 45 | iso = img + ".install.iso" |
| 46 | |
| 47 | self.print_step("Preparing iso and disk image") |
| 48 | subprocess.check_call(["cp", "-f", cimg, iso]) |
| 49 | self.exec_qemu_img("create", "-f", "qcow2", img_tmp, self.size) |
| 50 | |
| 51 | self.print_step("Booting installer") |
| 52 | self.boot(img_tmp, extra_args = [ |
| 53 | "-machine", "graphics=off", |
| 54 | "-device", "VGA", |
| 55 | "-cdrom", iso |
| 56 | ]) |
| 57 | self.console_init() |
| 58 | self.console_wait_send("boot>", "set tty com0\n") |
| 59 | self.console_wait_send("boot>", "\n") |
| 60 | |
| 61 | # pre-install configuration |
| 62 | self.console_wait_send("(I)nstall", "i\n") |
| 63 | self.console_wait_send("Terminal type", "xterm\n") |
| 64 | self.console_wait_send("System hostname", "openbsd\n") |
| 65 | self.console_wait_send("Network interface to configure", "vio0\n") |
| 66 | self.console_wait_send("IPv4 address", "autoconf\n") |
| 67 | self.console_wait_send("IPv6 address", "none\n") |
| 68 | self.console_wait_send("Network interface to configure", "done\n") |
| 69 | self.console_wait("Password for root account") |
| 70 | self.console_send("%s\n" % self._config["root_pass"]) |
| 71 | self.console_wait("Password for root account") |
| 72 | self.console_send("%s\n" % self._config["root_pass"]) |
| 73 | self.console_wait_send("Start sshd(8)", "yes\n") |
| 74 | self.console_wait_send("X Window System", "no\n") |
| 75 | self.console_wait_send("console to com0", "\n") |
| 76 | self.console_wait_send("Which speed", "\n") |
| 77 | |
| 78 | self.console_wait("Setup a user") |
| 79 | self.console_send("%s\n" % self._config["guest_user"]) |
| 80 | self.console_wait("Full name") |
| 81 | self.console_send("%s\n" % self._config["guest_user"]) |
| 82 | self.console_wait("Password") |
| 83 | self.console_send("%s\n" % self._config["guest_pass"]) |
| 84 | self.console_wait("Password") |
| 85 | self.console_send("%s\n" % self._config["guest_pass"]) |
| 86 | |
| 87 | self.console_wait_send("Allow root ssh login", "yes\n") |
| 88 | self.console_wait_send("timezone", "UTC\n") |
| 89 | self.console_wait_send("root disk", "\n") |
| 90 | self.console_wait_send("Encrypt the root disk with a (p)assphrase", "no\n") |
| 91 | self.console_wait_send("(W)hole disk", "\n") |
| 92 | self.console_wait_send("(A)uto layout", "c\n") |
| 93 | |
| 94 | # 4000 MB / as /dev/sd0a, at start of disk |
| 95 | self.console_wait_send("sd0>", "a a\n") |
| 96 | self.console_wait_send("offset:", "\n") |
| 97 | self.console_wait_send("size:", "4000M\n") |
| 98 | self.console_wait_send("FS type", "4.2BSD\n") |
| 99 | self.console_wait_send("mount point:", "/\n") |
| 100 | |
| 101 | # 256 MB swap as /dev/sd0b |
| 102 | self.console_wait_send("sd0*>", "a b\n") |
| 103 | self.console_wait_send("offset:", "\n") |
| 104 | self.console_wait_send("size:", "256M\n") |
| 105 | self.console_wait_send("FS type", "swap\n") |
| 106 | |
| 107 | # All remaining space for /home as /dev/sd0d |
| 108 | # NB, 'c' isn't allowed to be used. |
| 109 | self.console_wait_send("sd0*>", "a d\n") |
| 110 | self.console_wait_send("offset:", "\n") |
| 111 | self.console_wait_send("size:", "\n") |
| 112 | self.console_wait_send("FS type", "4.2BSD\n") |
| 113 | self.console_wait_send("mount point:", "/home\n") |
| 114 | |
| 115 | self.console_wait_send("sd0*>", "q\n") |
| 116 | self.console_wait_send("Write new label?:", "y\n") |
| 117 | |
| 118 | self.console_wait_send("Location of sets", "cd0\n") |
| 119 | self.console_wait_send("Pathname to the sets", "\n") |
| 120 | self.console_wait_send("Set name(s)", "\n") |
| 121 | self.console_wait_send("without verification", "yes\n") |
| 122 | |
| 123 | self.print_step("Installation started now, this will take a while") |
| 124 | self.console_wait_send("Location of sets", "done\n") |
| 125 | |
| 126 | self.console_wait("successfully completed") |
| 127 | self.print_step("Installation finished, rebooting") |
| 128 | self.console_wait_send("(R)eboot", "reboot\n") |
| 129 | |
| 130 | # setup qemu user |
| 131 | prompt = "$" |
| 132 | self.console_ssh_init(prompt, self._config["guest_user"], |
| 133 | self._config["guest_pass"]) |
| 134 | self.console_wait_send(prompt, "exit\n") |
| 135 | |
| 136 | # setup root user |
| 137 | prompt = "openbsd#" |
| 138 | self.console_ssh_init(prompt, "root", self._config["root_pass"]) |
| 139 | self.console_sshd_config(prompt) |
| 140 | |
| 141 | # setup virtio-blk #1 (tarfile) |
| 142 | self.console_wait(prompt) |
| 143 | self.console_send("echo 'chmod 666 /dev/rsd1c' >> /etc/rc.local\n") |
| 144 | |
| 145 | # enable w+x for /home |
| 146 | self.console_wait(prompt) |
| 147 | self.console_send("sed -i -e '/home/s/rw,/rw,wxallowed,/' /etc/fstab\n") |
| 148 | |
| 149 | # tweak datasize limit |
| 150 | self.console_wait(prompt) |
| 151 | self.console_send("sed -i -e 's/\\(datasize[^=]*\\)=[^:]*/\\1=infinity/' /etc/login.conf\n") |
| 152 | |
| 153 | # use http (be proxy cache friendly) |
| 154 | self.console_wait(prompt) |
| 155 | self.console_send("sed -i -e 's/https/http/' /etc/installurl\n") |
| 156 | |
| 157 | self.print_step("Configuration finished, rebooting") |
| 158 | self.console_wait_send(prompt, "reboot\n") |
| 159 | self.console_wait("login:") |
| 160 | self.wait_ssh() |
| 161 | |
| 162 | pkgs = self.get_qemu_packages_from_lcitool_json() |
| 163 | self.print_step("Installing packages") |
| 164 | self.ssh_root_check("pkg_add %s\n" % " ".join(pkgs)) |
| 165 | |
| 166 | # shutdown |
| 167 | self.ssh_root(self.poweroff) |
| 168 | self.wait() |
| 169 | |
| 170 | if os.path.exists(img): |
| 171 | os.remove(img) |
| 172 | os.rename(img_tmp, img) |
| 173 | os.remove(iso) |
| 174 | self.print_step("All done") |
| 175 | |
| 176 | if __name__ == "__main__": |
| 177 | sys.exit(basevm.main(OpenBSDVM)) |