Skip to main content

Command Palette

Search for a command to run...

Virtual Machines and Emulators

Updated
7 min read
A

I am a Student, who finds beauty in simple things. I like to teach sometimes.

The ability to run an operating system (OS) within another OS, or to mimic different hardware architectures, is a cornerstone of modern computing. This is achieved through virtualization and emulation.

Virtualization Explained

Virtualization is a technology that creates a simulated, or virtual, computing environment distinct from the underlying physical hardware. This virtual environment, known as a virtual machine (VM), functions as a self-contained computer with its own virtualized hardware components, such as a CPU, memory, storage, and network interface. The software layer that creates and manages these VMs is called a hypervisor.

There are two main types of hypervisors:

  • Type 1 (Bare-Metal) Hypervisors: These run directly on the host's hardware to control the hardware and manage guest operating systems. Examples include VMware ESXi, Microsoft Hyper-V, and Xen.1 Because they have direct access to the underlying hardware, they are generally more efficient and performant.

  • Type 2 (Hosted) Hypervisors: These run as a software application on top of a conventional operating system. The host OS provides I/O device support and memory management. Examples include Oracle VM VirtualBox, VMware Workstation, and QEMU (when used without KVM). They are often easier to set up and manage for desktop users.

Virtualization allows for multiple isolated VMs to run concurrently on a single physical machine. Each VM can run a different operating system (e.g., running a Linux distribution on a Windows host, or vice-versa). This isolation is a key characteristic, as software running inside a VM cannot directly affect the host OS or other VMs.

The benefits of virtualization include:

  • Server Consolidation: Multiple underutilized physical servers can be consolidated into fewer servers, reducing hardware costs, power consumption, and physical space requirements.

  • Resource Optimization: Hardware resources of the physical host can be dynamically allocated and shared among VMs as needed, leading to better utilization.

  • Isolation and Security: VMs provide a sandboxed environment. If one VM is compromised by malware, it typically does not affect the host OS or other VMs. This is beneficial for testing potentially malicious software or running applications with different security requirements.

  • Disaster Recovery and Business Continuity: VMs can be easily backed up, migrated, and replicated to other physical hosts, facilitating quicker recovery in case of hardware failure.

  • Testing and Development: Developers and testers can create multiple VMs with different operating systems and configurations to test software compatibility and performance across various environments without needing multiple physical machines.

  • Legacy Application Support: Older applications that may not be compatible with modern operating systems can be run within a VM that hosts an older, compatible OS.

Implementing Virtualization: VirtualBox and QEMU

Two popular open-source solutions for creating and managing virtual machines are VirtualBox and QEMU.

Oracle VM VirtualBox

VirtualBox is a Type 2 hypervisor for x86 and AMD64/Intel64 hardware. It installs as an application on the host operating system and allows users to create and run multiple guest VMs, each with its own OS.

Key technical aspects of VirtualBox include:

  • Guest Additions: These are software packages that can be installed inside guest VMs to improve performance and usability. They provide features like better video resolution, mouse pointer integration, shared folders between host and guest, and seamless window mode.

  • Hardware Virtualization Support: VirtualBox leverages hardware virtualization extensions like Intel VT-x and AMD-V to improve the performance of guest VMs. These CPU features allow the VM to execute instructions directly on the host CPU in a protected mode, reducing the overhead of software-based virtualization.

  • Snapshotting: Users can save the current state of a VM, allowing them to revert to that state later. This is useful for testing software or configurations without risking the stability of the VM.

  • Virtual Disk Formats: VirtualBox supports several virtual disk formats, including its native VDI (Virtual Disk Image), VMDK (Virtual Machine Disk) used by VMware, and VHD (Virtual Hard Disk) used by Microsoft.

  • Network Configuration: VirtualBox offers various networking modes for VMs, such as NAT (Network Address Translation), Bridged networking, Internal networking, and Host-only networking, allowing flexible network configurations for different use cases.

QEMU (Quick Emulator)

