How to Install and Manage Virtual Machines and Containers
Virtualization and containers are hot topics in today’s IT industry. In this article we will list the necessary tools to manage and configure both in Linux systems.
For many decades, virtualization has helped IT professionals to reduce operational costs and increase energy savings. A virtual machine (or VM for short) is an emulated computer system that runs on top of another system known as host.
VMs have limited access to the host’s hardware resources (CPU, memory, storage, network interfaces, USB devices, and so forth). The operating system running on the virtual machine is often referred to as the guest operating system.
Before we proceed, we need to check if the virtualization extensions are enabled on our CPU(s). To do that, use the following command, where vmx and svm are the virtualization flags on Intel and AMD processors, respectively:
# grep --color -E 'vmx|svm' /proc/cpuinfo
No output means the extensions are either not available or not enabled in the BIOS. While you may continue without them, performance will be negatively impacted.
Install Virtualization Tools in Linux
To begin, let’s install the necessary tools. In CentOS you will need the following packages:
# yum install qemu-kvm libvirt libvirt-client virt-install virt-viewer
whereas in Ubuntu:
$ sudo apt-get install qemu-kvm qemu virt-manager virt-viewer libvirt-bin libvirt-dev
Next, we will download a CentOS 7 minimal ISO file for later use:
# wget http://mirror.clarkson.edu/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1804.iso
At this point we are ready to create our first virtual machine with the following specifications:
- RAM: 512 MB (Note that the host must have at least 1024 MB)
- 1 virtual CPU
- 8 GB disk
- Name: centos7vm
# virt-install --name=centos7vm --ram=1024 --vcpus=1 --cdrom=/home/user/CentOS-7-x86_64-Minimal-1804.iso --os-type=linux --os-variant=rhel7 --network type=direct,source=eth0 --disk path=/var/lib/libvirt/images/centos7vm.dsk,size=8
Depending on the computing resources available on the host, the above command may take some time to bring up the virtualization viewer. This tool will enable you to perform the installation as if you were doing it on a bare metal machine.
How to Manage Virtual Machines in Linux
After you have created a virtual machine, here are some commands you can use to manage it:
List all VMs:
# virsh --list all
Get info about a VM (centos7vm in this case):
# virsh dominfo centos7vm
Edit the settings of centos7vm in your default text editor:
# virsh edit centos7vm
Enable or disable autostart to have the virtual machine boot (or not) when the host does:
# virsh autostart centos7vm # virsh autostart --disable centos7vm
# virsh shutdown centos7vm
Once it is stopped, you can clone it into a new virtual machine called centos7vm2:
# virt-clone --original centos7vm --auto-clone --name centos7vm2
And that’s it. From this point on, you may want to refer to the virt-install, virsh, and virt-clone man pages for further info.