使用 qemu-kvm 安装和运行 Vagrant
Vagrant
![](/data/attachment/album/202010/03/094655p9j204bt8em0plje.png)


Vagrant 是一个出色的工具，DevOps 专业人员、程序员、系统管理员和普通极客来使用它来建立可重复的基础架构来进行开发和测试。引用自它的网站：



> 
> Vagrant 是用于在单工作流程中构建和管理虚拟机环境的工具。凭借简单易用的工作流程并专注于自动化，Vagrant 降低了开发环境的设置时间，提高了生产效率，并使“在我的机器上可以工作”的借口成为过去。
> 
> 
> 如果你已经熟悉 Vagrant 的基础知识，那么该文档为所有的功能和内部结构提供了更好的参考。
> 
> 
> Vagrant 提供了基于行业标准技术构建的、易于配置、可复制、可移植的工作环境，并由一个一致的工作流程控制，帮助你和你的团队最大限度地提高生产力和灵活性。
> 
> 
> <https://www.vagrantup.com/intro>
> 
> 
> 


本指南将逐步介绍使 Vagrant 在基于 Fedora 的计算机上工作所需的步骤。


我从最小化安装 Fedora 服务器开始，因为这样可以减少宿主机操作系统的内存占用，但如果你已经有一台可以使用的 Fedora 机器，无论是服务器还是工作站版本，那么也没问题。


### 检查机器是否支持虚拟化



```
$ sudo lscpu | grep Virtualization

```


```
Virtualization:                  VT-x
Virtualization type:             full

```

### 安装 qemu-kvm



```
sudo dnf install qemu-kvm libvirt libguestfs-tools virt-install rsync

```

### 启用并启动 libvirt 守护进程



```
sudo systemctl enable --now libvirtd

```

### 安装 Vagrant



```
sudo dnf install vagrant

```

### 安装 Vagrant libvirtd 插件



```
sudo vagrant plugin install vagrant-libvirt

```

### 添加一个 box



```
vagrant box add fedora/32-cloud-base --provider=libvirt

```

（LCTT 译注：以防你不知道，box 是 Vagrant 中的一种包格式，Vagrant 支持的任何平台上的任何人都可以使用盒子来建立相同的工作环境。）


### 创建一个最小化的 Vagrantfile 来测试



```
$ mkdir vagrant-test
$ cd vagrant-test
$ vi Vagrantfile

```


```
Vagrant.configure("2") do |config|
  config.vm.box = "fedora/32-cloud-base"
end

```

**注意文件名和文件内容的大小写。**


### 检查文件



```
vagrant status

```


```
Current machine states:

default not created (libvirt)

The Libvirt domain is not created. Run 'vagrant up' to create it.

```

### 启动 box



```
vagrant up

```

### 连接到你的新机器



```
vagrant ssh

```

完成了。现在你的 Fedora 机器上 Vagrant 可以工作了。


要停止该机器，请使用 `vagrant halt`。这只是简单地停止机器，但保留虚拟机和磁盘。 要关闭并删除它，请使用 `vagrant destroy`。这将删除整个机器和你在其中所做的任何更改。


### 接下来的步骤


在运行 `vagrant up` 命令之前，你不需要下载 box。你可以直接在 Vagrantfile 中指定 box 和提供者，如果还没有的话，Vagrant 会下载它。下面是一个例子，它还设置了内存量和 CPU 数量：



```
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "fedora/32-cloud-base"
  config.vm.provider :libvirt do |libvirt|
    libvirt.cpus = 1
    libvirt.memory = 1024
  end
end

```

关于使用 Vagrant、创建你自己的机器和使用不同 box 的更多信息，请参见官方文档 <https://www.vagrantup.com/docs>。


有一个庞大的仓库，你可以随时下载使用这些 box，它们的的官方仓库是 Vagrant Cloud - <https://app.vagrantup.com/boxes/search>。这里有些是基本的操作系统，有些提供完整的功能，如数据库、网络服务器等。




---


via: <https://fedoramagazine.org/vagrant-qemukvm-fedora-devops-sysadmin/>


