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