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