QEMU is a versatile open-source machine emulator and virtualizer. It can operate in two primary modes:

  • Full System Emulation: In this mode, QEMU emulates a complete computer system, including a processor and various peripherals. It can emulate a wide range of CPU architectures (x86, ARM, MIPS, PowerPC, SPARC, etc.) on a different host architecture. For instance, you could run an ARM-based operating system on an x86 host. This emulation process, while flexible, often incurs significant performance overhead because every guest instruction must be translated by QEMU.

  • User-mode Emulation: Here, QEMU can run programs compiled for one CPU architecture on another CPU architecture, provided the programs are compiled for the same operating system (or a compatible one).

When used as a virtualizer on systems with hardware virtualization extensions (like Intel VT-x or AMD-V), QEMU can integrate with the Kernel-based Virtual Machine (KVM) module in Linux. This combination, often referred to as QEMU-KVM, allows QEMU to run guest code directly on the host CPU, achieving near-native performance for VMs whose architecture matches the host's. In this scenario, QEMU handles the emulation of I/O hardware (like disk controllers, network cards, etc.), while KVM manages the CPU and memory virtualization.

Key technical aspects of QEMU:

  • Broad Architecture Support: QEMU's strength lies in its ability to emulate a vast array of CPU architectures.

  • KVM Integration: For x86-on-x86 virtualization, KVM integration provides significant performance benefits by leveraging hardware virtualization.

  • Live Migration: QEMU supports migrating a running VM from one physical host to another with minimal downtime, a crucial feature for high-availability environments.

  • Multiple Disk Image Formats: QEMU supports various disk image formats, including its native qcow2 (QEMU Copy On Write 2), raw images, VMDK, VDI, VHDX, and others. The qcow2 format supports features like snapshots, compression, and encryption.

  • Device Emulation: QEMU emulates a wide range of hardware devices, including network interface controllers (NICs), storage controllers (IDE, SCSI, SATA, VirtIO), USB controllers, and graphics cards. VirtIO devices are paravirtualized devices designed for high performance in virtualized environments.

Running an Operating System Inside Another Operating System

The core concept of running an OS inside another OS relies on the hypervisor (or emulator) creating a virtual hardware platform that the guest OS can interact with. The guest OS is unaware that it is running on virtualized hardware; it behaves as if it has exclusive control over a physical machine.

The process generally involves:

  1. Hypervisor Installation: The hypervisor software (e.g., VirtualBox, or QEMU with KVM) is installed on the host operating system or directly on the hardware.

  2. VM Creation and Configuration: A new virtual machine is defined. This includes allocating virtual resources like CPU cores, RAM, disk space, and network interfaces. An installation medium for the guest OS (typically an ISO image) is specified.

  3. Guest OS Installation: The VM is powered on. The hypervisor directs the VM to boot from the specified installation medium. The user then proceeds with the installation of the guest OS, just as they would on a physical machine. The guest OS installer detects the virtualized hardware provided by the hypervisor.

  4. Guest OS Operation: Once installed, the guest OS boots up and runs within the VM. The hypervisor manages the execution of the guest OS's instructions, either by translating them (in full emulation mode) or by passing them directly to the host CPU (when hardware virtualization is used). It also handles requests from the guest OS for hardware resources, translating them into requests to the host's physical hardware or emulating the hardware behavior.

  5. Interaction: The user interacts with the guest OS through a console window provided by the hypervisor or via remote desktop protocols. Peripherals like keyboards, mice, and USB devices can be passed through from the host to the guest VM.

In summary, virtualization and emulation technologies provide powerful capabilities for running diverse operating systems and software environments in isolation on a single physical machine. Tools like VirtualBox offer user-friendly interfaces for desktop virtualization, while QEMU provides extensive emulation capabilities and high performance when combined with KVM for virtualization, making them indispensable tools for developers, system administrators, and researchers.

More from this blog

Aman Pathak

58 posts

Things I would speak if the person in front of me is me