Skip to content

Instantly share code, notes, and snippets.

@x86-39
Last active September 11, 2022 21:06
Show Gist options
  • Save x86-39/799949a7a3116eb301c6cbc3b9884ca5 to your computer and use it in GitHub Desktop.
Save x86-39/799949a7a3116eb301c6cbc3b9884ca5 to your computer and use it in GitHub Desktop.
Red Hat Enterprise Linux create Libvirt Vagrant Box for local testing

Steps

Replace X with major version and Y with minor version in this guide
  1. Register for a no-cost Red Hat Developer subscription
  2. Download RHEL DVD Image (Tested with 8.6 and 7.9)
  3. Create a QEMU/KVM libvirt VM (I use virt-manager) Attach this boot image as boot disk. I used 20GB as the disk size. Name this VM rhelX.Y
  4. Set installation destination to the local drive
  5. configure networking so that you have internet access (Should work out of the box with DHCP)
  6. Create a user "vagrant" with password "vagrant"
  7. Select minimal installation
  8. Proceed with installation
  9. After installation is complete, reboot
  10. Log in as vagrant user and elevate to root sudo su
  11. Register to Red Hat and log in subscription-manager register
  12. Install necessary packages yum install -y openssh wget gcc
  13. Configure sudoers visudo Add a line vagrant ALL=(ALL) NOPASSWD: ALL
  14. Shut down the VM
  15. Create a directory for the box and navigate to it mkdir rhelX && cd rhelX
  16. Compress the disk image to this directory sudo qemu-img convert -c -O qcow2 /var/lib/libvirt/images/rhelX.Y.qcow2 box.img
  17. Make your user owner of this file sudo chown USER:USER box.img
Replace USER with your username
  1. Create a file metadata.json with the content
{
  "provider"     : "libvirt",
  "format"       : "qcow2",
  "virtual_size" : 20
}
  1. Create a file Vagrantfile with the content
Vagrant.configure("2") do |config|
  config.ssh.username = "vagrant"
  config.ssh.password = "vagrant"
  config.vm.provider :libvirt do |libvirt|
    libvirt.driver = "kvm"
    libvirt.host = 'localhost'
    libvirt.uri = 'qemu:///system'
  end
  config.vm.define "new" do |custombox|
    custombox.vm.box = "rhelX"       # REPLACE THIS!!!
    custombox.vm.provider :libvirt do |virt|
      virt.memory = 2048
      virt.cpus = 2
    end
  end
end
  1. Create Vagrant box file tar cvzf rhelX.box ./metadata.json ./Vagrantfile ./box.img
  2. Import box into Vagrant vagrant box add --name rhelX rhelX.box

You can now use the box with

  host.vm.box = "rhelX"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment