Virtual Machines - aryanjoshi0823/5143-Operating-System GitHub Wiki

Virtual Machines (VMs)

A Virtual Machine (VM) is a software abstraction that emulates physical hardware, enabling multiple operating systems to run independently on a single physical machine. Virtualization creates isolated environments using a Virtual Machine Manager (VMM) or Hypervisor, allowing better resource utilization and flexibility.


How Virtual Machines Work

  1. Host Hardware: The physical resources, such as CPU, RAM, and storage, form the foundation for running virtual machines.
  2. Hypervisor:
    • The VMM abstracts hardware resources.
    • It provides virtual hardware for each guest OS, including virtual CPUs (VCPUs), memory, network interfaces, and storage.
  3. Guest Operating System:
    • Runs within the virtual environment created by the hypervisor.
    • Believes it has exclusive access to the hardware.
  4. Execution Process:
    • User Mode: Standard application execution occurs natively.
    • Kernel Mode: Privileged operations are handled by the hypervisor via trap-and-emulate or binary translation.
  5. Hardware Virtualization:
    • Hardware features like VT-x (Intel) and AMD-V enhance performance by reducing overhead and enabling direct access to physical hardware resources.
  6. Resource Sharing:
    • The hypervisor schedules CPU cycles, allocates memory dynamically, and manages I/O to ensure optimal performance for all running VMs.
  7. Isolation:
    • Each VM is sandboxed, preventing interference between guest systems and the host.

Virtualization Techniques

Virtualization relies on several core techniques to provide isolation, performance, and compatibility:

1. Trap-and-Emulate

  • Guest systems operate in a virtual user or kernel mode, but privileged operations trap to the hypervisor for handling.
  • Process:
    1. Guest executes instructions in user mode.
    2. Privileged instructions (e.g., system calls) generate traps.
    3. Hypervisor emulates the operation and returns control.

2. Binary Translation

  • Rewrites certain instructions dynamically to enable virtualization on CPUs without full virtualization support.
  • Process:
    1. VMM examines guest instructions before execution.
    2. Replaces "special" instructions with compatible ones.
    3. Optimized for repeated execution using caching.
  • Early x86 CPUs (e.g., VMware used this for legacy support).
  • Significant improvements with modern optimizations like caching.

3. Nested Page Tables (NPT)

  • Manages memory mapping for guest systems efficiently.
  • Process:
    1. Guest maintains its page tables (virtual to physical mapping).
    2. VMM maintains nested page tables (NPT) to translate guest mappings into host physical memory.
    3. CPU uses these NPTs to handle memory access transparently.

4. Hardware-Assisted Virtualization

  • Modern CPUs provide specific instructions and modes to simplify and enhance virtualization.
  • Key Features:
    • Intel VT-x and AMD-V add guest and host modes to CPU.
    • Reduces reliance on binary translation and trap-emulate.
    • Supports virtualized DMA, interrupts, and nested page tables.

Types of Hypervisors

Hypervisors manage the creation and execution of virtual machines. They come in several forms based on their architecture and functionality:

1. Type 0: Hardware-Based Hypervisors

  • Implemented directly in firmware with minimal software involvement.
  • Characteristics:
    • Dedicated hardware resources for each VM.
    • Limited flexibility due to reliance on physical hardware.
  • Examples:
    • IBM LPARs.
    • Oracle LDOMs.

2. Type 1: Bare-Metal Hypervisors

  • Runs directly on the host hardware, managing VMs as the primary software layer.
  • Characteristics:
    • Acts as a minimal operating system.
    • Provides advanced features like live migration, cloning, and snapshots.
  • Examples:
    • VMware ESXi.
    • Citrix XenServer.
    • Microsoft Hyper-V.

3. Type 2: Hosted Hypervisors

  • Runs as an application on a standard operating system.
  • Characteristics:
    • Leverages the host OS for device management and I/O.
    • Easier to install and use.
  • Examples:
    • VMware Workstation.
    • Oracle VirtualBox.
    • Parallels Desktop.

4. Paravirtualization

  • Guests are aware they are virtualized and interact with the hypervisor for optimized performance.
  • Characteristics:
    • Modified guest OS uses hypervisor services.
    • Reduces overhead for privileged operations.
  • Examples:
    • Xen.

5. Programming Environment Virtualization

  • Virtualizes a programming runtime instead of hardware.
  • Example: Java Virtual Machine (JVM) for running Java applications.
  • Advantages:
    • Platform independence.
    • Simplified deployment for specific applications.

6. Emulation

  • Emulates an entirely different hardware architecture.
  • Characteristics:
    • Guest and host architectures can differ.
    • Useful for legacy applications.
  • Examples:
    • Gaming console emulators.
  • Challenges:
    • Significant performance trade-offs.

7. Application Containment

  • Provides virtualization-like features by isolating applications.
  • Examples:
    • Oracle Solaris Zones.
    • BSD Jails.
  • Advantages:
    • Lightweight alternative to full virtualization.