Skip to content

Latest commit

Β 

History

History
356 lines (273 loc) Β· 14.3 KB

File metadata and controls

356 lines (273 loc) Β· 14.3 KB

πŸ“Œ Lecture 5 β€” Virtualization: One Box, Many Worlds


πŸ“ Slide 1 – πŸ’₯ The Day VMware Saved a Datacenter

  • πŸ—“οΈ 2002 β€” Diane Greene's team at VMware demos ESX Server, the first production hypervisor for x86
  • πŸͺ¦ Before ESX: a typical datacenter ran 5-10% CPU utilization because every app needed a dedicated box "to be safe"
  • πŸ“ˆ After ESX: enterprises consolidated 10:1, 20:1, 50:1 β€” entire physical rooms emptied of servers, costs dropped 70%
  • πŸš€ The pattern made AWS possible: by August 2006, EC2 launched, renting virtual machines by the hour
  • πŸŽ“ Lesson: Software pretending to be hardware unlocked the entire cloud era

πŸ€” Think: Before virtualization, "deploying QuickNotes" meant buying a server, racking it, and installing Linux. How would your weekend look?


πŸ“ Slide 2 – 🎯 Learning Outcomes

# πŸŽ“ Outcome
1 βœ… Distinguish Type 1 vs Type 2 hypervisors with examples
2 βœ… Explain what a VM image actually is (and isn't)
3 βœ… Snapshot, restore, clone β€” the three superpowers a VM gives you
4 βœ… Compare VM vs container (preview of Lecture 6)
5 βœ… Spin a Linux VM in VirtualBox and SSH into it
6 βœ… Use Vagrant to make VM setup reproducible (declarative, in Git)

πŸ“ Slide 3 – πŸ—ΊοΈ Lecture Overview

graph LR
    A["πŸ’Ύ What is a VM?"] --> B["πŸ›οΈ Type 1 vs Type 2"]
    B --> C["πŸ› οΈ Lifecycle"]
    C --> D["πŸ“¦ Images & Snapshots"]
    D --> E["πŸ“œ Vagrant"]
    E --> F["🐳 VM vs Container"]
Loading
  • πŸ“ Slides 1-7 β€” VM fundamentals
  • πŸ“ Slides 8-12 β€” Lifecycle, images, snapshots
  • πŸ“ Slides 13-16 β€” Vagrant + the QuickNotes VM
  • πŸ“ Slides 17-21 β€” VM vs container, real story, takeaways

πŸ“ Slide 4 – πŸ’Ύ What Is a Virtual Machine, Really?

A VM is a software emulation of a complete computer β€” its own CPU, RAM, disks, NIC β€” running on top of real hardware.

graph TB
    subgraph "Host"
        H1["πŸ”§ Hardware (CPU, RAM, Disks, NIC)"] --> HV["πŸͺž Hypervisor"]
        HV --> V1["πŸ’» VM 1<br/>Ubuntu + QuickNotes"]
        HV --> V2["πŸ’» VM 2<br/>Windows + IIS"]
        HV --> V3["πŸ’» VM 3<br/>Alpine + DB"]
    end
Loading
  • πŸͺž The hypervisor is the magic layer that fools each VM into thinking it has the whole machine
  • πŸ†” The VM has its own kernel β€” that's the key difference from a container (next lecture)
  • ⏳ Hardware features make this fast: Intel VT-x (2005), AMD-V (2006), nested page tables (2008)

πŸ“ Slide 5 – πŸ›οΈ Type 1 vs Type 2 Hypervisors

Type 1 (Bare-metal) Type 2 (Hosted)
Where it sits Directly on hardware On top of a host OS
Examples VMware ESXi, Microsoft Hyper-V, KVM (Linux kernel), Xen VirtualBox, VMware Workstation, Parallels
Performance Closer to bare metal A few % slower (host OS overhead)
Use case Datacenter / cloud Developer laptops
graph LR
    subgraph "Type 1"
        HW1["Hardware"] --> HV1["Hypervisor (KVM)"]
        HV1 --> VM1["VM 1"]
        HV1 --> VM2["VM 2"]
    end
    subgraph "Type 2"
        HW2["Hardware"] --> OS2["Host OS"]
        OS2 --> HV2["VirtualBox"]
        HV2 --> VMa["VM"]
    end
Loading
  • πŸ§ͺ In this lab you'll use VirtualBox (Type 2) β€” free, cross-platform, perfect for learning
  • πŸ›οΈ AWS EC2, GCP Compute, every "cloud VM" is KVM (Type 1) behind the scenes

πŸ“ Slide 6 – πŸ“œ A Short History of x86 Virtualization

  • πŸ–₯️ 1972 β€” IBM ships VM/370 on mainframes β€” virtualization was already old then
  • πŸ€” 1999 β€” VMware founders prove you can virtualize x86 even though the architecture wasn't designed for it (binary translation)
  • 🏎️ 2005-2006 β€” Intel VT-x and AMD-V add hardware support β†’ 2-5Γ— speedup
  • 🐧 2007 β€” KVM merges into the Linux kernel; suddenly every Linux box is a hypervisor
  • ☁️ 2006-2008 β€” AWS launches EC2, S3, RDS β€” the cloud era is here
  • πŸ§ͺ 2013-2015 β€” Docker and Kubernetes start to eat the VM's lunch (Lecture 6)

πŸ’‘ Virtualization didn't replace bare metal β€” it added an option. Cloud + containers didn't replace VMs β€” they added more options. Pick the right tool, not the new tool.


πŸ“ Slide 7 – πŸ› οΈ VM Lifecycle: From Image to Running

stateDiagram-v2
    [*] --> Image: download / build
    Image --> Created: clone template
    Created --> Running: power on (boot)
    Running --> Paused: suspend
    Paused --> Running: resume
    Running --> Stopped: shutdown
    Running --> Snapshot: snapshot
    Snapshot --> Running: restore
    Stopped --> [*]
Loading
State What it is Disk footprint
Image Cold file on disk (.vdi, .qcow2, .ova) Base size
Created VM exists in inventory but never booted Same as image
Running CPU + RAM allocated, OS executing + RAM size on disk if paged
Snapshot Copy-on-write fork of the disk Ξ” from base
Paused RAM dumped to disk, VM frozen + RAM file

πŸ“ Slide 8 – πŸ“¦ VM Images & Disk Formats

Format Origin Compression Snapshots
.vdi VirtualBox βœ… βœ…
.qcow2 QEMU/KVM βœ… βœ… (copy-on-write)
.vmdk VMware βœ… βœ…
.vhd / .vhdx Microsoft βœ… βœ…
.ova / .ovf Open Virtualization Format βœ… (tar of VM + metadata) β€”
  • πŸ“ An image is a file. You can copy it, version it, scan it
  • πŸ†” Cloud images (Ubuntu cloud-images, Amazon Linux) are pre-baked with cloud-init so they boot, accept your SSH key, install your packages, with no manual interaction
  • 🧰 qemu-img convert -f qcow2 -O vdi src.qcow2 dst.vdi β€” convert between formats

πŸ“ Slide 9 – πŸ“Έ Snapshots: The Time Machine

# VirtualBox CLI
$ VBoxManage snapshot quicknotes-vm take pre-deploy
$ VBoxManage snapshot quicknotes-vm restore pre-deploy
$ VBoxManage snapshot quicknotes-vm delete pre-deploy
  • βͺ A snapshot freezes the VM's disk + memory state at a moment in time
  • πŸͺ€ Copy-on-write β€” the snapshot doesn't duplicate the disk; only changed blocks are written
  • ⚠️ Snapshots are not backups β€” they live on the same disk. A failing disk takes both
  • 🚫 Don't run a VM on dozens of snapshots β€” every read traverses the chain. Snapshot, restore, delete

πŸ’‘ In Lab 5, you'll snapshot a clean QuickNotes VM, deliberately break it, restore in 30 seconds.


πŸ“ Slide 10 – πŸ”— VM Networking: Three Modes

Mode What it does When to use
NAT VM gets a private IP, shares host's IP for outbound Default; just need internet from VM
Bridged VM gets an IP on the same LAN as the host Other machines on the LAN can reach the VM
Host-only Isolated network between host ↔ VM (no internet) Lab environments, secret testing
# VirtualBox: tell the VM to forward host port 8080 β†’ VM port 8080
$ VBoxManage modifyvm quicknotes-vm \
    --natpf1 "quicknotes,tcp,127.0.0.1,8080,,8080"
  • πŸ§ͺ Lab 5 uses NAT with port forwarding so curl localhost:8080/notes from your host reaches QuickNotes inside the VM

πŸ“ Slide 11 – πŸ“œ Vagrant: Make VMs Reproducible

Vagrant turns "click through 27 GUI screens" into a single text file in Git:

# Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box      = "ubuntu/jammy64"
  config.vm.hostname = "quicknotes-vm"
  config.vm.network "forwarded_port", guest: 8080, host: 18080

  config.vm.provider "virtualbox" do |vb|
    vb.memory = 1024
    vb.cpus   = 2
  end

  config.vm.provision "shell", inline: <<-SHELL
    apt-get update && apt-get install -y curl
  SHELL
end
vagrant up        # build + boot
vagrant ssh       # log in
vagrant snapshot save clean-quicknotes
vagrant destroy   # nuke it from orbit
  • πŸ“ One file = one reproducible VM. Commit it. Diff it. Code-review it
  • πŸ§ͺ Lab 5 ships you a Vagrantfile as plumbing; you'll customize provisioning

πŸ“ Slide 12 – πŸš€ cloud-init: The Other Half of Reproducibility

For cloud VMs, cloud-init is the universal first-boot configurator:

#cloud-config
hostname: quicknotes
users:
  - name: deploy
    ssh-authorized-keys: [ssh-ed25519 AAAA...]
    sudo: ALL=(ALL) NOPASSWD:ALL
packages:
  - curl
  - jq
runcmd:
  - systemctl enable --now quicknotes
write_files:
  - path: /etc/quicknotes/env
    content: |
      ADDR=:8080
  • 🌐 Supported by AWS, GCP, Azure, OpenStack, DigitalOcean β€” every cloud
  • πŸ§ͺ Lab 10 will use cloud-init to spin a Cloud Run-adjacent test VM
  • πŸ“š The same YAML works in Vagrant too

πŸ“ Slide 13 – πŸ“ˆ Resource Sizing: How Much Is Enough?

Workload vCPU RAM Disk
Idle Linux + sshd 1 256 MB 1 GB
QuickNotes (this course) 1 512 MB 2 GB
Postgres dev DB 2 2 GB 10 GB
Build/CI runner 4 8 GB 20 GB
  • πŸ“Š Over-provisioning wastes money in the cloud and host RAM on your laptop
  • πŸ“‰ Under-provisioning triggers OOM kills, swap thrashing, mystery slowness
  • 🧰 free -h, vmstat 1, iostat 1 β€” measure before you guess
  • πŸ’‘ In Lab 8 (SRE) you'll learn to size based on observed load, not guessed load

πŸ“ Slide 14 – 🐳 VM vs Container: The 30-Second Preview

Virtual Machine Container
Has its own kernel? βœ… Yes ❌ Shares host kernel
Boot time 30-90 s 1-3 s
Disk size 1-10 GB 5-200 MB
Strong isolation βœ… Hardware-level ⚠️ Namespace + cgroup level
Lifetime Days to years Seconds to days
Use it for Multi-tenant clouds, full OS choice Microservices, CI jobs, dev environments
graph LR
    subgraph "VM"
        HV["Hypervisor"] --> KG["Guest kernel 1"]
        HV --> KG2["Guest kernel 2"]
    end
    subgraph "Containers"
        K["Shared host kernel"]
        K --> C1["containerd 1"]
        K --> C2["containerd 2"]
        K --> C3["containerd 3"]
    end
Loading
  • 🀝 In production, containers usually run inside VMs (cloud-provided isolation + container density)
  • 🎯 Next lecture: containers in depth

πŸ“ Slide 15 – πŸ§ͺ Lab 5 Preview: QuickNotes in a VM

  • πŸ› οΈ Task 1 (6 pts): vagrant up a Ubuntu 24.04 VM, install Go, build QuickNotes inside, expose :8080 to the host, hit it with curl from your laptop
  • πŸ“Έ Task 2 (4 pts): Snapshot the clean VM, deliberately break the binary, restore the snapshot, measure recovery time
  • 🎁 Bonus (2 pts): Compare boot time + RAM footprint of a Vagrant VM vs running QuickNotes in a Docker container (preview of Lab 6)
  • πŸ“œ Deliverable: submissions/lab5.md with vagrant up log, snapshot timestamps, and a one-paragraph reflection

πŸ“ Slide 16 – ❌ Common VM Antipatterns

πŸ”₯ Antipattern βœ… Better
GUI-click your way through VM setup, then "remember" what you did Vagrantfile + cloud-init = version-controlled
Run a VM on a chain of 20 snapshots Use snapshots transiently; delete after restore
Allocate 16 vCPU on a 4-core host Over-provisioning leads to thrashing β€” cap at host cores
Skip apt-get update in provisioning ("the image was new last month") Build images with Packer; pin versions; refresh weekly
Treat VM as a pet ("this is the build server") Treat VM as cattle β€” destroyable + rebuildable in one command

πŸ“ Slide 17 – πŸ“œ Real Story: Heartbleed and the Patch Storm

  • πŸ—“οΈ April 7, 2014 β€” Heartbleed (CVE-2014-0160) disclosed: any TLS-using OpenSSL server can be remotely memory-dumped
  • 🧨 ~17% of the public internet's HTTPS servers were vulnerable
  • πŸƒ At cloud scale, this meant rebuilding tens of thousands of VM images from base + new OpenSSL + redeploying
  • πŸ› οΈ Companies with proper image-pipelines (Packer + version-controlled cloud-init) shipped patches in hours. Companies that hand-built VMs took weeks
  • πŸŽ“ Lesson: If you can't rebuild a VM from a text file, you can't respond to a CVE at speed

πŸ’¬ "Treat your servers like cattle, not pets." β€” Bill Baker (Microsoft, 2012) β€” and Heartbleed was the moment that became survival, not philosophy


πŸ“ Slide 18 – 🧠 Key Takeaways

  1. πŸ’Ύ A VM is software pretending to be hardware β€” its own kernel, its own networking, its own everything
  2. πŸ›οΈ Type 1 = bare-metal hypervisor (datacenter), Type 2 = hosted (laptop) β€” same idea, different host
  3. πŸ“Έ Snapshots are time machines, not backups β€” same disk, same failure domain
  4. πŸ“œ Vagrant + cloud-init make VMs declarative β€” commit the file, not the clicks
  5. 🐳 VMs and containers are complementary, not competitive β€” containers run inside VMs in production
  6. πŸ„ Treat servers like cattle β€” when you can rebuild from text, you can survive Heartbleed-class events

πŸ“ Slide 19 – πŸš€ What's Next + πŸ“š Resources

  • πŸ“ Next lecture: Containers β€” same OS, lighter isolation, faster everything
  • πŸ§ͺ Lab 5: Vagrant + VirtualBox + QuickNotes; snapshot lifecycle; resource comparison
  • πŸ“– Read this week:
  • πŸ› οΈ Tools to install this week: VirtualBox 7.1.x, Vagrant 2.4.x
graph LR
    P["πŸ’» Week 4<br/>OS + Networking"] --> Y["πŸ“ You Are Here<br/>Virtualization"]
    Y --> N["🐳 Week 6<br/>Containers"]
    N --> M["πŸ”§ Week 7<br/>Config Mgmt (Ansible)"]
Loading

🎯 Remember: Virtualization was the abstraction that made everything else β€” clouds, containers, immutable infrastructure β€” possible. The pattern (run N tenants on shared hardware via a thin abstraction layer) keeps repeating.