Last modified: June 11, 2024

This article is written in: 🇺🇸

Virtual Machines

Virtualization is the method of creating software-based representations of computing resources such as servers, storage devices, networks, or even an operating system. Virtual machines (VMs) are software emulations of physical computers that allow for the execution of programs as if they were being run on an actual computer.

VMs empower multiple operating systems to operate in parallel on a single physical host, ensuring a level of isolation between the various applications and services. Their applications are diverse, ranging from cloud computing, data centers, and for software testing and development purposes.

Types of Virtualization

There are numerous types of virtualization, with the following being particularly prevalent:

  1. Hardware-Level Virtualization: This form of virtualization emulates the computer's hardware, enabling a different operating system to operate as though it's running on its own machine.

  2. Operating System-Level Virtualization: This allows for multiple instances of an operating system to run on a single physical server. Each instance, or 'container', runs and operates as an isolated standalone server.

  3. Application Virtualization: This type isolates applications from the underlying operating system and from other applications to increase compatibility and portability.

A commonly used virtualization type is KVM (Kernel-based Virtual Machine). KVM allows for the transformation of a Linux kernel into a hypervisor, enabling a single physical server to run multiple independent virtual machines.

Key Virtualization Technologies

The creation of VMs is commonly achieved using one of the two main types of virtualization technologies:

  1. Full Virtualization: This technology duplicates an entire hardware environment, including the CPU, memory, and other hardware components. It enables the guest operating system to run unmodified on the virtual hardware. VMware Workstation and Oracle VM VirtualBox are among the solutions that utilize full virtualization.

+-----------------------------------------+
|        Full Virtualization              |
+-----------------------------------------+
| +-----------+ +-----------+ +---------+ |
| |   CPU     | |   Memory  | | Others  | |
| | Emulation | | Emulation | | Emulate | |
| +-----------+ +-----------+ +---------+ |
|         |           |            |      |
| +-------------------------------------+ |
| |   Virtual Hardware Environment      | |
| +-------------------------------------+ |
|                   |                     |
| +-------------------------------------+ |
| | Guest OS (Unmodified) Running on    | |
| | Virtual Hardware                    | |
| +-------------------------------------+ |
| Examples: VMware Workstation, Oracle VM |
| VirtualBox                              |
+-----------------------------------------+

  1. Paravirtualization: Rather than emulating an entire hardware system, paravirtualization creates a similar, albeit not identical, software interface to the underlying hardware. To function with the paravirtualization interface, guest operating systems must be modified. Generally, this technology offers superior performance than full virtualization. The Xen hypervisor is an example of a solution employing paravirtualization.

+-------------------------------------------+
|        Paravirtualization                 |
+-------------------------------------------+
| +-----------+ +-----------+ +-----------+ |
| |   CPU     | |   Memory  | | Others    | |
| | Interface | | Interface | | Interface | |
| +-----------+ +-----------+ +-----------+ |
|         |           |            |        |
| +---------------------------------------+ |
| |   Similar Software Interface to       | |
| |   Underlying Hardware                 | |
| +---------------------------------------+ |
|                   |                       |
| +---------------------------------------+ |
| | Modified Guest OS for                 | |
| | Paravirtualization Interface          | |
| +---------------------------------------+ |
| Example: Xen Hypervisor                   |
+-------------------------------------------+

Understanding Hypervisors

The hypervisor, also known as a virtual machine monitor, is a software layer tasked with creating, running, and managing VMs. There are two types of hypervisors:

  1. Type 1 (Bare-Metal) Hypervisors: These hypervisors operate directly on the host's hardware, providing enhanced performance and security compared to Type 2 hypervisors. Examples include VMware ESXi, Microsoft Hyper-V, and Xen.

+--------------------------------------------------+
|        Type 1 (Bare-Metal) Hypervisors           |
+--------------------------------------------------+
| +----------------------------------------------+ |
| |                   Hardware                   | |
| +----------------------------------------------+ |
|                       |                          |
| +----------------------------------------------+ |
| |        Hypervisor Layer (Direct Control)     | |
| |         Examples: VMware ESXi, Microsoft     | |
| |         Hyper-V, Xen                         | |
| +----------------------------------------------+ |
|                       |                          |
| +----------------------------------------------+ |
| |       Virtual Machines and Guest OSes        | |
| +----------------------------------------------+ |
| Enhanced Performance and Security               |
+--------------------------------------------------+

  1. Type 2 (Hosted) Hypervisors: These hypervisors run as applications within a host operating system, offering easier installation and usability. However, they generally have reduced performance and security compared to Type 1 hypervisors. VMware Workstation, Oracle VM VirtualBox, and Parallels Desktop are examples of Type 2 hypervisors.

