xref: /freebsd/sys/contrib/openzfs/.github/workflows/scripts/qemu-2-start.sh (revision 24e4dcf4ba5e9dedcf89efd358ea3e1fe5867020)
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-ALPHA2"
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  tumbleweed)
131    OSNAME="openSUSE Tumbleweed"
132    OSv="opensusetumbleweed"
133    MIRROR="http://opensuse-mirror-gce-us.susecloud.net"
134    URL="$MIRROR/tumbleweed/appliances/openSUSE-MicroOS.x86_64-OpenStack-Cloud.qcow2"
135    ;;
136  ubuntu22)
137    OSNAME="Ubuntu 22.04"
138    OSv="ubuntu22.04"
139    URL="$UBMIRROR/jammy/current/jammy-server-cloudimg-amd64.img"
140    ;;
141  ubuntu24)
142    OSNAME="Ubuntu 24.04"
143    OSv="ubuntu24.04"
144    URL="$UBMIRROR/noble/current/noble-server-cloudimg-amd64.img"
145    ;;
146  *)
147    echo "Wrong value for OS variable!"
148    exit 111
149    ;;
150esac
151
152# environment file
153ENV="/var/tmp/env.txt"
154echo "ENV=$ENV" >> $ENV
155
156# result path
157echo 'RESPATH="/var/tmp/test_results"' >> $ENV
158
159# FreeBSD 13 has problems with: e1000 and virtio
160echo "NIC=$NIC" >> $ENV
161
162# freebsd15 -> used in zfs-qemu.yml
163echo "OS=$OS" >> $ENV
164
165# freebsd14.0 -> used for virt-install
166echo "OSv=\"$OSv\"" >> $ENV
167
168# FreeBSD 15 (Current) -> used for summary
169echo "OSNAME=\"$OSNAME\"" >> $ENV
170
171# default vm count for testings
172VMs=2
173echo "VMs=\"$VMs\"" >> $ENV
174
175# default cpu count for testing vm's
176CPU=2
177echo "CPU=\"$CPU\"" >> $ENV
178
179sudo mkdir -p "/mnt/tests"
180sudo chown -R $(whoami) /mnt/tests
181
182DISK="/dev/zvol/zpool/openzfs"
183sudo zfs create -ps -b 64k -V 80g zpool/openzfs
184while true; do test -b $DISK && break; sleep 1; done
185
186# we are downloading via axel, curl and wget are mostly slower and
187# require more return value checking
188IMG="/mnt/tests/cloud-image"
189if [ ! -z "$URLxz" ]; then
190  echo "Loading $URLxz ..."
191  time axel -q -o "$IMG" "$URLxz"
192  echo "Loading $KSRC ..."
193  time axel -q -o ~/src.txz $KSRC
194else
195  echo "Loading $URL ..."
196  time axel -q -o "$IMG" "$URL"
197fi
198
199echo "Importing VM image to zvol..."
200if [ ! -z "$URLxz" ]; then
201  xzcat -T0 $IMG | sudo dd of=$DISK bs=4M
202else
203  sudo qemu-img dd -f qcow2 -O raw if=$IMG of=$DISK bs=4M
204fi
205rm -f $IMG
206
207PUBKEY=$(cat ~/.ssh/id_ed25519.pub)
208if [ ${OS:0:7} != "freebsd" ]; then
209  cat <<EOF > /tmp/user-data
210#cloud-config
211
212hostname: $OS
213
214users:
215- name: root
216  shell: $BASH
217- name: zfs
218  sudo: ALL=(ALL) NOPASSWD:ALL
219  shell: $BASH
220  ssh_authorized_keys:
221    - $PUBKEY
222
223growpart:
224  mode: auto
225  devices: ['/']
226  ignore_growroot_disabled: false
227EOF
228else
229  cat <<EOF > /tmp/user-data
230#cloud-config
231
232hostname: $OS
233
234# minimized config without sudo for nuageinit of FreeBSD
235growpart:
236  mode: auto
237  devices: ['/']
238  ignore_growroot_disabled: false
239EOF
240fi
241
242sudo virsh net-update default add ip-dhcp-host \
243  "<host mac='52:54:00:83:79:00' ip='192.168.122.10'/>" --live --config
244
245sudo virt-install \
246  --os-variant $OSv \
247  --name "openzfs" \
248  --cpu host-passthrough \
249  --virt-type=kvm --hvm \
250  --vcpus=4,sockets=1 \
251  --memory $((1024*12)) \
252  --memballoon model=virtio \
253  --graphics none \
254  --network bridge=virbr0,model=$NIC,mac='52:54:00:83:79:00' \
255  --cloud-init user-data=/tmp/user-data \
256  --disk $DISK,bus=virtio,cache=none,format=raw,driver.discard=unmap \
257  --import --noautoconsole ${OPTS[0]} ${OPTS[1]} >/dev/null
258
259# Give the VMs hostnames so we don't have to refer to them with
260# hardcoded IP addresses.
261#
262# vm0:          Initial VM we install dependencies and build ZFS on.
263# vm1..2        Testing VMs
264for ((i=0; i<=VMs; i++)); do
265  echo "192.168.122.1$i vm$i" | sudo tee -a /etc/hosts
266done
267
268# in case the directory isn't there already
269mkdir -p $HOME/.ssh
270
271cat <<EOF >> $HOME/.ssh/config
272# no questions please
273StrictHostKeyChecking no
274
275# small timeout, used in while loops later
276ConnectTimeout 1
277EOF
278
279if [ ${OS:0:7} != "freebsd" ]; then
280  # enable KSM on Linux
281  sudo virsh dommemstat --domain "openzfs" --period 5
282  sudo virsh node-memory-tune 100 50 1
283  echo 1 | sudo tee /sys/kernel/mm/ksm/run > /dev/null
284else
285  # on FreeBSD we need some more init stuff, because of nuageinit
286  BASH="/usr/local/bin/bash"
287  while pidof /usr/bin/qemu-system-x86_64 >/dev/null; do
288    ssh 2>/dev/null root@vm0 "uname -a" && break
289  done
290  ssh root@vm0 "pkg install -y bash ca_root_nss git qemu-guest-agent python3 py311-cloud-init"
291  ssh root@vm0 "chsh -s $BASH root"
292  ssh root@vm0 'sysrc qemu_guest_agent_enable="YES"'
293  ssh root@vm0 'sysrc cloudinit_enable="YES"'
294  ssh root@vm0 "pw add user zfs -w no -s $BASH"
295  ssh root@vm0 'mkdir -p ~zfs/.ssh'
296  ssh root@vm0 'echo "zfs ALL=(ALL:ALL) NOPASSWD: ALL" >> /usr/local/etc/sudoers'
297  ssh root@vm0 'echo "PubkeyAuthentication yes" >> /etc/ssh/sshd_config'
298  scp ~/.ssh/id_ed25519.pub "root@vm0:~zfs/.ssh/authorized_keys"
299  ssh root@vm0 'chown -R zfs ~zfs'
300  ssh root@vm0 'service sshd restart'
301  scp ~/src.txz "root@vm0:/tmp/src.txz"
302  ssh root@vm0 'tar -C / -zxf /tmp/src.txz'
303fi
304