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