+--------------------------------------------------+
|        Type 2 (Hosted) Hypervisors               |
+--------------------------------------------------+
| +----------------------------------------------+ |
| |                   Hardware                   | |
| +----------------------------------------------+ |
|                       |                          |
| +----------------------------------------------+ |
| |           Host Operating System              | |
| +----------------------------------------------+ |
|                       |                          |
| +----------------------------------------------+ |
| |     Hypervisor as an Application Layer       | |
| |     Examples: VMware Workstation, Oracle VM  | |
| |     VirtualBox, Parallels Desktop            | |
| +----------------------------------------------+ |
|                       |                          |
| +----------------------------------------------+ |
| |       Virtual Machines and Guest OSes        | |
| +----------------------------------------------+ |
| Easier Installation, Usability, with Generally   |
| Reduced Performance and Security                 |
+--------------------------------------------------+

Networking Methods

In the context of virtualization, networking is a critical aspect, enabling communication between virtual machines (VMs) on the same host, between VMs and external services, and from external services to VMs. The design of this network can impact system performance, security, and functionality. Here's an overview of the key networking options in virtual environments:

Network Address Translation (NAT)

NAT-based networking enables VMs to communicate with external services via the host machine. The host acts as a gateway, translating the VMs' private IP addresses into its own public IP address for outbound traffic. For inbound traffic, the host translates its own IP address into the corresponding private IP of the VMs.

However, this method can pose challenges for external services trying to access guest VMs. It often requires the configuration of complex port forwarding or IP masquerading rules to enable external access to the VMs. Despite these challenges, NAT offers an added layer of protection as VMs are not directly exposed to the network.

+---------------------------------+
|           External Network      |
|                                 |
|     +--------+                  |
|     | Router |                  |
|     +--------+                  |
|         ^                       |
|         |                       |
|         | Public IP Address     |
|         v                       |
+--------+------------------------+
|          Host Machine           |
|       (Acts as Gateway)         |
|                                 |
|  +---------------------------+  |
|  |  NAT Network              |  |
|  |                           |  |
|  |  +-------------------+    |  |
|  |  | VM1               |    |  |
|  |  | Private IP        |    |  |
|  |  +-------------------+    |  |
|  |  +-------------------+    |  |
|  |  | VM2               |    |  |
|  |  | Private IP        |    |  |
|  |  +-------------------+    |  |
|  |                           |  |
|  |  (Port Forwarding for     |  |
|  |   inbound traffic)        |  |
|  +---------------------------+  |
+---------------------------------+

Bridged Networking

In a bridged network setup, the virtual network interface of the VM is connected to the physical network of the host. Essentially, the VM is an equal participant in the network, much like a physical machine. The VM is assigned an IP address by a Dynamic Host Configuration Protocol (DHCP) server on the network or manually set with a static IP.

This setup enables any device on the Local Area Network (LAN) to access the VM, provided network permissions and firewall rules allow it. While bridged networking enhances connectivity and simplifies the network setup, it can expose the VMs to potential security risks.

+------------------+              +------------------+               +------------------+
|                  |              |                  |               |                  |
|    Physical      |              |    Virtual       |               |    Virtual       |
|    Network       |              |    Machine (VM1) |               |    Machine (VM2) |
|                  |              |                  |               |                  |
|    +--------+    |     +------> |    +--------+    |    +------>   |    +--------+    |
|    | Router |--------|          |    |  vNIC1 |    |               |    |  vNIC2 |    |
|    +--------+    |     <------+ |    +--------+    |    <------+   |    +--------+    |
+------------------+              +------------------+               +------------------+
        ^                                   ^                               ^
        |                                   |                               |
        |          +----------------+       |                               |
        +----------| Host Machine   |-------+                               |
                   | with Bridged   |---------------------------------------+
                   | Networking     |
                   +----------------+

Host-Only Networking

The host-only network configuration creates a network connection between the VM and the host machine, isolating the VM from the broader network. This means external devices cannot directly access the VMs.

However, VMs can still access the internet if port forwarding is set up on the host. Port forwarding involves mapping a port from the host to a corresponding port on the guest VM. This setup is particularly useful in development or testing environments where network isolation is desirable for security reasons.

Each of these networking options provides a different level of accessibility, performance, and security, and can be chosen based on the specific requirements of the virtual environment.

+---------------------------------+
|           External Network      |
|                                 |
|     +--------+                  |
|     | Router |                  |
|     +--------+                  |
+--------^------------------------+
         | (No Direct Connection
         |  to the VM)
