xref: /freebsd/sys/contrib/openzfs/.github/workflows/scripts/qemu-2-start.sh (revision 8ccc0d235c226d84112561d453c49904398d085c)
1#!/usr/bin/env bash
2
3######################################################################
4# 2) start qemu with some operating system, init via cloud-init
5######################################################################
6
7set -eu
8
9# short name used in zfs-qemu.yml
10OS="$1"
11
12# OS variant (virt-install --os-variant list)
13OSv=$OS
14
15# FreeBSD urls's
16FREEBSD_REL="https://download.freebsd.org/releases/CI-IMAGES"
17FREEBSD_SNAP="https://download.freebsd.org/snapshots/CI-IMAGES"
18URLxz=""
19
20# Ubuntu mirrors
21UBMIRROR="https://cloud-images.ubuntu.com"
22#UBMIRROR="https://mirrors.cloud.tencent.com/ubuntu-cloud-images"
23#UBMIRROR="https://mirror.citrahost.com/ubuntu-cloud-images"
24
25# default nic model for vm's
26NIC="virtio"
27
28# additional options for virt-install
29OPTS[0]=""
30OPTS[1]=""
31
32case "$OS" in
33  almalinux8)
34    OSNAME="AlmaLinux 8"
35    URL="https://repo.almalinux.org/almalinux/8/cloud/x86_64/images/AlmaLinux-8-GenericCloud-latest.x86_64.qcow2"
36    ;;
37  almalinux9)
38    OSNAME="AlmaLinux 9"
39    URL="https://repo.almalinux.org/almalinux/9/cloud/x86_64/images/AlmaLinux-9-GenericCloud-latest.x86_64.qcow2"
40    ;;
41  almalinux10)
42    OSNAME="AlmaLinux 10"
43    OSv="almalinux9"
44    URL="https://repo.almalinux.org/almalinux/10/cloud/x86_64/images/AlmaLinux-10-GenericCloud-latest.x86_64.qcow2"
45    ;;
46  archlinux)
47    OSNAME="Archlinux"
48    URL="https://geo.mirror.pkgbuild.com/images/latest/Arch-Linux-x86_64-cloudimg.qcow2"
49    ;;
50  centos-stream9)
51    OSNAME="CentOS Stream 9"
52    URL="https://cloud.centos.org/centos/9-stream/x86_64/images/CentOS-Stream-GenericCloud-9-latest.x86_64.qcow2"
53    ;;
54  centos-stream10)
55    OSNAME="CentOS Stream 10"
56    OSv="centos-stream9"
57    URL="https://cloud.centos.org/centos/10-stream/x86_64/images/CentOS-Stream-GenericCloud-10-latest.x86_64.qcow2"
58    ;;
59  debian11)
60    OSNAME="Debian 11"
61    URL="https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-generic-amd64.qcow2"
62    ;;
63  debian12)
64    OSNAME="Debian 12"
65    URL="https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2"
66    ;;
67  debian13)
68    OSNAME="Debian 13"
69    # TODO: Overwrite OSv to debian13 for virt-install until it's added to osinfo
70    OSv="debian12"
71    URL="https://cloud.debian.org/images/cloud/trixie/latest/debian-13-generic-amd64.qcow2"
72    OPTS[0]="--boot"
73    OPTS[1]="uefi=on"
74    ;;
75  fedora41)
76    OSNAME="Fedora 41"
77    OSv="fedora-unknown"
78    URL="https://download.fedoraproject.org/pub/fedora/linux/releases/41/Cloud/x86_64/images/Fedora-Cloud-Base-Generic-41-1.4.x86_64.qcow2"
79    ;;
80  fedora42)
81    OSNAME="Fedora 42"
82    OSv="fedora-unknown"
83    URL="https://download.fedoraproject.org/pub/fedora/linux/releases/42/Cloud/x86_64/images/Fedora-Cloud-Base-Generic-42-1.1.x86_64.qcow2"
84    ;;
85  fedora43)
86    OSNAME="Fedora 43"
87    OSv="fedora-unknown"
88    URL="https://download.fedoraproject.org/pub/fedora/linux/releases/43/Cloud/x86_64/images/Fedora-Cloud-Base-Generic-43-1.6.x86_64.qcow2"
89    ;;
90  freebsd13-5r)
91    FreeBSD="13.5-RELEASE"
92    OSNAME="FreeBSD $FreeBSD"
93    OSv="freebsd13.0"
94    URLxz="$FREEBSD_REL/$FreeBSD/amd64/Latest/FreeBSD-$FreeBSD-amd64-BASIC-CI.raw.xz"
95    KSRC="$FREEBSD_REL/../amd64/$FreeBSD/src.txz"
96    NIC="rtl8139"
97    ;;
98  freebsd14-2r)
99    FreeBSD="14.2-RELEASE"
100    OSNAME="FreeBSD $FreeBSD"
101    OSv="freebsd14.0"
102    URLxz="$FREEBSD_REL/$FreeBSD/amd64/Latest/FreeBSD-$FreeBSD-amd64-BASIC-CI.raw.xz"
103    KSRC="$FREEBSD_REL/../amd64/$FreeBSD/src.txz"
104    ;;
105  freebsd14-3r)
106    FreeBSD="14.3-RELEASE"
107    OSNAME="FreeBSD $FreeBSD"
108    OSv="freebsd14.0"
109    URLxz="$FREEBSD_REL/$FreeBSD/amd64/Latest/FreeBSD-$FreeBSD-amd64-BASIC-CI.raw.xz"
110    KSRC="$FREEBSD_REL/../amd64/$FreeBSD/src.txz"
111    ;;
112  freebsd13-5s)
113    FreeBSD="13.5-STABLE"
114    OSNAME="FreeBSD $FreeBSD"
115    OSv="freebsd13.0"
116    URLxz="$FREEBSD_SNAP/$FreeBSD/amd64/Latest/FreeBSD-$FreeBSD-amd64-BASIC-CI.raw.xz"
117    KSRC="$FREEBSD_SNAP/../amd64/$FreeBSD/src.txz"
118    NIC="rtl8139"
119    ;;
120  freebsd14-3s)
121    FreeBSD="14.3-STABLE"
122    OSNAME="FreeBSD $FreeBSD"
123    OSv="freebsd14.0"
124    URLxz="$FREEBSD_SNAP/$FreeBSD/amd64/Latest/FreeBSD-$FreeBSD-amd64-BASIC-CI-ufs.raw.xz"
125    KSRC="$FREEBSD_SNAP/../amd64/$FreeBSD/src.txz"
126    ;;
127  freebsd15-0s)
128    FreeBSD="15.0-STABLE"
129    OSNAME="FreeBSD $FreeBSD"
130    OSv="freebsd14.0"
131    URLxz="$FREEBSD_SNAP/$FreeBSD/amd64/Latest/FreeBSD-$FreeBSD-amd64-BASIC-CI-ufs.raw.xz"
132    KSRC="$FREEBSD_SNAP/../amd64/$FreeBSD/src.txz"
133    ;;
134  freebsd16-0c)
135    FreeBSD="16.0-CURRENT"
136    OSNAME="FreeBSD $FreeBSD"
137    OSv="freebsd14.0"
138    URLxz="$FREEBSD_SNAP/$FreeBSD/amd64/Latest/FreeBSD-$FreeBSD-amd64-BASIC-CI-ufs.raw.xz"
139    KSRC="$FREEBSD_SNAP/../amd64/$FreeBSD/src.txz"
140    ;;
141  tumbleweed)
142    OSNAME="openSUSE Tumbleweed"
143    OSv="opensusetumbleweed"
144    MIRROR="http://opensuse-mirror-gce-us.susecloud.net"
145    URL="$MIRROR/tumbleweed/appliances/openSUSE-MicroOS.x86_64-OpenStack-Cloud.qcow2"
146    ;;
147  ubuntu22)
148    OSNAME="Ubuntu 22.04"
149    OSv="ubuntu22.04"
150    URL="$UBMIRROR/jammy/current/jammy-server-cloudimg-amd64.img"
151    ;;
152  ubuntu24)
153    OSNAME="Ubuntu 24.04"
154    OSv="ubuntu24.04"
155    URL="$UBMIRROR/noble/current/noble-server-cloudimg-amd64.img"
156    ;;
157  *)
158    echo "Wrong value for OS variable!"
159    exit 111
160    ;;
161esac
162
163# environment file
164ENV="/var/tmp/env.txt"
165echo "ENV=$ENV" >> $ENV
166
167# result path
168echo 'RESPATH="/var/tmp/test_results"' >> $ENV
169
170# FreeBSD 13 has problems with: e1000 and virtio
171echo "NIC=$NIC" >> $ENV
172
173# freebsd15 -> used in zfs-qemu.yml
174echo "OS=$OS" >> $ENV
175
176# freebsd14.0 -> used for virt-install
177echo "OSv=\"$OSv\"" >> $ENV
178
179# FreeBSD 15 (Current) -> used for summary
180echo "OSNAME=\"$OSNAME\"" >> $ENV
181
182# default vm count for testings
183VMs=2
184echo "VMs=\"$VMs\"" >> $ENV
185
186# default cpu count for testing vm's
187CPU=2
188echo "CPU=\"$CPU\"" >> $ENV
189
190sudo mkdir -p "/mnt/tests"
191sudo chown -R $(whoami) /mnt/tests
192
193DISK="/dev/zvol/zpool/openzfs"
194sudo zfs create -ps -b 64k -V 80g zpool/openzfs
195while true; do test -b $DISK && break; sleep 1; done
196
197# we are downloading via axel, curl and wget are mostly slower and
198# require more return value checking
199IMG="/mnt/tests/cloud-image"
200if [ ! -z "$URLxz" ]; then
201  echo "Loading $URLxz ..."
202  time axel -q -o "$IMG" "$URLxz"
203  echo "Loading $KSRC ..."
204  time axel -q -o ~/src.txz $KSRC
205else
206  echo "Loading $URL ..."
207  time axel -q -o "$IMG" "$URL"
208fi
209
210echo "Importing VM image to zvol..."
211if [ ! -z "$URLxz" ]; then
212  xzcat -T0 $IMG | sudo dd of=$DISK bs=4M
213else
214  sudo qemu-img dd -f qcow2 -O raw if=$IMG of=$DISK bs=4M
215fi
216rm -f $IMG
217
218PUBKEY=$(cat ~/.ssh/id_ed25519.pub)
219if [ ${OS:0:7} != "freebsd" ]; then
220  cat <<EOF > /tmp/user-data
221#cloud-config
222
223hostname: $OS
224
225users:
226- name: root
227  shell: $BASH
228- name: zfs
229  sudo: ALL=(ALL) NOPASSWD:ALL
230  shell: $BASH
231  ssh_authorized_keys:
232    - $PUBKEY
233
234growpart:
235  mode: auto
236  devices: ['/']
237  ignore_growroot_disabled: false
238EOF
239else
240  cat <<EOF > /tmp/user-data
241#cloud-config
242
243hostname: $OS
244
245# minimized config without sudo for nuageinit of FreeBSD
246growpart:
247  mode: auto
248  devices: ['/']
249  ignore_growroot_disabled: false
250EOF
251fi
252
253sudo virsh net-update default add ip-dhcp-host \
254  "<host mac='52:54:00:83:79:00' ip='192.168.122.10'/>" --live --config
255
256sudo virt-install \
257  --os-variant $OSv \
258  --name "openzfs" \
259  --cpu host-passthrough \
260  --virt-type=kvm --hvm \
261  --vcpus=4,sockets=1 \
262  --memory $((1024*12)) \
263  --memballoon model=virtio \
264  --graphics none \
265  --network bridge=virbr0,model=$NIC,mac='52:54:00:83:79:00' \
266  --cloud-init user-data=/tmp/user-data \
267  --disk $DISK,bus=virtio,cache=none,format=raw,driver.discard=unmap \
268  --import --noautoconsole ${OPTS[0]} ${OPTS[1]} >/dev/null
269
270# Give the VMs hostnames so we don't have to refer to them with
271# hardcoded IP addresses.
272#
273# vm0:          Initial VM we install dependencies and build ZFS on.
274# vm1..2        Testing VMs
275for ((i=0; i<=VMs; i++)); do
276  echo "192.168.122.1$i vm$i" | sudo tee -a /etc/hosts
277done
278
279# in case the directory isn't there already
280mkdir -p $HOME/.ssh
281
282cat <<EOF >> $HOME/.ssh/config
283# no questions please
284StrictHostKeyChecking no
285
286# small timeout, used in while loops later
287ConnectTimeout 1
288EOF
289
290if [ ${OS:0:7} != "freebsd" ]; then
291  # enable KSM on Linux
292  sudo virsh dommemstat --domain "openzfs" --period 5
293  sudo virsh node-memory-tune 100 50 1
294  echo 1 | sudo tee /sys/kernel/mm/ksm/run > /dev/null
295else
296  # on FreeBSD we need some more init stuff, because of nuageinit
297  BASH="/usr/local/bin/bash"
298  while pidof /usr/bin/qemu-system-x86_64 >/dev/null; do
299    ssh 2>/dev/null root@vm0 "uname -a" && break
300  done
301  ssh root@vm0 "env IGNORE_OSVERSION=yes pkg install -y bash ca_root_nss git qemu-guest-agent python3 py311-cloud-init"
302  ssh root@vm0 "chsh -s $BASH root"
303  ssh root@vm0 'sysrc qemu_guest_agent_enable="YES"'
304  ssh root@vm0 'sysrc cloudinit_enable="YES"'
305  ssh root@vm0 "pw add user zfs -w no -s $BASH"
306  ssh root@vm0 'mkdir -p ~zfs/.ssh'
307  ssh root@vm0 'echo "zfs ALL=(ALL:ALL) NOPASSWD: ALL" >> /usr/local/etc/sudoers'
308  ssh root@vm0 'echo "PubkeyAuthentication yes" >> /etc/ssh/sshd_config'
309  scp ~/.ssh/id_ed25519.pub "root@vm0:~zfs/.ssh/authorized_keys"
310  ssh root@vm0 'chown -R zfs ~zfs'
311  ssh root@vm0 'service sshd restart'
312  scp ~/src.txz "root@vm0:/tmp/src.txz"
313  ssh root@vm0 'tar -C / -zxf /tmp/src.txz'
314fi
315