---
title: "KVM On Arch"
date: 2021-10-07
draft: false
tags: ["Arch", "linux", "kvm", "virtualmachine"]
authors: ["trent"]
post: 24
---
date: 2021-10-07

## Introduction
This is not intended to be a tutorial, but rather a walk-through of
how I would install
[libvirt/kvm on Arch Linux](https://wiki.archlinux.org/title/Libvirt){target="_blank"}.

## Packages
* iptables-nft
* dnsmasq
* bridge-utils
* openbsd-netcat
* libvirt
* qemu-headless
* virt-install

`virt-install` is not needed if connecting remotely with virt-manager,
but it does provide `virt-clone`.

## Configuration
* enable libvirtd service
    * `systemctl enable libvirtd`
* add user to libvirt group
    * `usermod -a -G libvirt <user>`
### environment/bashrc
```cfg
# ~/.bashrc
export LIBVIRT_DEFAULT_URI="qemu:///system"
```
reboot the machine

## Network
The default network is defined in `/etc/libvirt/qemu/networks/default.xml`.

* Start the default network `virsh net-start default`.
* Permanently enable the default network `virsh net-autostart default`.

## Jump Host With `virt-manager`
Abstract your jump host in `~/.ssh/config`
```cfg
# ~/.ssh/config
Host jumphost
        Hostname <ip address>
        Port 22
        User <user>

Host kvmhost
        Hostname <ip address>
        ProxyJump jumphost
        Port 22
        User <user>
```
Now you can connect `virt-manager` to <user\>@kvmhost

## Console Access
Enable serial console on guest.
`systemctl enable serial-getty@ttyS0.service`

## Nested KVM
I was going to try to figure out how to permantly set the cpu mode default
such that all virtualmachines will be capable of nested virtualization,
but it already is. Perhaps that is the default in `virt-manager` now?

Anyway, in case you want to make sure
[nested virtualization](https://wiki.archlinux.org/title/KVM#Nested_virtualization){target="_blank"}
is enabled in the host kernel.

## Clone Ip Address Conflict
I found a great tutorial
[for assigning ip addresses](https://bobcares.com/blog/virsh-set-ip-address/){target="_blank"}.

The problem we need to solve here is that virtual machine clones won't necessarily
solicit a unique ip address, although a clone will have a new `mac address`.

So, you clone a vm:

  * `virt-clone --original arch --name archone --auto-clone`

Get the clone's mac address:

  * `virsh dumpxml archone | grep mac`

### Now assign the clone a dhcp reservation:

  * `virsh net-edit default`

Notice that I tighten up the dhcp range, and add a
reservation outside the new dhcp range.

```xml
<network connections='1'>
  <name>default</name>
  <uuid>8013c9a5-606f-48a0-a3ec-1cf097e76fb1</uuid>
  <forward mode='nat'>
    <nat>
      <port start='1024' end='65535'/>
    </nat>
  </forward>
  <bridge name='virbr0' stp='on' delay='0'/>
  <mac address='52:54:00:ef:cb:d2'/>
  <ip address='192.168.122.1' netmask='255.255.255.0'>
    <dhcp>
      <!-- previous dhcp range
      <range start='192.168.122.2' end='192.168.122.254'/>
      -->
      <!-- begin new lines -->
      <range start='192.168.122.50' end='192.168.122.150'/>
      <host mac='52:54:00:cd:7d:7f' name='archone' ip='192.168.122.25'/>
      <!-- end new lines -->
    </dhcp>
  </ip>
</network>
```

### Restart Default Network
* `virsh net-destroy default`
* `virsh net-start default`