作者：[Andy Mott](https://fedoramagazine.org/author/amott/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Vagrant is a brilliant tool, used by DevOps professionals, coders, sysadmins and regular geeks to stand up repeatable infrastructure for development and testing. From their website:

Vagrant is a tool for building and managing virtual machine environments in a single workflow. With an easy-to-use workflow and focus on automation, Vagrant lowers development environment setup time, increases production parity, and makes the “works on my machine” excuse a relic of the past.

https://www.vagrantup.com/intro

If you are already familiar with the basics of Vagrant, the documentation provides a better reference build for all available features and internals.

Vagrant provides easy to configure, reproducible, and portable work environments built on top of industry-standard technology and controlled by a single consistent workflow to help maximize the productivity and flexibility of you and your team.

This guide will walk through the steps necessary to get Vagrant working on a Fedora-based machine.

I started with a minimal install of Fedora Server as this reduces the memory footprint of the host OS, but if you already have a working Fedora machine, either Server or Workstation, then this should still work.

#### Check the machine supports virtualisation:

$

sudo lscpu | grep VirtualizationVirtualization: VT-x Virtualization type: full

#### Install qemu-kvm:


sudo dnf install qemu-kvm libvirt libguestfs-tools virt-install rsync

#### Enable and start the libvirt daemon:


sudo systemctl enable --now libvirtd

#### Install Vagrant:


sudo dnf install vagrant

#### Install the Vagrant libvirtd plugin:


sudo vagrant plugin install vagrant-libvirt

#### Add a box


vagrant box add fedora/32-cloud-base --provider=libvirt

#### Create a minimal Vagrantfile to test

$

Vagrant.configure("2") do |config| config.vm.box = "fedora/32-cloud-base" endmkdir vagrant-test$cd vagrant-test$vi Vagrantfile

**Note the capitalisation of the file name and in the file itself.**

#### Check the file:

Current machine states: default not created (libvirt) The Libvirt domain is not created. Run 'vagrant up' to create it.

vagrant status

#### Start the box:


vagrant up

#### Connect to your new machine:


vagrant ssh

That’s it – you now have Vagrant working on your Fedora machine.

To stop the machine, use *vagrant halt*. This simply halts the machine but leaves the VM and disk in place.

To shut it down and delete it use *vagrant destroy*. This will remove the whole machine and any changes you’ve made in it.

### Next steps

You don’t need to download boxes before issuing the *vagrant up* command – you can specify the box and the provider in the Vagrantfile directly and Vagrant will download it if it’s not already there. Below is an example which also sets the amount memory and number of CPUs:

# -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure("2") do |config| config.vm.box = "fedora/32-cloud-base" config.vm.provider :libvirt do |libvirt| libvirt.cpus = 1 libvirt.memory = 1024 end end

For more information on using Vagrant, creating your own machines and using different boxes, see the official documentation at [https://www.vagrantup.com/docs](https://www.vagrantup.com/docs)

There is a huge repository of boxes ready to download and use, and the official location for these is Vagrant Cloud – [https://app.vagrantup.com/boxes/search](https://app.vagrantup.com/boxes/search). Some are basic operating systems and some offer complete functionality such as databases, web servers etc.

## Lucas

Very useful. I will test with qemu-kvm. I’ve had problems with vagrant and vbox, so I stopped using it.

## Sergey

I understand the use of Vagrant for Virtual Box, etc., but why use it for KVM ?

We have a virsh and ansible.

## Andy Mott

As with anything Unix/Linux there are many ways to get the job done!

While virsh commands and Ansible would also work, the simplicity of finding a suitable Vagrant box and getting it up and running quickly reduces the learning curve for anyone new to automated provisioning as it takes away a number of the steps required. I believe this makes it a fantastic tool for quickly creating machines for testing or other lab work.

## Dominique

Andy could you do a Virsh and Ansible tutorial ?

## Sandra

With KVM you would have to set up all the networking and partitioning and so on you self, and Vagrant have to handy builtin commands, which should make many taskes easier.

There would however be 2 good reasons to go with KVM over Vagrant:

you can use snapshots with KVM, so you can make a snapshot between each of your virsh commands and that way simulate the layers in Docker/Podman that speeds up building the box/image. Vagrant can’t do this for some strange reason.

All commands in a Vagrant file are executed as an non-privileged user, so you can’t copy a file directly to e.g. /etc. You first have to copy the file to /tmp and then write a script that copies the file from /tmp to /etc and then execute it. With virsh that is a one-liner.

## Serge Meeuwsen

Hi,

Thanks for providing this write up! This works fine with Linux based boxes but unfortunately it seems to be broken for Windows based boxes.

I have tried several and always get the “kernel_require.rb:83:in `require’: cannot load such file — winrm (LoadError)” error.

According to the hashicorp site you need to install vagrant 2.2.10 but then I had problems installing the vagrant-libvirt plugin… It is probably a dependency issue but I couldn’t figure it out.

## Mike Rosile

You may need the “vagrant-winrm” plugin. It can be installed using the following command:

vagrant plugin install vagrant-winrm

## tsyang

Serge,

It is the Fedora libssh binding issue. Please see this site for solution:

https://github.com/hashicorp/vagrant/issues/11020

## Mehdi

Thank you for the article. Didn’t get a chance to work with Vagrant before. Seems like some Docker projects can be done with Vagrant.

Btw, does Vagrant anything to do with Gnome Boxes?

## MattPie

@Mehdi,

Vagrant doesn’t really have much to do with Gnome Boxes, other than Gnome Boxes manages KVM VMs so you may be able to see the VMs Vagrant spins up in there.

And FWIW, Fedora (32, at least) comes with podman install, which is CLI-compatible with docker so many of the same commands will work. Like:

piechota@localhost Downloads]$ podman run -it ubuntu bash

…

Trying to pull docker.io/library/ubuntu…

Getting image source signatures

Copying blob 990916bd23bb done

Copying blob 534a5505201d done

Copying blob e6ca3592b144 done

Copying config bb0eaf4eee done

Writing manifest to image destination

Storing signatures

root@223636bf41b2:/# more /etc/lsb-release

DISTRIB_ID=Ubuntu

DISTRIB_RELEASE=20.04

DISTRIB_CODENAME=focal

DISTRIB_DESCRIPTION=”Ubuntu 20.04.1 LTS”

root@223636bf41b2:/# exit

exit

## Sandra

Why do you install vagrant-libvirt from Vagrant and not

?

## tsyang

Sandra,

The reason is stated in Peru’s Windows 10 box page:

https://app.vagrantup.com/peru/boxes/windows-10-enterprise-x64-eval

## Josef

Why not to mention the developer guide from Fedora Developer [0]? You would also see that we have vagrant-libvirt directly packaged in Fedora.

[0] https://developer.fedoraproject.org/tools/vagrant/vagrant-libvirt.html

## Sandra

Ok, so it makes a lot of sense for Windows boxes. So why still do it, when the box is Fedora/Linux?

## Eric L.

I also don’t install

because I know that I need more Vagrant plugins than are packaged as RPM, and, if you dare mixing up RPM-packaged and “native” Vagrant-plugins, it

alwaysgo wrong at some point in time. I even blockallvagrant plugins to avoid any issue with Ruby dependencies:[main]

excludepkgs=vagrant-*

It still goes wrong from time to time when the Vagrant-RPM is upgraded, but then you follow

allthe recommendations from the Vagrant-CLI (clean-up/rebuild/update the plug-ins) and it fixes the issue.To be honest, I’d like to get rid of Vagrant and replace it fully with Ansible. It’s technically possible but, as someone else wrote, the Vagrant library of prepared images is just too comfortable.

## Jeff Pettorino

So this does not work with Fedora 33 beta, nor Fedora 32. I tried both, a couple of times.

The problem comes from an old and well established bug:

https://bugzilla.redhat.com/show_bug.cgi?id=1835308

So what did you do to overcome this, and can you update the article to include the steps?

I’ve tried both the vagrant plugin installer and the RPM package vagrant-libvirtd. Same problems with racc-1.5.0 in both cases.

## Andy Mott

Hi Jeff

I just built a new F32 VM to test this on, and everything worked as expected. This is a vanilla build using Fedora 32 Server. No updates installed, just right out of the box. The only thing I did in the OS install was add my user account to the wheel group so I could use sudo.

$ libvirtd –version

libvirtd (libvirt) 6.1.0

$ vagrant –version

Vagrant 2.2.9

$ sudo vagrant plugin install vagrant-libvirt

Installing the ‘vagrant-libvirt’ plugin. This can take a few minutes…

Installed the plugin ‘vagrant-libvirt (0.0.45)’!

I copied and pasted commands from this article and saw no errors. Sorry I can’t offer more advice, but the steps above do work.

## Jeff Pettorino

Hi Andy, I double checked and found an error, I was deploying F33-Beta for all of my tests. I can confirm as well that on a (properly) installed F32 the article instructions work fine.

Thanks for being patient. 🙂

## Andy Mott

Hi Jeff

Glad you got it sorted! 🙂

I’m always happy to help if people have questions about my articles, so no worries about asking.

## Josef

I have Fedora 32 and Vagrant installed as vagrant-libvirt RPM package. I don’t use any non packaged plugins, perhaps this is why it works for me?

## Tom Christopher

The only way I found around this is to install toolbox on the system. Then run toolbox create -c libvirt -r f29 to create a Fedora 29 container. Then I run the installation as documented on the vagrant-libvirt github page. I also run terraform-libvirt from this same container.