summaryrefslogtreecommitdiffstats
path: root/tools/vm/launch_vgpu_qemu.sh
blob: 818f0bbb8f587257f0c7d2507f7eb517682efe1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2026 Open-IOV contributors
# QEMU launch script for NVIDIA vGPU testing
# This script assumes:
# 1. nvidia-vgpu-vfio.ko is loaded on host (with dry_run=1 for testing)
# 2. An mdev device has been created via sysfs
# 3. Guest OS has NVIDIA vGPU guest driver installed

set -euo pipefail

# Configuration
MDEV_UUID="${MDEV_UUID:-$(cat /proc/sys/kernel/random/uuid)}"
VGPU_TYPE="${VGPU_TYPE:-nvidia-vgpu-turing}"  # or ampere, hopper, pascal
QEMU_BIN="${QEMU_BIN:-qemu-system-x86_64}"
GUEST_IMG="${GUEST_IMG:-/var/lib/libvirt/images/ubuntu-vgpu.qcow2}"
GUEST_KERNEL="${GUEST_KERNEL:-/boot/vmlinuz-$(uname -r)}"
GUEST_INITRD="${GUEST_INITRD:-/boot/initrd.img-$(uname -r)}"

# PCI address of the NVIDIA GPU (TU104: 10de:1ed3)
GPU_PCI_ADDR="${GPU_PCI_ADDR:-0000:01:00.0}"

echo "=== NVIDIA vGPU QEMU Launch Script ==="
echo "MDEV UUID: $MDEV_UUID"
echo "VGPU Type: $VGPU_TYPE"
echo "GPU PCI: $GPU_PCI_ADDR"
echo ""

# Check if mdev device exists
MDEV_PATH="/sys/bus/mdev/devices/$MDEV_UUID"
if [[ ! -d "$MDEV_PATH" ]]; then
    echo "ERROR: mdev device $MDEV_UUID not found"
    echo ""
    echo "To create an mdev device (requires NVIDIA vGPU host driver):"
    echo "  echo $MDEV_UUID > /sys/bus/pci/devices/$GPU_PCI_ADDR/mdev_supported_types/$VGPU_TYPE/create"
    echo ""
    echo "For dry-run testing without host driver, this script will not work for actual QEMU launch."
    echo "The kernel module loads in dry-run mode but cannot create real mdev devices."
    exit 1
fi

# Get the mdev device's VFIO group
VFIO_GROUP=$(basename $(readlink -f "$MDEV_PATH/iommu_group"))
VFIO_DEV="/dev/vfio/$VFIO_GROUP"

if [[ ! -e "$VFIO_DEV" ]]; then
    echo "ERROR: VFIO device $VFIO_DEV not found"
    exit 1
fi

echo "Using VFIO device: $VFIO_DEV"
echo ""

# QEMU command
exec $QEMU_BIN \
    -name "vgpu-guest" \
    -enable-kvm \
    -cpu host,host-passthrough=on \
    -smp 4,sockets=1,cores=4,threads=1 \
    -m 8G \
    -device vfio-pci,sysfsdev=/sys/bus/mdev/devices/$MDEV_UUID,display=on,x-igd-opregion=on \
    -device virtio-net-pci,netdev=net0 \
    -netdev user,id=net0,hostfwd=tcp::2222-:22 \
    -drive file=$GUEST_IMG,if=virtio,format=qcow2 \
    -display gtk,gl=on \
    -vga none \
    "$@"