+--------+--------------------------+
|          Host Machine             |
|                                   |
|    +--------------------------+   |
|    |  Host-Only Network       |   |
|    |                          |   |
|    |  +-------------------+   |   |
|    |  | Virtual Machine   |   |   |
|    |  |       (VM)        |   |   |
|    |  +-------------------+   |   |
|    |                          |   |
|    |  (Isolated from External |   |
|    |   Network but can        |   |
|    |   access internet via    |   |
|    |   Port Forwarding)       |   |
|    +--------------------------+   |
+-----------------------------------+

Comparison

Feature Bridged Networking Host-Only Networking NAT Networking
Network Accessibility Full network access No external access Access through host
Isolation from LAN None Complete Complete
External Devices Access Yes No Via port forwarding
IP Address Assignment DHCP/static from LAN Static or DHCP by host Private IP by host
Visibility in Network Visible like physical Only to host Only to host
Security Risk Higher (direct access) Lower (isolated) Moderate (controlled)
Complexity Simple Moderate Complex (port forwarding)
Use Case Production environments Testing/Development Controlled external access

Creating Virtual Machines

The creation of a virtual machine (VM) is a multi-step process that involves the following:

  1. Hypervisor Installation: The process begins by installing a hypervisor on the host system. The choice of hypervisor can depend on various factors such as performance needs, security requirements, and compatibility with the host and guest operating systems.

  2. New VM Creation: Once the hypervisor is installed and running, you create a new VM within the hypervisor's management interface.

  3. Resource Allocation: The next step involves allocating hardware resources, such as CPU, memory, disk space, and network interfaces, to the VM. It's crucial to strike a balance that ensures good performance without overtaxing the host system.

  4. Guest Operating System Installation: With the VM set up, you can now install the guest operating system. This process is very similar to installing an OS on a physical machine.

  5. Hypervisor Tools Installation: After the OS is installed, it's usually a good idea to install any drivers or tools provided by the hypervisor. These tools improve the performance of the VM and enable better integration with the host system.

Managing Virtual Machines

Hypervisors come with numerous tools and features for managing VMs, such as:

Benefits of Virtual Machines

Virtual machines offer a multitude of advantages, including:

VirtualBox

VirtualBox is a popular open-source tool for creating and managing virtual machines (VMs). It's a Type 2 hypervisor that runs as an application within the host operating system. It supports a wide range of guest operating systems, including Windows, Linux, and macOS.

Networking in VirtualBox

VirtualBox supports several types of network configurations:

You can retrieve the IP address of a running VM using the following command:

VBoxManage guestproperty get "VM Name" "/VirtualBox/GuestInfo/Net/0/V4/IP"

Creating, Configuring, Starting, and Stopping VMs with Commands

VirtualBox comes with a powerful command-line tool called VBoxManage. Here's how you can use it to manage VMs:

  1. Create a new VM:

VBoxManage createvm --name "My VM" --register

  1. Allocate memory and create a virtual hard disk:

VBoxManage modifyvm "My VM" --memory 2048 --acpi on --boot1 dvd
VBoxManage createhd --filename "MyVM.vdi" --size 10000

  1. Set up a virtual DVD drive and install an OS:

VBoxManage storagectl "My VM" --name "IDE Controller" --add ide
VBoxManage storageattach "My VM" --storagectl "IDE Controller" --port 0 --device 0 --type dvddrive --medium /path/to/OS.iso

  1. Start the VM:

VBoxManage startvm "My VM"

  1. Stop the VM:

VBoxManage controlvm "My VM" poweroff

  1. Set network mode to bridged:

VBoxManage modifyvm "My VM" --nic1 bridged --bridgeadapter1 eth0

  1. Enable host-only networking:

VBoxManage modifyvm "My VM" --nic1 hostonly

  1. Change allocated memory:

VBoxManage modifyvm "My VM" --memory 4096

  1. Change the number of CPU cores:

VBoxManage modifyvm "My VM" --cpus 2

Replace "My VM" with your VM's name and adjust the settings as needed.

VMware

VMware is a leading provider of virtualization software. The company offers various products, such as VMware Workstation and VMware ESXi. The former is a Type 2 hypervisor for running VMs on a desktop or laptop, while the latter is a Type 1, bare-metal hypervisor designed for servers.

Networking in VMware

VMware products provide several networking options:

You can find the IP address within the VM by running the following command in the VM's terminal:

ip addr show

or for Windows VMs:

ipconfig

Creating, Configuring, Starting, and Stopping VMs

