2013-03-27 00:55:30 +01:00
# Vagrant Libvirt Provider
2016-11-26 11:38:02 +00:00
[](https://gitter.im/vagrant-libvirt/vagrant-libvirt?utm_source=badge& utm_medium=badge& utm_campaign=pr-badge& utm_content=badge)
2021-10-21 04:28:57 -04:00
[](https://github.com/vagrant-libvirt/vagrant-libvirt/actions/workflows/unit-tests.yml)
2016-11-27 02:08:08 +01:00
[](https://coveralls.io/github/vagrant-libvirt/vagrant-libvirt?branch=master)
2021-11-04 12:48:37 +00:00
[](https://badge.fury.io/rb/vagrant-libvirt)
2013-03-27 00:55:30 +01:00
2019-02-11 20:18:02 -05:00
This is a [Vagrant ](http://www.vagrantup.com ) plugin that adds a
2013-03-27 00:55:30 +01:00
[Libvirt ](http://libvirt.org ) provider to Vagrant, allowing Vagrant to
control and provision machines via Libvirt toolkit.
2016-09-07 14:41:25 +01:00
**Note:** Actual version is still a development one. Feedback is welcome and
can help a lot :-)
2013-03-27 00:55:30 +01:00
2016-12-20 14:22:34 +01:00
## Index
2020-05-10 14:23:25 +01:00
<!-- note in vim set "let g:vmt_list_item_char=' - '" to generate the correct output -->
<!-- vim - markdown - toc GFM -->
2016-12-20 14:22:34 +01:00
2020-10-03 17:16:00 +01:00
* [Features ](#features )
* [Future work ](#future-work )
2021-09-30 05:24:47 -07:00
* [Using the container image ](#using-the-container-image )
* [Using Docker ](#using-docker )
* [Using Podman ](#using-podman )
2021-06-25 19:28:17 +01:00
* [Extending the Docker image with additional vagrant plugins ](#extending-the-docker-image-with-additional-vagrant-plugins )
2020-10-03 17:16:00 +01:00
* [Installation ](#installation )
* [Possible problems with plugin installation on Linux ](#possible-problems-with-plugin-installation-on-linux )
2020-12-16 13:14:22 +00:00
* [Additional Notes for Fedora and Similar Linux Distributions ](#additional-notes-for-fedora-and-similar-linux-distributions )
2020-10-03 17:16:00 +01:00
* [Vagrant Project Preparation ](#vagrant-project-preparation )
* [Add Box ](#add-box )
* [Create Vagrantfile ](#create-vagrantfile )
* [Start VM ](#start-vm )
* [How Project Is Created ](#how-project-is-created )
* [Libvirt Configuration ](#libvirt-configuration )
* [Provider Options ](#provider-options )
* [Domain Specific Options ](#domain-specific-options )
* [Reload behavior ](#reload-behavior )
* [Networks ](#networks )
* [Private Network Options ](#private-network-options )
* [Public Network Options ](#public-network-options )
* [Management Network ](#management-network )
* [Additional Disks ](#additional-disks )
* [Reload behavior ](#reload-behavior-1 )
* [CDROMs ](#cdroms )
* [Input ](#input )
* [PCI device passthrough ](#pci-device-passthrough )
* [Using USB Devices ](#using-usb-devices )
* [USB Controller Configuration ](#usb-controller-configuration )
* [USB Device Passthrough ](#usb-device-passthrough )
* [USB Redirector Devices ](#usb-redirector-devices )
* [Filter for USB Redirector Devices ](#filter-for-usb-redirector-devices )
* [Random number generator passthrough ](#random-number-generator-passthrough )
2021-10-19 16:37:46 +02:00
* [Serial Console Devices ](#serial-console-devices )
2020-10-03 17:16:00 +01:00
* [Watchdog device ](#watchdog-device )
* [Smartcard device ](#smartcard-device )
* [Hypervisor Features ](#hypervisor-features )
2020-12-16 20:19:24 +01:00
* [Clock ](#clock )
2020-10-03 17:16:00 +01:00
* [CPU features ](#cpu-features )
* [Memory Backing ](#memory-backing )
* [No box and PXE boot ](#no-box-and-pxe-boot )
* [SSH Access To VM ](#ssh-access-to-vm )
* [Forwarded Ports ](#forwarded-ports )
2019-06-11 18:36:10 +02:00
* [Forwarding the ssh-port ](#forwarding-the-ssh-port )
2020-10-03 17:16:00 +01:00
* [Synced Folders ](#synced-folders )
* [QEMU Session Support ](#qemu-session-support )
* [Customized Graphics ](#customized-graphics )
* [TPM Devices ](#tpm-devices )
2020-12-16 13:14:22 +00:00
* [Memory balloon ](#memory-balloon )
2020-10-03 17:16:00 +01:00
* [Libvirt communication channels ](#libvirt-communication-channels )
* [Custom command line arguments and environment variables ](#custom-command-line-arguments-and-environment-variables )
2021-05-22 16:59:11 +01:00
* [Box Formats ](#box-formats )
* [Version 1 ](#version-1 )
* [Version 2 (Experimental) ](#version-2-experimental )
2020-10-03 17:16:00 +01:00
* [Create Box ](#create-box )
* [Package Box from VM ](#package-box-from-vm )
* [Troubleshooting VMs ](#troubleshooting-vms )
* [Development ](#development )
* [Contributing ](#contributing )
2015-09-25 16:39:20 +03:00
2020-05-10 14:23:25 +01:00
<!-- vim - markdown - toc -->
2014-02-24 17:24:22 +01:00
## Features
2013-03-27 00:55:30 +01:00
2014-05-06 21:01:43 +01:00
* Control local Libvirt hypervisors.
2016-09-07 14:41:25 +01:00
* Vagrant `up` , `destroy` , `suspend` , `resume` , `halt` , `ssh` , `reload` ,
`package` and `provision` commands.
2013-03-27 00:55:30 +01:00
* Upload box image (qcow2 format) to Libvirt storage pool.
* Create volume as COW diff image for domains.
2013-05-10 15:11:13 +02:00
* Create private networks.
2013-03-27 00:55:30 +01:00
* Create and boot Libvirt domains.
* SSH into domains.
2013-05-10 15:11:13 +02:00
* Setup hostname and network interfaces.
2013-03-27 00:55:30 +01:00
* Provision domains with any built-in Vagrant provisioner.
2021-04-17 01:37:59 +08:00
* Synced folder support via `rsync` , `nfs` , `9p` or `virtiofs` .
2014-05-06 21:01:43 +01:00
* Snapshots via [sahara ](https://github.com/jedi4ever/sahara ).
2016-09-07 14:41:25 +01:00
* Package caching via
[vagrant-cachier ](http://fgrehm.viewdocs.io/vagrant-cachier/ ).
* Use boxes from other Vagrant providers via
[vagrant-mutate ](https://github.com/sciurus/vagrant-mutate ).
2015-08-14 14:33:01 +02:00
* Support VMs with no box for PXE boot purposes (Vagrant 1.6 and up)
2013-03-27 00:55:30 +01:00
2013-04-02 20:06:52 +02:00
## Future work
2016-09-07 14:41:25 +01:00
* Take a look at [open
issues](https://github.com/vagrant-libvirt/vagrant-libvirt/issues?state=open).
2013-03-27 00:55:30 +01:00
2021-09-30 05:24:47 -07:00
## Using the container image
2020-10-03 17:16:00 +01:00
Due to the number of issues encountered around compatibility between the ruby runtime environment
that is part of the upstream vagrant installation and the library dependencies of libvirt that
this project requires to communicate with libvirt, there is a docker image build and published.
This should allow users to execute vagrant with vagrant-libvirt without needing to deal with
the compatibility issues, though you may need to extend the image for your own needs should
you make use of additional plugins.
2021-06-25 19:28:17 +01:00
Note the default image contains the full toolchain required to build and install vagrant-libvirt
and it's dependencies. There is also a smaller image published with the `-slim` suffix if you
just need vagrant-libvirt and don't need to install any additional plugins for your environment.
2021-09-30 05:24:47 -07:00
If you are connecting to a remote system libvirt, you may omit the
`-v /var/run/libvirt/:/var/run/libvirt/` mount bind. Some distributions patch the local
vagrant environment to ensure vagrant-libvirt uses `qemu:///session` , which means you
may need to set the environment variable `LIBVIRT_DEFAULT_URI` to the same value if
looking to use this in place of your distribution provided installation.
### Using Docker
2021-07-01 10:53:50 +01:00
To get the image with the most recent release:
2020-10-03 17:16:00 +01:00
```bash
docker pull vagrantlibvirt/vagrant-libvirt:latest
```
2021-07-01 10:53:50 +01:00
---
**Note** If you want the very latest code you can use the `edge` tag instead.
```bash
docker pull vagrantlibvirt/vagrant-libvirt:edge
```
---
2020-10-03 17:16:00 +01:00
Running the image:
```bash
2020-10-09 16:53:59 +08:00
docker run -it --rm \
2020-10-03 17:16:00 +01:00
-e LIBVIRT_DEFAULT_URI \
-v /var/run/libvirt/:/var/run/libvirt/ \
-v ~/.vagrant.d:/.vagrant.d \
2021-06-30 17:56:09 +01:00
-v $(realpath "${PWD}"):${PWD} \
-w $(realpath "${PWD}") \
2021-01-05 11:35:20 +01:00
--network host \
2020-10-03 17:16:00 +01:00
vagrantlibvirt/vagrant-libvirt:latest \
vagrant status
```
2021-09-30 05:24:47 -07:00
It's possible to define a function in `~/.bashrc` , for example:
2021-01-05 11:35:20 +01:00
```bash
2021-09-30 05:24:47 -07:00
vagrant(){
2021-01-05 11:35:20 +01:00
docker run -it --rm \
-e LIBVIRT_DEFAULT_URI \
-v /var/run/libvirt/:/var/run/libvirt/ \
-v ~/.vagrant.d:/.vagrant.d \
2021-06-30 17:56:09 +01:00
-v $(realpath "${PWD}"):${PWD} \
-w $(realpath "${PWD}") \
2021-01-05 11:35:20 +01:00
--network host \
vagrantlibvirt/vagrant-libvirt:latest \
2021-09-30 05:24:47 -07:00
vagrant $@
}
2021-01-05 11:35:20 +01:00
```
2021-09-30 05:24:47 -07:00
### Using Podman
2021-10-22 17:47:16 +01:00
Preparing the podman run, only once:
```bash
mkdir -p ~/.vagrant.d/{boxes,data,tmp}
```
_N.B. This is needed until the entrypoint works for podman to only mount the `~/.vagrant.d` directory_
2021-09-30 05:24:47 -07:00
To run with Podman you need to include
```bash
--entrypoint /bin/bash \
--security-opt label=disable \
-v ~/.vagrant.d/boxes:/vagrant/boxes \
-v ~/.vagrant.d/data:/vagrant/data \
2022-01-05 13:10:06 +09:00
-v ~/.vagrant.d/tmp:/vagrant/tmp \
2021-09-30 05:24:47 -07:00
```
for example:
```bash
vagrant(){
podman run -it --rm \
-e LIBVIRT_DEFAULT_URI \
-v /var/run/libvirt/:/var/run/libvirt/ \
-v ~/.vagrant.d/boxes:/vagrant/boxes \
-v ~/.vagrant.d/data:/vagrant/data \
2022-01-05 13:10:06 +09:00
-v ~/.vagrant.d/tmp:/vagrant/tmp \
2021-09-30 05:24:47 -07:00
-v $(realpath "${PWD}"):${PWD} \
-w $(realpath "${PWD}") \
--network host \
--entrypoint /bin/bash \
--security-opt label=disable \
docker.io/vagrantlibvirt/vagrant-libvirt:latest \
vagrant $@
}
```
Running Podman in rootless mode maps the root user inside the container to your host user so we need to bypass [entrypoint.sh ](https://github.com/vagrant-libvirt/vagrant-libvirt/blob/master/entrypoint.sh ) and mount persistent storage directly to `/vagrant` .
2020-10-03 17:16:00 +01:00
2021-06-25 19:28:17 +01:00
### Extending the Docker image with additional vagrant plugins
By default the image published and used contains the entire tool chain required
to reinstall the vagrant-libvirt plugin and it's dependencies, as this is the
default behaviour of vagrant anytime a new plugin is installed. This means it
should be possible to use a simple `FROM` statement and ask vagrant to install
additional plugins.
```
FROM vagrantlibvirt/vagrant-libvirt:latest
RUN vagrant plugin install < plugin >
```
2013-04-02 20:06:52 +02:00
## Installation
2019-02-11 20:18:02 -05:00
First, you should have both QEMU and Libvirt installed if you plan to run VMs
on your local system. For instructions, refer to your Linux distribution's
2016-09-07 14:41:25 +01:00
documentation.
2014-05-18 17:27:32 -05:00
2019-02-11 20:18:02 -05:00
**NOTE:** Before you start using vagrant-libvirt, please make sure your Libvirt
and QEMU installation is working correctly and you are able to create QEMU or
KVM type virtual machines with `virsh` or `virt-manager` .
2014-05-18 17:27:32 -05:00
2016-09-07 14:41:25 +01:00
Next, you must have [Vagrant
installed](http://docs.vagrantup.com/v2/installation/index.html).
2019-07-01 10:43:21 +01:00
Vagrant-libvirt supports Vagrant 2.0, 2.1 & 2.2. It should also work with earlier
releases from 1.5 onwards but they are not actively tested.
2021-10-21 04:28:57 -04:00
Check the [unit tests ](https://github.com/vagrant-libvirt/vagrant-libvirt/blob/master/.github/workflows/unit-tests.yml )
2019-07-01 10:43:21 +01:00
for the current list of tested versions.
2019-02-11 20:18:02 -05:00
*We only test with the upstream version!* If you decide to install your distro's
2016-10-08 00:02:03 +02:00
version and you run into problems, as a first step you should switch to upstream.
2016-12-06 19:45:27 +01:00
Now you need to make sure your have all the build dependencies installed for
2016-10-08 00:02:03 +02:00
vagrant-libvirt. This depends on your distro. An overview:
2019-10-20 15:35:44 +03:00
* Ubuntu 18.10, Debian 9 and up:
```shell
apt-get build-dep vagrant ruby-libvirt
apt-get install qemu libvirt-daemon-system libvirt-clients ebtables dnsmasq-base
apt-get install libxslt-dev libxml2-dev libvirt-dev zlib1g-dev ruby-dev
2021-06-04 13:10:53 -04:00
apt-get install libguestfs-tools
2019-10-20 15:35:44 +03:00
```
* Ubuntu 18.04, Debian 8 and older:
2016-10-08 00:02:03 +02:00
```shell
2016-10-30 15:05:43 -07:00
apt-get build-dep vagrant ruby-libvirt
2018-05-07 00:16:06 -05:00
apt-get install qemu libvirt-bin ebtables dnsmasq-base
2016-10-30 15:05:43 -07:00
apt-get install libxslt-dev libxml2-dev libvirt-dev zlib1g-dev ruby-dev
2021-06-01 17:45:02 +01:00
apt-get install libguestfs-tools
2016-10-08 00:02:03 +02:00
```
2016-10-30 15:05:43 -07:00
(It is possible some users will already have libraries from the third line installed, but this is the way to make it work OOTB.)
2016-10-08 00:02:03 +02:00
* CentOS 6, 7, Fedora 21:
```shell
2021-06-01 17:45:02 +01:00
yum install qemu libvirt libvirt-devel ruby-devel gcc qemu-kvm libguestfs-tools
2016-10-08 00:02:03 +02:00
```
* Fedora 22 and up:
```shell
2021-06-01 17:45:02 +01:00
dnf install -y gcc libvirt libvirt-devel libxml2-devel make ruby-devel libguestfs-tools
2016-10-08 00:02:03 +02:00
```
2013-03-27 00:55:30 +01:00
2019-07-05 13:46:53 +02:00
* OpenSUSE leap 15.1:
```shell
2021-06-01 17:45:02 +01:00
zypper install qemu libvirt libvirt-devel ruby-devel gcc qemu-kvm libguestfs
2019-07-05 13:46:53 +02:00
```
2019-02-11 20:18:02 -05:00
* Arch Linux: please read the related [ArchWiki ](https://wiki.archlinux.org/index.php/Vagrant#vagrant-libvirt ) page.
2016-10-18 20:05:02 +03:00
```shell
2016-12-09 15:26:06 +01:00
pacman -S vagrant
2016-10-18 20:05:02 +03:00
```
2016-09-07 14:41:25 +01:00
Now you're ready to install vagrant-libvirt using standard [Vagrant
plugin](http://docs.vagrantup.com/v2/plugins/usage.html) installation methods.
2020-05-10 15:54:56 +02:00
For some distributions you will need to specify `CONFIGURE_ARGS` variable before
running `vagrant plugin install` :
* Fedora 32 + upstream Vagrant:
```shell
export CONFIGURE_ARGS="with-libvirt-include=/usr/include/libvirt with-libvirt-lib=/usr/lib64"
```
2016-09-07 14:41:25 +01:00
```shell
2020-05-10 15:54:56 +02:00
vagrant plugin install vagrant-libvirt
2013-03-27 00:55:30 +01:00
```
2013-05-10 15:11:13 +02:00
### Possible problems with plugin installation on Linux
2013-03-27 00:55:30 +01:00
2013-04-11 17:03:24 +02:00
In case of problems with building nokogiri and ruby-libvirt gem, install
missing development libraries for libxslt, libxml2 and libvirt.
2013-03-27 00:55:30 +01:00
2016-09-07 14:41:25 +01:00
2016-10-30 15:05:43 -07:00
On Ubuntu, Debian, make sure you are running all three of the `apt` commands above with `sudo` .
2013-03-27 00:55:30 +01:00
2016-09-07 14:41:25 +01:00
On RedHat, Centos, Fedora, ...
2013-03-27 00:55:30 +01:00
2016-09-07 14:41:25 +01:00
```shell
2021-06-01 17:45:02 +01:00
$ sudo dnf install libxslt-devel libxml2-devel libvirt-devel ruby-devel gcc
2015-05-15 12:38:50 +02:00
```
2016-09-07 14:41:25 +01:00
2019-02-11 20:18:02 -05:00
On Arch Linux it is recommended to follow [steps from ArchWiki ](https://wiki.archlinux.org/index.php/Vagrant#vagrant-libvirt ).
2016-10-18 20:05:02 +03:00
2016-09-07 14:41:25 +01:00
If have problem with installation - check your linker. It should be `ld.gold` :
```shell
2015-05-15 12:38:50 +02:00
sudo alternatives --set ld /usr/bin/ld.gold
# OR
sudo ln -fs /usr/bin/ld.gold /usr/bin/ld
2013-03-27 00:55:30 +01:00
```
2016-10-08 00:02:03 +02:00
If you have issues building ruby-libvirt, try the following:
```shell
CONFIGURE_ARGS='with-ldflags=-L/opt/vagrant/embedded/lib with-libvirt-include=/usr/include/libvirt with-libvirt-lib=/usr/lib' GEM_HOME=~/.vagrant.d/gems GEM_PATH=$GEM_HOME:/opt/vagrant/embedded/gems PATH=/opt/vagrant/embedded/bin:$PATH vagrant plugin install vagrant-libvirt
```
2020-11-16 07:03:30 -08:00
### Additional Notes for Fedora and Similar Linux Distributions
If you encounter the following load error when using the vagrant-libvirt plugin (note the required by libssh):
```shell
/opt/vagrant/embedded/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require': /opt/vagrant/embedded/lib64/libcrypto.so.1.1: version ` OPENSSL_1_1_1b' not found (required by /lib64/libssh.so.4) - /home/xxx/.vagrant.d/gems/2.4.6/gems/ruby-libvirt-0.7.1/lib/_libvirt.so (LoadError)
```
then the following steps have been found to resolve the problem. Thanks to James Reynolds (see https://github.com/hashicorp/vagrant/issues/11020#issuecomment -540043472). The specific version of libssh will change over time so references to the rpm in the commands below will need to be adjusted accordingly.
```shell
2021-03-13 10:15:52 -05:00
# Fedora
2020-11-16 07:03:30 -08:00
dnf download --source libssh
2021-03-13 10:15:52 -05:00
# centos 8 stream, doesn't provide source RPMs, so you need to download like so
git clone https://git.centos.org/centos-git-common
# centos-git-common needs its tools in PATH
export PATH=$(readlink -f ./centos-git-common):$PATH
git clone https://git.centos.org/rpms/libssh
cd libssh
git checkout imports/c8s/libssh-0.9.4-1.el8
into_srpm.sh -d c8s
cd SRPMS
# common commands (make sure to adjust verison accordingly)
2020-11-16 07:03:30 -08:00
rpm2cpio libssh-0.9.0-5.fc30.src.rpm | cpio -imdV
tar xf libssh-0.9.0.tar.xz
mkdir build
cd build
cmake ../libssh-0.9.0 -DOPENSSL_ROOT_DIR=/opt/vagrant/embedded/
make
sudo cp lib/libssh* /opt/vagrant/embedded/lib64
```
If you encounter the following load error when using the vagrant-libvirt plugin (note the required by libk5crypto):
```shell
/opt/vagrant/embedded/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require': /usr/lib64/libk5crypto.so.3: undefined symbol: EVP_KDF_ctrl, version OPENSSL_1_1_1b - /home/rbelgrave/.vagrant.d/gems/2.4.9/gems/ruby-libvirt-0.7.1/lib/_libvirt.so (LoadError)
```
then the following steps have been found to resolve the problem. After the steps below are complete, then reinstall the vagrant-libvirt plugin without setting the `CONFIGURE_ARGS` . Thanks to Marco Bevc (see https://github.com/hashicorp/vagrant/issues/11020#issuecomment -625801983):
```shell
2021-03-13 10:15:52 -05:00
# Fedora
2020-11-16 07:03:30 -08:00
dnf download --source krb5-libs
2021-03-13 10:15:52 -05:00
# centos 8 stream, doesn't provide source RPMs, so you need to download like so
git clone https://git.centos.org/centos-git-common
# centos-git-common needs its tools in PATH
export PATH=$(readlink -f ./centos-git-common):$PATH
git clone https://git.centos.org/rpms/krb5
cd krb5
git checkout imports/c8s/krb5-1.18.2-8.el8
into_srpm.sh -d c8s
cd SRPMS
# common commands (make sure to adjust verison accordingly)
2020-11-16 07:03:30 -08:00
rpm2cpio krb5-1.18-1.fc32.src.rpm | cpio -imdV
tar xf krb5-1.18.tar.gz
cd krb5-1.18/src
./configure
make
sudo cp -P lib/crypto/libk5crypto.* /opt/vagrant/embedded/lib64/
```
2016-10-08 00:02:03 +02:00
2013-04-02 20:06:52 +02:00
## Vagrant Project Preparation
2013-03-27 00:55:30 +01:00
2014-05-11 14:46:45 -05:00
### Add Box
2013-03-27 00:55:30 +01:00
After installing the plugin (instructions above), the quickest way to get
2016-09-07 14:41:25 +01:00
started is to add Libvirt box and specify all the details manually within a
`config.vm.provider` block. So first, add Libvirt box using any name you want.
2019-02-11 20:18:02 -05:00
You can find more Libvirt-ready boxes at
[Vagrant Cloud ](https://app.vagrantup.com/boxes/search?provider=libvirt ). For
2016-09-07 14:41:25 +01:00
example:
2013-03-27 00:55:30 +01:00
2016-09-07 14:41:25 +01:00
```shell
2020-06-04 01:04:18 +01:00
vagrant init fedora/32-cloud-base
2013-03-27 00:55:30 +01:00
```
2014-05-11 14:46:45 -05:00
### Create Vagrantfile
2013-05-10 15:11:13 +02:00
And then make a Vagrantfile that looks like the following, filling in your
2016-09-07 14:41:25 +01:00
information where necessary. For example:
2013-03-27 00:55:30 +01:00
2013-04-02 20:06:52 +02:00
```ruby
2013-03-27 00:55:30 +01:00
Vagrant.configure("2") do |config|
config.vm.define :test_vm do |test_vm|
2020-06-04 01:04:18 +01:00
test_vm.vm.box = "fedora/32-cloud-base"
2013-03-27 00:55:30 +01:00
end
end
```
2013-05-10 15:11:13 +02:00
2014-05-11 14:46:45 -05:00
### Start VM
In prepared project directory, run following command:
2016-09-07 14:41:25 +01:00
```shell
2014-05-11 14:46:45 -05:00
$ vagrant up --provider=libvirt
```
Vagrant needs to know that we want to use Libvirt and not default VirtualBox.
That's why there is `--provider=libvirt` option specified. Other way to tell
Vagrant to use Libvirt provider is to setup environment variable
2015-04-13 17:30:01 +02:00
2016-09-07 14:41:25 +01:00
```shell
export VAGRANT_DEFAULT_PROVIDER=libvirt
```
2014-05-11 14:46:45 -05:00
### How Project Is Created
Vagrant goes through steps below when creating new project:
2019-02-11 20:18:02 -05:00
1. Connect to Libvirt locally or remotely via SSH.
2016-09-07 14:41:25 +01:00
2. Check if box image is available in Libvirt storage pool. If not, upload it
to remote Libvirt storage pool as new volume.
2016-09-06 16:26:33 +01:00
3. Create COW diff image of base box image for new Libvirt domain.
4. Create and start new domain on Libvirt host.
5. Check for DHCP lease from dnsmasq server.
6. Wait till SSH is available.
2016-09-07 14:41:25 +01:00
7. Sync folders and run Vagrant provisioner on new domain if setup in
Vagrantfile.
2014-05-11 14:46:45 -05:00
### Libvirt Configuration
### Provider Options
2013-03-27 00:55:30 +01:00
2016-09-07 14:41:25 +01:00
Although it should work without any configuration for most people, this
provider exposes quite a few provider-specific configuration options. The
following options allow you to configure how vagrant-libvirt connects to
2019-02-11 20:18:02 -05:00
Libvirt, and are used to generate the [Libvirt connection
2016-09-07 14:41:25 +01:00
URI](http://libvirt.org/uri.html):
2019-02-11 20:18:02 -05:00
* `driver` - A hypervisor name to access. For now only KVM and QEMU are
2016-09-07 14:41:25 +01:00
supported
2019-02-11 20:18:02 -05:00
* `host` - The name of the server, where Libvirtd is running
2016-09-07 14:41:25 +01:00
* `connect_via_ssh` - If use ssh tunnel to connect to Libvirt. Absolutely
2019-02-11 20:18:02 -05:00
needed to access Libvirt on remote host. It will not be able to get the IP
2016-09-07 14:41:25 +01:00
address of a started VM otherwise.
* `username` - Username and password to access Libvirt
* `password` - Password to access Libvirt
* `id_ssh_key_file` - If not nil, uses this ssh private key to access Libvirt.
Default is `$HOME/.ssh/id_rsa` . Prepends `$HOME/.ssh/` if no directory
2019-02-11 20:18:02 -05:00
* `socket` - Path to the Libvirt unix socket (e.g.
2016-09-07 14:41:25 +01:00
`/var/run/libvirt/libvirt-sock` )
2021-03-21 15:19:53 +00:00
* `proxy_command` - For advanced usage. When connecting to remote libvirt
instances, if the default constructed proxy\_command which uses `-W %h:%p`
does not work, set this as needed. It performs interpolation using `{key}`
and supports only `{host}` , `{username}` , and `{id_ssh_key_file}` . This is
to try and avoid issues with escaping `%` and `$` which might be necessary
to the ssh command itself. e.g.:
`libvirt.proxy_command = "ssh {host} -l {username} -i {id_ssh_key_file} nc %h %p"`
2019-02-11 20:18:02 -05:00
* `uri` - For advanced usage. Directly specifies what Libvirt connection URI
2016-09-07 14:41:25 +01:00
vagrant-libvirt should use. Overrides all other connection configuration
options
2014-06-09 00:12:50 -07:00
2021-03-17 14:41:14 +00:00
In the event that none of these are set (excluding the `driver` option) the
provider will attempt to retrieve the uri from the environment variable
`LIBVIRT_DEFAULT_URI` similar to how virsh works. If any of them are set, it
will ignore the environment variable. The reason the driver option is ignored
is that it is not uncommon for this to be explicitly set on the box itself
and there is no easily to determine whether it is being set by the user or
the box packager.
2014-06-09 00:12:50 -07:00
Connection-independent options:
2016-09-07 14:41:25 +01:00
* `storage_pool_name` - Libvirt storage pool name, where box image and instance
2020-06-04 16:19:34 +02:00
snapshots (if `snapshot_pool_name` is not set) will be stored.
* `snapshot_pool_name` - Libvirt storage pool name. If set, the created
snapshot of the instance will be stored at this location instead of
`storage_pool_name` .
2013-03-27 00:55:30 +01:00
2016-09-07 14:41:25 +01:00
For example:
2014-05-11 14:40:51 -05:00
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
libvirt.host = "example.com"
end
end
```
2013-04-11 17:03:24 +02:00
### Domain Specific Options
2020-08-12 21:26:36 +01:00
* `title` - A short description of the domain.
* `description` - A human readable description of the virtual machine.
2016-09-07 14:41:25 +01:00
* `disk_bus` - The type of disk device to emulate. Defaults to virtio if not
2019-02-11 20:18:02 -05:00
set. Possible values are documented in Libvirt's [description for
2016-09-07 14:41:25 +01:00
_target_ ](http://libvirt.org/formatdomain.html#elementsDisks ). NOTE: this
option applies only to disks associated with a box image. To set the bus type
on additional disks, see the [Additional Disks ](#additional-disks ) section.
2017-01-30 17:06:31 +01:00
* `disk_device` - The disk device to emulate. Defaults to vda if not
set, which should be fine for paravirtualized guests, but some fully
virtualized guests may require hda. NOTE: this option also applies only to
disks associated with a box image.
2020-12-17 22:03:47 +11:00
* `disk_driver` - Extra options for the main disk driver ([see Libvirt documentation ](http://libvirt.org/formatdomain.html#elementsDisks )).
NOTE: this option also applies only to disks associated with a box image. In all cases, the value `nil` can be used to force the hypervisor default behaviour (e.g. to override settings defined in top-level Vagrantfiles). Supported options include:
* `:cache` - Controls the cache mechanism. Possible values are "default", "none", "writethrough", "writeback", "directsync" and "unsafe".
* `:io` - Controls specific policies on I/O. Possible values are "threads" and "native".
* `:copy_on_read` - Controls whether to copy read backing file into the image file. The value can be either "on" or "off".
* `:discard` - Controls whether discard requests (also known as "trim" or "unmap") are ignored or passed to the filesystem. Possible values are "unmap" or "ignore".
Note: for discard to work, you will likely also need to set `disk_bus = 'scsi'`
* `:detect_zeroes` - Controls whether to detect zero write requests. The value can be "off", "on" or "unmap".
2016-09-07 14:41:25 +01:00
* `nic_model_type` - parameter specifies the model of the network adapter when
you create a domain value by default virtio KVM believe possible values, see
the [documentation for
2019-02-11 20:18:02 -05:00
Libvirt](https://libvirt.org/formatdomain.html#elementsNICSModel ).
2020-05-10 05:24:06 -05:00
* `shares` - Proportional weighted share for the domain relative to others. For more details see [documentation ](https://libvirt.org/formatdomain.html#elementsCPUTuning ).
2013-04-11 17:03:24 +02:00
* `memory` - Amount of memory in MBytes. Defaults to 512 if not set.
* `cpus` - Number of virtual cpus. Defaults to 1 if not set.
2020-04-29 13:08:01 -05:00
* `cpuset` - Physical cpus to which the vcpus can be pinned. For more details see [documentation ](https://libvirt.org/formatdomain.html#elementsCPUAllocation ).
2018-01-17 18:21:29 +07:00
* `cputopology` - Number of CPU sockets, cores and threads running per core. All fields of `:sockets` , `:cores` and `:threads` are mandatory, `cpus` domain option must be present and must be equal to total count of **sockets * cores * threads** . For more details see [documentation ](https://libvirt.org/formatdomain.html#elementsCPU ).
2020-05-10 04:52:46 -05:00
* `nodeset` - Physical NUMA nodes where virtual memory can be pinned. For more details see [documentation ](https://libvirt.org/formatdomain.html#elementsNUMATuning ).
2018-01-17 18:21:29 +07:00
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
libvirt.cpus = 4
2020-04-29 13:08:01 -05:00
libvirt.cpuset = '1-4,^3,6'
2018-01-17 18:21:29 +07:00
libvirt.cputopology :sockets => '2', :cores => '2', :threads => '1'
end
end
```
2016-09-07 14:41:25 +01:00
* `nested` - [Enable nested
2020-04-29 16:46:56 +02:00
virtualization](https://docs.fedoraproject.org/en-US/quick-docs/using-nested-virtualization-in-kvm/).
2016-09-07 14:41:25 +01:00
Default is false.
* `cpu_mode` - [CPU emulation
mode](https://libvirt.org/formatdomain.html#elementsCPU ). Defaults to
'host-model' if not set. Allowed values: host-model, host-passthrough,
custom.
2016-10-23 17:17:05 +02:00
* `cpu_model` - CPU Model. Defaults to 'qemu64' if not set and `cpu_mode` is
`custom` and to '' otherwise. This can really only be used when setting
`cpu_mode` to `custom` .
2019-02-11 20:18:02 -05:00
* `cpu_fallback` - Whether to allow Libvirt to fall back to a CPU model close
2016-09-07 14:41:25 +01:00
to the specified model if features in the guest CPU are not supported on the
host. Defaults to 'allow' if not set. Allowed values: `allow` , `forbid` .
2017-11-23 09:27:41 +01:00
* `numa_nodes` - Specify an array of NUMA nodes for the guest. The syntax is similar to what would be set in the domain XML. `memory` must be in MB. Symmetrical and asymmetrical topologies are supported but make sure your total count of defined CPUs adds up to `v.cpus` .
2017-08-02 01:07:23 -04:00
The sum of all the memory defined here will act as your total memory for your guest VM. **This sum will override what is set in `v.memory`**
```
2017-08-02 01:16:40 -04:00
v.cpus = 4
2017-08-02 01:07:23 -04:00
v.numa_nodes = [
2017-08-16 11:50:29 -04:00
{:cpus => "0-1", :memory => "1024"},
{:cpus => "2-3", :memory => "4096"}
2017-11-23 09:27:41 +01:00
]
2017-08-02 01:07:23 -04:00
```
2015-08-03 16:24:55 -05:00
* `loader` - Sets path to custom UEFI loader.
2016-09-07 14:41:25 +01:00
* `kernel` - To launch the guest with a kernel residing on host filesystems.
Equivalent to qemu `-kernel` .
* `initrd` - To specify the initramfs/initrd to use for the guest. Equivalent
to qemu `-initrd` .
* `random_hostname` - To create a domain name with extra information on the end
to prevent hostname conflicts.
2019-02-11 20:18:02 -05:00
* `default_prefix` - The default Libvirt guest name becomes a concatenation of the
2020-05-10 14:23:25 +01:00
`<current_directory>_<guest_name>` . The current working directory is the default prefix
2018-08-12 10:00:34 +10:00
to the guest name. The `default_prefix` options allow you to set the guest name prefix.
2016-09-07 14:41:25 +01:00
* `cmd_line` - Arguments passed on to the guest kernel initramfs or initrd to
2017-05-08 13:57:07 +02:00
use. Equivalent to qemu `-append` , only possible to use in combination with `initrd` and `kernel` .
2016-09-07 14:41:25 +01:00
* `graphics_type` - Sets the protocol used to expose the guest display.
Defaults to `vnc` . Possible values are "sdl", "curses", "none", "gtk", "vnc"
or "spice".
* `graphics_port` - Sets the port for the display protocol to bind to.
Defaults to 5900.
* `graphics_ip` - Sets the IP for the display protocol to bind to. Defaults to
"127.0.0.1".
* `graphics_passwd` - Sets the password for the display protocol. Working for
2019-02-11 20:18:02 -05:00
vnc and Spice. by default working without passsword.
* `graphics_autoport` - Sets autoport for graphics, Libvirt in this case
2016-09-07 14:41:25 +01:00
ignores graphics_port value, Defaults to 'yes'. Possible value are "yes" and
"no"
2021-10-26 15:11:30 +06:00
* `graphics_gl` - Set to `true` to enable OpenGL. Defaults to `true` if
`video_accel3d` is `true` .
2015-03-26 08:07:27 +01:00
* `keymap` - Set keymap for vm. default: en-us
2016-09-07 14:41:25 +01:00
* `kvm_hidden` - [Hide the hypervisor from the
guest](https://libvirt.org/formatdomain.html#elementsFeatures ). Useful for
2016-10-18 20:24:19 +03:00
[GPU passthrough ](#pci-device-passthrough ) on stubborn drivers. Default is false.
2016-09-07 14:41:25 +01:00
* `video_type` - Sets the graphics card type exposed to the guest. Defaults to
"cirrus". [Possible
values](http://libvirt.org/formatdomain.html#elementsVideo ) are "vga",
"cirrus", "vmvga", "xen", "vbox", or "qxl".
* `video_vram` - Used by some graphics card types to vary the amount of RAM
2022-01-17 15:10:23 +01:00
dedicated to video. Defaults to 16384.
2021-10-26 15:11:30 +06:00
* `video_accel3d` - Set to `true` to enable 3D acceleration. Defaults to
`false` .
2016-10-05 10:17:44 +11:00
* `sound_type` - [Set the virtual sound card ](https://libvirt.org/formatdomain.html#elementsSound )
Defaults to "ich6".
2016-09-07 14:41:25 +01:00
* `machine_type` - Sets machine type. Equivalent to qemu `-machine` . Use
`qemu-system-x86_64 -machine help` to get a list of supported machines.
2019-02-11 20:18:02 -05:00
* `machine_arch` - Sets machine architecture. This helps Libvirt to determine
the correct emulator type. Possible values depend on your version of QEMU.
2016-09-07 14:41:25 +01:00
For possible values, see which emulator executable `qemu-system-*` your
system provides. Common examples are `aarch64` , `alpha` , `arm` , `cris` ,
`i386` , `lm32` , `m68k` , `microblaze` , `microblazeel` , `mips` , `mips64` ,
`mips64el` , `mipsel` , `moxie` , `or32` , `ppc` , `ppc64` , `ppcemb` , `s390x` ,
`sh4` , `sh4eb` , `sparc` , `sparc64` , `tricore` , `unicore32` , `x86_64` ,
`xtensa` , `xtensaeb` .
* `machine_virtual_size` - Sets the disk size in GB for the machine overriding
the default specified in the box. Allows boxes to defined with a minimal size
disk by default and to be grown to a larger size at creation time. Will
ignore sizes smaller than the size specified by the box metadata. Note that
currently there is no support for automatically resizing the filesystem to
take advantage of the larger disk.
* `emulator_path` - Explicitly select which device model emulator to use by
providing the path, e.g. `/usr/bin/qemu-system-x86_64` . This is especially
useful on systems that fail to select it automatically based on
`machine_arch` which then results in a capability error.
* `boot` - Change the boot order and enables the boot menu. Possible options
are "hd", "network", "cdrom". Defaults to "hd" with boot menu disabled. When
"network" is set without "hd", only all NICs will be tried; see below for
more detail.
* `nic_adapter_count` - Defaults to '8'. Only use case for increasing this
count is for VMs that virtualize switches such as Cumulus Linux. Max value
for Cumulus Linux VMs is 33.
2019-02-11 20:18:02 -05:00
* `uuid` - Force a domain UUID. Defaults to autogenerated value by Libvirt if
2016-09-07 14:41:25 +01:00
not set.
* `suspend_mode` - What is done on vagrant suspend. Possible values: 'pause',
'managedsave'. Pause mode executes a la `virsh suspend` , which just pauses
execution of a VM, not freeing resources. Managed save mode does a la `virsh
managedsave` which frees resources suspending a domain.
2016-01-06 09:48:20 -05:00
* `tpm_model` - The model of the TPM to which you wish to connect.
* `tpm_type` - The type of TPM device to which you are connecting.
* `tpm_path` - The path to the TPM device on the host system.
2020-12-15 06:43:46 -06:00
* `tpm_version` - The TPM version to use.
2016-09-07 14:41:25 +01:00
* `dtb` - The device tree blob file, mostly used for non-x86 platforms. In case
the device tree isn't added in-line to the kernel, it can be manually
specified here.
* `autostart` - Automatically start the domain when the host boots. Defaults to
'false'.
2019-02-11 20:18:02 -05:00
* `channel` - [Libvirt
2016-09-07 14:41:25 +01:00
channels](https://libvirt.org/formatdomain.html#elementCharChannel ).
Configure a private communication channel between the host and guest, e.g.
2019-02-11 20:18:02 -05:00
for use by the [QEMU guest
2016-09-07 14:41:25 +01:00
agent](http://wiki.libvirt.org/page/Qemu_guest_agent) and the Spice/QXL
graphics type.
2016-12-13 15:36:05 +01:00
* `mgmt_attach` - Decide if VM has interface in mgmt network. If set to 'false'
it is not possible to communicate with VM through `vagrant ssh` or run
provisioning. Setting to 'false' is only possible when VM doesn't use box.
Defaults set to 'true'.
2021-10-19 16:37:46 +02:00
* `serial` - [libvirt serial devices ](https://libvirt.org/formatdomain.html#elementsConsole ).
Configure a serial/console port to communicate with the guest. Can be used
to log to file boot time messages sent to ttyS0 console by the guest.
2013-04-11 17:03:24 +02:00
Specific domain settings can be set for each domain separately in multi-VM
environment. Example below shows a part of Vagrantfile, where specific options
are set for dbserver domain.
```ruby
Vagrant.configure("2") do |config|
config.vm.define :dbserver do |dbserver|
dbserver.vm.box = "centos64"
dbserver.vm.provider :libvirt do |domain|
domain.memory = 2048
domain.cpus = 2
2013-07-24 15:44:30 +02:00
domain.nested = true
2020-12-17 22:03:47 +11:00
domain.disk_driver :cache => 'none'
2013-04-11 17:03:24 +02:00
end
end
2013-05-10 15:11:13 +02:00
# ...
2013-04-11 17:03:24 +02:00
```
2016-09-07 14:41:25 +01:00
The following example shows part of a Vagrantfile that enables the VM to boot
from a network interface first and a hard disk second. This could be used to
run VMs that are meant to be a PXE booted machines. Be aware that if `hd` is
not specified as a boot option, it will never be tried.
2015-06-02 20:31:21 -05:00
```ruby
Vagrant.configure("2") do |config|
config.vm.define :pxeclient do |pxeclient|
pxeclient.vm.box = "centos64"
pxeclient.vm.provider :libvirt do |domain|
domain.boot 'network'
domain.boot 'hd'
end
end
# ...
```
2015-09-26 20:43:24 +03:00
#### Reload behavior
2016-09-06 16:26:33 +01:00
2016-09-07 14:41:25 +01:00
On `vagrant reload` the following domain specific attributes are updated in
defined domain:
* `disk_bus` - Is updated only on disks. It skips CDROMs
* `nic_model_type` - Updated
* `memory` - Updated
* `cpus` - Updated
* `nested` - Updated
* `cpu_mode` - Updated. Pay attention that custom mode is not supported
* `graphics_type` - Updated
* `graphics_port` - Updated
* `graphics_ip` - Updated
* `graphics_passwd` - Updated
* `graphics_autoport` - Updated
* `keymap` - Updated
* `video_type` - Updated
* `video_vram` - Updated
* `tpm_model` - Updated
* `tpm_type` - Updated
* `tpm_path` - Updated
2020-12-15 06:43:46 -06:00
* `tpm_version` - Updated
2015-09-26 20:43:24 +03:00
2013-03-27 00:55:30 +01:00
## Networks
2013-05-10 15:11:13 +02:00
Networking features in the form of `config.vm.network` support private networks
2016-09-07 14:41:25 +01:00
concept. It supports both the virtual network switch routing types and the
point to point Guest OS to Guest OS setting using UDP/Mcast/TCP tunnel
interfaces.
2015-07-11 10:49:00 -04:00
http://wiki.libvirt.org/page/VirtualNetworking
2015-08-03 09:32:56 -04:00
2015-07-11 10:49:00 -04:00
https://libvirt.org/formatdomain.html#elementsNICSTCP
2013-09-10 00:12:24 -07:00
2015-08-03 09:32:56 -04:00
http://libvirt.org/formatdomain.html#elementsNICSMulticast
2019-02-11 20:18:02 -05:00
http://libvirt.org/formatdomain.html#elementsNICSUDP _(in Libvirt v1.2.20 and higher)_
2015-08-03 09:32:56 -04:00
2016-09-07 14:41:25 +01:00
Public Network interfaces are currently implemented using the macvtap driver.
The macvtap driver is only available with the Linux Kernel version >= 2.6.24.
2019-02-11 20:18:02 -05:00
See the following Libvirt documentation for the details of the macvtap usage.
2013-09-10 00:12:24 -07:00
http://www.libvirt.org/formatdomain.html#elementsNICSDirect
2013-05-10 15:11:13 +02:00
An examples of network interface definitions:
```ruby
2015-07-11 10:49:00 -04:00
# Private network using virtual network switching
2013-05-10 15:11:13 +02:00
config.vm.define :test_vm1 do |test_vm1|
2013-09-17 00:23:24 +03:00
test_vm1.vm.network :private_network, :ip => "10.20.30.40"
2013-05-10 15:11:13 +02:00
end
2013-09-10 00:12:24 -07:00
2017-11-23 09:27:41 +01:00
# Private network using DHCP and a custom network
config.vm.define :test_vm1 do |test_vm1|
test_vm1.vm.network :private_network,
:type => "dhcp",
:libvirt__network_address => '10.20.30.0'
end
2017-11-21 15:58:35 +01:00
# Private network (as above) using a domain name
config.vm.define :test_vm1 do |test_vm1|
test_vm1.vm.network :private_network,
:ip => "10.20.30.40",
:libvirt__domain_name => "test.local"
end
2015-07-11 10:49:00 -04:00
# Private network. Point to Point between 2 Guest OS using a TCP tunnel
# Guest 1
config.vm.define :test_vm1 do |test_vm1|
test_vm1.vm.network :private_network,
2015-10-31 17:11:31 -06:00
:libvirt__tunnel_type => 'server',
# default is 127.0.0.1 if omitted
# :libvirt__tunnel_ip => '127.0.0.1',
:libvirt__tunnel_port => '11111'
2016-12-07 00:40:44 +01:00
# network with ipv6 support
test_vm1.vm.network :private_network,
:ip => "10.20.5.42",
:libvirt__guest_ipv6 => "yes",
:libvirt__ipv6_address => "2001:db8:ca2:6::1",
:libvirt__ipv6_prefix => "64"
2015-07-11 10:49:00 -04:00
# Guest 2
2015-07-11 10:53:23 -04:00
config.vm.define :test_vm2 do |test_vm2|
2015-07-11 10:55:21 -04:00
test_vm2.vm.network :private_network,
2015-10-31 17:11:31 -06:00
:libvirt__tunnel_type => 'client',
# default is 127.0.0.1 if omitted
# :libvirt__tunnel_ip => '127.0.0.1',
:libvirt__tunnel_port => '11111'
2016-12-07 00:40:44 +01:00
# network with ipv6 support
test_vm2.vm.network :private_network,
:ip => "10.20.5.45",
:libvirt__guest_ipv6 => "yes",
:libvirt__ipv6_address => "2001:db8:ca2:6::1",
:libvirt__ipv6_prefix => "64"
2015-07-11 10:49:00 -04:00
2013-09-10 00:12:24 -07:00
# Public Network
config.vm.define :test_vm1 do |test_vm1|
2015-04-09 07:50:28 +02:00
test_vm1.vm.network :public_network,
2015-10-31 17:11:31 -06:00
:dev => "virbr0",
:mode => "bridge",
:type => "bridge"
2013-09-10 00:12:24 -07:00
end
2013-05-10 15:11:13 +02:00
```
2016-09-07 14:41:25 +01:00
In example below, one network interface is configured for VM `test_vm1` . After
you run `vagrant up` , VM will be accessible on IP address `10.20.30.40` . So if
2013-05-10 15:11:13 +02:00
you install a web server via provisioner, you will be able to access your
2016-09-07 14:41:25 +01:00
testing server on `http://10.20.30.40` URL. But beware that this address is
2019-02-11 20:18:02 -05:00
private to Libvirt host only. It's not visible outside of the hypervisor box.
2013-05-10 15:11:13 +02:00
2016-09-07 14:41:25 +01:00
If network `10.20.30.0/24` doesn't exist, provider will create it. By default
2013-05-10 15:11:13 +02:00
created networks are NATed to outside world, so your VM will be able to connect
to the internet (if hypervisor can). And by default, DHCP is offering addresses
on newly created networks.
2016-09-07 14:41:25 +01:00
The second interface is created and bridged into the physical device `eth0` .
This mechanism uses the macvtap Kernel driver and therefore does not require an
existing bridge device. This configuration assumes that DHCP and DNS services
are being provided by the public network. This public interface should be
reachable by anyone with access to the public network.
2013-09-10 00:12:24 -07:00
2013-05-10 15:11:13 +02:00
### Private Network Options
2013-09-10 00:12:24 -07:00
*Note: These options are not applicable to public network interfaces.*
2019-02-11 20:18:02 -05:00
There is a way to pass specific options for Libvirt provider when using
2013-05-10 15:11:13 +02:00
`config.vm.network` to configure new network interface. Each parameter name
2016-09-07 14:41:25 +01:00
starts with `libvirt__` string. Here is a list of those options:
2013-05-10 15:11:13 +02:00
2019-02-11 20:18:02 -05:00
* `:libvirt__network_name` - Name of Libvirt network to connect to. By default,
2013-05-10 15:11:13 +02:00
network 'default' is used.
* `:libvirt__netmask` - Used only together with `:ip` option. Default is
'255.255.255.0'.
2017-11-23 09:27:41 +01:00
* `:libvirt__network_address` - Used only when `:type` is set to `dhcp` . Only `/24` subnet is supported. Default is `172.28.128.0` .
2017-05-11 23:04:23 -04:00
* `:libvirt__host_ip` - Address to use for the host (not guest). Default is
2016-09-07 14:41:25 +01:00
first possible address (after network address).
2017-11-21 15:58:35 +01:00
* `:libvirt__domain_name` - DNS domain of the DHCP server. Used only
when creating new network.
2013-05-10 15:11:13 +02:00
* `:libvirt__dhcp_enabled` - If DHCP will offer addresses, or not. Used only
when creating new network. Default is true.
2016-09-07 14:41:25 +01:00
* `:libvirt__dhcp_start` - First address given out via DHCP. Default is third
address in range (after network name and gateway).
* `:libvirt__dhcp_stop` - Last address given out via DHCP. Default is last
possible address in range (before broadcast address).
* `:libvirt__dhcp_bootp_file` - The file to be used for the boot image. Used
only when dhcp is enabled.
* `:libvirt__dhcp_bootp_server` - The server that runs the DHCP server. Used
only when dhcp is enabled.By default is the same host that runs the DHCP
server.
2021-11-30 11:11:52 +01:00
* `:libvirt__tftp_root` - Path to the root directory served via TFTP.
2013-05-10 15:11:13 +02:00
* `:libvirt__adapter` - Number specifiyng sequence number of interface.
2020-11-16 17:45:09 +01:00
* `:libvirt__forward_mode` - Specify one of `veryisolated` , `none` , `open` , `nat`
or `route` options. This option is used only when creating new network. Mode
2016-09-07 14:41:25 +01:00
`none` will create isolated network without NATing or routing outside. You
will want to use NATed forwarding typically to reach networks outside of
hypervisor. Routed forwarding is typically useful to reach other networks
within hypervisor. `veryisolated` described
[here ](https://libvirt.org/formatnetwork.html#examplesNoGateway ). By
default, option `nat` is used.
2013-07-24 15:44:30 +02:00
* `:libvirt__forward_device` - Name of interface/device, where network should
be forwarded (NATed or routed). Used only when creating new network. By
default, all physical interfaces are used.
2016-09-07 14:41:25 +01:00
* `:libvirt__tunnel_type` - Set to 'udp' if using UDP unicast tunnel mode
(libvirt v1.2.20 or higher). Set this to either "server" or "client" for tcp
tunneling. Set this to 'mcast' if using multicast tunneling. This
configuration type uses tunnels to generate point to point connections
between Guests. Useful for Switch VMs like Cumulus Linux. No virtual switch
setting like `libvirt__network_name` applies with tunnel interfaces and will
be ignored if configured.
2019-02-11 20:18:02 -05:00
* `:libvirt__tunnel_ip` - Sets the source IP of the Libvirt tunnel interface.
2016-09-07 14:41:25 +01:00
By default this is `127.0.0.1` for TCP and UDP tunnels and `239.255.1.1` for
Multicast tunnels. It populates the address field in the `< source
address="XXX">` of the interface xml configuration.
* `:libvirt__tunnel_port` - Sets the source port the tcp/udp/mcast tunnel with
use. This port information is placed in the `<source port=XXX/>` section of
2015-08-03 09:32:56 -04:00
interface xml configuration.
* `:libvirt__tunnel_local_port` - Sets the local port used by the udp tunnel
2016-09-07 14:41:25 +01:00
interface type. It populates the port field in the `<local port=XXX">`
section of the interface xml configuration. _(This feature only works in
2019-02-11 20:18:02 -05:00
Libvirt 1.2.20 and higher)_
2015-08-03 09:32:56 -04:00
* `:libvirt__tunnel_local_ip` - Sets the local IP used by the udp tunnel
2016-09-07 14:41:25 +01:00
interface type. It populates the ip entry of the `<local address=XXX">`
section of the interface xml configuration. _(This feature only works in
2019-02-11 20:18:02 -05:00
Libvirt 1.2.20 and higher)_
2015-11-09 17:45:10 +00:00
* `:libvirt__guest_ipv6` - Enable or disable guest-to-guest IPv6 communication.
2016-09-07 14:41:25 +01:00
See [here ](https://libvirt.org/formatnetwork.html#examplesPrivate6 ), and
[here ](http://libvirt.org/git/?p=libvirt.git;a=commitdiff;h=705e67d40b09a905cd6a4b8b418d5cb94eaa95a8 )
2016-11-25 16:59:35 +01:00
for for more information. *Note: takes either 'yes' or 'no' for value*
2016-12-07 00:40:44 +01:00
* `:libvirt__ipv6_address` - Define ipv6 address, require also prefix.
* `:libvirt__ipv6_prefix` - Define ipv6 prefix. generate string `<ip family="ipv6" address="address" prefix="prefix" >`
2016-09-07 14:41:25 +01:00
* `:libvirt__iface_name` - Define a name for the private network interface.
With this feature one can [simulate physical link
failures](https://github.com/vagrant-libvirt/vagrant-libvirt/pull/498)
* `:mac` - MAC address for the interface. *Note: specify this in lowercase
since Vagrant network scripts assume it will be!*
2019-02-11 20:18:02 -05:00
* `:libvirt__mtu` - MTU size for the Libvirt network, if not defined, the
created network will use the Libvirt default (1500). VMs still need to set the
2017-10-20 15:03:09 +02:00
MTU accordingly.
2016-09-07 14:41:25 +01:00
* `:model_type` - parameter specifies the model of the network adapter when you
create a domain value by default virtio KVM believe possible values, see the
2019-02-11 20:18:02 -05:00
documentation for Libvirt
2017-02-08 19:59:59 +01:00
* `:libvirt__driver_name` - Define which network driver to use. [More
info](https://libvirt.org/formatdomain.html#elementsDriverBackendOptions )
* `:libvirt__driver_queues` - Define a number of queues to be used for network
interface. Set equal to numer of vCPUs for best performance. [More
info](http://www.linux-kvm.org/page/Multiqueue)
2019-02-11 20:18:02 -05:00
* `:autostart` - Automatic startup of network by the Libvirt daemon.
2016-12-16 12:21:30 +01:00
If not specified the default is 'false'.
2018-01-24 18:08:59 +01:00
* `:bus` - The bus of the PCI device. Both :bus and :slot have to be defined.
* `:slot` - The slot of the PCI device. Both :bus and :slot have to be defined.
2021-10-19 14:52:54 +01:00
* `:libvirt__always_destroy` - Allow domains that use but did not create a
network to destroy it when the domain is destroyed (default: `true` ). Set to
`false` to only allow the domain that created the network to destroy it.
2013-03-27 00:55:30 +01:00
2015-04-01 14:49:35 +02:00
When the option `:libvirt__dhcp_enabled` is to to 'false' it shouldn't matter
whether the virtual network contains a DHCP server or not and vagrant-libvirt
2016-09-07 14:41:25 +01:00
should not fail on it. The only situation where vagrant-libvirt should fail is
when DHCP is requested but isn't configured on a matching already existing
2015-04-01 14:49:35 +02:00
virtual network.
2013-09-10 22:45:40 -07:00
### Public Network Options
2016-09-07 14:41:25 +01:00
* `:dev` - Physical device that the public interface should use. Default is
'eth0'.
2013-09-10 22:45:40 -07:00
* `:mode` - The mode in which the public interface should operate in. Supported
2016-09-07 14:41:25 +01:00
modes are available from the [libvirt
documentation](http://www.libvirt.org/formatdomain.html#elementsNICSDirect ).
2013-09-10 22:45:40 -07:00
Default mode is 'bridge'.
2015-04-09 07:50:28 +02:00
* `:type` - is type of interface.(`<interface type="#{@type}">` )
2013-09-17 00:23:24 +03:00
* `:mac` - MAC address for the interface.
2019-02-11 20:18:02 -05:00
* `:network_name` - Name of Libvirt network to connect to.
* `:portgroup` - Name of Libvirt portgroup to connect to.
2016-09-07 14:41:25 +01:00
* `:ovs` - Support to connect to an Open vSwitch bridge device. Default is
'false'.
2020-11-16 16:11:59 +01:00
* :ovs_interfaceid - Add Open vSwitch 'interfaceid' parameter.
2016-09-13 09:14:20 +02:00
* `:trust_guest_rx_filters` - Support trustGuestRxFilters attribute. Details
are listed [here ](http://www.libvirt.org/formatdomain.html#elementsNICSDirect ).
Default is 'false'.
2013-09-10 22:45:40 -07:00
2014-01-20 15:55:25 -06:00
### Management Network
2016-09-07 14:41:25 +01:00
vagrant-libvirt uses a private network to perform some management operations on
VMs. All VMs will have an interface connected to this network and an IP address
2019-02-11 20:18:02 -05:00
dynamically assigned by Libvirt unless you set `:mgmt_attach` to 'false'.
2016-12-13 15:36:05 +01:00
This is in addition to any networks you configure. The name and address
used by this network are configurable at the provider level.
2016-09-07 14:41:25 +01:00
2019-02-11 20:18:02 -05:00
* `management_network_name` - Name of Libvirt network to which all VMs will be
2016-09-07 14:41:25 +01:00
connected. If not specified the default is 'vagrant-libvirt'.
* `management_network_address` - Address of network to which all VMs will be
connected. Must include the address and subnet mask. If not specified the
default is '192.168.121.0/24'.
2019-02-11 20:18:02 -05:00
* `management_network_mode` - Network mode for the Libvirt management network.
2020-11-16 17:45:09 +01:00
Specify one of veryisolated, none, open, nat or route options. Further
documented under [Private Networks ](#private-network-options )
2016-09-07 14:41:25 +01:00
* `management_network_guest_ipv6` - Enable or disable guest-to-guest IPv6
communication. See
[here ](https://libvirt.org/formatnetwork.html#examplesPrivate6 ), and
[here ](http://libvirt.org/git/?p=libvirt.git;a=commitdiff;h=705e67d40b09a905cd6a4b8b418d5cb94eaa95a8 )
for for more information.
2016-12-16 12:21:30 +01:00
* `management_network_autostart` - Automatic startup of mgmt network, if not
specified the default is 'false'.
2018-11-10 12:16:17 +01:00
* `management_network_pci_bus` - The bus of the PCI device.
* `management_network_pci_slot` - The slot of the PCI device.
2018-01-30 15:24:13 +01:00
* `management_network_mac` - MAC address of management network interface.
2018-11-10 12:16:17 +01:00
* `management_network_domain` - Domain name assigned to the management network.
2021-04-06 16:25:14 +02:00
* `management_network_mtu` - MTU size of management network. If not specified,
the Libvirt default (1500) will be used.
2021-12-21 13:45:33 +01:00
* `management_network_keep` - Starting from version *0.7.0* , *always_destroy* is set to *true* by default for any network.
This option allows to change this behaviour for the management network.
2016-09-07 14:41:25 +01:00
You may wonder how vagrant-libvirt knows the IP address a VM received. Libvirt
doesn't provide a standard way to find out the IP address of a running domain.
But we do know the MAC address of the virtual machine's interface on the
management network. Libvirt is closely connected with dnsmasq, which acts as a
DHCP server. dnsmasq writes lease information in the `/var/lib/libvirt/dnsmasq`
2014-01-20 15:55:25 -06:00
directory. Vagrant-libvirt looks for the MAC address in this file and extracts
the corresponding IP address.
2021-09-22 11:41:19 +02:00
It is also possible to use the Qemu Agent to extract the management interface
configuration from the booted virtual machine. This is helpful in libvirt
environments where no local dnsmasq is used for automatic address assigment,
but external dhcp services via bridged libvirt networks.
Prerequisite is to enable the qemu agent channel via ([Libvirt communication
channels](#libvirt -communication-channels)) and the virtual machine image must
have the agent pre-installed before deploy. The agent will start automatically
if it detects an attached channel during boot.
* `qemu_use_agent` - false by default, if set to true, attempt to extract configured
ip address via qemu agent.
2021-11-26 18:12:21 +00:00
By default if `qemu_use_agent` is set to `true` the code will automatically
inject a suitable channel unless there already exists an entry with a
`:target_name` matching `'org.qemu.guest_agent.'` .
Alternatively if setting `qemu_use_agent` but, needing to disable the addition
of the channel, simply use a disabled flag as follows:
```ruby
Vagrant.configure(2) do |config|
config.vm.provider :libvirt do |libvirt|
libvirt.channel :type => 'unix', :target_name => 'org.qemu.guest_agent.0', :disabled => true
end
end
```
2021-09-22 11:41:19 +02:00
To use the management network interface with an external dhcp service you need
to setup a bridged host network manually and define it via
`management_network_name` in your Vagrantfile.
2013-03-27 00:55:30 +01:00
2014-05-11 14:40:51 -05:00
## Additional Disks
2016-09-07 14:41:25 +01:00
You can create and attach additional disks to a VM via `libvirt.storage :file` .
It has a number of options:
2014-05-11 14:40:51 -05:00
2016-09-07 14:41:25 +01:00
* `path` - Location of the disk image. If unspecified, a path is automtically
chosen in the same storage pool as the VMs primary disk.
* `device` - Name of the device node the disk image will have in the VM, e.g.
*vdb* . If unspecified, the next available device is chosen.
2014-05-11 14:40:51 -05:00
* `size` - Size of the disk image. If unspecified, defaults to 10G.
* `type` - Type of disk image to create. Defaults to *qcow2* .
2014-10-28 21:31:06 +02:00
* `bus` - Type of bus to connect device to. Defaults to *virtio* .
2016-09-07 14:41:25 +01:00
* `allow_existing` - Set to true if you want to allow the VM to use a
2016-09-07 17:57:08 +02:00
pre-existing disk. If the disk doesn't exist it will be created.
Disks with this option set to true need to be removed manually.
* `shareable` - Set to true if you want to simulate shared SAN storage.
2017-03-16 10:44:07 -05:00
* `serial` - Serial number of the disk device.
2020-06-14 20:00:15 -05:00
* `wwn` - WWN number of the disk device.
2014-05-11 14:40:51 -05:00
2020-12-17 22:03:47 +11:00
The following disk performance options can also be configured
(see the [libvirt documentation for possible values ](http://libvirt.org/formatdomain.html#elementsDisks )
or [here ](https://www.suse.com/documentation/sles11/book_kvm/data/sect1_chapter_book_kvm.html ) for a fuller explanation).
In all cases, the options use the hypervisor default if not specified, or if set to `nil` .
* `cache` - Cache mode to use. Value may be `default` , `none` , `writeback` , `writethrough` , `directsync` or `unsafe` .
* `io` - Controls specific policies on I/O. Value may be `threads` or `native` .
* `copy_on_read` - Controls whether to copy read backing file into the image file. Value may be `on` or `off` .
* `discard` - Controls whether discard requests (also known as "trim" or "unmap") are ignored or passed to the filesystem. Value may be `unmap` or `ignore` .
Note: for discard to work, you will likely also need to set `:bus => 'scsi'`
* `detect_zeroes` - Controls whether to detect zero write requests. Value may be `off` , `on` or `unmap` .
2014-05-11 14:40:51 -05:00
The following example creates two additional disks.
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
libvirt.storage :file, :size => '20G'
2020-12-17 22:03:47 +11:00
libvirt.storage :file, :size => '40G', :bus => 'scsi', :type => 'raw', :discard => 'unmap', :detect_zeroes => 'on'
2014-05-11 14:40:51 -05:00
end
end
```
2016-09-07 17:57:08 +02:00
For shared SAN storage to work the following example can be used:
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
libvirt.storage :file, :size => '20G', :path => 'my_shared_disk.img', :allow_existing => true, :shareable => true, :type => 'raw'
end
end
```
2015-10-06 11:59:36 +03:00
### Reload behavior
2016-09-07 14:41:25 +01:00
On `vagrant reload` the following additional disk attributes are updated in
defined domain:
2015-10-06 11:59:36 +03:00
2016-09-07 14:41:25 +01:00
* `bus` - Updated. Uses `device` as a search marker. It is not required to
define `device` , but it's recommended. If `device` is defined then the order
of addtitional disk definition becomes irrelevant.
2015-10-06 11:59:36 +03:00
2015-04-10 15:42:35 -05:00
## CDROMs
2016-09-07 14:41:25 +01:00
You can attach up to four CDROMs to a VM via `libvirt.storage :file,
:device => :cdrom`. Available options are:
2015-04-10 15:42:35 -05:00
* `path` - The path to the iso to be used for the CDROM drive.
2016-09-07 14:41:25 +01:00
* `dev` - The device to use (`hda` , `hdb` , `hdc` , or `hdd` ). This will be
automatically determined if unspecified.
2015-04-10 15:42:35 -05:00
* `bus` - The bus to use for the CDROM drive. Defaults to `ide`
The following example creates three CDROM drives in the VM:
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
libvirt.storage :file, :device => :cdrom, :path => '/path/to/iso1.iso'
libvirt.storage :file, :device => :cdrom, :path => '/path/to/iso2.iso'
libvirt.storage :file, :device => :cdrom, :path => '/path/to/iso3.iso'
end
end
```
2015-08-05 10:37:19 -05:00
## Input
2016-09-07 14:41:25 +01:00
You can specify multiple inputs to the VM via `libvirt.input` . Available
options are listed below. Note that both options are required:
2015-08-05 10:37:19 -05:00
* `type` - The type of the input
2016-10-26 18:40:38 -04:00
* `bus` - The bus of the input
2015-08-05 10:37:19 -05:00
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
# this is the default
# libvirt.input :type => "mouse", :bus => "ps2"
# very useful when having mouse issues when viewing VM via VNC
2015-08-05 10:38:35 -05:00
libvirt.input :type => "tablet", :bus => "usb"
2015-08-05 10:37:19 -05:00
end
end
```
2015-10-23 11:12:57 +03:00
## PCI device passthrough
2016-09-07 14:41:25 +01:00
You can specify multiple PCI devices to passthrough to the VM via
`libvirt.pci` . Available options are listed below. Note that all options are
2020-05-10 05:40:29 -05:00
required, except domain, which defaults to `0x0000` :
2015-10-23 11:12:57 +03:00
2020-05-10 05:40:29 -05:00
* `domain` - The domain of the PCI device
2015-10-23 11:12:57 +03:00
* `bus` - The bus of the PCI device
* `slot` - The slot of the PCI device
* `function` - The function of the PCI device
2016-09-07 14:41:25 +01:00
You can extract that information from output of `lspci` command. First
2020-05-10 05:40:29 -05:00
characters of each line are in format `[<domain>]:[<bus>]:[<slot>].[<func>]` . For example:
2015-10-23 11:12:57 +03:00
2016-09-07 14:41:25 +01:00
```shell
2015-10-23 11:12:57 +03:00
$ lspci| grep NVIDIA
2020-05-10 05:40:29 -05:00
0000:03:00.0 VGA compatible controller: NVIDIA Corporation GK110B [GeForce GTX TITAN Black] (rev a1)
2015-10-23 11:12:57 +03:00
```
2020-05-10 05:40:29 -05:00
In that case `domain` is `0x0000` , `bus` is `0x03` , `slot` is `0x00` and `function` is `0x0` .
2015-10-23 11:12:57 +03:00
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
2020-05-10 05:40:29 -05:00
libvirt.pci :domain => '0x0000', :bus => '0x06', :slot => '0x12', :function => '0x5'
2015-10-23 11:12:57 +03:00
# Add another one if it is neccessary
2020-05-10 05:40:29 -05:00
libvirt.pci :domain => '0x0000', :bus => '0x03', :slot => '0x00', :function => '0x0'
2015-10-23 11:12:57 +03:00
end
end
```
2016-10-18 20:24:19 +03:00
Note! Above options affect configuration only at domain creation. It won't change VM behaviour on `vagrant reload` after domain was created.
Don't forget to [set ](#domain-specific-options ) `kvm_hidden` option to `true` especially if you are passthroughing NVIDIA GPUs. Otherwise GPU is visible from VM but cannot be operated.
2018-03-18 19:25:01 +11:00
2018-11-04 19:07:40 +11:00
## Using USB Devices
There are several ways to pass a USB device through to a running instance:
* Use `libvirt.usb` to [attach a USB device at boot ](#usb-device-passthrough ), with the device ID specified in the Vagrantfile
* Use a client (such as `virt-viewer` or `virt-manager` ) to attach the device at runtime [via USB redirectors ](#usb-redirector-devices )
* Use `virsh attach-device` once the VM is running (however, this is outside the scope of this readme)
In all cases, if you wish to use a high-speed USB device,
you will need to use `libvirt.usb_controller` to specify a USB2 or USB3 controller,
as the default configuration only exposes a USB1.1 controller.
### USB Controller Configuration
2018-03-18 19:25:01 +11:00
The USB controller can be configured using `libvirt.usb_controller` , with the following options:
* `model` - The USB controller device model to emulate. (mandatory)
* `ports` - The number of devices that can be connected to the controller.
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
# Set up a USB3 controller
2021-09-27 16:49:15 +01:00
libvirt.usb_controller :model => "qemu-xhci"
2018-03-18 19:25:01 +11:00
end
end
```
2018-11-04 19:07:40 +11:00
See the [libvirt documentation ](https://libvirt.org/formatdomain.html#elementsControllers ) for a list of valid models.
2021-09-27 16:49:15 +01:00
If any USB devices are passed through by setting `libvirt.usb` or `libvirt.redirdev` , a default controller will be added using the model `qemu-xhci` in the absence of a user specified one. This should help ensure more devices work out of the box as the default configured by libvirt is pii3-uhci, which appears to only work for USB 1 devices and does not work as expected when connected via a USB 2 controller, while the xhci stack should work for all versions of USB.
2018-11-04 19:07:40 +11:00
### USB Device Passthrough
You can specify multiple USB devices to passthrough to the VM via
`libvirt.usb` . The device can be specified by the following options:
* `bus` - The USB bus ID, e.g. "1"
* `device` - The USB device ID, e.g. "2"
* `vendor` - The USB devices vendor ID (VID), e.g. "0x1234"
* `product` - The USB devices product ID (PID), e.g. "0xabcd"
At least one of these has to be specified, and `bus` and `device` may only be
used together.
The example values above match the device from the following output of `lsusb` :
```
Bus 001 Device 002: ID 1234:abcd Example device
```
2021-09-27 16:49:15 +01:00
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
# pass through specific device based on identifying it
2022-02-02 11:19:45 +01:00
libvirt.usb :vendor => '0x1234', :product => '0xabcd'
2021-09-27 16:49:15 +01:00
# pass through a host device where multiple of the same vendor/product exist
2022-02-02 11:19:45 +01:00
libvirt.usb :bus => '1', :device => '1'
2021-09-27 16:49:15 +01:00
end
end
```
2018-11-04 19:07:40 +11:00
Additionally, the following options can be used:
2019-02-11 20:18:02 -05:00
* `startupPolicy` - Is passed through to Libvirt and controls if the device has
to exist. Libvirt currently allows the following values: "mandatory",
2018-11-04 19:07:40 +11:00
"requisite", "optional".
2018-03-18 19:25:01 +11:00
2018-11-04 19:07:40 +11:00
### USB Redirector Devices
2016-10-18 16:37:23 +02:00
You can specify multiple redirect devices via `libvirt.redirdev` . There are two types, `tcp` and `spicevmc` supported, for forwarding USB-devices to the guest. Available options are listed below.
* `type` - The type of the USB redirector device. (`tcp` or `spicevmc` )
* `host` - The host where the device is attached to. (mandatory for type `tcp` )
* `port` - The port where the device is listening. (mandatory for type `tcp` )
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
# add two devices using spicevmc channel
(1..2).each do
libvirt.redirdev :type => "spicevmc"
end
# add device, provided by localhost:4000
libvirt.redirdev :type => "tcp", :host => "localhost", :port => "4000"
end
end
```
2018-11-04 19:07:40 +11:00
Note that in order to enable USB redirection with Spice clients,
you may need to also set `libvirt.graphics_type = "spice"`
#### Filter for USB Redirector Devices
2016-10-18 16:37:23 +02:00
You can define filter for redirected devices. These filters can be positiv or negative, by setting the mandatory option `allow=yes` or `allow=no` . All available options are listed below. Note the option `allow` is mandatory.
* `class` - The device class of the USB device. A list of device classes is available on [Wikipedia ](https://en.wikipedia.org/wiki/USB#Device_classes ).
* `vendor` - The vendor of the USB device.
* `product` - The product id of the USB device.
* `version` - The version of the USB device. Note that this is the version of `bcdDevice`
* `allow` - allow or disallow redirecting this device. (mandatory)
You can extract that information from output of `lsusb` command. Every line contains the information in format `Bus [<bus>] Device [<device>]: ID [<vendor>:[<product>]` . The `version` can be extracted from the detailed output of the device using `lsusb -D /dev/usb/[<bus>]/[<device>]` . For example:
```shell
2016-12-06 19:45:27 +01:00
# get bcdDevice from
2016-10-18 16:37:23 +02:00
$: lsusb
Bus 001 Device 009: ID 08e6:3437 Gemalto (was Gemplus) GemPC Twin SmartCard Reader
$: lsusb -D /dev/bus/usb/001/009 | grep bcdDevice
bcdDevice 2.00
```
In this case, the USB device with `class 0x0b` , `vendor 0x08e6` , `product 0x3437` and `bcdDevice version 2.00` is allowed to be redirected to the guest. All other devices will be refused.
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
libvirt.redirdev :type => "spicevmc"
2021-09-27 16:49:15 +01:00
libvirt.redirfilter :class => "0x0b", :vendor => "0x08e6", :product => "0x3437", :version => "2.00", :allow => "yes"
2016-10-18 16:37:23 +02:00
libvirt.redirfilter :allow => "no"
end
end
```
2021-10-19 16:37:46 +02:00
## Serial Console Devices
You can define settings to redirect output from the serial console of any VM brought up with libvirt to a file or other devices that are listening. [See libvirt documentation ](https://libvirt.org/formatdomain.html#elementCharSerial ).
Currently only redirecting to a file is supported.
* `type` - only value that has an effect is file, in the future support may be added for virtual console, pty, dev, pipe, tcp, udp, unix socket, spiceport & nmdm.
* `source` - options pertaining to how the connection attaches to the host, contains sub-settings dependent on `type` .
`source` options for type `file`
* `path` - file on host to connect to the serial port to record all output. May be created by qemu system user causing some permissions issues.
```ruby
Vagrant.configure("2") do |config|
config.vm.define :test do |test|
test.vm.provider :libvirt do |domain|
domain.serial :type => "file", :source => {:path => "/var/log/vm_consoles/test.log}
end
end
end
```
2016-09-15 16:52:26 +02:00
## Random number generator passthrough
You can pass through `/dev/random` to your VM by configuring the domain like this:
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
# Pass through /dev/random from the host to the VM
libvirt.random :model => 'random'
end
end
```
At the moment only the `random` backend is supported.
2017-01-10 00:10:56 +01:00
## Watchdog device
A virtual hardware watchdog device can be added to the guest via the `libvirt.watchdog` element. The option `model` is mandatory and could have on of the following values.
* `i6300esb` - the recommended device, emulating a PCI Intel 6300ESB
* 'ib700` - emulating an ISA iBase IB700
* `diag288` - emulating an S390 DIAG288 device
The optional action attribute describes what `action` to take when the watchdog expires. Valid values are specific to the underlying hypervisor. The default behavior is `reset` .
* `reset` - default, forcefully reset the guest
* `shutdown` - gracefully shutdown the guest (not recommended)
* `poweroff` - forcefully power off the guest
* `pause` - pause the guest
* `none` - do nothing
* `dump` - automatically dump the guest
* `inject-nmi` - inject a non-maskable interrupt into the guest
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
2019-02-11 20:18:02 -05:00
# Add Libvirt watchdog device model i6300esb
2017-01-10 00:10:56 +01:00
libvirt.watchdog :model => 'i6300esb', :action => 'reset'
end
end
```
2017-01-12 09:31:12 +01:00
## Smartcard device
A virtual smartcard device can be supplied to the guest via the `libvirt.smartcard` element. The option `mode` is mandatory and currently only value `passthrough` is supported. The value `spicevmc` for option `type` is default value and can be supressed. On using `type = tcp` , the options `source_mode` , `source_host` and `source_service` are mandatory.
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
# Add smartcard device with type 'spicevmc'
libvirt.smartcard :mode => 'passthrough', :type => 'spicevmc'
end
end
```
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
# Add smartcard device with type 'tcp'
domain.smartcard :mode => 'passthrough', :type => 'tcp', :source_mode => 'bind', :source_host => '127.0.0.1', :source_service => '2001'
end
end
```
2018-03-21 19:00:28 +01:00
## Hypervisor Features
2017-03-23 14:57:28 +01:00
Hypervisor features can be specified via `libvirt.features` as a list. The default
options that are enabled are `acpi` , `apic` and `pae` . If you define `libvirt.features`
you overwrite the defaults, so keep that in mind.
An example:
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
# Specify the default hypervisor features
libvirt.features = ['acpi', 'apic', 'pae' ]
end
end
```
A different example for ARM boards:
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
# Specify the default hypervisor features
libvirt.features = ["apic", "gic version='2'" ]
end
end
```
2017-01-12 09:31:12 +01:00
2018-03-21 19:00:28 +01:00
You can also specify a special set of features that help improve the behavior of guests
running Microsoft Windows.
You can specify HyperV features via `libvirt.hyperv_feature` . Available
options are listed below. Note that both options are required:
2019-02-11 20:18:02 -05:00
* `name` - The name of the feature Hypervisor feature (see Libvirt doc)
2018-03-21 19:00:28 +01:00
* `state` - The state for this feature which can be either `on` or `off` .
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
# Relax constraints on timers
libvirt.hyperv_feature :name => 'relaxed', :state => 'on'
# Enable virtual APIC
libvirt.hyperv_feature :name => 'vapic', :state => 'on'
2019-09-04 20:57:15 +02:00
# Enable spinlocks (requires retries to be specified)
libvirt.hyperv_feature :name => 'spinlocks', :state => 'on', :retries => '8191'
2018-03-21 19:00:28 +01:00
end
end
```
2020-12-16 20:19:24 +01:00
## Clock
Clock offset can be specified via `libvirt.clock_offset` . (Default is utc)
Additionally timers can be specified via `libvirt.clock_timer` .
Available options for timers are: name, track, tickpolicy, frequency, mode, present
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
# Set clock offset to localtime
libvirt.clock_offset = 'localtime'
# Timers ...
libvirt.clock_timer :name => 'rtc', :tickpolicy => 'catchup'
libvirt.clock_timer :name => 'pit', :tickpolicy => 'delay'
libvirt.clock_timer :name => 'hpet', :present => 'no'
libvirt.clock_timer :name => 'hypervclock', :present => 'yes'
end
end
```
2016-02-11 14:56:08 +01:00
## CPU features
2016-09-07 14:41:25 +01:00
You can specify CPU feature policies via `libvirt.cpu_feature` . Available
options are listed below. Note that both options are required:
2016-02-11 14:56:08 +01:00
2019-02-11 20:18:02 -05:00
* `name` - The name of the feature for the chosen CPU (see Libvirt's
2016-09-07 14:41:25 +01:00
`cpu_map.xml` )
* `policy` - The policy for this feature (one of `force` , `require` ,
2019-02-11 20:18:02 -05:00
`optional` , `disable` and `forbid` - see Libvirt documentation)
2016-02-11 14:56:08 +01:00
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
# The feature will not be supported by virtual CPU.
libvirt.cpu_feature :name => 'hypervisor', :policy => 'disable'
# Guest creation will fail unless the feature is supported by host CPU.
libvirt.cpu_feature :name => 'vmx', :policy => 'require'
# The virtual CPU will claim the feature is supported regardless of it being supported by host CPU.
libvirt.cpu_feature :name => 'pdpe1gb', :policy => 'force'
end
end
```
2017-11-12 16:25:13 -06:00
## Memory Backing
You can specify memoryBacking options via `libvirt.memorybacking` . Available options are shown below. Full documentation is available at the [libvirt _memoryBacking_ section ](https://libvirt.org/formatdomain.html#elementsMemoryBacking ).
NOTE: The hugepages `<page>` element is not yet supported
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
libvirt.memorybacking :hugepages
libvirt.memorybacking :nosharepages
libvirt.memorybacking :locked
libvirt.memorybacking :source, :type => 'file'
libvirt.memorybacking :access, :mode => 'shared'
libvirt.memorybacking :allocation, :mode => 'immediate'
end
end
```
2015-11-25 00:01:59 +01:00
2015-08-10 17:10:23 +02:00
## No box and PXE boot
2016-09-07 14:41:25 +01:00
There is support for PXE booting VMs with no disks as well as PXE booting VMs
with blank disks. There are some limitations:
2015-08-10 17:10:23 +02:00
2015-08-14 14:33:01 +02:00
* Requires Vagrant 1.6.0 or newer
2015-08-10 17:10:23 +02:00
* No provisioning scripts are ran
* No network configuration is being applied to the VM
* No SSH connection can be made
2016-09-07 14:41:25 +01:00
* `vagrant halt` will only work cleanly if the VM handles ACPI shutdown signals
2015-08-10 17:10:23 +02:00
2016-09-07 14:41:25 +01:00
In short, VMs without a box can be created, halted and destroyed but all other
functionality cannot be used.
2015-08-10 17:10:23 +02:00
An example for a PXE booted VM with no disks whatsoever:
2015-09-25 15:36:24 +03:00
```ruby
2015-08-10 17:10:23 +02:00
Vagrant.configure("2") do |config|
config.vm.define :pxeclient do |pxeclient|
pxeclient.vm.provider :libvirt do |domain|
domain.boot 'network'
end
end
2015-09-25 15:36:24 +03:00
end
```
2015-08-10 17:10:23 +02:00
2015-09-25 15:36:24 +03:00
And an example for a PXE booted VM with no box but a blank disk which will boot from this HD if the NICs fail to PXE boot:
2015-08-11 00:39:53 +02:00
2015-09-25 15:36:24 +03:00
```ruby
2015-08-11 00:39:53 +02:00
Vagrant.configure("2") do |config|
config.vm.define :pxeclient do |pxeclient|
pxeclient.vm.provider :libvirt do |domain|
domain.storage :file, :size => '100G', :type => 'qcow2'
domain.boot 'network'
domain.boot 'hd'
end
end
2015-09-25 15:36:24 +03:00
end
```
2015-08-11 00:39:53 +02:00
2016-12-06 19:45:27 +01:00
Example for vm with 2 networks and only 1 is bootable and has dhcp server in this subnet, for example foreman with dhcp server
Name of network "foreman_managed" is key for define boot order
```ruby
config.vm.define :pxeclient do |pxeclient|
pxeclient.vm.network :private_network,ip: '10.0.0.5',
2018-03-17 14:28:23 +01:00
libvirt__network_name: "foreman_managed",
2016-12-06 19:45:27 +01:00
libvirt__dhcp_enabled: false,
libvirt__host_ip: '10.0.0.1'
pxeclient.vm.provider :libvirt do |domain|
domain.memory = 1000
boot_network = {'network' => 'foreman_managed'}
domain.storage :file, :size => '100G', :type => 'qcow2'
domain.boot boot_network
domain.boot 'hd'
end
end
```
2021-05-27 07:40:25 +01:00
An example VM that is PXE booted from the `br1` device (which must already be configured in the host machine), and if that fails, is booted from the disk:
```ruby
Vagrant.configure("2") do |config|
config.vm.define :pxeclient do |pxeclient|
pxeclient.vm.network :public_network,
dev: 'br1',
auto_config: false
pxeclient.vm.provider :libvirt do |domain|
boot_network = {'dev' => 'br1'}
domain.storage :file, :size => '100G'
domain.boot boot_network
domain.boot 'hd'
end
end
end
```
2013-07-24 15:44:30 +02:00
## SSH Access To VM
2016-09-07 14:41:25 +01:00
vagrant-libvirt supports vagrant's [standard ssh
settings](https://docs.vagrantup.com/v2/vagrantfile/ssh_settings.html).
2013-07-24 15:44:30 +02:00
2014-01-24 07:48:00 -08:00
## Forwarded Ports
2016-09-07 14:41:25 +01:00
vagrant-libvirt supports Forwarded Ports via ssh port forwarding. Please note
that due to a well known limitation only the TCP protocol is supported. For
each `forwarded_port` directive you specify in your Vagrantfile,
vagrant-libvirt will maintain an active ssh process for the lifetime of the VM.
2017-05-11 23:04:23 -04:00
If your VM should happen to be rebooted, the SSH session will need to be
restablished by halting the VM and bringing it back up.
2014-01-24 07:48:00 -08:00
2016-09-07 14:41:25 +01:00
vagrant-libvirt supports an additional `forwarded_port` option `gateway_ports`
which defaults to `false` , but can be set to `true` if you want the forwarded
port to be accessible from outside the Vagrant host. In this case you should
also set the `host_ip` option to `'*'` since it defaults to `'localhost'` .
2014-09-16 18:01:46 +02:00
2016-09-07 14:41:25 +01:00
You can also provide a custom adapter to forward from by 'adapter' option.
Default is `eth0` .
2016-01-12 10:29:01 +01:00
2017-05-11 23:04:23 -04:00
**Internally Accessible Port Forward**
`config.vm.network :forwarded_port, guest: 80, host: 2000`
**Externally Accessible Port Forward**
`config.vm.network :forwarded_port, guest: 80, host: 2000, host_ip: "0.0.0.0"`
2019-06-11 18:36:10 +02:00
### Forwarding the ssh-port
2021-05-18 18:30:56 +01:00
Vagrant-libvirt now supports forwarding the standard ssh-port on port 2222 from
the localhost to allow for consistent provisioning steps/ports to be used when
2019-06-11 18:36:10 +02:00
defining across multiple providers.
2021-05-18 18:30:56 +01:00
To enable, set the following:
2019-06-11 18:36:10 +02:00
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
2021-05-18 18:30:56 +01:00
# Enable forwarding of forwarded_port with id 'ssh'.
libvirt.forward_ssh_port = true
2019-06-11 18:36:10 +02:00
end
2021-05-18 18:30:56 +01:00
end
2019-06-11 18:36:10 +02:00
```
2021-05-18 18:30:56 +01:00
Previously by default libvirt skipped the forwarding of the ssh-port because
you can access the machine directly. In the future it is expected that this
will be enabled by default once autocorrect support is added to handle port
collisions for multi machine environments gracefully.
2013-03-27 00:55:30 +01:00
## Synced Folders
2021-04-17 01:37:59 +08:00
Vagrant automatically syncs the project folder on the host to `/vagrant` in
the guest. You can also configure additional synced folders.
2013-03-27 00:55:30 +01:00
2021-04-17 01:37:59 +08:00
**SECURITY NOTE:** for remote Libvirt, nfs synced folders requires a bridged
public network interface and you must connect to Libvirt via ssh.
2017-10-14 12:59:36 +03:00
2021-04-17 01:37:59 +08:00
**NFS**
2014-05-03 17:14:15 -05:00
2021-04-17 01:37:59 +08:00
`vagrant-libvirt` supports
[NFS ](https://www.vagrantup.com/docs/synced-folders/nfs ) as default with
bidirectional synced folders.
2013-03-27 00:55:30 +01:00
2021-04-17 01:37:59 +08:00
Example with NFS:
2015-05-17 19:47:07 +02:00
2021-04-17 01:37:59 +08:00
``` ruby
Vagrant.configure("2") do |config|
config.vm.synced_folder "./", "/vagrant"
end
2016-09-07 14:41:25 +01:00
```
2013-03-27 00:55:30 +01:00
2021-04-17 01:37:59 +08:00
**RSync**
2015-10-25 19:20:30 +00:00
2021-04-17 01:37:59 +08:00
`vagrant-libvirt` supports
[rsync ](https://www.vagrantup.com/docs/synced-folders/rsync ) with
unidirectional synced folders.
Example with rsync:
``` ruby
Vagrant.configure("2") do |config|
config.vm.synced_folder "./", "/vagrant", type: "rsync"
end
2016-09-07 14:41:25 +01:00
```
2015-10-25 19:20:30 +00:00
2021-04-17 01:37:59 +08:00
**9P**
`vagrant-libvirt` supports [VirtFS ](http://www.linux-kvm.org/page/VirtFS ) ([9p
or Plan 9](https://en.wikipedia.org/wiki/9P_\(protocol\))) with bidirectional
synced folders.
Difference between NFS and 9p is explained
[here ](https://unix.stackexchange.com/questions/240281/virtfs-plan-9-vs-nfs-as-tool-for-share-folder-for-virtual-machine ).
2016-09-07 14:41:25 +01:00
For 9p shares, a `mount: false` option allows to define synced folders without
mounting them at boot.
2015-10-25 19:20:30 +00:00
2021-04-17 01:37:59 +08:00
Example for `accessmode: "squash"` with 9p:
2016-10-07 18:53:29 +02:00
2021-04-17 01:37:59 +08:00
``` ruby
Vagrant.configure("2") do |config|
config.vm.synced_folder "./", "/vagrant", type: "9p", disabled: false, accessmode: "squash", owner: "1000"
end
```
Example for `accessmode: "mapped"` with 9p:
``` ruby
Vagrant.configure("2") do |config|
config.vm.synced_folder "./", "/vagrant", type: "9p", disabled: false, accessmode: "mapped", mount: false
end
```
Further documentation on using 9p can be found in [kernel
docs](https://www.kernel.org/doc/Documentation/filesystems/9p.txt) and in
[QEMU
wiki](https://wiki.qemu.org/Documentation/9psetup#Starting_the_Guest_directly ).
Please do note that 9p depends on support in the guest and not all distros
come with the 9p module by default.
**Virtio-fs**
`vagrant-libvirt` supports [Virtio-fs ](https://virtio-fs.gitlab.io/ ) with
bidirectional synced folders.
For virtiofs shares, a `mount: false` option allows to define synced folders
without mounting them at boot.
So far, passthrough is the only supported access mode and it requires running
the virtiofsd daemon as root.
QEMU needs to allocate the backing memory for all the guest RAM as shared
memory, e.g. [Use file-backed
memory](https://libvirt.org/kbase/virtiofs.html#host -setup) by enable
`memory_backing_dir` option in `/etc/libvirt/qemu.conf` :
``` shell
memory_backing_dir = "/dev/shm"
```
Example for Libvirt \>= 6.2.0 (e.g. Ubuntu 20.10 with Linux 5.8.0 + QEMU 5.0 +
Libvirt 6.6.0, i.e. NUMA nodes required) with virtiofs:
``` ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
libvirt.cpus = 2
libvirt.numa_nodes = [{ :cpus => "0-1", :memory => 8192, :memAccess => "shared" }]
libvirt.memorybacking :access, :mode => "shared"
end
config.vm.synced_folder "./", "/vagrant", type: "virtiofs"
end
```
Example for Libvirt \>= 6.9.0 (e.g. Ubuntu 21.04 with Linux 5.11.0 + QEMU 5.2 +
Libvirt 7.0.0, or Ubuntu 20.04 + [PPA
enabled](https://launchpad.net/~savoury1/+archive/ubuntu/virtualisation)) with
virtiofs:
``` ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
libvirt.cpus = 2
libvirt.memory = 8192
libvirt.memorybacking :access, :mode => "shared"
end
config.vm.synced_folder "./", "/vagrant", type: "virtiofs"
end
```
Further documentation on using virtiofs can be found in [official
HowTo](https://virtio-fs.gitlab.io/index.html#howto ) and in [Libvirt
KB](https://libvirt.org/kbase/virtiofs.html).
Please do note that virtiofs depends on:
- Host: Linux \>= 5.4, QEMU \>= 4.2 and Libvirt \>= 6.2 (e.g. Ubuntu 20.10)
- Guest: Linux \>= 5.4 (e.g. Ubuntu 20.04)
2015-05-18 07:48:39 +02:00
2018-02-25 12:43:07 +08:00
## QEMU Session Support
2019-08-30 11:02:16 +02:00
vagrant-libvirt supports using QEMU user sessions to maintain Vagrant VMs. As the session connection does not have root access to the system features which require root will not work. Access to networks created by the system QEMU connection can be granted by using the [QEMU bridge helper ](https://wiki.qemu.org/Features/HelperNetworking ). The bridge helper is enabled by default on some distros but may need to be enabled/installed on others.
There must be a virbr network defined in the QEMU system session. The libvirt `default` network which comes by default, the vagrant `vagrant-libvirt` network which is generated if you run a Vagrantfile using the System session, or a manually defined network can be used. These networks can be set to autostart with `sudo virsh net-autostart <net-name>` , which'll mean no further root access is required even after reboots.
The QEMU bridge helper is configured via `/etc/qemu/bridge.conf` . This file must include the virbr you wish to use (e.g. virbr0, virbr1, etc). You can find this out via `sudo virsh net-dumpxml <net-name>` .
```
allow virbr0
```
2018-02-25 12:43:07 +08:00
An example configuration of a machine using the QEMU session connection:
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
# Use QEMU session instead of system connection
libvirt.qemu_use_session = true
# URI of QEMU session connection, default is as below
libvirt.uri = 'qemu:///session'
2019-08-30 11:02:16 +02:00
# URI of QEMU system connection, use to obtain IP address for management, default is below
2018-02-25 12:43:07 +08:00
libvirt.system_uri = 'qemu:///system'
2019-02-11 20:18:02 -05:00
# Path to store Libvirt images for the virtual machine, default is as ~/.local/share/libvirt/images
2018-02-25 12:43:07 +08:00
libvirt.storage_pool_path = '/home/user/.local/share/libvirt/images'
2019-08-30 11:02:16 +02:00
# Management network device, default is below
2018-02-25 12:43:07 +08:00
libvirt.management_network_device = 'virbr0'
end
2018-03-17 14:28:23 +01:00
2018-02-25 12:43:07 +08:00
# Public network configuration using existing network device
# Note: Private networks do not work with QEMU session enabled as root access is required to create new network devices
config.vm.network :public_network, :dev => "virbr1",
:mode => "bridge",
:type => "bridge"
end
```
2015-05-18 11:46:04 +08:00
2014-09-28 10:29:04 -04:00
## Customized Graphics
vagrant-libvirt supports customizing the display and video settings of the
2016-09-07 14:41:25 +01:00
managed guest. This is probably most useful for VNC-type displays with
multiple guests. It lets you specify the exact port for each guest to use
deterministically.
2014-09-28 10:29:04 -04:00
Here is an example of using custom display options:
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
libvirt.graphics_port = 5901
libvirt.graphics_ip = '0.0.0.0'
libvirt.video_type = 'qxl'
end
end
```
2013-05-14 01:16:28 +02:00
2016-01-06 09:48:20 -05:00
## TPM Devices
Modern versions of Libvirt support connecting to TPM devices on the host
system. This allows you to enable Trusted Boot Extensions, among other
features, on your guest VMs.
2020-12-15 06:43:46 -06:00
To passthrough a hardware TPM, you will generally only need to modify the
`tpm_path` variable in your guest configuration. However, advanced usage,
such as the application of a Software TPM, may require modifying the
`tpm_model` , `tpm_type` and `tpm_version` variables.
2016-01-06 09:48:20 -05:00
2020-12-15 06:43:46 -06:00
The TPM options will only be used if you specify a TPM path or version.
Declarations of any TPM options without specifying a path or version will
result in those options being ignored.
2016-01-06 09:48:20 -05:00
Here is an example of using the TPM options:
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
libvirt.tpm_model = 'tpm-tis'
libvirt.tpm_type = 'passthrough'
libvirt.tpm_path = '/dev/tpm0'
end
end
```
2020-12-15 06:43:46 -06:00
It's also possible for Libvirt to start an emulated TPM device on the host.
Requires `swtpm` and `swtpm-tools`
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
libvirt.tpm_model = "tpm-crb"
libvirt.tpm_type = "emulator"
libvirt.tpm_version = "2.0"
end
end
```
2020-12-15 12:40:05 -05:00
## Memory balloon
The configuration of the memory balloon device can be overridden. By default,
libvirt will automatically attach a memory balloon; this behavior is preserved
by not configuring any memballoon-related options. The memory balloon can be
explicitly disabled by setting `memballoon_enabled` to `false` . Setting
`memballoon_enabled` to `true` will allow additional configuration of
memballoon-related options.
Here is an example of using the memballoon options:
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
libvirt.memballoon_enabled = true
libvirt.memballoon_model = 'virtio'
libvirt.memballoon_pci_bus = '0x00'
libvirt.memballoon_pci_slot = '0x0f'
end
end
```
2016-06-10 10:00:09 +07:00
## Libvirt communication channels
For certain functionality to be available within a guest, a private
2016-09-07 14:41:25 +01:00
communication channel must be established with the host. Two notable examples
2019-02-11 20:18:02 -05:00
of this are the QEMU guest agent, and the Spice/QXL graphics type.
2016-06-10 10:00:09 +07:00
2016-09-07 14:41:25 +01:00
Below is a simple example which exposes a virtio serial channel to the guest.
Note: in a multi-VM environment, the channel would be created for all VMs.
2016-06-10 10:00:09 +07:00
```ruby
vagrant.configure(2) do |config|
config.vm.provider :libvirt do |libvirt|
libvirt.channel :type => 'unix', :target_name => 'org.qemu.guest_agent.0', :target_type => 'virtio'
end
end
```
2016-09-07 14:41:25 +01:00
Below is the syntax for creating a spicevmc channel for use by a qxl graphics
card.
2016-06-10 10:00:09 +07:00
```ruby
vagrant.configure(2) do |config|
config.vm.provider :libvirt do |libvirt|
2016-09-06 16:26:33 +01:00
libvirt.channel :type => 'spicevmc', :target_name => 'com.redhat.spice.0', :target_type => 'virtio'
2016-06-10 10:00:09 +07:00
end
end
```
2016-09-07 14:41:25 +01:00
These settings can be specified on a per-VM basis, however the per-guest
settings will OVERRIDE any global 'config' setting. In the following example,
2019-02-11 20:18:02 -05:00
we create 3 VMs with the following configuration:
2016-06-10 10:00:09 +07:00
2016-09-07 14:41:25 +01:00
* **master**: No channel settings specified, so we default to the provider
setting of a single virtio guest agent channel.
* **node1**: Override the channel setting, setting both the guest agent
channel, and a spicevmc channel
* **node2**: Override the channel setting, setting both the guest agent
channel, and a 'guestfwd' channel. TCP traffic sent by the guest to the given
IP address and port is forwarded to the host socket `/tmp/foo` . Note: this
device must be unique for each VM.
2016-06-10 10:00:09 +07:00
2016-09-07 14:41:25 +01:00
For example:
2016-06-10 10:00:09 +07:00
```ruby
Vagrant.configure(2) do |config|
2020-06-04 01:04:18 +01:00
config.vm.box = "fedora/32-cloud-base"
2016-06-10 10:00:09 +07:00
config.vm.provider :libvirt do |libvirt|
libvirt.channel :type => 'unix', :target_name => 'org.qemu.guest_agent.0', :target_type => 'virtio'
2016-09-07 14:41:25 +01:00
end
2016-06-10 10:00:09 +07:00
config.vm.define "master" do |master|
master.vm.provider :libvirt do |domain|
domain.memory = 1024
2016-09-07 14:41:25 +01:00
end
end
2016-06-10 10:00:09 +07:00
config.vm.define "node1" do |node1|
node1.vm.provider :libvirt do |domain|
domain.channel :type => 'unix', :target_name => 'org.qemu.guest_agent.0', :target_type => 'virtio'
domain.channel :type => 'spicevmc', :target_name => 'com.redhat.spice.0', :target_type => 'virtio'
2016-09-07 14:41:25 +01:00
end
end
2016-06-10 10:00:09 +07:00
config.vm.define "node2" do |node2|
node2.vm.provider :libvirt do |domain|
domain.channel :type => 'unix', :target_name => 'org.qemu.guest_agent.0', :target_type => 'virtio'
domain.channel :type => 'unix', :target_type => 'guestfwd', :target_address => '192.0.2.42', :target_port => '4242',
:source_path => '/tmp/foo'
2016-09-07 14:41:25 +01:00
end
end
2016-06-10 10:00:09 +07:00
end
```
2020-05-10 09:37:31 -04:00
## Custom command line arguments and environment variables
You can also specify multiple qemuargs arguments or qemuenv environment variables for qemu-system
2017-01-10 20:07:15 +03:00
* `value` - Value
```ruby
Vagrant.configure("2") do |config|
config.vm.provider :libvirt do |libvirt|
libvirt.qemuargs :value => "-device"
libvirt.qemuargs :value => "intel-iommu"
2020-05-10 09:37:31 -04:00
libvirt.qemuenv QEMU_AUDIO_DRV: 'pa'
libvirt.qemuenv QEMU_AUDIO_TIMER_PERIOD: '150'
libvirt.qemuenv QEMU_PA_SAMPLES: '1024', QEMU_PA_SERVER: '/run/user/1000/pulse/native'
2017-01-10 20:07:15 +03:00
end
end
```
2021-05-22 16:59:11 +01:00
## Box Formats
### Version 1
This is the original format that most boxes currently use.
2013-04-02 20:06:52 +02:00
2016-09-07 14:41:25 +01:00
You can view an example box in the
[`example_box/directory` ](https://github.com/vagrant-libvirt/vagrant-libvirt/tree/master/example_box ).
That directory also contains instructions on how to build a box.
2013-04-02 20:06:52 +02:00
The box is a tarball containing:
2016-09-07 14:41:25 +01:00
* qcow2 image file named `box.img`
* `metadata.json` file describing box image (`provider` , `virtual_size` ,
`format` )
* `Vagrantfile` that does default settings for the provider-specific
configuration for this provider
2013-04-02 20:06:52 +02:00
2021-05-22 16:59:11 +01:00
### Version 2 (Experimental)
Due to the limitation of only being able to handle a single disk with the version 1 format, a new
format was added to support boxes that need to specify multiple disks. This is still currently
experimental and as such support for packaging has yet to be added. There is a script in the tools
folder (tools/create_box_with_two_disks.sh) that should provide a guideline on how to create such
a box for those that wish to experiment and provide early feedback.
At it's most basic, it expects an array of disks to allow a specific order to be presented. Disks
will be attached in this order and as such assume device names base on this within the VM. The
'path' attribute is required, and is expected to be relative to the base of the box. This should
allow placing the disk images within a nested directory within the box if it useful for those
with a larger number of disks. The name allows overriding the target volume name that will be
used in the libvirt storage pool. Note that vagrant-libvirt will still prefix the volume name
with `#{box_name}_vagrant_box_image_#{box_version}_` to avoid accidental clashes with other boxes.
Format and virtual size need no longer be specified as they are now retrieved directly from the
provided image using `qemu-img info ...` .
Example format:
```json
{
'disks': [
{
'path': 'disk1.img'
},
{
'path': 'disk2.img',
'name': 'secondary_disk'
},
{
'path': 'disk3.img'
}
],
'provider': 'libvirt'
}
```
2015-04-09 08:24:42 +02:00
## Create Box
2020-11-17 19:31:19 +00:00
If creating a box from a modified vagrant-libvirt machine, ensure that
you have set the `config.ssh.insert_key = false` in the original Vagrantfile
as otherwise Vagrant will replace the default connection key-pair that is
required on first boot with one specific to the machine and prevent
the default key from working on the exported result.
```ruby
Vagrant.configure("2") do |config|
# this setting is only recommended if planning to export the
# resulting machine
config.ssh.insert_key = false
config.vm.define :test_vm do |test_vm|
test_vm.vm.box = "fedora/32-cloud-base"
end
end
```
2016-09-07 14:41:25 +01:00
To create a vagrant-libvirt box from a qcow2 image, run `create_box.sh`
(located in the tools directory):
```shell
$ create_box.sh ubuntu14.qcow2
```
2015-04-09 08:24:42 +02:00
2016-09-07 14:41:25 +01:00
You can also create a box by using [Packer ](https://packer.io ). Packer
templates for use with vagrant-libvirt are available at
https://github.com/jakobadam/packer-qemu-templates. After cloning that project
you can build a vagrant-libvirt box by running:
2015-04-09 08:24:42 +02:00
2016-09-07 14:41:25 +01:00
```shell
$ cd packer-qemu-templates
$ packer build ubuntu-14.04-server-amd64-vagrant.json
```
2015-04-09 08:24:42 +02:00
2019-02-27 20:32:37 +08:00
## Package Box from VM
vagrant-libvirt has native support for [`vagrant
package`](https://www.vagrantup.com/docs/cli/package.html) via
libguestfs [virt-sysprep ](http://libguestfs.org/virt-sysprep.1.html ).
virt-sysprep operations can be customized via the
`VAGRANT_LIBVIRT_VIRT_SYSPREP_OPERATIONS` environment variable; see the
[upstream
documentation](http://libguestfs.org/virt-sysprep.1.html#operations ) for
further details especially on default sysprep operations enabled for
your system.
2020-04-21 12:37:03 +02:00
Options to the virt-sysprep command call can be passed via
`VAGRANT_LIBVIRT_VIRT_SYSPREP_OPTIONS` environment variable.
```shell
$ export VAGRANT_LIBVIRT_VIRT_SYSPREP_OPTIONS="--delete /etc/hostname"
$ vagrant package
```
2019-02-27 20:32:37 +08:00
For example, on Chef [bento ](https://github.com/chef/bento ) VMs that
require SSH hostkeys already set (e.g. bento/debian-7) as well as leave
existing LVM UUIDs untouched (e.g. bento/ubuntu-18.04), these can be
packaged into vagrant-libvirt boxes like so:
```shell
$ export VAGRANT_LIBVIRT_VIRT_SYSPREP_OPERATIONS="defaults,-ssh-userdir,-ssh-hostkeys,-lvm-uuids"
$ vagrant package
```
2019-10-10 12:39:58 -07:00
## Troubleshooting VMs
The first step for troubleshooting a VM image that appears to not boot correctly,
or hangs waiting to get an IP, is to check it with a VNC viewer. A key thing
to remember is that if the VM doesn't get an IP, then vagrant can't communicate
2020-05-10 14:23:25 +01:00
with it to configure anything, so a problem at this stage is likely to come from
2019-10-10 12:39:58 -07:00
the VM, but we'll outline the tools and common problems to help you troubleshoot
that.
By default, when you create a new VM, a vnc server will listen on `127.0.0.1` on
port `TCP5900` . If you connect with a vnc viewer you can see the boot process. If
your VM isn't listening on `5900` by default, you can use `virsh dumpxml` to find
out which port it's listening on, or can configure it with `graphics_port` and
`graphics_ip` (see 'Domain Specific Options' above).
Note: Connecting with the console (`virsh console` ) requires additional config,
so some VMs may not show anything on the console at all, instead displaying it in
2020-05-10 14:23:25 +01:00
the VNC console. The issue with the text console is that you also need to build the
image used to tell the kernel to output to the console during boot, and typically
2019-10-10 12:39:58 -07:00
most do not have this built in.
Problems we've seen in the past include:
- Forgetting to remove `/etc/udev/rules.d/70-persistent-net.rules` before packaging
the VM
- VMs expecting a specific disk device to be connected
If you're still confused, check the Github Issues for this repo for anything that
looks similar to your problem.
2020-05-10 14:23:25 +01:00
[Github Issue #1032 ](https://github.com/vagrant-libvirt/vagrant-libvirt/issues/1032 )
contains some historical troubleshooting for VMs that appeared to hang.
2019-10-10 12:39:58 -07:00
Did you hit a problem that you'd like to note here to save time in the future?
Please do!
2013-03-27 00:55:30 +01:00
## Development
To work on the `vagrant-libvirt` plugin, clone this repository out, and use
[Bundler ](http://gembundler.com ) to get the dependencies:
2016-09-07 14:41:25 +01:00
```shell
2016-04-27 14:08:25 +02:00
$ git clone https://github.com/vagrant-libvirt/vagrant-libvirt.git
2013-04-02 20:06:52 +02:00
$ cd vagrant-libvirt
$ bundle install
2013-03-27 00:55:30 +01:00
```
2015-10-22 08:43:06 +03:00
Once you have the dependencies, verify the unit tests pass with `rspec` :
2013-03-27 00:55:30 +01:00
2016-09-07 14:41:25 +01:00
```shell
2021-05-15 14:47:05 +01:00
$ export VAGRANT_HOME=$(mktemp -d)
$ bundle exec rspec --fail-fast --color --format documentation
2013-03-27 00:55:30 +01:00
```
2021-05-15 14:47:05 +01:00
If those pass, you're ready to start developing the plugin.
Setting `VAGRANT_HOME` is to avoid issues with conflicting with other
plugins/gems or data already present under `~/.vagrant.d` .
Additionally if you wish to test against a specific version of vagrant you
can control the version using the following before running the tests:
```shell
$ export VAGRANT_VERSION=v2.2.14
```
**Note** rvm is used by the maintainers to help provide an environment to test
against multiple ruby versions that align with the ones used by vagrant for
their embedded ruby depending on the release. You can see what version is used
by looking at the current [unit tests ](.github/workflows/unit-tests.yml )
workflow.
You can test the plugin without installing it into your Vagrant environment by
just creating a `Vagrantfile` in the top level of this directory (it is
gitignored) that uses it. You can add the following line to your Vagrantfile
while in development to ensure vagrant checks that the plugin is installed:
2013-04-10 17:34:09 +02:00
```ruby
2021-03-22 11:51:41 +00:00
Vagrant.configure("2") do |config|
config.vagrant.plugins = "vagrant-libvirt"
end
```
Or add the following to the top of the file to ensure that any required plugins
are installed globally:
```ruby
REQUIRED_PLUGINS = %w(vagrant-libvirt)
exit unless REQUIRED_PLUGINS.all? do |plugin|
Vagrant.has_plugin?(plugin) || (
puts "The #{plugin} plugin is required. Please install it with:"
puts "$ vagrant plugin install #{plugin}"
false
)
end
2013-04-10 17:34:09 +02:00
```
Now you can use bundler to execute Vagrant:
2013-03-27 00:55:30 +01:00
2016-09-07 14:41:25 +01:00
```shell
2013-03-27 00:55:30 +01:00
$ bundle exec vagrant up --provider=libvirt
```
2016-09-07 14:41:25 +01:00
**IMPORTANT NOTE:** bundle is crucial. You need to use bundled Vagrant.
2013-12-10 16:43:26 +01:00
2013-04-02 20:06:52 +02:00
## Contributing
2016-09-07 14:41:25 +01:00
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature` )
3. Commit your changes (`git commit -am 'Add some feature'` )
4. Push to the branch (`git push origin my-new-feature` )
5. Create new Pull Request
2020-05-10 14:23:25 +01:00
<!--
# styling for TOC
vim: expandtab shiftwidth=2
-->