Skip to main content

Build a lab in five minutes with three simple commands

It's handy to have a lab environment separate from your day-to-day workstation. Use these commands to set up a place to learn and experiment without risking your work environment.
Image
Test tubes

Photo by Kindel Media from Pexels

As sysadmins, we live at a frenetic day-to-day pace, maintaining and creating new systems, resolving issues and requirements as they are encountered, and inventing solutions to problems nobody knew existed the day before. With that kind of busy schedule, studying to learn new technologies can seem like a monumental challenge.

Being able to study and test new technologies and software versions implies the risk of losing information. The risk increases if you do not have your own computer or one dedicated to this exclusive purpose and you instead have to use the same machine for both work and study (as in my case).

That's why I set the task of finding a way to configure a dedicated environment where I could study and test everything I wanted in a fast and reproducible process while minimizing the risk of losing the information on my work computer.

Setting up a study environment

This magic process takes advantage of the images created for OpenStack, most of them in the qcow2 format. You can import these images into KVM for immediate use.

To be able to use these images, your laptop or computer must have a processor that allows virtualization:

$ sudo grep -E 'svm|vmx' /proc/cpuinfo

You must also install the following packages:

  • qemu-kvm
  • virt-manager
  • virt-viewer
  • libguestfs-tools
  • virt-install
  • genisoimage

Each image is about 400MB, so make sure you have ample disk space to store and allow them to grow nominally as you use them.

You can find images of minimal operating system installs, RHEL and Fedora included, on the OpenStack image guide.

Download the images you want and move them to the KVM working directory /var/lib/libvirt/images to start running the timer.

[ Download now: A system administrator's guide to IT automation. ]

Customize the virtual machine (VM)

Once you've downloaded the images you want to use, you can customize them to suit your needs better. I use this command:

$ sudo virt-customize \
-a /var/lib/libvirt/images/imagetest1.qcow2 \
--hostname vm01.test.lab \
--root-password password:rootpw \
--ssh-inject 'root:file:labkey.pub' \
--uninstall cloud-init \
--selinux-relabel

Let's examine these options in more detail:

  • virt-customize modifies the guest or disk image in place. The guest must be shut down. You do not need to run virt-customize as root. In this case, you must use sudo because you do not have access rights to the path where the image is hosted.
  • -a indicates the path of the disk to be customized.
  • --hostname sets the VM hostname.
  • --root-password sets the new root password, in this case: rootpw.
  • --ssh-inject allows you to inject an SSH key to some user. In this case, that's the labkey to root.
  • --uninstall allows you to uninstall software contained by default in the downloaded image.

Cloud-init is a suite that helps initialize an image for use in OpenStack. In your case, it is not necessary since you're customizing it to a basic functional level.

Note: If cloud-init is not uninstalled, the VM will take a long time to boot, as it waits for the parameters for initialization with cloud-init scripts.

If you're using a RHEL/CentOS/Fedora-based image, you must relabel the SELinux contexts with --selinux-relabel because several files were modified or deleted.

Install the VM

This imports the disk as a VM already created to your KVM using the virt-install command:

$ sudo virt-install \
--name vm01 \
--memory 1024 \
--vcpus 1 \
--disk /var/lib/libvirt/images/image.qcow2 \
--import \
--os-type linux --os-variant generic \
--noautoconsole

Here are details about the parameters above:

  • virt-install is a command-line tool for creating new KVM, Xen, or Linux container guests using the libvirt hypervisor management library.
  • --name sets the name of the new guest VM instance. This must be unique among all guests known to the hypervisor on the connection, including those not currently active.
  • --memory indicates the memory to allocate for the guest in MiB.
  • --vcpus indicates the number of virtual CPUs to configure for the guest.
  • --disk specifies what media to use as storage for the guest.
  • --import indicates that the VM is to be created from an existing disk, skipping the operating system installation process and building a guest around this disk image.
  • --os-type and --os-variant optimize the guest configuration for a specific operating system. While they're not required, specifying these options is highly recommended, as it can significantly increase performance by specifying virtio, among other guest tweaks.
  • --noautoconsole indicates not to try to connect to the guest console automatically.

To find out the exact version of the supported operating system, use the osinfo-query command:

$ sudo osinfo-query os | grep -i rhel

In these two steps, you customized and created your VM for your study lab. The next step is to access it to start the tests and your studies.

[ Get more insight about creating a lab environment in Building a home lab: Sysadmin after dark. ]

Access the VM

To access your VM, you need to discover the IP address KVM assigned it. Use the virsh command to display it:

$ sudo virsh domifaddr vmlab01

The virsh program is the primary interface for managing virsh guest domains. The domifaddr option gets a list of running domain interfaces and their IP and MAC addresses.

Once you've confirmed the VM's IP address, and because you have customized access to it through an SSH key, you can use it as an environment isolated from your day-to-day work.

Summary

The average time for setup, with the selected image downloaded, is about five minutes. Here are the steps in an abbreviated form:

1. Customize

$ sudo virt-customize -a /var/lib/libvirt/images/imagetest1.qcow2 --hostname vm01.test.lab --root-password password:rootpw --ssh-inject 'root:file:labkey.pub' --uninstall cloud-init --selinux-relabel

2. Import and install

$ sudo virt-install --name vm01 --memory 1024 --vcpus 1 --disk /var/lib/libvirt/images/image.qcow2 --import --os-type linux --os-variant generic --noautoconsole

3. Access

$ sudo virsh domifaddr vmlab01

$ ssh -i labkey root@192.168.122.76

4. Test

That's it

With this simple recipe, you can explore new technologies such as containers, Kubernetes, functional testing, and new versions of operating systems without affecting the work data on your laptop.

I hope this helps you with your continuous study.

Check out these related articles on Enable Sysadmin

Topics:   Testing   DevOps   Virtualization  
Author’s photo

Alex Callejas

Alex Callejas is a Services Content Architect of Red Hat, based in Mexico City and an Enable Sysadmin contributor. With more than 20 years of experience as Sysadmin, he has strong expertise on infrastructure hardening and automation. More about me

On Demand: Red Hat Summit 2021 Virtual Experience

Relive our April event with demos, keynotes, and technical sessions from
experts, all available on demand.

Related Content

OUR BEST CONTENT, DELIVERED TO YOUR INBOX