VMware provides a graphical user interface for creating and managing VMs. However, for VMware ESXi, users can also use the vSphere Command-Line Interface (vSphere CLI).

  1. Creating a VM in vSphere:

Please note that you'll need to use the vSphere Client (a web interface) to create a new VM.

  1. Powering on a VM:

vim-cmd vmsvc/power.on <vmid>

  1. Powering off a VM:

vim-cmd vmsvc/power.off <vmid>

Replace <vmid> with the ID of your VM.

Configuration of VMs in VMware ESXi can be done through the vSphere Client. In VMware Workstation, users can directly modify VM settings through its GUI. For more advanced or automated tasks, VMware provides APIs and CLIs such as PowerCLI.

  1. Change allocated memory:

You'll need to power off the VM first. After that, you can go to the VM settings in the vSphere Client or VMware Workstation and modify the memory allocation.

  1. Change the number of CPU cores:

This is also done in the VM settings after powering off the VM. You can modify the number of cores per CPU and the number of CPUs.

  1. Set up networking:

You can change the network configuration in the VM settings. You can choose between bridged, NAT, and host-only networking, or set up a custom network.

KVM

Kernel-based Virtual Machine (KVM) is a full virtualization solution for Linux systems. It's a Type 1 hypervisor integrated into the Linux kernel. KVM requires a processor with hardware virtualization extensions, such as Intel VT or AMD-V.

Networking in KVM

KVM networking is highly customizable. Some common configurations include:

If you're using the default networking mode (NAT), you can use the virsh domifaddr command to find the IP address of a VM:

virsh domifaddr "VM Name"

Creating, Configuring, Starting, and Stopping VMs with Commands

KVM operations are usually performed using the virsh command-line interface provided by the libvirt library.

  1. Create a new VM:

virt-install --name "MyVM" --memory 2048 --vcpus 2 --disk path=/path/to/disk.img,size=20 --cdrom /path/to/os.iso

  1. Start the VM:

virsh start MyVM

  1. Stop the VM:

virsh destroy MyVM

  1. Set network mode to bridged:

virsh attach-interface --domain MyVM --type bridge --source br0 --config --live

  1. Change allocated memory:

virsh setmem MyVM 4096 --config --live

  1. Change the number of CPU cores:

virsh setvcpus MyVM 4 --config --live

Again, replace "MyVM" with your VM's name and adjust the settings as needed.

Challenges

  1. Explain the concept of virtualization in your own words. What does it mean to virtualize a piece of hardware or a system?
  2. Discuss different types of virtualization, such as hardware-level virtualization, operating system-level virtualization, and application virtualization. What are the differences between these types, and what are some examples of each?
  3. What are some potential benefits of using virtualization in an IT environment? Consider aspects such as resource utilization, isolation, flexibility, and scalability.
  4. What is a virtual machine? How does it differ from a physical machine, and what roles does it commonly play in IT infrastructure?
  5. Discuss different networking configurations for virtual machines, such as NAT, bridged, and host-only networking. How do these configurations affect the VM's ability to communicate with other devices and access the internet?
  6. Research VirtualBox, VMware, and KVM. What are these tools, and what are they commonly used for? What are some unique features or advantages of each?
  7. Using VirtualBox, VMware, and KVM, create a virtual machine and determine its IP address. What steps did you take to find the IP address, and how might the networking configuration of the VM affect this process?
  8. Compare and contrast NAT networking and bridged networking for virtual machines. How do they handle communication with the local network and the internet? What might be some reasons to choose one over the other?
  9. Using the command-line tools for VirtualBox, VMware, and KVM, create a VM, start it, stop it, and modify its configuration. Document the commands you used and any challenges you encountered.
  10. Explain the differences between Type 1 (bare-metal) and Type 2 (hosted) hypervisors. Discuss the potential advantages and disadvantages of each type, and give examples of situations where one might be preferable to the other.

Table of Contents

  1. Types of Virtualization
  2. Key Virtualization Technologies
  3. Understanding Hypervisors
  4. Networking Methods
    1. Network Address Translation (NAT)
    2. Bridged Networking
    3. Host-Only Networking
    4. Comparison
  5. Creating Virtual Machines
  6. Managing Virtual Machines
  7. Benefits of Virtual Machines
  8. VirtualBox
    1. Networking in VirtualBox
    2. Creating, Configuring, Starting, and Stopping VMs with Commands
  9. VMware
    1. Networking in VMware
    2. Creating, Configuring, Starting, and Stopping VMs
  10. KVM
    1. Networking in KVM
    2. Creating, Configuring, Starting, and Stopping VMs with Commands
  11. Challenges