Merge pull request #115 from scottslowe/kvm-lv

Add Libvirt support using nested virtualization for KVM learning environment
This commit is contained in:
Scott S. Lowe 2018-09-24 21:17:08 -06:00 committed by GitHub
commit 6bc323391d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 31 deletions

View file

@ -1,14 +1,14 @@
# Virtualization with KVM
These files were created to allow users to use Vagrant ([http://www.vagrantup.com](http://www.vagrantup.com)) and Ansible ([http://www.ansible.com](http://www.ansible.com)) to quickly and relatively easily experiment with KVM on Ubuntu 14.04 LTS. This configuration was tested using Vagrant 1.8.1, VMware Fusion 8.1.0, the Vagrant VMware plugin, and Ansible 1.9.1. Other versions of these products are likely to work, but haven't been tested.
These files were created to allow users to use Vagrant ([http://www.vagrantup.com](http://www.vagrantup.com)) and Ansible ([http://www.ansible.com](http://www.ansible.com)) to quickly and relatively easily experiment with KVM on Ubuntu 16.04 LTS. This configuration was tested using Vagrant, VMware Fusion with the Vagrant VMware plugin, KVM/Libvirt with the Libvirt provider, and Ansible.
Note that this environment does **not** work with VirtualBox, as it relies on support for nested virtualization (which VirtualBox does not support).
## Contents
* **ansible.cfg**: This Ansible configuration file instructs Ansible to use an inventory file named `hosts` in the current directory, to use the default vagrant user (`vagrant`) and default insecure Vagrant SSH private key, and to use the Vagrant-generated Ansible inventory file (found, by default, in `./.vagrant/provisioners/inventory/vagrant_ansible_inventory`).
* **ansible.cfg**: This Ansible configuration file instructs Ansible to use the Vagrant-provided inventory, to use the default vagrant user (`vagrant`) and default insecure Vagrant SSH private key, and to use the Vagrant-generated Ansible inventory file (found, by default, in `./.vagrant/provisioners/inventory/vagrant_ansible_inventory`).
* **machines.yml**: This YAML file contains a list of VM definitions and associated configuration data. It is referenced by `Vagrantfile` when Vagrant instantiates the VMs. Generally the only change this file needs is to ensure the "box" value correctly references the Vagrant box you're using. The "ip_addr" value is also required in order to build the Ansible inventory file; you may want to edit this to a value appropriate for your environment.
* **machines.yml**: This YAML file contains a list of VM definitions and associated configuration data. It is referenced by `Vagrantfile` when Vagrant instantiates the VMs.
* **provision.yml**: This YAML file is an Ansible playbook that configures the VMs created by Vagrant when they are first provisioned. Ansible is called automatically by Vagrant; you do not need to invoke Ansible separately. No changes are needed to this file.
@ -18,15 +18,13 @@ Note that this environment does **not** work with VirtualBox, as it relies on su
## Instructions
These instructions assume you've already installed VMware Fusion, Vagrant, the Vagrant VMware plugin, and Ansible. Please refer to the documentation for those products for more information on installation or configuration.
These instructions assume you've already installed Vagrant, the virtualizatoin provider (VMware Fusion or Libvirt), the necessary Vagrant plugin, and Ansible. Please refer to the documentation for those products for more information on installation or configuration.
1. Use `vagrant box add` to install an Ubuntu 14.04 x64 box for the "vmware_fusion" provider. The "bento/ubuntu-14.04" box is a good option here. (In theory you should be able to use this Vagrant environment with VMware Workstation as well, but only VMware Fusion was tested.)
1. Use `vagrant box add` to install an Ubuntu 16.04 x64 box for VMware Fusion or Libvirt. The `machines.yml` file contains some suggested boxes. If you download a box other than one of the ones listed in `machines.yml`, be sure to edit `machines.yml` appropriately.
2. Copy the files from the `kvm` directory of this repository (the "learning-tools" repository) to a directory on your system. You can clone the entire "learning-tools" repository (using `git clone`), or just download the specific files from the `kvm` directory.
2. Copy the files from the `kvm-generic` directory of this repository (the "learning-tools" repository) to a directory on your system. You can clone the entire "learning-tools" repository (using `git clone`), or just download the specific files from the `kvm-generic` directory.
3. Edit `machines.yml` to ensure that the box specified in that file matches the Ubuntu 14.04 x64 base box you just installed and will be using. _I recommend that you do not change any other values in this file unless you know it is necessary._
4. From a terminal window, change into the directory where the files from this directory are stored and run `vagrant up` to bring up the VMs according to the instructions in `machines.yml` and `Vagrantfile`. (By default, it will create and power on only a single VM.)
3. From a terminal window, change into the directory where the files from this directory are stored and run `vagrant up` to bring up the VMs according to the instructions in `machines.yml` and `Vagrantfile`. (By default, it will create and power on only a single VM.)
5. Once Vagrant has finished creating, booting, and provisioning the VM (note you'll need Internet access for this step), log into the VM (named "kvm-01" by default) using `vagrant ssh`.

View file

@ -5,14 +5,10 @@
Vagrant.require_version '>= 1.6.0'
VAGRANTFILE_API_VERSION = '2'
# Explicitly specify the VMware provider (req'd for nested virtualization)
ENV['VAGRANT_DEFAULT_PROVIDER'] = 'vmware_fusion'
# Require 'yaml' module
require 'yaml'
# Read YAML file with VM details (box, CPU, RAM, IP addresses)
# Edit machines.yml to change VM configuration details
# Read YAML file with VM details (box, CPU, and RAM)
machines = YAML.load_file(File.join(File.dirname(__FILE__), 'machines.yml'))
# Create and configure the VMs
@ -30,11 +26,11 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# Don't check for box updates
srv.vm.box_check_update = false
# Specify the hostname of the VM
# Set machine's hostname
srv.vm.hostname = machine['name']
# Specify the Vagrant box to use (must use VMware box)
srv.vm.box = machine['vmw_box']
# Use VMware box by default
srv.vm.box = machine['box']['vmw']
# Configure default synced folder (disable by default)
if machine['sync_disabled'] != nil
@ -43,10 +39,14 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
srv.vm.synced_folder '.', '/vagrant', disabled: true
end #if machine['sync_disabled']
# Assign additional private network
if machine['ip_addr'] != nil
srv.vm.network 'private_network', ip: machine['ip_addr']
end # if machine['ip_addr']
# Iterate through networks as per settings in machines.yml
machine['nics'].each do |net|
if net['ip_addr'] == 'dhcp'
srv.vm.network net['type'], type: net['ip_addr']
else
srv.vm.network net['type'], ip: net['ip_addr']
end # if net['ip_addr']
end # machine['nics'].each
# Configure CPU & RAM per settings in machines.yml (Fusion)
srv.vm.provider 'vmware_fusion' do |vmw|
@ -57,10 +57,21 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
vmw.vmx['vhv.enable'] = 'TRUE'
end # srv.vm.provider 'vmware_fusion'
# Provision the VM with Ansible
config.vm.provision 'ansible' do |ansible|
ansible.playbook = 'provision.yml'
end # config.vm.provision
# Configure CPU & RAM per settings in machines.yml (Libvirt)
srv.vm.provider 'libvirt' do |lv,override|
lv.memory = machine['ram']
lv.cpus = machine['vcpu']
override.vm.box = machine['box']['lv']
# Enable nested virtualization (required for KVM)
lv.nested = true
lv.cpu_mode = 'host-passthrough'
end # srv.vm.provider 'libvirt'
end # config.vm.define
end # machines.each
# Provision the VM with Ansible
config.vm.provision 'ansible' do |ansible|
ansible.playbook = 'provision.yml'
end # config.vm.provision
end # Vagrant.configure

View file

@ -2,4 +2,4 @@
inventory = ./.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory
private_key_file = ~/.vagrant.d/insecure_private_key
remote_user = vagrant
host_key_checking = False
host_key_checking = False

View file

@ -1,7 +1,11 @@
---
- name: "kvm-01"
vmw_box: "bento/ubuntu-14.04"
- box:
vmw: "bento/ubuntu-16.04"
lv: "generic/ubuntu1604"
name: "kvm-01"
nics:
- type: "private_network"
ip_addr: "dhcp"
ram: "1024"
sync_disabled: true
vcpu: "1"
ip_addr: "192.168.100.100"
nested: true

View file

@ -1,9 +1,13 @@
---
- hosts: "all"
sudo: "yes"
become: "yes"
remote_user: "vagrant"
gather_facts: false
tasks:
- name: "Install Python 2"
raw: "apt-get -y -q install python"
- name: "Install KVM and Libvirt"
apt:
state: "present"