# Installing on KVM and libvirt

> One VM for the control plane, GPU VMs added and removed as demand changes.

## Why two virtual machines

The control plane can run continuously in its own VM, and you add or remove GPU VMs with any number of GPUs as you wish, with no change for API clients. This gives you full flexibility on your GPU resources, yet continuous operations on the inference services and APIs.

## Overview

| | Control plane VM | Inference node VM |
|---|---|---|
| Example name and address | `onprem-ai-control-plane`, `10.20.0.10` | `onprem-ai-inference-node`, `10.20.0.21` |
| vCPUs | 4 | 8 for 1-2 GPUs, 16 for 4 GPUs, 32 for 8 GPUs |
| Memory | 8 GB | 32 GB for 1 GPU, 64 GB for 2-4 GPUs, 128 GB for 5-8 GPUs |
| Disk | 128 GB | 64 GB system + 500 GB or more for model weights |
| GPU | none | 1 or more, VFIO passthrough |
| Runs all the time | yes | as demand needs |

- A few special models have larger CPU workloads and need more vCPUs and memory than these sizes. This is uncommon; the model's deploy profile lists its limits.
- Tested with: not yet. Written for an RHEL, Rocky Linux or AlmaLinux 9/10 or Ubuntu 22.04/24.04 host with `virt-install`, NVIDIA GPUs, Ubuntu Server 24.04 LTS in the VMs. Cockpit and `virt-manager` offer the same settings.
- Not covered: NVIDIA vGPU (one GPU shared between VMs; needs an NVIDIA AI Enterprise license).
- Examples: bridge `br0`, GPU at `0000:41:00.0`.
- Install options in detail: **Installing and updating the control plane**, **Installing, updating, moving and removing an inference node**.

## Before you start

- **Host firmware:** VT-d (Intel) or AMD-Vi / IOMMU (AMD) on ("Auto" is not always on), Above 4G decoding on.
- **Network bridge** on the host, here `br0` (NetworkManager, Cockpit **Networking → Add bridge**, or netplan). libvirt's NAT network `default` is not enough.
- **Host memory:** both VMs, plus about 8 GB for the host. A GPU VM locks all its memory.
- **Addresses:** fixed for both VMs (static or DHCP reservation). The control plane's address must never change.
- **Network:**

  | From | To | Port |
  |---|---|---|
  | Inference node VM | Control plane VM | 443 |
  | Browsers | Control plane VM | 3443 |
  | API clients | Control plane VM | 443 |
  | Anything | Inference node VM | none |

- **Internet:** both VMs, directly or through a proxy. Behind a proxy, do the steps of the guide [Outbound proxy](/en/platform/docs/v5.10/guides/certificates-and-network/outbound-proxy/index.md) on both VMs before the install: the proxy for Docker, the proxy's certificate authority when it inspects TLS, and the proxy in both install commands: the variable `HTTPS_PROXY` for the control plane VM, the option `--outbound-proxy-url` for the inference node VM.

## Step 1: Prepare the host

As root on the host.

**KVM, libvirt, UEFI firmware:**

```sh
# RHEL, Rocky Linux, AlmaLinux
dnf install -y qemu-kvm libvirt virt-install edk2-ovmf
systemctl enable --now libvirtd

# Ubuntu
apt-get install -y qemu-system-x86 libvirt-daemon-system virtinst ovmf
```

- RHEL 9 and later with modular daemons: `systemctl enable --now virtqemud.socket` instead of `libvirtd`.

**IOMMU on, host drivers off the GPU** (no NVIDIA driver on the host):

```sh
# RHEL, Rocky Linux, AlmaLinux (AMD hosts: leave out intel_iommu=on)
grubby --update-kernel=ALL --args="intel_iommu=on iommu=pt rd.driver.blacklist=nouveau modprobe.blacklist=nouveau"

# Ubuntu: add "intel_iommu=on iommu=pt" to GRUB_CMDLINE_LINUX_DEFAULT in /etc/default/grub, then
update-grub
echo "blacklist nouveau" > /etc/modprobe.d/blacklist-gpu.conf
update-initramfs -u
```

Reboot.

**Check the IOMMU and the GPU's IOMMU group:**

```sh
dmesg | grep -e DMAR -e IOMMU -e AMD-Vi
lspci -nn | grep -i nvidia
virsh nodedev-list --cap pci | grep 41_00
find /sys/kernel/iommu_groups/ -type l | grep 41:00
```

- `virsh nodedev-list` names the GPU for libvirt, for example `pci_0000_41_00_0`.
- Other devices in the GPU's IOMMU group: use another PCIe slot.

## Step 2: Create the control plane VM

Download the Ubuntu Server 24.04 ISO to `/var/lib/libvirt/images/`.

