Virtualization: Virtual Machines
What virtual machines are
Virtual machines are running on top of a hypervisor; they have each been configured with resources they can request access to. The hypervisor has abstracted these resources, as all guests share physical resources on the device on which the hypervisor resides. It is the hypervisor that manages the virtual machines. With the most common technique, full virtualization, the virtual machine’s operating system does not know it is not real; it believes it has direct access to the hardware and is unaware it is only being shown parts of the physical resource. All virtual machines on a hypervisor can run simultaneously and contain different operating systems.
A virtual machine is flexible, as its physical form is a set of files, configuration file, and virtual disk files. These can easily be modified; it is possible to back up a server by copying the virtual machine files. The configuration file contains information about resources that the virtual machine is made up of, like memory, CPU, storage, networking, and disk drives. Virtual machines also have a second physical form, which is created in memory when it is booted.
Central Processing Unit
The Central Processing Unit, CPU, execute requests sent from the host operating system or hypervisor. CPUs with more than one processor are called multicore CPUs, with each processor within being called a core. When the hypervisor abstracts CPU for the virtual machine, it is not giving it a particular CPU; the virtual machine can schedule slices of time on the available processors. To dedicate a CPU for a virtual machine would be contrary to one of the main reasons for virtualization, efficient allocation of resources by gathering multiple servers on one physical device.
When a virtual machine sends a request, the hypervisor halts it, and with full virtualization it gets translated, before scheduling time on the available processors on behalf of the virtual machine. The hypervisor ensures requests from all its guests are handled accordingly to utilize the CPU efficiently. The hypervisor has settings to prioritize certain guests, applications, or other workloads. The request wait in line in the cache before entering the CPU. If the virtual machine is configured with one virtual CPU, also called vCPU, it can only be scheduled for the capacity of a single CPU core. If it is configured with two vCPUs, there need to be two CPU cores available at the same time for the request to be processed. When the physical processor is done executing the request, the result is sent back to the virtual machine through the hypervisor and appropriate device driver. Hypervisors limit how many virtual CPUs are allowed to allocate for each guest; this varies depending on the hypervisor.
Hyper-threading can be used for optimizing the CPU processing by presenting two logical processors per each physical processor. Hypervisors can then schedule two threads of work on one physical CPU core. By improving the CPU scheduling, there will be an increase in the performance of the guests.
Random Access Memory (RAM) is the memory resources abstracted to the different guests on a physical host device. RAM is used to store data and programs at high speed for fast CPU access. An application’s performance depends on having access to memory, and there must be enough for the amount of data the application uses without compromising the performance. While in a shared virtualized environment, too much memory for each application is not an efficient use of resources. Both the hypervisor and guest operating system run their code in memory. The same does applications code when they are running in the virtual machine.
Virtualization makes use of memory efficiently through memory management techniques like page sharing, paging, ballooning, and memory swapping. When creating a virtual machine, there also needs to be memory allocated for the hypervisor and processes like memory mapping tables, where the hypervisor keeps track of the connection between all the virtual machines allocated memory and memory on the physical device.
When a virtual machine needs more memory, it can be claimed from other virtual machines that do not use all the memory allocated to them. The process of claiming is called memory ballooning and is possible when a balloon driver is installed on the virtual machines with which the hypervisor communicates. The hypervisor requests a claim for memory, while the guest operating system decides how much memory the virtual machine can give up. The balloon driver in the guest gets activated and virtually inflates, flushing out the memory pages and later deflates, as seen in figure 1. The hypervisor can then reclaim the memory for use. In Oracle VM VirtualBox, it is the VBoxManage that supports memory ballooning on 64-bit hosts. It is possible to resize the memory balloon of a virtual machine while it is running.
When new data or programs require space in RAM, when there is none, blocks of memory, also called pages, are moved to disks. Pages that have not been used recently are moved out of RAM. The memory blocks are copied to a file usually called Pagefile, which is illustrated in figure 1. The hypervisor manages memory and decides which pages are written to disks. If a virtual machine has less memory than what is needed for an application, paging will happen repeatedly, affecting the performance since disks work slower than RAM. In a virtual environment, paging should not be necessary if virtual machines are configured appropriately and continuously have their performance metrics measured.
Since the hypervisor manages memory for all guests, it can keep track of identical pages and use page sharing to only store one copy of duplicated data blocks. This can be done across all guests, which is illustrated in figure 1, as well as inside one virtual machine. If a shared page needs to be written to, the hypervisor will create a copy for the virtual machine that requests changes to the file and write those to the copy; this is called copy-on-write. For example, a company can benefit from page sharing when creating many identical virtual desktops.
The hypervisor creates swap space when a virtual machine is booted, and it is for storing memory blocks in case of paging. If the balloon driver can not free up enough memory and there is a contention, the final solution is memory swapping. When the hypervisor swap pages, they are written to physical disks. Memory swapping consumes more resources and will affect the performance of a virtual machine. To avoid it, the hypervisor will check if it is possible to compress files and move them to the compression cache instead.
Cloning can be used to prevent data loss in the event of failure by regularly copying the virtual machine files to another device. It can also be used to create virtual machines by copying the physical files of an existing virtual machine. The cloned virtual machine must go through an identity configuration before being deployed, giving it a unique system name and network address as there can not be machines with the same identity. For Windows machines, this can be done with the System Preparation tool, also called Sysprep, which resets machine-specific information, including the security identifier (SID).
Another way to create virtual machines is with templates, which are preconfigured virtual machines that can not be booted or edited. Templates can be preloaded with operating systems and applications. They are quick and easy to copy, but the copied virtual machine must be configured with its own unique system information in the same way as a clone. Creating a template is done by converting an existing virtual machine, with the template mode enabled, into a template. In figure 2 below, the box is ticked off and template mode is enabled for this virtual machine running on VMware Workstation Pro. Updating and patching a template will require reverting the template back to a virtual machine, as templates can not run. To prevent unintended changes, it is best practice for templates to be in a read-only format.
Snapshots can obtain a virtual machine and all of its data and configuration at a given time, including if the machine is powered on, off, or suspended; making it possible to revert the virtual machine back to an earlier state, removing all changes made since that snapshot was taken. Snapshots are useful in an environment with testing and developing, being able to easily go back when needed. A new disk is created every time a snapshot is taken. All new changes are written to the newly created disk since the original disk will remain unchanged. Snapshots are unsuitable as a backup solution as all the disks may affect the performance.
Comer, D. 2021. The Cloud Computing Book
Oracle: Memory Overcommitment
Portnoy, M. 2016. Virtualization Essentials. 2nd ed.