Build a lab in five minutes with three simple commands
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
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:
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 \
Let's examine these options in more detail:
virt-customizemodifies the guest or disk image in place. The guest must be shut down. You do not need to run
virt-customizeas root. In this case, you must use
sudobecause you do not have access rights to the path where the image is hosted.
-aindicates the path of the disk to be customized.
--hostnamesets the VM hostname.
--root-passwordsets the new root password, in this case: rootpw.
--ssh-injectallows you to inject an SSH key to some user. In this case, that's the labkey to root.
--uninstallallows 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
$ sudo virt-install \
--name vm01 \
--memory 1024 \
--vcpus 1 \
--disk /var/lib/libvirt/images/image.qcow2 \
--os-type linux --os-variant generic \
Here are details about the parameters above:
virt-installis a command-line tool for creating new KVM, Xen, or Linux container guests using the
libvirthypervisor management library.
--namesets 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.
--memoryindicates the memory to allocate for the guest in MiB.
--vcpusindicates the number of virtual CPUs to configure for the guest.
--diskspecifies what media to use as storage for the guest.
--importindicates 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-variantoptimize 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.
--noautoconsoleindicates not to try to connect to the guest console automatically.
To find out the exact version of the supported operating system, use the
$ 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
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.
The average time for setup, with the selected image downloaded, is about five minutes. Here are the steps in an abbreviated form:
$ 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
$ sudo virsh domifaddr vmlab01
$ ssh -i labkey email@example.com
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.