```sh
virt-install --name onprem-ai-control-plane \
  --osinfo ubuntu24.04 --cpu host-passthrough --vcpus 4 --memory 8192 \
  --disk size=128,bus=virtio,discard=unmap \
  --network bridge=br0,model=virtio \
  --cdrom /var/lib/libvirt/images/ubuntu-24.04.3-live-server-amd64.iso \
  --autostart --graphics vnc
```

- Older `virt-install`: `--os-variant` instead of `--osinfo`.
- Console: `virt-viewer onprem-ai-control-plane`, Cockpit, or `virt-manager`.

**Ubuntu installer:**

- Network: manual, `10.20.0.10/24`
- Storage: entire disk; set the root volume to the full size
- OpenSSH server: yes
- Featured snaps: none (Docker as a snap is not supported)

**After the first boot:**

```sh
sudo apt-get update
sudo apt-get install -y qemu-guest-agent docker.io docker-compose-v2
```

## Step 3: Install the control plane

```sh
curl -fsSL https://get.onprem.ai/control-plane | sh
```

- Asks for `sudo` once.
- Open `https://10.20.0.10:3443`. The first visit creates the first administrator.
- Browser certificate warning: see **Trusting the control plane in your browser**.

## Step 4: Create the inference node VM

Clone or make a template now, if you want to. Never after step 6: a clone after step 6 copies the inference node's identity.

Example for 1 GPU (8 vCPUs, 32 GB):

```sh
virt-install --name onprem-ai-inference-node \
  --osinfo ubuntu24.04 --machine q35 \
  --boot uefi,firmware.feature0.name=secure-boot,firmware.feature0.enabled=no \
  --cpu host-passthrough --vcpus 8 --memory 32768 \
  --disk size=64,bus=virtio,discard=unmap \
  --disk size=1000,bus=virtio,discard=unmap \
  --network bridge=br0,model=virtio \
  --hostdev pci_0000_41_00_0 \
  --cdrom /var/lib/libvirt/images/ubuntu-24.04.3-live-server-amd64.iso \
  --autostart --graphics vnc
```

| Option | Why |
|---|---|
| `--machine q35`, `--boot uefi,...enabled=no` | PCIe and UEFI, Secure Boot off; otherwise the NVIDIA driver needs an enrolled key |
| `--cpu host-passthrough` | Lets the firmware map GPUs with 24 GB or more |
| Second `--disk` | Disk 2, model weights. Exclude it from backups: model weights can be downloaded again |
| `--hostdev pci_0000_41_00_0` | The GPU. One `--hostdev` per GPU, and one for its audio function when it shares the IOMMU group |

- Older `virt-install` without `firmware.feature0`: `--boot loader=/usr/share/OVMF/OVMF_CODE_4M.fd,loader.readonly=yes,loader.type=pflash` (Ubuntu) or `--boot loader=/usr/share/edk2/ovmf/OVMF_CODE.fd,loader.readonly=yes,loader.type=pflash` (RHEL family).

Install Ubuntu as in step 2, with `10.20.0.21/24`. Leave disk 2 empty.

**Limits of a GPU VM:**

- No live migration, no snapshots with memory.
- Stop it with `virsh shutdown`, never `virsh destroy`. Some GPUs then need a host power cycle.

## Step 5: GPU driver, Docker, NVIDIA Container Toolkit

On the inference node VM.

**GPU visible?**

```sh
lspci -nn | grep -i nvidia
```

**Driver:**

```sh
sudo apt-get update
sudo apt-get install -y qemu-guest-agent linux-headers-$(uname -r)
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2404/x86_64/cuda-keyring_1.1-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb
sudo apt-get update
sudo apt-get install -y nvidia-open
```

- `nvidia-open`: Turing (2018) and newer. Older GPUs: `cuda-drivers`.

**Display modules off** (they can hang at shutdown and lock the GPU):

```sh
printf 'blacklist nvidia_drm\nblacklist nvidia_modeset\ninstall nvidia_drm /bin/false\ninstall nvidia_modeset /bin/false\n' \
  | sudo tee /etc/modprobe.d/nvidia-headless.conf
sudo update-initramfs -u
sudo reboot
```

**Check:** every GPU with its memory; only `nvidia` and `nvidia_uvm` loaded.

```sh
nvidia-smi
lsmod | grep nvidia
```

**Docker and the NVIDIA Container Toolkit:**

```sh
sudo apt-get install -y docker.io
curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey \
  | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg
curl -fsSL https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list \
  | sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' \
  | sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
sudo apt-get update
sudo apt-get install -y nvidia-container-toolkit
sudo nvidia-ctk runtime configure --runtime=docker
sudo systemctl restart docker
```

**Check:** the answer names `nvidia`.

```sh
sudo docker info --format '{{json .Runtimes}}'
```

## Step 6: Install the inference node

1. Control plane: **Servers → Add Inference Node**, copy the command.
2. Run it on the inference node VM within 15 minutes (the join token works once, for 15 minutes):

   ```sh
   curl -fsSL https://get.onprem.ai/node | sh -s -- \
     --control-plane-gateway-address 10.20.0.10 \
     --control-plane-root-certificate-sha256 <fingerprint> --join-token <token>
   ```

