summaryrefslogtreecommitdiff
path: root/run
blob: 55ea44f1f95ca0fc26d2f2366d7eab23319512a7 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#! /bin/sh

# Auto-install OpenBSD/amd64 6.6 to a QEMU guest machine.
#
# Run the following command to serve the OpenBSD mirror at
# http://127.0.0.1:8080:
#
#   python3 -m http.server \
#     --directory ./openbsd-vm/mirror \
#     --bind 127.0.0.1 8080
#
# Pass the following options to ssh or scp to connect to the guest machine:
#
#   ssh \
#     -o "StrictHostKeyChecking no" \
#     -o "UserKnownHostsFile /dev/null" \
#     -o "Port 2222" \
#     puffy@127.0.0.1
#
# For example, the following command forwards port 8080 on the host to port 80
# on the guest:
#
#   ssh \
#     -o "StrictHostKeyChecking no" \
#     -o "UserKnownHostsFile /dev/null" \
#     -o "Port 2222" \
#     -N \
#     -L 127.0.0.1:8080:127.0.0.1:80 \
#     puffy@127.0.0.1
#
# Press C-a x to stop the guest machine. Press C-a h to show other options.
#
# The virtual network:
#
#   network    = 10.0.2.0/24
#   host       = 10.0.2.2
#   nameserver = 10.0.2.3
#   guest      = 10.0.2.15-31
#
# Port forwardings:
#
#   10.0.2.1:80 -> host:8080
#   host:2222   -> guest:22
#
# Copyright (c) 2020 Stefan Kreutz <mail@skreutz.com>

set -o errexit
set -o nounset
set -o xtrace

# Set parameters.
VM_FILE="${VM_FILE-openbsd-vm.qcow2}"
DISK_SIZE="${DISK_SIZE-160G}"
CPU_COUNT="${CPU_COUNT-6}"
MEMORY_SIZE="${MEMORY_SIZE-4G}"

# Remove existing virtual machine if configuration changed.
if [ -e "${VM_FILE}" ] ;
then
  vm_created="$( stat -c %W "${VM_FILE}" )"
  for f in boot.conf disklabel install.conf install.site
  do
    if [ "${vm_created}" -lt "$( stat -c %Y "$f" )" ] ;
    then
      ( >&2 printf "%s changed. Recreating virtual machine." "$f" )
      rm "${VM_FILE}"
    fi
  done
fi

if [ ! -e "${VM_FILE}" ] ;
then

  # Download and verify OpenBSD/amd64 6.6 installation images and file sets.
  mkdir -p .openbsd-vm/mirror/pub/OpenBSD/6.6
  if [ ! -e .openbsd-vm/mirror/pub/OpenBSD/6.6/openbsd-66-base.pub ] ;
  then
    curl \
      --output .openbsd-vm/mirror/pub/OpenBSD/6.6/openbsd-66-base.pub \
      --silent \
      https://ftp.openbsd.org/pub/OpenBSD/6.6/openbsd-66-base.pub
  fi
  if [ ! -d .openbsd-vm/mirror/pub/OpenBSD/6.6/amd64 ] ;
  then
    mkdir -p .openbsd-vm/tmp
    rsync --recursive --delete --quiet \
      rsync://ftp.halifax.rwth-aachen.de/openbsd/6.6/amd64/ \
      .openbsd-vm/tmp/
    ( cd .openbsd-vm/tmp && \
      signify -C -q -p ../mirror/pub/OpenBSD/6.6/openbsd-66-base.pub -x SHA256.sig )
    mv .openbsd-vm/tmp .openbsd-vm/mirror/pub/OpenBSD/6.6/amd64
  fi

  # Create site-specific file set.
  if [ ! -x install.site ] ;
  then
    chmod +x install.site
  fi
  rm -f .openbsd-vm/mirror/pub/OpenBSD/6.6/amd64/site66.tgz
  tar -czf .openbsd-vm/mirror/pub/OpenBSD/6.6/amd64/site66.tgz install.site
  ( cd .openbsd-vm/mirror/pub/OpenBSD/6.6/amd64 && ls -l > index.txt )

  # Add public ssh key to install.conf.
  cp install.conf .openbsd-vm/mirror/
  if ! grep -q -e "^Public ssh key for user" install.conf ;
  then
    ssh_pub_key="$( cat ~/.ssh/id_rsa.pub )"
    echo "Public ssh key for user = ${ssh_pub_key}" \
      >> .openbsd-vm/mirror/install.conf
  fi

  # Copy disklabel template.
  cp disklabel .openbsd-vm/mirror/

  # Wait until the HTTP server is online.
  #
  while [ ! "$( curl --silent --location --write-out '%{http_code}\n' --output /dev/null http://127.0.0.1:8080/install.conf )" = 200 ] ;
  do
    ( >&2 printf "Please serve the directory ./openbsd-vm/mirror at http://127.0.0.1:8080.\n" )
    sleep 5
  done

  # Collect files to be served over TFTP.
  rm -rf .openbsd-vm/tftp
  mkdir .openbsd-vm/tftp
  ln -s ../mirror/pub/OpenBSD/6.6/amd64/pxeboot .openbsd-vm/tftp/auto_install
  ln -s ../mirror/pub/OpenBSD/6.6/amd64/bsd.rd .openbsd-vm/tftp/bsd.rd
  mkdir .openbsd-vm/tftp/etc
  cp boot.conf .openbsd-vm/tftp/etc/

  # Create copy-on-write disk image.
  qemu-img create -f qcow2 "${VM_FILE}" "${DISK_SIZE}"

fi

# Auto-install guest machine.
qemu-system-x86_64 \
  -enable-kvm \
  -m "${MEMORY_SIZE}" \
  -smp "cpus=${CPU_COUNT}" \
  -device e1000,netdev=n1 \
  -netdev "user,id=n1,hostname=openbsd-vm,tftp-server-name=10.0.2.1,tftp=.openbsd-vm/tftp,bootfile=auto_install,hostfwd=tcp::2222-:22,guestfwd=tcp:10.0.2.1:80-cmd:socat - tcp:127.0.0.1:8080" \
  -drive "file=${VM_FILE},media=disk,if=virtio" \
  -nographic
Generated by cgit. See skreutz.com for my tech blog and contact information.