summaryrefslogtreecommitdiff
path: root/run
blob: c2a960233458d55b7c7af0844c05a494bb241767 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#! /bin/sh

# Auto-install OpenBSD/amd64 6.6 to a QEMU guest machine.
#
# Inspired by:
#
# - https://man.openbsd.org/autoinstall
#
# - https://eradman.com/posts/autoinstall-openbsd.html
#
# - https://drewdevault.com/2018/09/10/Getting-started-with-qemu.html
#
# Copyright (c) 2020 Stefan Kreutz <mail@skreutz.com>

set -o errexit
set -o xtrace

# Fail early if any necessary tools are missing.
for c in curl qemu-img qemu-system-x86_64 rsync signify ssh
do
  command -v "$c" >/dev/null
done

# Read the public ssh key to be installed in the guest.
ssh_pub_key="$( cat ~/.ssh/id_rsa.pub )"

# Generate a response file for unattended installation.
mkdir -p http
cat << EOF > http/install.conf
Change the default console to com0 = yes
Which speed should com0 use = 115200
System hostname = openbsd-vm
Password for root = *************
Public ssh key for root account = ${ssh_pub_key}
Allow root ssh login = prohibit-password
Setup a user = puffy
Password for user = *************
Public ssh key for user = ${ssh_pub_key}
What timezone are you in = UTC
Location of sets = http
HTTP Server = 10.0.2.2
Unable to connect using https. Use http instead = yes
EOF

# Download OpenBSD/amd64 6.6 distribution.
[ -e openbsd-66-base.pub ] || curl --output openbsd-66-base.pub --silent https://ftp.openbsd.org/pub/OpenBSD/6.6/openbsd-66-base.pub
( [ -d http/pub/OpenBSD/6.6/amd64 ] && ( cd http/pub/OpenBSD/6.6/amd64 && signify -C -q -p ../../../../../openbsd-66-base.pub -x SHA256.sig ) ) || {
  mkdir -p http/pub/OpenBSD/6.6/amd64
  rsync --recursive --delete --quiet rsync://ftp.halifax.rwth-aachen.de/openbsd/6.6/amd64/ http/pub/OpenBSD/6.6/amd64/
  ( cd http/pub/OpenBSD/6.6/amd64 && signify -C -q -p ../../../../../openbsd-66-base.pub -x SHA256.sig )
}

# TODO: Start HTTP server.
# sudo python3 -m http.server --directory http 80

# Collect files to be served over TFTP.
mkdir -p tftp
cmp -s http/pub/OpenBSD/6.6/amd64/pxeboot tftp/pxeboot || cp -a http/pub/OpenBSD/6.6/amd64/pxeboot tftp/pxeboot
cmp -s http/pub/OpenBSD/6.6/amd64/bsd.rd tftp/bsd.rd || cp -a http/pub/OpenBSD/6.6/amd64/bsd.rd tftp/bsd.rd
rm -f tftp/auto_install
ln -s pxeboot tftp/auto_install

# Create boot configuration.
mkdir -p tftp/etc
cat << EOF > tftp/etc/boot.conf
stty com0 115200
set tty com0
boot tftp:/bsd.rd
EOF

# Create copy-on-write disk image.
[ -e vm.qcow2 ] || qemu-img create -f qcow2 vm.qcow2 16G

# Auto-install and start guest machine.
#
# network = 10.0.2.0/24
# host    = 10.0.2.2
# dns     = 10.0.2.3
# guest   = 10.0.2.15-31
qemu-system-x86_64 \
  -enable-kvm \
  -m 2G \
  -device e1000,netdev=n1 \
  -netdev user,id=n1,tftp=tftp,bootfile=auto_install,hostfwd=tcp::2222-:22 \
  -drive file=vm.qcow2,media=disk,if=virtio \
  -nographic

# Connect with the following command:
#
#   ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -o "Port 2222" root@127.0.0.1
#
# Execute a command (add -T):
#
#   ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -o "Port 2222" -T root@127.0.0.1 "uptime"
#
# Execute a heredoc:
#
#   ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -o "Port 2222" -T root@127.0.0.1 << SSHEOF
#     set -o errexit
#     uptime
#   SSHEOF
#
# Copy a file:
#
#   scp -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -o "Port 2222" -r notes.txt root@127.0.0.1:
#
# Stop guest machine with C-a x
#
# Show help with C-a h

# TODO: Install a site-specific file set to apply the following patches.
#
# echo 'https://cdn.openbsd.org/pub/OpenBSD' > /etc/installurl
# echo 'permit keepenv :wheel' > /etc/doas.conf
# syspatch
# shutdown -r now
Generated by cgit. See skreutz.com for my tech blog and contact information.