3. Mount disk 2 as the cached file set directory (`lsblk` names it, here `/dev/vdb`):

   ```sh
   sudo onprem-ai-node stop
   sudo mkfs.xfs -L cached-file-sets /dev/vdb
   sudo mkdir -p /var/lib/onprem-ai/inference-node/cached-file-sets
   echo 'LABEL=cached-file-sets /var/lib/onprem-ai/inference-node/cached-file-sets xfs defaults,nofail 0 2' \
     | sudo tee -a /etc/fstab
   sudo systemctl daemon-reload
   sudo mount /var/lib/onprem-ai/inference-node/cached-file-sets
   sudo chown onprem-ai:onprem-ai /var/lib/onprem-ai/inference-node/cached-file-sets
   sudo onprem-ai-node start
   ```

   The node's identity stays on disk 1, which your backup keeps. Details: **Data directories**.

## Step 7: Check

- **Servers:** the inference node is **Connected**, with every GPU.
- On the VM: `sudo onprem-ai-node doctor` names anything wrong.
- First model: **Getting Started**.

## Adding and removing GPU virtual machines

The control plane keeps running through all of these.

| Task | Steps |
|---|---|
| Add an inference node | Steps 4 to 7 with a new VM, a new address and a new command from **Add Inference Node**. |
| More GPUs in an existing VM | `virsh shutdown`, `virsh attach-device --config` (or `virsh edit`) for the GPU, raise vCPUs and memory, `virsh start`. **Servers** shows the new GPUs. Its models stop while the VM is down. |
| Remove an inference node for good | Move its models to other inference nodes first. Then on the VM: `sudo onprem-ai-node uninstall --remove-data`. Delete the VM. |
| Pause an inference node | `sudo onprem-ai-node stop`, then `virsh shutdown`. It keeps its models and comes back with `sudo onprem-ai-node start`. |

- Models keep serving from their replicas on the other inference nodes.
- A model with all its replicas on the removed node stops serving. Deploy a replica elsewhere first: **Deploying models**.

## When a step fails

| What you see | Fix |
|---|---|
| `virsh start`: `IOMMU not found` or `vfio ... failed to setup container` | IOMMU on in the firmware and the kernel (step 1). |
| `virsh start`: group is not viable | Pass the GPU's own functions through too, or use another PCIe slot. |
| `virsh start`: cannot allocate or lock memory | Lower the VM's memory, or stop other VMs. |
| VM log: `NVRM: BAR0 is 0M`; `nvidia-smi`: `No devices were found` | Check `host-passthrough` in `virsh dumpxml`. Still failing: `virsh edit`, add `<maxphysaddr mode='passthrough'/>` inside `<cpu>` (libvirt 8.7+), shut down and start. Still failing: `virsh edit`, add the `qemu:commandline` element (QEMU namespace on `<domain>`) with `-fw_cfg name=opt/ovmf/X-PciMmio64Mb,string=524288` (in MB, the safe values as for VMware: `524288` for 1 x H200, `1048576` for 2, `2097152` for 4, `4194304` for 8), shut down and start. |
| `nvidia-smi`: couldn't communicate with the NVIDIA driver | `mokutil --sb-state`: enabled means Secure Boot is on; recreate the VM with it off (step 4). GPU older than Turing: `cuda-drivers`. |
| After a VM restart: `No devices were found`, log shows `WPR2 already up` | Power cycle the host. Check the display modules are off (step 5). Use `virsh shutdown`, never `virsh destroy`. |
| Multi-GPU model stuck in Starting, log stops at `NCCL`, GPUs at 100% | The GPUs cannot send data directly to each other in this VM. Use one GPU per replica, or an inference node installed on the machine itself. |
| Two inference nodes connect and disconnect in turn | A clone made after step 6. On the clone only: `sudo onprem-ai-node remove-data --reset-identity`, then `sudo onprem-ai-node connect` with a new command from **Add Inference Node**. Never `uninstall --remove-data` on the clone: it removes the original too. |
| Browsers or the inference node cannot reach the control plane | `virsh domiflist onprem-ai-control-plane` must show `br0`, not the NAT network; change it with `virsh edit`. |
| Install stops: Docker not running, or a snap | `sudo snap remove docker`, then `docker.io` (step 2). |
| Install stops: token expired or already used | New command from **Add Inference Node**. |
| Install stops: control plane not reachable | Open port 443 from `10.20.0.21` to `10.20.0.10` (`firewalld` on the host, network, `ufw`). |
| Install stops: certificate does not name the address | See **Installing an inference node**, The address in the install command. |
| `doctor`: `nvidia-container-runtime-not-registered` | Repeat the toolkit block (step 5). |
| **Servers** shows no GPUs | `sudo onprem-ai-node doctor`; **Inference Node Container Detection** explains each finding. |
