In this assignment, weβll use virtual machines (VMs) to install operating systems and tinker with them.
Luckily, youβve already worked with virtualization in AMSA, right? Letβs start with a short quiz to see what you remember and whether we can jump straight to the βimportantβ part.
Quizz
What is a virtual machine (VM)?
What is the difference between a VM and a container?
Can you draw a high-level architecture diagram of a container?
Can you draw a high-level architecture diagram of a virtual machine (VM)?
Note
If you already know the answers to these questions, feel free to skip ahead to the βHow Virtual Machines Workβ section.
Virtualitzation
Definition
Definition:
Has some characteristic that is apparent
(in oppositon to a real or absolute one)
Webster:
Existing in essence or effect though not in actual fact.
Our Definition
Virtual Systems: use logic objects to abstract physical components while keeping behaviour intact.
Examples:
Netwok: Virtual LAN (VLAN), Virtual Private Network (VPN)
Storage: Storage Area Network (SAN), LUN
Computers: Virtual Machine (VM), simulators or emulators
Servers: VHOST, ParaVirtualitzation
Machine Definition
Machine definition:
Hardware:
Processor, Memory, Disk, β¦
Usually under used
Software:
An OS image
Tighly coupled to HW
OS controls HW
Machine Virtualization
Machine Abstraction
Machine abstraction:
Virtual hardware: processors, memory, I/O devices, etc.
Encapsulates OS status and applications
Virtualitzation software:
Additional indirection level: isolates OS and HW.
Multiplexes hardware for VMs
VMs highly isolated
Better resource management
VM Isolation
Safe Multiplexing:
Multiples VM in only one host
Isolating HW from VM
Intermachine safety:
Virii, problems, etc. are confined to a single VM
Capability insulation:
VMM (Hipervisor) assigns and controls resources: better usage, no monopolization.
VM Encapsulation
Each VM in a single file/directory:
Storage (OS,Apps, Data)
Device State, Memory
Clones & Snapshots:
Freeze VM state and point-in-time recovery
Ease of distribution:
Pre-config machines, demos, etc.
Virtual servers (appliances) .
VM Compatibility
Hardware isolation
Real HW hidden to VM
VM accesses βstandardβ virtual HW
Migration
Host change != VM reconfig
VM legacy
Execute legacy apps/OS on new platforms (HW, OS) βcreatingβ standard/legacy HW
Allows mixes of architectures (sometimes)/systems (sometimes)
Requires special hardware and systems
Paravirtualitzation HW
Good Security:
Perfectly isolated machines
Virtualizator controls addressing/resource access
Good performance on good HW, underperforming otherwise
Usually doesnβt require special OS versions
Allows arch/system mixing (sometimes)
Doesnβt require special HW (affects performance)
Hosted Machine
Good security:
Perfectly isolated HW
Virtualizator controls addressing/resource access
Good performance (Special HW required)
Doesnβt require special OS versions
Can allow arch/systems mixing (with limitations)
Requires special HW (virtualization abled processors)
How VirtualBox and QEMU works (overview)
Weβll explore two hypervisors:
VirtualBox (the easy one, but slower and painfull when you try to do hardcore stuff).
Qemu (recommended for the real linux gurus, maybe a little bit faster because virt-io)
Note
Itβs going to be tough, but donβt worry if you donβt catch all the details (just focus on getting the overall picture).
How VirtualBox (more or less) works.
VMMDev (Virtual Machine Manager Device) in Oracle VirtualBox is a special emulated virtual hardware device that lets the host system and the guest operating system talk to each other.
qemu (for talking with KVM), allowing us to create virtual machines, map devices, etc.
(optional, but recommended): libvirtd daemon for having a βstandardβ userspace API to create virtualmachines. Already includes a βgoodβ network configuration.
(optional, but recommended): virt-manager (or similar: gnome-boxes, virsh-cli etc.) as a βfrontendβ for libvirtd. So you can create virtual machines via UI, etc.
On guest:
If youβre virtualizing a windows machine, youβll probably need on the guest the windows virtio guest drivers so it can paravirtualize the I/O devices.
Networking
TUN/TAP devices
This is essentially how connections to VPNs, virtual machines, and similar systems work. In fact, when you were using GNS3, you were creating TUN and TAP devices all day and all night.
If you use QEMU, youβll probably use some mechanism of TUN/TAP devices for networking, virtualbox does things differently nowadays.
Note
We wonβt dive into implementing a TUN/TAP interface here, but itβs a great exercise if you want to understand how they work. As a bonus, youβll likely come away with a much better understanding of how VPNs like OpenVPN and WireGuard work under the hood.
Configuration 1: Not attached NIC
No usable network connection
Really, thatβs it. What did you expect?
Configuration 2: NAT
Easiest and default configuration for VirtualBox and QEMU.
VirtualBox
Itβs the default, youβve internet connection on guest, but no ip to address to the guest (since virtualbox manages itβs himself).
If you want to have SSH connection, or open other port, youβll have to enable forwarding (just like you would do on your router):
VirtualBox β VM Settings β Network β Adapter 1 β NAT β Advanced β Port Forwarding
And youβll connect via the forward:
ssh user@localhost -p 2222
QEMU
With libvirtd, itβs the default, and itβs similar to:
Configuration 3: NAT Network
Multiple VMs share a private network:
VirtualBox
Similar to one NAT device, but:
You can ping between devices on the same network.
Host still does not have access to the NAT network, so, if you want to access some service, youβll have to forwardβ¦
QEMU
With libvirt, there isnβt really a separate concept of a βNAT Networkβ like there is in VirtualBox.
The default libvirt NAT configuration is already a shared virtual network.
The previous example simply had one VM attached to it, with multiple VMs, they would all connect to that same NAT network. Thatβs essentially the only difference.
Configuration 4: Bridged Adapter
VM appears directly on the physical LAN like another computer.
VirtualBox
Ethernet
Putβs the interface on promiscuous mode, and then weβve N MACβs for the N operating systems. The VirtualBox Kernel module is the one that forwards the packets to the virtual machines.
Wireless
Itβs not that easy to enable promiscuous mode on wireless devices
You can have a MAC address associated with multiple ipβs (see 3.3.4.1 in RFC 1122). So, the virtualbox kernel module does a mapping of IP <=> Virtual Machine.
QEMU (part 1)
For QEMU/KVM with libvirt, itβs going to be thoughβ¦.
It uses Linux Bridges for it, and they act as a βvirtualβ ethernet switch.
BUT, you donβt use raw ip addresses configured via ip addr, you use NetworkManager for handling the DHCP, ip configuration, wireless, etcβ¦
QEMU (part 2)
NetworkManager owns the host network configuration.
The Linux bridge, e.g. br0, is the software Ethernet switch.
The physical Ethernet NIC, e.g. enp3s0, becomes a port of that switch.
The host IP address goes on br0, not on enp3s0.
virt-manager/libvirt creates a TAP device, e.g. vnetX, for each VM and plugs it into br0.
QEMU (part 3)
With NetworkManager (nmcli), create the bridge profile and attach the wired NIC:
# Find the real wired interface and the active wired profile name.$ nmcli device status$ nmcli connection show --active# Create br0. STP is usually disabled for a simple laptop/desktop bridge.$ sudo nmcli connection add type bridge con-name br0 ifname br0$ sudo nmcli connection modify br0 bridge.stp no ipv4.method auto ipv6.method auto# Attach the physical Ethernet device to the bridge.# Replace enp3s0 with your wired interface.$ sudo nmcli connection add type ethernet con-name br0-port-enp3s0 ifname enp3s0 controller br0# If the old direct Ethernet profile autoconnects, disable it.# Replace "Wired connection 1" with the active profile name found above.$ sudo nmcli connection modify "Wired connection 1" connection.autoconnect no$ sudo nmcli connection down "Wired connection 1"# Bring up the bridge and its port.$ sudo nmcli connection up br0$ sudo nmcli connection up br0-port-enp3s0
Check that the IP is on the bridge and that the Ethernet NIC is only a port:
$ ip addr show br0$ bridge link$ nmcli connection show --active
QEMU (part 4)
Then configure the VM in virt-manager:
Open the VM details.
Select the NIC.
Set Network source to Bridge device.
Set Device name to br0.
Set Device model to virtio.
Apply and start/restart the VM.
In libvirt XML, virt-manager is creating the same idea as:
A Linux bridge wants Ethernet-like ports that can forward frames for several MAC addresses.
Normal Wi-Fi station mode is usually associated with one client MAC address.
Many wireless drivers/APs do not support the needed 4-address mode.
Promiscuous mode on Wi-Fi does not magically make the AP accept frames for the VM MAC addresses.
The usual failure is: Device does not allow enslaving to a bridge.
This is why the libvirt networking documentation warns that wireless interfaces cannot be attached to a Linux host bridge. If the host is on Wi-Fi, use libvirt NAT unless the VM really must be reachable from the LAN.
This can be used to create a network containing the host and a set of virtual machines, without the need for the hostβs physical network interface. Instead, a virtual network interface, similar to a loopback interface, is created on the host, providing connectivity among virtual machines and the host
We will NOT have internet on our VMβsβ¦.
VirtualBox
The vboxnetadp kernel module creates the host-side virtual NIC, e.g. vboxnet0, and attaches that NIC as a trunk port to the internal VirtualBox network (sending packets to the vboxnetflt kernel module).
The vboxnetflt kernel module intercept/injects the ethernet frames and sends it to the Internal Network of virtualbox (vboxdrv kernel module).
The vboxdrv kernel module implements the virtualbox internal networking and performs the virtual ethernet switching between ports.
QEMU
With libvirtd (using the virt-manager client):
Open Edit, Connection Details.
Go to Virtual Networks.
Click + to create a new network.
Give it a name, e.g. host-only.
Configuration 6: Internal Network
This can be used to create a different kind of software-based network which is visible to selected virtual machines, but not to applications running on the host or to the outside world.
VirtualBox
The same as Host-only but without the Host part.
Really, thatβs it, an internal network for vmβs.
Then, manually configure the ipβs inside each one of the VMβs.
Other configurations
MacVTAP (just for QEMU): Virtual Machines can access to the internet, and can ping each other, but the host operating system canβt see them.
vDPA devices: Uses standard Virtio rings/queues directly accessible by the guest or container via DMA (Direct Memory Access), removing software emulation overhead. See this redhat post.
Generic Driver (virtualbox) and other modes: Allow the user to select a driver which can be included with Oracle VM VirtualBox or be distributed in an extension pack, connect a local VM to a subnet on a remote cloud service, etc.
Disks
VirtualBox Virtual Disk Formats
A virtual disk image is a file on the host that is presented to the guest operating system as if it were a physical hard drive.
VirtualBox supports several formats:
VDI: VirtualBoxβs native disk format.
VMDK: Originally developed by VMware.
VHD: Microsoftβs Virtual Hard Disk format.
Inside the VM, these files appear as normal disks such as /dev/sda.
QEMU
QEMU/KVM commonly uses the QCOW2 format (QCOW2 = QEMU Copy-On-Write version 2)
Example for creating a disk of 40GB:
$ qemu-img create -f qcow2 debian.qcow2 40G
The QCOW format provides:
Dynamic disk allocation.
Copy-on-Write (you can have an image as base, and then have derived images for student 1 and student 2)
Snapshots.
Compression.
Encryption.
Move the Virtual Machine
A simple VM can often be moved or backed up by copying its virtual disk.
The operating system, programs and user files are still there because they are stored inside the virtual disk.
However, the disk does not copy the complete VM configuration (RAM config, CPUβs, network config, USB passhtrough, PCI passthrough, etc).
USB passthrough
Identify your devices
On Linux, we can inspect the devices connected to the host with:
$ lsusb
Or the pci devices with:
$ lspci
Passhtrough your devices
USB passthrough allows a physical USB device connected to the host to be attached directly to a virtual machine.
Connect the USB device to the host.
Open the VM configuration.
Select the USB device to attach (the identifier can be usefull)
Start the VM or attach it while it is running.
Note
Once attached, the USB device is used by the virtual machine and is normally unavailable to the host until it is detached.