Virtual machine
A virtual machine (VM) is an emulation of a computer system. Virtual machines are based on computer architectures and provide the functionality of a physical computer. Their implementations may involve specialized hardware, software, or a combination of both.
The key to a virtual machine is the concept of virtualization, which allows a single set of physical computer hardware to be shared by multiple operating system instances. The software or firmware that creates and runs the virtual machines is called a hypervisor or Virtual Machine Monitor (VMM). The original operating system running directly on the hardware is called the host operating system, and the operating system running inside the VM is called the guest operating system.
Contents
Overview
Virtual machines provide an isolated environment, meaning that software running inside a VM does not interfere with the host operating system or other VMs. They share the host computer's resources, such as CPU time, RAM, disk space, and network interfaces, which are allocated by the hypervisor.
VMs abstract the underlying hardware, presenting a consistent virtual hardware environment to the guest OS, regardless of the physical hardware. This allows an operating system compiled for a specific architecture to run on different physical machines, as long as the hypervisor supports that architecture.
How it Works
A hypervisor manages the host computer's resources and allocates them to each VM. There are two main types:
- Type 1 (Native or Bare-Metal) Hypervisors: These run directly on the host hardware, controlling the hardware and managing guest VMs. Examples include VMware ESXi, Microsoft Hyper-V, and Xen.
- Type 2 (Hosted) Hypervisors: These run as a software layer on top of a conventional host operating system. The guest OS runs as a process on the host. Examples include VirtualBox, VMware Workstation, and QEMU (though QEMU can also act as a Type 1 with KVM).
The hypervisor is responsible for emulating virtual hardware for the guest OS (like a virtual CPU, virtual memory, virtual disk controllers, virtual network cards) and managing the execution of the guest OS's instructions on the physical CPU, often using hardware virtualization extensions (like Intel VT-x or AMD-V).
Uses
Virtual machines are widely used for various purposes:
- Server Consolidation: Running multiple server operating systems on a single physical server to save hardware costs and energy.
- Development and Testing: Providing isolated environments to test software on different operating systems and configurations without affecting the main system.
- Security and Isolation: Running potentially unsafe applications or visiting risky websites within a confined environment to protect the host system.
- Operating System Compatibility: Running software designed for a different operating system than the host.
- Snapshots and Rollbacks: Easily saving the state of a VM at a specific point in time and reverting to it later.
Setting up a VM on Linux (CLI Tutorial)
This tutorial demonstrates setting up a virtual machine using KVM, QEMU, and libvirt on a Debian-based Linux distribution via the command line. KVM (Kernel-based Virtual Machine) is a virtualization capability built into the Linux kernel, QEMU is a machine emulator and virtualizer, and libvirt is an API and management toolset for managing virtualization platforms.
Prerequisites:
- A Linux system (Debian, Ubuntu, etc.).
- Hardware virtualization support enabled in your computer's BIOS/UEFI (Intel VT-x or AMD-V).
- An internet connection to download packages.
Steps:
- Check Virtualization Support:
First, verify that your CPU supports hardware virtualization and that the necessary kernel modules are loaded.
egrep -c '(svm|vmx)' /proc/cpuinfo
If this command returns a number greater than 0, your CPU supports hardware virtualization.
kvm-ok
This command specifically checks KVM usability. If it says "KVM acceleration can be used", you're good to go. If not, ensure virtualization is enabled in your BIOS/UEFI and the KVM kernel modules are loaded.
- Install Necessary Packages:
Install QEMU, libvirt, and related tools.
sudo apt update sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virtinst
- `qemu-kvm`: Provides the QEMU emulator with KVM support.
- `libvirt-daemon-system`: The libvirt service that runs in the background.
- `libvirt-clients`: Command-line tools like `virsh` to interact with libvirt.
- `bridge-utils`: Tools for configuring network bridges, often used for VM networking.
- `virtinst`: Provides the `virt-install` command for easily creating VMs.
- Add Your User to the Libvirt Group:
To manage VMs without `sudo` constantly (after initial setup), add your user to the `libvirt` group.
sudo usermod -aG libvirt $USER
You will need to log out and log back in (or reboot) for this change to take effect.
- Download an Operating System Image:
Obtain an installation image (like an ISO file) for the guest operating system you want to install. For example, download an Ubuntu server ISO.
wget https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64.tar.gz
(Note: Using a cloud image here for example, standard ISOs work with `virt-install` too). You might need to extract disk images from cloud images or use standard installer ISOs (e.g., `.iso`). Let's proceed assuming you have either an `.iso` or a `.qcow2` image.
- Create a Virtual Disk Image:
Create a file that will serve as the hard drive for your VM. The `.qcow2` format is flexible (supports snapshots, can be smaller than the virtual size initially).
qemu-img create -f qcow2 /var/lib/libvirt/images/your-vm-disk.qcow2 20G
< This creates a 20GB virtual disk image file. Change the path and size as needed. `/var/lib/libvirt/images/` is a common location managed by libvirt, but requires sudo or proper permissions if your user isn't in `libvirt`. A path in your home directory is easier if not using libvirt's default pool.
qemu-img create -f qcow2 ~/your-vm-disk.qcow2 20G
- Create and Install the Virtual Machine:
Use the `virt-install` command to create and start the VM installation process. This command has many options; here's a basic example using an ISO:
virt-install \ --name YourVMName \ --ram 2048 \ --vcpus 1 \ --disk path=/var/lib/libvirt/images/your-vm-disk.qcow2,bus=virtio \ --cdrom /path/to/your/installer.iso \ --network network=default \ --graphics spice \ --noautoconsole
- `--name`: Name for your VM.
- `--ram`: Amount of RAM in MB.
- `--vcpus`: Number of virtual CPUs.
- `--disk path=...,bus=virtio`: Specifies the virtual disk image and the high-performance virtio bus driver.
- `--cdrom`: Path to the OS installer ISO. Use `--location` instead of `--cdrom` for network-based installs or using extracted disk images (like from cloud images).
- `--network network=default`: Connects to the default libvirt virtual network (provides NAT internet access).
- `--graphics spice`: Uses SPICE for the graphical console (often better performance than VNC). Use `--graphics vnc` if SPICE is not preferred or available.
- `--noautoconsole`: Prevents `virt-install` from automatically connecting to the console, allowing it to run in the background.
If using a pre-made disk image (like a cloud image `.qcow2` file) instead of an ISO, you might use `--location` or `--import`:
virt-install \ --name YourVMName \ --ram 2048 \ --vcpus 1 \ --disk path=~/your-vm-disk.qcow2,bus=virtio \ --import \ --network network=default \ --graphics spice \ --noautoconsole
Here, `--import` tells libvirt to use the existing disk image as the main disk without running an installer.
- Connect to the VM Console:
If you used `--noautoconsole`, you need to connect to the VM's console to perform the OS installation or initial setup.
virsh console YourVMName
For graphical VMs using VNC or SPICE, you might use a viewer application on your host system and connect to the address/port provided by `virsh dumpxml YourVMName | grep graphics`. Or, the `virt-viewer` command can often connect directly by name:
virt-viewer YourVMName
- Manage the VM using virsh:
Once created, you can manage the VM using `virsh`:
- List running VMs:
virsh list
- List all VMs (running and stopped):
virsh list --all
- Start a stopped VM:
virsh start YourVMName
- Shutdown a running VM (graceful):
virsh shutdown YourVMName
- Force stop a running VM (ungraceful):
virsh destroy YourVMName
- Define a VM from an XML file (advanced):
virsh define /path/to/your-vm.xml
- Undefine (remove) a VM:
virsh undefine YourVMName
Caution: Undefining does not delete the virtual disk image file.
Note: For simpler VM management with a graphical interface, consider installing `virt-manager`.
Advantages
- Resource Efficiency: Better utilization of physical hardware resources.
- Isolation: VMs are isolated from the host and each other, preventing conflicts and enhancing security.
- Portability: VM images can often be moved between different physical machines or cloud environments.
- Flexibility: Easy to create, clone, and destroy VMs for various tasks.
- Cost Savings: Reduces the need for dedicated physical hardware for each application or OS.
Disadvantages
- Performance Overhead: Running an OS on top of a hypervisor can introduce some performance degradation compared to running directly on hardware.
- Resource Contention: If the host is overloaded, VMs may compete for resources, leading to performance issues.
- Complexity: Setting up and managing a virtualization environment can be more complex than managing a single physical machine.
See also
- Hypervisor
- Virtualization
- Cloud computing
- Containerization (e.g., Docker)