{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "skip"
    },
    "tags": [
     "remove-cell",
     "remove_cell"
    ]
   },
   "outputs": [],
   "source": [
    "my_notebook_text = \"\"\"\n",
    "# Virtualization\n",
    "\n",
    "## Course Topics\n",
    "\n",
    "- Introduction to operating system virtualization\n",
    "- Modern virtualization types\n",
    "- Kernel-based Virtual Machine (KVM) virtualization\n",
    "- Virtual Machine CLI management tool: `virsh`\n",
    "- Virtual Machine deployment\n",
    "- Virtual Machine cloning\n",
    "- Virtual drive access and recovery\n",
    "- Virtual Machine disk resizing\n",
    "- Virtual Machine removal\n",
    "- Cloud image deployment and customization\n",
    "\n",
    "---\n",
    "\n",
    "## References\n",
    "\n",
    "- [KVM and QEMU Integration Guide](https://cloudbuilder.in/blogs/2014/03/09/kvm-and-qemu): Explains how the KVM kernel module and QEMU work together for hardware-accelerated virtualization.\n",
    "\n",
    "---\n",
    "\n",
    "## Prerequisites for Practical Exercises\n",
    "\n",
    "Create a working directory named `KVM` and navigate into it:\n",
    "\n",
    "```bash\n",
    "mkdir KVM\n",
    "cd KVM\n",
    "```\n",
    "\n",
    "Download the `kvm1` VM archive and the Rocky Linux 10 cloud image:\n",
    "\n",
    "```bash\n",
    "wget http://capone.rutgers.edu/coursefiles/kvm1.tgz\n",
    "wget https://dl.rockylinux.org/pub/rocky/10/images/x86_64/Rocky-10-GenericCloud-Base.latest.x86_64.qcow2\n",
    "```\n",
    "\n",
    "---\n",
    "\n",
    "## What is Virtualization?\n",
    "\n",
    "```text\n",
    "+-------------------------------------------------------------------+\n",
    "|  Virtual Machine 1   |  Virtual Machine 2   |  VM 3   |   VM 4    |\n",
    "|  (Guest OS / Apps)   |  (Guest OS / Apps)   | (Guest) |  (Guest)  |\n",
    "+-------------------------------------------------------------------+\n",
    "|              Hypervisor / Virtualization Layer                    |\n",
    "+-------------------------------------------------------------------+\n",
    "|                   Physical Hardware                               |\n",
    "|              (CPU, RAM, Storage, Network)                         |\n",
    "+-------------------------------------------------------------------+\n",
    "```\n",
    "\n",
    "![virtualization](img/virtualization.gif)\n",
    "\n",
    "**Accessible Image Description:**\n",
    "- **Overview:** An architectural diagram showing how a hypervisor layer abstracts physical host hardware to run multiple isolated virtual machines simultaneously.\n",
    "- **Setup:** The baseline consists of physical hardware components (CPU, RAM, Storage, Network Interface). Above the hardware is the Hypervisor/Virtualization Layer. Sitting on top are individual Virtual Machines (VM 1, VM 2, VM 3, VM 4), each operating with its own independent Guest OS and application stack.\n",
    "- **Key Takeaways:** Virtualization decouples software environments from physical hardware, enabling concurrent, isolated execution of multiple operating systems on a single physical host.\n",
    "\n",
    "In computing, **virtualization** is a technology that allows multiple operating systems (Virtual Machines or VMs) to run simultaneously, safely, and efficiently on a single computer system.\n",
    "\n",
    "### Primary Purposes of Virtualization\n",
    "- **Application Development & Testing:** Isolating environments from the host system.\n",
    "- **Server Consolidation:** Combining multiple virtual workloads onto a single physical server platform.\n",
    "- **Virtual Appliances:** Distributing pre-configured VMs for specific applications or services.\n",
    "- **Prototyping & Cloud Staging:** Building local VMs and migrating them to remote cloud providers.\n",
    "- **Multi-OS Desktop Workflows:** Running alternate operating systems alongside a primary host OS.\n",
    "\n",
    "---\n",
    "\n",
    "## OpenStack Computational Cloud: JetStream 2\n",
    "\n",
    "```text\n",
    "+---------------------------------------------------------+\n",
    "|                 JetStream 2 Cloud Cluster               |\n",
    "+---------------------------------------------------------+\n",
    "|  +--------------------------------------------------+   |\n",
    "|  |             OpenStack Management Layer           |   |\n",
    "|  +--------------------------------------------------+   |\n",
    "|  | Compute Node 1 | Compute Node 2 | Compute Node N |   |\n",
    "|  | (Ubuntu Host)  | (Ubuntu Host)  | (Ubuntu Host)  |   |\n",
    "|  |  +-----------+  |  +-----------+  |  +-----------+   |  \n",
    "|  |  | KVM VMs   |  |  | KVM VMs   |  |  | KVM VMs   |   |  \n",
    "|  |  +-----------+  |  +-----------+  |  +-----------+   |  \n",
    "|  +--------------------------------------------------+   |\n",
    "|  |                Physical Server Racks             |   |\n",
    "+---------------------------------------------------------+\n",
    "```\n",
    "\n",
    "![J2 cluster](img/J2.jpg)\n",
    "\n",
    "**Accessible Image Description:**\n",
    "- **Overview:** A photograph showing rows of physical rack-mounted server hardware that power the JetStream 2 cloud computing infrastructure at Indiana University.\n",
    "- **Setup:** High-density enterprise server racks contain multiple compute nodes. Each compute node runs an Ubuntu host operating system using KVM hypervisors, orchestrated centrally by OpenStack.\n",
    "- **Key Takeaways:** Illustrates production enterprise hardware configured to host large-scale, virtualized compute workloads for scientific research and cloud applications.\n",
    "\n",
    "---\n",
    "\n",
    "## Virtualization Challenges and Hardware Solutions\n",
    "\n",
    "Virtualization requires physical resources—CPU, RAM, and I/O devices—to be shared securely among multiple virtual machines. The traditional x86 architecture was not originally built to support full hardware virtualization. Modern processor technologies overcome these legacy limitations:\n",
    "\n",
    "1. **CPU Virtualization:**\n",
    "   - *Challenge:* Managing privileged CPU instructions (Ring 0 access across multiple guest kernels).\n",
    "   - *Solutions:* Binary translation/emulation and hardware-assisted virtualization.\n",
    "\n",
    "2. **Memory Virtualization:**\n",
    "   - *Challenge:* Safely partitioning physical system memory and dynamically allocating non-contiguous physical pages to virtual machines.\n",
    "   - *Solution:* Two distinct translations:Guest Virtual Address (GVA) to Guest Physical Address (GPA): Managed entirely by the guest OS page tables. Guest Physical Address (GPA) to Host Physical Address (HPA): Managed by the hypervisor to isolate and map actual hardware RAM.\n",
    "\n",
    "3. **Device and I/O Virtualization:**\n",
    "   - *Challenge:* Routing input/output requests cleanly between virtual device controllers and shared physical hardware adapters.\n",
    "   - *Solution:* **SR-IOV (Single Root I/O Virtualization)** allows a single physical PCIe device to present itself as multiple separate virtual functions directly to guest VMs.\n",
    "\n",
    "4. **GPU/Video Virtualization:**\n",
    "   - *Challenge:* Concurrent Video output from multiple VMs through one GPU. \n",
    "   - *Solution:* Direct GPU passthrough using **IOMMU**, **vGPU**, or **MIG (Multi-Instance GPU)** technology to assign dedicated hardware graphics slices to VMs.\n",
    "\n",
    "---\n",
    "\n",
    "## Common Virtualization Types on the x86 Platform\n",
    "\n",
    "1. **Operating System-Level Virtualization (Containers):**\n",
    "   - The system runs a single shared operating system kernel. Applications run inside isolated user-space environments (containers).\n",
    "   - *Examples:* Solaris Zones, LXC, Docker, Podman.\n",
    "\n",
    "2. **Hardware Emulation (Full Virtualization):**\n",
    "   - A hypervisor completely emulates a physical hardware environment, translating guest system calls into host execution calls for unmodified guest OSs.\n",
    "   - *Examples:* VMware Workstation/Player, VirtualBox, QEMU (in emulation mode).\n",
    "\n",
    "3. **Hardware-Assisted Virtualization:**\n",
    "   - Uses dedicated CPU hardware extensions (such as `Intel VT-x` or `AMD-V`) to execute guest code directly on the host processor with hypervisor privilege traps.\n",
    "   - *Examples:* KVM, VirtualBox, VMware ESXi, Microsoft Hyper-V.\n",
    "\n",
    "> **Note:** **Proxmox VE** is a popular Debian-based hypervisor platform that combines KVM hardware-assisted virtualization and LXC container management under a single administrative interface.\n",
    "\n",
    "---\n",
    "\n",
    "## Virtualization with KVM\n",
    "\n",
    "- **QEMU alone:** Provides full hardware emulation and software system-call trapping.\n",
    "- **QEMU + KVM:** Combines QEMU user-space device handling with the Linux kernel's KVM module (`kvm.ko`) to deliver near-native CPU execution speeds using `Intel VT-x` or `AMD-V` extensions.\n",
    "\n",
    "```text\n",
    "+-------------------------------------------------------+\n",
    "|                   User Space / Guest                  |\n",
    "|  +-------------------------------------------------+  |\n",
    "|  |               QEMU Process (User Mode)          |  |\n",
    "|  |     (Emulates I/O devices, PC hardware, BIOS)   |  |\n",
    "|  +-------------------------------------------------+  |\n",
    "+-------------------------------------------------------+\n",
    "|                   Host Linux Kernel                   |\n",
    "|  +-------------------------------------------------+  |\n",
    "|  |                   /dev/kvm                      |  |\n",
    "|  |            KVM Kernel Module (kvm.ko)           |  |\n",
    "|  |      (Schedules guest CPU execution via VT-x)   |  |\n",
    "|  +-------------------------------------------------+  |\n",
    "+-------------------------------------------------------+\n",
    "|           Hardware (CPU with Intel VT-x / AMD-V)      |\n",
    "+-------------------------------------------------------+\n",
    "```\n",
    "\n",
    "```{figure} img/KVM_QEMU.png\n",
    "---\n",
    "height: 300px\n",
    "---\n",
    "```\n",
    "\n",
    "**Accessible Image Description:**\n",
    "- **Overview:** A architectural diagram showing the layered relationship between user-space QEMU processes, the KVM Linux kernel module, and physical x86 CPU hardware extensions.\n",
    "- **Setup:** The architectural stack consists of three main tiers: User Space (where QEMU handles virtual machine creation, device emulation, and virtual storage/network calls), Host Kernel Space (where `/dev/kvm` and `kvm.ko` schedule guest thread execution directly on CPU hardware), and Physical Hardware (x86 CPU equipped with Intel VT-x or AMD-V extensions).\n",
    "- **Key Takeaways:** KVM turns the Linux kernel into a bare-metal hypervisor, allowing QEMU to offload core CPU and RAM execution to native physical hardware for maximum performance.\n",
    "\n",
    "---\n",
    "\n",
    "### Core Virtual Machine Files\n",
    "\n",
    "A standard KVM virtual machine consists of two primary files:\n",
    "1. **XML Configuration File:** Defines virtual hardware parameters (located at `/etc/libvirt/qemu/vm.xml`).\n",
    "2. **Disk Image File:** Holds the guest OS filesystems (typically stored at `/var/lib/libvirt/images/vm.qcow2`).\n",
    "\n",
    "---\n",
    "\n",
    "### Hardware Support Verification\n",
    "\n",
    "To verify if your CPU processor supports hardware virtualization:\n",
    "\n",
    "```bash\n",
    "egrep -c '(vmx|svm)' /proc/cpuinfo\n",
    "```\n",
    "\n",
    "- **Output `0`:** CPU lacks hardware virtualization support.\n",
    "- **Output `1` or greater:** CPU supports hardware virtualization extensions (ensure hardware virtualization is enabled in system BIOS/UEFI settings).\n",
    "\n",
    "To verify if hardware virtualization is active in BIOS/UEFI:\n",
    "\n",
    "```bash\n",
    "sudo apt update\n",
    "sudo apt install cpu-checker\n",
    "kvm-ok\n",
    "```\n",
    "\n",
    "- **If output reads:**\n",
    "  ```text\n",
    "  INFO: Your CPU supports KVM extensions\n",
    "  INFO: KVM is disabled by your BIOS\n",
    "  ```\n",
    "  *Action:* Reboot into BIOS settings and enable **Intel Virtualization Technology (VT-x)** or **AMD-V**.\n",
    "\n",
    "- **If output reads:**\n",
    "  ```text\n",
    "  INFO: Your CPU supports KVM extensions\n",
    "  INFO: /dev/kvm exists\n",
    "  KVM acceleration can be used\n",
    "  ```\n",
    "  *Action:* System is fully configured and ready to run accelerated VMs.\n",
    "\n",
    "---\n",
    "\n",
    "## KVM Setup (Hands-On Exercise)\n",
    "\n",
    "1. Open a system shell and switch to root:\n",
    "   ```bash\n",
    "   sudo -s\n",
    "   apt update\n",
    "   apt install virt-manager\n",
    "   ```\n",
    "\n",
    "2. Fix permissions on the default VM storage directory:\n",
    "   ```bash\n",
    "   chown hostadm:hostadm /var/lib/libvirt/images\n",
    "   ```\n",
    "\n",
    "3. Add user `hostadm` to the administrative `libvirt` groups:\n",
    "   ```bash\n",
    "   usermod -a -G libvirt hostadm\n",
    "   usermod -a -G libvirt-qemu hostadm\n",
    "   ```\n",
    "\n",
    "4. Install additional KVM packages:\n",
    "   ```bash\n",
    "   apt install qemu-kvm qemu-system\n",
    "   ```\n",
    "\n",
    "5. Restart the `libvirtd` service daemon:\n",
    "   ```bash\n",
    "   systemctl restart libvirtd \n",
    "   ```\n",
    "\n",
    "6. Log out of your desktop session and log back in to finalize group membership updates.\n",
    "\n",
    "---\n",
    "\n",
    "## Command Line Interface Management with `virsh`\n",
    "\n",
    "The `virsh` utility is a command-line interface alternative to the graphical `virt-manager` software. Both tools rely on the underlying `libvirt` API framework.\n",
    "\n",
    "To display all running VMs on the system default hypervisor connection:\n",
    "\n",
    "```bash\n",
    "virsh -c qemu:///system list\n",
    "```\n",
    "\n",
    "### Setting Default Connection URI\n",
    "\n",
    "To avoid appending `-c qemu:///system` to every command, configure the `LIBVIRT_DEFAULT_URI` environment variable.\n",
    "\n",
    "Edit `~/.bashrc` for user `hostadm` and add this line at the bottom:\n",
    "\n",
    "```bash\n",
    "export LIBVIRT_DEFAULT_URI='qemu:///system'\n",
    "```\n",
    "\n",
    "Apply the environment update:\n",
    "\n",
    "```bash\n",
    "source ~/.bashrc\n",
    "```\n",
    "\n",
    "### Enabling Default Virtual Network\n",
    "\n",
    "Enable and start the default libvirt network:\n",
    "\n",
    "```bash\n",
    "virsh net-autostart default\n",
    "virsh net-start default\n",
    "```\n",
    "\n",
    "Verify virtual network operational status:\n",
    "\n",
    "```bash\n",
    "virsh net-list --all\n",
    "```\n",
    "\n",
    "---\n",
    "\n",
    "## Deploying an Ubuntu 26.04 Virtual Appliance\n",
    "\n",
    "1. Navigate to your working directory and download the `kvm1` appliance tarball (if not previously downloaded):\n",
    "   ```bash\n",
    "   cd KVM\n",
    "   wget http://capone.rutgers.edu/coursefiles/kvm1.tgz\n",
    "   ```\n",
    "\n",
    "2. Extract the archive contents:\n",
    "   ```bash\n",
    "   tar -zxvf kvm1.tgz\n",
    "   ```\n",
    "\n",
    "3. Place configuration and disk files into system directories and adjust ownership settings:\n",
    "   ```bash\n",
    "   sudo cp kvm1.xml /etc/libvirt/qemu/\n",
    "   sudo mv kvm1.qcow2 /var/lib/libvirt/images/\n",
    "   sudo chown hostadm:hostadm /etc/libvirt/qemu/kvm1.xml\n",
    "   sudo chown hostadm:hostadm /var/lib/libvirt/images/kvm1.qcow2\n",
    "   ```\n",
    "\n",
    "4. Remove the tarball archive to free up disk space:\n",
    "   ```bash\n",
    "   rm kvm1.tgz\n",
    "   ```\n",
    "\n",
    "5. Register the new VM XML definition with KVM:\n",
    "   ```bash\n",
    "   sudo virsh define /etc/libvirt/qemu/kvm1.xml\n",
    "   ```\n",
    "\n",
    "6. Start the virtual machine:\n",
    "   ```bash\n",
    "   virsh start kvm1\n",
    "   ```\n",
    "\n",
    "7. Connect to the virtual console:\n",
    "   ```bash\n",
    "   virsh console kvm1\n",
    "   ```\n",
    "   *Login credentials:* Username `hostadm`, Password `unisys`.\n",
    "\n",
    "8. Perform a clean virtual machine shutdown from within the guest console:\n",
    "   ```bash\n",
    "   shutdown -h now\n",
    "   ```\n",
    "\n",
    "---\n",
    "\n",
    "## Essential `virsh` Control Commands\n",
    "\n",
    "- **List active VMs:**\n",
    "  ```bash\n",
    "  virsh list\n",
    "  ```\n",
    "\n",
    "- **List all registered VMs (active and inactive):**\n",
    "  ```bash\n",
    "  virsh list --all\n",
    "  ```\n",
    "\n",
    "- **Start a defined VM (`kvm1`):**\n",
    "  ```bash\n",
    "  virsh start kvm1\n",
    "  ```\n",
    "\n",
    "- **Gracefully shut down a VM (`kvm1`):**\n",
    "  ```bash\n",
    "  virsh shutdown kvm1\n",
    "  ```\n",
    "\n",
    "- **Get the IP address assigned to a running VM:**\n",
    "  ```bash\n",
    "  virsh domifaddr kvm1\n",
    "  ```\n",
    "\n",
    "- **Access guest serial console:**\n",
    "  ```bash\n",
    "  virsh console kvm1\n",
    "  ```\n",
    "\n",
    "- **Escape/Exit guest serial console back to host shell:**\n",
    "  Press key sequence: `Ctrl + ]` (or `^]`).\n",
    "\n",
    "---\n",
    "\n",
    "## Cloning Virtual Machines with `virt-clone`\n",
    "\n",
    "1. Clone existing VM `kvm1` to a new target named `kvm3`:\n",
    "   ```bash\n",
    "   virt-clone -o kvm1 -n kvm3 -f /home/hostadm/KVM/kvm3.qcow2\n",
    "   ```\n",
    "\n",
    "2. Verify registration, boot the cloned instance, and open its console session:\n",
    "   ```bash\n",
    "   virsh list --all\n",
    "   virsh start kvm3\n",
    "   virsh console kvm3\n",
    "   ```\n",
    "\n",
    "   > **Troubleshooting Permission Errors:**\n",
    "   > If starting `kvm3` displays:\n",
    "   > ```text\n",
    "   > error: Failed to start domain 'kvm3'\n",
    "   > error: Cannot access storage file '/home/hostadm/KVM/kvm3.qcow2' (as uid:64055, gid:64055): Permission denied\n",
    "   > ```\n",
    "   > Grant access permissions to directory `/home/hostadm`:\n",
    "   > ```bash\n",
    "   > sudo chmod 755 /home/hostadm\n",
    "   > ```\n",
    "\n",
    "3. Update guest Hostname inside `kvm3`:\n",
    "   Log in as `hostadm`, gain root privileges (`sudo -s`), edit `/etc/hostname`, and change `kvm1` to `kvm3`.\n",
    "\n",
    "4. Reset System Machine ID on `kvm3`:\n",
    "   ```bash\n",
    "   ./vm_id_reset.sh\n",
    "   ```\n",
    "\n",
    "5. Reboot `kvm3`:\n",
    "   ```bash\n",
    "   reboot\n",
    "   ```\n",
    "\n",
    "---\n",
    "\n",
    "## Accessing and Mounting Virtual Disk Files Offline\n",
    "\n",
    "If a VM experiences boot failures, you can mount its underlying `qcow2` virtual disk directly on the host to examine system logs and repair configuration files.\n",
    "\n",
    "1. Shut down target VM `kvm3`:\n",
    "   ```bash\n",
    "   virsh shutdown kvm3\n",
    "   ```\n",
    "\n",
    "2. Load host Network Block Device (`nbd`) kernel module:\n",
    "   ```bash\n",
    "   sudo -s\n",
    "   cd KVM\n",
    "   modprobe nbd max_part=2\n",
    "   qemu-nbd --connect=/dev/nbd0 /home/hostadm/KVM/kvm3.qcow2\n",
    "   ```\n",
    "\n",
    "3. Create a local mount directory:\n",
    "   ```bash\n",
    "   mkdir -p /mnt/vm\n",
    "   ```\n",
    "\n",
    "4. List partitions mapped onto device `/dev/nbd0`:\n",
    "   ```bash\n",
    "   fdisk -l /dev/nbd0\n",
    "   ```\n",
    "\n",
    "5. Mount target Linux filesystem partition (e.g., `/dev/nbd0p1`):\n",
    "   ```bash\n",
    "   mount /dev/nbd0p1 /mnt/vm\n",
    "   ```\n",
    "\n",
    "6. Inspect/modify files on the virtual disk:\n",
    "   ```bash\n",
    "   cd /mnt/vm\n",
    "   ls -la\n",
    "   cat etc/hostname\n",
    "   ```\n",
    "\n",
    "7. Safely unmount filesystem and disconnect block device:\n",
    "   ```bash\n",
    "   cd\n",
    "   umount /mnt/vm\n",
    "   qemu-nbd --disconnect /dev/nbd0\n",
    "   ```\n",
    "\n",
    "---\n",
    "\n",
    "## Expanding Virtual Machine Disk Capacity\n",
    "\n",
    "Expanding guest disk space involves three sequential steps:\n",
    "1. Increase physical capacity of the host `.qcow2` image file.\n",
    "2. Expand partition table boundaries inside the virtual machine.\n",
    "3. Resize internal guest filesystem structures.\n",
    "\n",
    "### Step 1: Expand Virtual Disk File Size\n",
    "\n",
    "1. Install `libguestfs-tools` on the host system:\n",
    "   ```bash\n",
    "   sudo apt install libguestfs-tools\n",
    "   ```\n",
    "\n",
    "2. Ensure `kvm3` is completely powered down:\n",
    "   ```bash\n",
    "   virsh shutdown kvm3\n",
    "   ```\n",
    "\n",
    "3. Inspect current image capacity and structural information:\n",
    "   ```bash\n",
    "   qemu-img info KVM/kvm3.qcow2\n",
    "   sudo virt-filesystems --long -h --all -a KVM/kvm3.qcow2\n",
    "   ```\n",
    "\n",
    "4. Resize virtual disk image to 10 GB:\n",
    "   ```bash\n",
    "   qemu-img resize KVM/kvm3.qcow2 10G\n",
    "   ```\n",
    "\n",
    "5. Verify updated virtual disk size:\n",
    "   ```bash\n",
    "   qemu-img info KVM/kvm3.qcow2\n",
    "   ```\n",
    "\n",
    "---\n",
    "\n",
    "### Step 2: Grow Partition and Filesystem inside Guest OS\n",
    "\n",
    "1. Start `kvm3` and connect to guest console:\n",
    "   ```bash\n",
    "   virsh start kvm3\n",
    "   virsh console kvm3\n",
    "   ```\n",
    "\n",
    "2. Inspect current block device geometry:\n",
    "   ```bash\n",
    "   lsblk\n",
    "   ```\n",
    "\n",
    "3. Check existing filesystem space utilization:\n",
    "   ```bash\n",
    "   df -h\n",
    "   ```\n",
    "\n",
    "4. Install guest cloud partitioning tools inside guest OS:\n",
    "   ```bash\n",
    "   sudo apt install cloud-guest-utils\n",
    "   ```\n",
    "\n",
    "5. Expand target root partition boundary (e.g., partition `1` on `/dev/vda`):\n",
    "   ```bash\n",
    "   sudo growpart /dev/vda 1\n",
    "   ```\n",
    "\n",
    "6. Resize filesystem (`ext4` format):\n",
    "   ```bash\n",
    "   sudo resize2fs /dev/vda1\n",
    "   ```\n",
    "\n",
    "   > **Note:** The `resize2fs` command specifically targets `ext4`/`ext3` filesystems. For `xfs` filesystems (commonly used in Red Hat/Rocky distributions), use `xfs_growfs` instead.\n",
    "\n",
    "7. Verify expanded filesystem capacity:\n",
    "   ```bash\n",
    "   df -h\n",
    "   ```\n",
    "\n",
    "---\n",
    "\n",
    "## Deleting a Virtual Machine\n",
    "\n",
    "1. Ensure VM `kvm3` is powered off:\n",
    "   ```bash\n",
    "   virsh shutdown kvm3\n",
    "   ```\n",
    "\n",
    "2. Unregister domain configuration from KVM:\n",
    "   ```bash\n",
    "   virsh undefine kvm3\n",
    "   ```\n",
    "   *If NVRAM error occurs during undefine, append `--nvram`:*\n",
    "   ```bash\n",
    "   virsh undefine kvm3 --nvram\n",
    "   ```\n",
    "\n",
    "3. Delete corresponding disk image file from host filesystem:\n",
    "   ```bash\n",
    "   rm KVM/kvm3.qcow2\n",
    "   ```\n",
    "\n",
    "4. Confirm deletion by checking hypervisor inventory:\n",
    "   ```bash\n",
    "   virsh list --all\n",
    "   ```\n",
    "\n",
    "---\n",
    "\n",
    "## Deploying a Rocky Linux Cloud Base Image\n",
    "\n",
    "1. Navigate to local `KVM` working directory:\n",
    "   ```bash\n",
    "   cd KVM\n",
    "   ```\n",
    "\n",
    "2. Convert and copy downloaded generic base image into system VM disk location:\n",
    "   ```bash\n",
    "   sudo qemu-img convert -f qcow2 -O qcow2 Rocky-10-GenericCloud-Base.latest.x86_64.qcow2 /var/lib/libvirt/images/rocky10.qcow2\n",
    "   ```\n",
    "\n",
    "3. Provision VM `rocky10` (1 vCPU core, 1024 MB RAM, UEFI boot mode):\n",
    "   ```bash\n",
    "   virt-install --name rocky10 --memory 1024 --vcpus 1 --os-variant rhel10-unknown --disk /var/lib/libvirt/images/rocky10.qcow2 --virt-type kvm --graphics none --import --network network=default,model=virtio --cloud-init --boot uefi\n",
    "   ```\n",
    "\n",
    "4. After the system boots up, disconnect from the console, Ctrl + ]\n",
    "\n",
    "5. Shutdown the VM:\n",
    "   ```bash\n",
    "   virsh shutdown rocky10 \n",
    "   ```\n",
    "   \n",
    "6. Ensure VM `rocky10` is powered off:\n",
    "   ```bash\n",
    "   virsh list --all\n",
    "   ```\n",
    "\n",
    "7. Set new root password directly using `virt-customize`:\n",
    "   ```bash\n",
    "   sudo virt-customize -a /var/lib/libvirt/images/rocky10.qcow2 --root-password password:coolpass\n",
    "   ```\n",
    "\n",
    "---\n",
    "\n",
    "## Customizing Rocky Linux Guest Installation\n",
    "\n",
    "1. Start `rocky10` instance and connect to guest console:\n",
    "   ```bash\n",
    "   virsh start rocky10\n",
    "   virsh console rocky10\n",
    "   ```\n",
    "   *Log in as user `root` using password `coolpass`.*\n",
    "\n",
    "   You will be prompted to reset the root password. Use `unisyscoolpass`.\n",
    "\n",
    "2. Install text editor utility `nano`:\n",
    "   ```bash\n",
    "   dnf install nano -y\n",
    "   ```\n",
    "\n",
    "3. Set local system hostname:\n",
    "   ```bash\n",
    "   nano /etc/hostname\n",
    "   ```\n",
    "   *Add `rocky10` to file contents and save.*\n",
    "\n",
    "4. Add administrative user `hostadm`:\n",
    "   ```bash\n",
    "   adduser hostadm\n",
    "   passwd hostadm\n",
    "   ```\n",
    "\n",
    "5. Grant administrative `sudo` rights by assigning `wheel` group membership:\n",
    "   ```bash\n",
    "   usermod -a -G wheel hostadm\n",
    "   ```\n",
    "\n",
    "6. Reboot system to confirm configuration persistence:\n",
    "   ```bash\n",
    "   reboot\n",
    "   ```\n",
    "\n",
    "7. After verifying system boot and `sudo` privilege escalation for user `hostadm`, issue clean console shutdown command:\n",
    "   ```bash\n",
    "   shutdown -h now\n",
    "   ```\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "skip"
    },
    "tags": [
     "remove-cell",
     "remove_cell"
    ]
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Both GOOGLE_API_KEY and GEMINI_API_KEY are set. Using GOOGLE_API_KEY.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# Virtualization\n",
      "\n",
      "## Course Topics\n",
      "\n",
      "- Introduction to operating system virtualization\n",
      "- Modern virtualization types\n",
      "- Kernel-based Virtual Machine (KVM) virtualization\n",
      "- Virtual Machine CLI management tool: `virsh`\n",
      "- Virtual Machine deployment\n",
      "- Virtual Machine cloning\n",
      "- Virtual drive access and recovery\n",
      "- Virtual Machine disk resizing\n",
      "- Virtual Machine removal\n",
      "- Cloud image deployment and customization\n",
      "\n",
      "---\n",
      "\n",
      "## References\n",
      "\n",
      "- [KVM and QEMU Integration Guide](https://cloudbuilder.in/blogs/2014/03/09/kvm-and-qemu): Explains how the KVM kernel module and QEMU work together for hardware-accelerated virtualization.\n",
      "\n",
      "---\n",
      "\n",
      "## Prerequisites for Practical Exercises\n",
      "\n",
      "Create a working directory named `KVM` and navigate into it:\n",
      "\n",
      "```bash\n",
      "mkdir KVM\n",
      "cd KVM\n",
      "```\n",
      "\n",
      "Download the `kvm1` VM archive and the Rocky Linux 10 cloud image:\n",
      "\n",
      "```bash\n",
      "wget http://capone.rutgers.edu/coursefiles/kvm1.tgz\n",
      "wget https://dl.rockylinux.org/pub/rocky/10/images/x86_64/Rocky-10-GenericCloud-Base.latest.x86_64.qcow2\n",
      "```\n",
      "\n",
      "---\n",
      "\n",
      "## What is Virtualization?\n",
      "\n",
      "```text\n",
      "+-------------------------------------------------------------------+\n",
      "|  Virtual Machine 1   |  Virtual Machine 2   |  VM 3   |   VM 4    |\n",
      "|  (Guest OS / Apps)   |  (Guest OS / Apps)   | (Guest) |  (Guest)  |\n",
      "+-------------------------------------------------------------------+\n",
      "|              Hypervisor / Virtualization Layer                    |\n",
      "+-------------------------------------------------------------------+\n",
      "|                   Physical Hardware                               |\n",
      "|              (CPU, RAM, Storage, Network)                         |\n",
      "+-------------------------------------------------------------------+\n",
      "```\n",
      "\n",
      "![virtualization](img/virtualization.gif)\n",
      "\n",
      "**Accessible Image Description:**\n",
      "- **Overview:** An architectural diagram showing how a hypervisor layer abstracts physical host hardware to run multiple isolated virtual machines simultaneously.\n",
      "- **Setup:** The baseline consists of physical hardware components (CPU, RAM, Storage, Network Interface). Above the hardware is the Hypervisor/Virtualization Layer. Sitting on top are individual Virtual Machines (VM 1, VM 2, VM 3, VM 4), each operating with its own independent Guest OS and application stack.\n",
      "- **Key Takeaways:** Virtualization decouples software environments from physical hardware, enabling concurrent, isolated execution of multiple operating systems on a single physical host.\n",
      "\n",
      "In computing, **virtualization** is a technology that allows multiple operating systems (Virtual Machines or VMs) to run simultaneously, safely, and efficiently on a single computer system.\n",
      "\n",
      "### Primary Purposes of Virtualization\n",
      "\n",
      "- **Application Development & Testing:** Isolating development environments from the host system.\n",
      "- **Server Consolidation:** Combining multiple virtual workloads onto a single physical server platform.\n",
      "- **Virtual Appliances:** Distributing pre-configured VMs for specific applications or services.\n",
      "- **Prototyping & Cloud Staging:** Building local VMs and migrating them to remote cloud providers.\n",
      "- **Multi-OS Desktop Workflows:** Running alternate operating systems alongside a primary host OS.\n",
      "\n",
      "---\n",
      "\n",
      "## OpenStack Computational Cloud: JetStream 2\n",
      "\n",
      "```text\n",
      "+---------------------------------------------------------+\n",
      "|                 JetStream 2 Cloud Cluster               |\n",
      "+---------------------------------------------------------+\n",
      "|  +--------------------------------------------------+   |\n",
      "|  |             OpenStack Management Layer           |   |\n",
      "|  +--------------------------------------------------+   |\n",
      "|  | Compute Node 1 | Compute Node 2 | Compute Node N |   |\n",
      "|  | (Ubuntu Host)  | (Ubuntu Host)  | (Ubuntu Host)  |   |\n",
      "|  |  +-----------+  |  +-----------+  |  +-----------+   |  \n",
      "|  |  | KVM VMs   |  |  | KVM VMs   |  |  | KVM VMs   |   |  \n",
      "|  |  +-----------+  |  +-----------+  |  +-----------+   |  \n",
      "|  +--------------------------------------------------+   |\n",
      "|  |                Physical Server Racks             |   |\n",
      "+---------------------------------------------------------+\n",
      "```\n",
      "\n",
      "![J2 cluster](img/J2.jpg)\n",
      "\n",
      "**Accessible Image Description:**\n",
      "- **Overview:** A photograph showing rows of physical rack-mounted server hardware that power the JetStream 2 cloud computing infrastructure at Indiana University.\n",
      "- **Setup:** High-density enterprise server racks contain multiple compute nodes. Each compute node runs an Ubuntu host operating system using KVM hypervisors, orchestrated centrally by OpenStack.\n",
      "- **Key Takeaways:** Illustrates production enterprise hardware configured to host large-scale, virtualized compute workloads for scientific research and cloud applications.\n",
      "\n",
      "---\n",
      "\n",
      "## Virtualization Challenges and Hardware Solutions\n",
      "\n",
      "Virtualization requires physical resources—CPU, RAM, and I/O devices—to be shared securely among multiple virtual machines. The traditional x86 architecture was not originally built to support full hardware virtualization. Modern processor technologies overcome these legacy limitations:\n",
      "\n",
      "1. **CPU Virtualization:**\n",
      "   - *Challenge:* Managing privileged CPU instructions (Ring 0 access across multiple guest kernels).\n",
      "   - *Solutions:* Binary translation/emulation and hardware-assisted virtualization.\n",
      "\n",
      "2. **Memory Virtualization:**\n",
      "   - *Challenge:* Safely partitioning physical system memory and dynamically allocating non-contiguous physical pages to virtual machines.\n",
      "   - *Solution:* Two distinct translations:\n",
      "     - **Guest Virtual Address (GVA) to Guest Physical Address (GPA):** Managed entirely by the guest OS page tables.\n",
      "     - **Guest Physical Address (GPA) to Host Physical Address (HPA):** Managed by the hypervisor to isolate and map actual hardware RAM.\n",
      "\n",
      "3. **Device and I/O Virtualization:**\n",
      "   - *Challenge:* Routing input/output requests cleanly between virtual device controllers and shared physical hardware adapters.\n",
      "   - *Solution:* **SR-IOV (Single Root I/O Virtualization)** allows a single physical PCIe device to present itself as multiple separate virtual functions directly to guest VMs.\n",
      "\n",
      "4. **GPU/Video Virtualization:**\n",
      "   - *Challenge:* Concurrent video output from multiple VMs through one GPU.\n",
      "   - *Solution:* Direct GPU passthrough using **IOMMU**, **vGPU**, or **MIG (Multi-Instance GPU)** technology to assign dedicated hardware graphics slices to VMs.\n",
      "\n",
      "---\n",
      "\n",
      "## Common Virtualization Types on the x86 Platform\n",
      "\n",
      "1. **Operating System-Level Virtualization (Containers):**\n",
      "   - The system runs a single shared operating system kernel. Applications run inside isolated user-space environments (containers).\n",
      "   - *Examples:* Solaris Zones, LXC, Docker, Podman.\n",
      "\n",
      "2. **Hardware Emulation (Full Virtualization):**\n",
      "   - A hypervisor completely emulates a physical hardware environment, translating guest system calls into host execution calls for unmodified guest OSs.\n",
      "   - *Examples:* VMware Workstation/Player, VirtualBox, QEMU (in emulation mode).\n",
      "\n",
      "3. **Hardware-Assisted Virtualization:**\n",
      "   - Uses dedicated CPU hardware extensions (such as `Intel VT-x` or `AMD-V`) to execute guest code directly on the host processor with hypervisor privilege traps.\n",
      "   - *Examples:* KVM, VirtualBox, VMware ESXi, Microsoft Hyper-V.\n",
      "\n",
      "> **Note:** **Proxmox VE** is a popular Debian-based hypervisor platform that combines KVM hardware-assisted virtualization and LXC container management under a single administrative interface.\n",
      "\n",
      "---\n",
      "\n",
      "## Virtualization with KVM\n",
      "\n",
      "- **QEMU alone:** Provides full hardware emulation and software system-call trapping.\n",
      "- **QEMU + KVM:** Combines QEMU user-space device handling with the Linux kernel's KVM module (`kvm.ko`) to deliver near-native CPU execution speeds using `Intel VT-x` or `AMD-V` extensions.\n",
      "\n",
      "```text\n",
      "+-------------------------------------------------------+\n",
      "|                   User Space / Guest                  |\n",
      "|  +-------------------------------------------------+  |\n",
      "|  |               QEMU Process (User Mode)          |  |\n",
      "|  |     (Emulates I/O devices, PC hardware, BIOS)   |  |\n",
      "|  +-------------------------------------------------+  |\n",
      "+-------------------------------------------------------+\n",
      "|                   Host Linux Kernel                   |\n",
      "|  +-------------------------------------------------+  |\n",
      "|  |                   /dev/kvm                      |  |\n",
      "|  |            KVM Kernel Module (kvm.ko)           |  |\n",
      "|  |      (Schedules guest CPU execution via VT-x)   |  |\n",
      "|  +-------------------------------------------------+  |\n",
      "+-------------------------------------------------------+\n",
      "|           Hardware (CPU with Intel VT-x / AMD-V)      |\n",
      "+-------------------------------------------------------+\n",
      "```\n",
      "\n",
      "```{figure} img/KVM_QEMU.png\n",
      "---\n",
      "height: 300px\n",
      "---\n",
      "```\n",
      "\n",
      "**Accessible Image Description:**\n",
      "- **Overview:** An architectural diagram showing the layered relationship between user-space QEMU processes, the KVM Linux kernel module, and physical x86 CPU hardware extensions.\n",
      "- **Setup:** The architectural stack consists of three main tiers: User Space (where QEMU handles virtual machine creation, device emulation, and virtual storage/network calls), Host Kernel Space (where `/dev/kvm` and `kvm.ko` schedule guest thread execution directly on CPU hardware), and Physical Hardware (x86 CPU equipped with Intel VT-x or AMD-V extensions).\n",
      "- **Key Takeaways:** KVM turns the Linux kernel into a bare-metal hypervisor, allowing QEMU to offload core CPU and RAM execution to native physical hardware for maximum performance.\n",
      "\n",
      "---\n",
      "\n",
      "### Core Virtual Machine Files\n",
      "\n",
      "A standard KVM virtual machine consists of two primary files:\n",
      "1. **XML Configuration File:** Defines virtual hardware parameters (located at `/etc/libvirt/qemu/vm.xml`).\n",
      "2. **Disk Image File:** Holds the guest OS filesystems (typically stored at `/var/lib/libvirt/images/vm.qcow2`).\n",
      "\n",
      "---\n",
      "\n",
      "### Hardware Support Verification\n",
      "\n",
      "To verify if your CPU processor supports hardware virtualization:\n",
      "\n",
      "```bash\n",
      "egrep -c '(vmx|svm)' /proc/cpuinfo\n",
      "```\n",
      "\n",
      "- **Output `0`:** CPU lacks hardware virtualization support.\n",
      "- **Output `1` or greater:** CPU supports hardware virtualization extensions (ensure hardware virtualization is enabled in system BIOS/UEFI settings).\n",
      "\n",
      "To verify if hardware virtualization is active in BIOS/UEFI:\n",
      "\n",
      "```bash\n",
      "sudo apt update\n",
      "sudo apt install cpu-checker\n",
      "kvm-ok\n",
      "```\n",
      "\n",
      "- **If output reads:**\n",
      "  ```text\n",
      "  INFO: Your CPU supports KVM extensions\n",
      "  INFO: KVM is disabled by your BIOS\n",
      "  ```\n",
      "  *Action:* Reboot into BIOS settings and enable **Intel Virtualization Technology (VT-x)** or **AMD-V**.\n",
      "\n",
      "- **If output reads:**\n",
      "  ```text\n",
      "  INFO: Your CPU supports KVM extensions\n",
      "  INFO: /dev/kvm exists\n",
      "  KVM acceleration can be used\n",
      "  ```\n",
      "  *Action:* System is fully configured and ready to run accelerated VMs.\n",
      "\n",
      "---\n",
      "\n",
      "## KVM Setup (Hands-On Exercise)\n",
      "\n",
      "1. Open a system shell and switch to root:\n",
      "   ```bash\n",
      "   sudo -s\n",
      "   apt update\n",
      "   apt install virt-manager\n",
      "   ```\n",
      "\n",
      "2. Fix permissions on the default VM storage directory:\n",
      "   ```bash\n",
      "   chown hostadm:hostadm /var/lib/libvirt/images\n",
      "   ```\n",
      "\n",
      "3. Add user `hostadm` to the administrative `libvirt` groups:\n",
      "   ```bash\n",
      "   usermod -a -G libvirt hostadm\n",
      "   usermod -a -G libvirt-qemu hostadm\n",
      "   ```\n",
      "\n",
      "4. Install additional KVM packages:\n",
      "   ```bash\n",
      "   apt install qemu-kvm qemu-system\n",
      "   ```\n",
      "\n",
      "5. Restart the `libvirtd` service daemon:\n",
      "   ```bash\n",
      "   systemctl restart libvirtd \n",
      "   ```\n",
      "\n",
      "6. Log out of your desktop session and log back in to finalize group membership updates.\n",
      "\n",
      "---\n",
      "\n",
      "## Command Line Interface Management with `virsh`\n",
      "\n",
      "The `virsh` utility is a command-line interface alternative to the graphical `virt-manager` software. Both tools rely on the underlying `libvirt` API framework.\n",
      "\n",
      "To display all running VMs on the system default hypervisor connection:\n",
      "\n",
      "```bash\n",
      "virsh -c qemu:///system list\n",
      "```\n",
      "\n",
      "### Setting Default Connection URI\n",
      "\n",
      "To avoid appending `-c qemu:///system` to every command, configure the `LIBVIRT_DEFAULT_URI` environment variable.\n",
      "\n",
      "Edit `~/.bashrc` for user `hostadm` and add this line at the bottom:\n",
      "\n",
      "```bash\n",
      "export LIBVIRT_DEFAULT_URI='qemu:///system'\n",
      "```\n",
      "\n",
      "Apply the environment update:\n",
      "\n",
      "```bash\n",
      "source ~/.bashrc\n",
      "```\n",
      "\n",
      "### Enabling Default Virtual Network\n",
      "\n",
      "Enable and start the default libvirt network:\n",
      "\n",
      "```bash\n",
      "virsh net-autostart default\n",
      "virsh net-start default\n",
      "```\n",
      "\n",
      "Verify virtual network operational status:\n",
      "\n",
      "```bash\n",
      "virsh net-list --all\n",
      "```\n",
      "\n",
      "---\n",
      "\n",
      "## Deploying an Ubuntu Virtual Appliance\n",
      "\n",
      "1. Navigate to your working directory and download the `kvm1` appliance tarball (if not previously downloaded):\n",
      "   ```bash\n",
      "   cd KVM\n",
      "   wget http://capone.rutgers.edu/coursefiles/kvm1.tgz\n",
      "   ```\n",
      "\n",
      "2. Extract the archive contents:\n",
      "   ```bash\n",
      "   tar -zxvf kvm1.tgz\n",
      "   ```\n",
      "\n",
      "3. Place configuration and disk files into system directories and adjust ownership settings:\n",
      "   ```bash\n",
      "   sudo cp kvm1.xml /etc/libvirt/qemu/\n",
      "   sudo mv kvm1.qcow2 /var/lib/libvirt/images/\n",
      "   sudo chown hostadm:hostadm /etc/libvirt/qemu/kvm1.xml\n",
      "   sudo chown hostadm:hostadm /var/lib/libvirt/images/kvm1.qcow2\n",
      "   ```\n",
      "\n",
      "4. Remove the tarball archive to free up disk space:\n",
      "   ```bash\n",
      "   rm kvm1.tgz\n",
      "   ```\n",
      "\n",
      "5. Register the new VM XML definition with KVM:\n",
      "   ```bash\n",
      "   sudo virsh define /etc/libvirt/qemu/kvm1.xml\n",
      "   ```\n",
      "\n",
      "6. Start the virtual machine:\n",
      "   ```bash\n",
      "   virsh start kvm1\n",
      "   ```\n",
      "\n",
      "7. Connect to the virtual console:\n",
      "   ```bash\n",
      "   virsh console kvm1\n",
      "   ```\n",
      "   *Login credentials:* Username `hostadm`, Password `unisys`.\n",
      "\n",
      "8. Perform a clean virtual machine shutdown from within the guest console:\n",
      "   ```bash\n",
      "   shutdown -h now\n",
      "   ```\n",
      "\n",
      "---\n",
      "\n",
      "## Essential `virsh` Control Commands\n",
      "\n",
      "- **List active VMs:**\n",
      "  ```bash\n",
      "  virsh list\n",
      "  ```\n",
      "\n",
      "- **List all registered VMs (active and inactive):**\n",
      "  ```bash\n",
      "  virsh list --all\n",
      "  ```\n",
      "\n",
      "- **Start a defined VM (`kvm1`):**\n",
      "  ```bash\n",
      "  virsh start kvm1\n",
      "  ```\n",
      "\n",
      "- **Gracefully shut down a VM (`kvm1`):**\n",
      "  ```bash\n",
      "  virsh shutdown kvm1\n",
      "  ```\n",
      "\n",
      "- **Get the IP address assigned to a running VM:**\n",
      "  ```bash\n",
      "  virsh domifaddr kvm1\n",
      "  ```\n",
      "\n",
      "- **Access guest serial console:**\n",
      "  ```bash\n",
      "  virsh console kvm1\n",
      "  ```\n",
      "\n",
      "- **Escape/Exit guest serial console back to host shell:**\n",
      "  Press key sequence: `Ctrl + ]` (or `^]`).\n",
      "\n",
      "---\n",
      "\n",
      "## Cloning Virtual Machines with `virt-clone`\n",
      "\n",
      "1. Clone existing VM `kvm1` to a new target named `kvm3`:\n",
      "   ```bash\n",
      "   virt-clone -o kvm1 -n kvm3 -f /home/hostadm/KVM/kvm3.qcow2\n",
      "   ```\n",
      "\n",
      "2. Verify registration, boot the cloned instance, and open its console session:\n",
      "   ```bash\n",
      "   virsh list --all\n",
      "   virsh start kvm3\n",
      "   virsh console kvm3\n",
      "   ```\n",
      "\n",
      "   > **Troubleshooting Permission Errors:**\n",
      "   > If starting `kvm3` displays:\n",
      "   > ```text\n",
      "   > error: Failed to start domain 'kvm3'\n",
      "   > error: Cannot access storage file '/home/hostadm/KVM/kvm3.qcow2' (as uid:64055, gid:64055): Permission denied\n",
      "   > ```\n",
      "   > Grant access permissions to directory `/home/hostadm`:\n",
      "   > ```bash\n",
      "   > sudo chmod 755 /home/hostadm\n",
      "   > ```\n",
      "\n",
      "3. Update guest Hostname inside `kvm3`:\n",
      "   Log in as `hostadm`, gain root privileges (`sudo -s`), edit `/etc/hostname`, and change `kvm1` to `kvm3`.\n",
      "\n",
      "4. Reset System Machine ID on `kvm3`:\n",
      "   ```bash\n",
      "   ./vm_id_reset.sh\n",
      "   ```\n",
      "\n",
      "5. Reboot `kvm3`:\n",
      "   ```bash\n",
      "   reboot\n",
      "   ```\n",
      "\n",
      "---\n",
      "\n",
      "## Accessing and Mounting Virtual Disk Files Offline\n",
      "\n",
      "If a VM experiences boot failures, you can mount its underlying `qcow2` virtual disk directly on the host to examine system logs and repair configuration files.\n",
      "\n",
      "1. Shut down target VM `kvm3`:\n",
      "   ```bash\n",
      "   virsh shutdown kvm3\n",
      "   ```\n",
      "\n",
      "2. Load host Network Block Device (`nbd`) kernel module:\n",
      "   ```bash\n",
      "   sudo -s\n",
      "   cd KVM\n",
      "   modprobe nbd max_part=2\n",
      "   qemu-nbd --connect=/dev/nbd0 /home/hostadm/KVM/kvm3.qcow2\n",
      "   ```\n",
      "\n",
      "3. Create a local mount directory:\n",
      "   ```bash\n",
      "   mkdir -p /mnt/vm\n",
      "   ```\n",
      "\n",
      "4. List partitions mapped onto device `/dev/nbd0`:\n",
      "   ```bash\n",
      "   fdisk -l /dev/nbd0\n",
      "   ```\n",
      "\n",
      "5. Mount target Linux filesystem partition (e.g., `/dev/nbd0p1`):\n",
      "   ```bash\n",
      "   mount /dev/nbd0p1 /mnt/vm\n",
      "   ```\n",
      "\n",
      "6. Inspect/modify files on the virtual disk:\n",
      "   ```bash\n",
      "   cd /mnt/vm\n",
      "   ls -la\n",
      "   cat etc/hostname\n",
      "   ```\n",
      "\n",
      "7. Safely unmount filesystem and disconnect block device:\n",
      "   ```bash\n",
      "   cd\n",
      "   umount /mnt/vm\n",
      "   qemu-nbd --disconnect /dev/nbd0\n",
      "   ```\n",
      "\n",
      "---\n",
      "\n",
      "## Expanding Virtual Machine Disk Capacity\n",
      "\n",
      "Expanding guest disk space involves three sequential steps:\n",
      "1. Increase physical capacity of the host `.qcow2` image file.\n",
      "2. Expand partition table boundaries inside the virtual machine.\n",
      "3. Resize internal guest filesystem structures.\n",
      "\n",
      "### Step 1: Expand Virtual Disk File Size\n",
      "\n",
      "1. Install `libguestfs-tools` on the host system:\n",
      "   ```bash\n",
      "   sudo apt install libguestfs-tools\n",
      "   ```\n",
      "\n",
      "2. Ensure `kvm3` is completely powered down:\n",
      "   ```bash\n",
      "   virsh shutdown kvm3\n",
      "   ```\n",
      "\n",
      "3. Inspect current image capacity and structural information:\n",
      "   ```bash\n",
      "   qemu-img info KVM/kvm3.qcow2\n",
      "   sudo virt-filesystems --long -h --all -a KVM/kvm3.qcow2\n",
      "   ```\n",
      "\n",
      "4. Resize virtual disk image to 10 GB:\n",
      "   ```bash\n",
      "   qemu-img resize KVM/kvm3.qcow2 10G\n",
      "   ```\n",
      "\n",
      "5. Verify updated virtual disk size:\n",
      "   ```bash\n",
      "   qemu-img info KVM/kvm3.qcow2\n",
      "   ```\n",
      "\n",
      "---\n",
      "\n",
      "### Step 2: Grow Partition and Filesystem inside Guest OS\n",
      "\n",
      "1. Start `kvm3` and connect to guest console:\n",
      "   ```bash\n",
      "   virsh start kvm3\n",
      "   virsh console kvm3\n",
      "   ```\n",
      "\n",
      "2. Inspect current block device geometry:\n",
      "   ```bash\n",
      "   lsblk\n",
      "   ```\n",
      "\n",
      "3. Check existing filesystem space utilization:\n",
      "   ```bash\n",
      "   df -h\n",
      "   ```\n",
      "\n",
      "4. Install guest cloud partitioning tools inside guest OS:\n",
      "   ```bash\n",
      "   sudo apt install cloud-guest-utils\n",
      "   ```\n",
      "\n",
      "5. Expand target root partition boundary (e.g., partition `1` on `/dev/vda`):\n",
      "   ```bash\n",
      "   sudo growpart /dev/vda 1\n",
      "   ```\n",
      "\n",
      "6. Resize filesystem (`ext4` format):\n",
      "   ```bash\n",
      "   sudo resize2fs /dev/vda1\n",
      "   ```\n",
      "\n",
      "   > **Note:** The `resize2fs` command specifically targets `ext4`/`ext3` filesystems. For `xfs` filesystems (commonly used in Red Hat/Rocky distributions), use `xfs_growfs` instead.\n",
      "\n",
      "7. Verify expanded filesystem capacity:\n",
      "   ```bash\n",
      "   df -h\n",
      "   ```\n",
      "\n",
      "---\n",
      "\n",
      "## Deleting a Virtual Machine\n",
      "\n",
      "1. Ensure VM `kvm3` is powered off:\n",
      "   ```bash\n",
      "   virsh shutdown kvm3\n",
      "   ```\n",
      "\n",
      "2. Unregister domain configuration from KVM:\n",
      "   ```bash\n",
      "   virsh undefine kvm3\n",
      "   ```\n",
      "   *If NVRAM error occurs during undefine, append `--nvram`:*\n",
      "   ```bash\n",
      "   virsh undefine kvm3 --nvram\n",
      "   ```\n",
      "\n",
      "3. Delete corresponding disk image file from host filesystem:\n",
      "   ```bash\n",
      "   rm KVM/kvm3.qcow2\n",
      "   ```\n",
      "\n",
      "4. Confirm deletion by checking hypervisor inventory:\n",
      "   ```bash\n",
      "   virsh list --all\n",
      "   ```\n",
      "\n",
      "---\n",
      "\n",
      "## Deploying a Rocky Linux Cloud Base Image\n",
      "\n",
      "1. Navigate to local `KVM` working directory:\n",
      "   ```bash\n",
      "   cd KVM\n",
      "   ```\n",
      "\n",
      "2. Convert and copy downloaded generic base image into system VM disk location:\n",
      "   ```bash\n",
      "   sudo qemu-img convert -f qcow2 -O qcow2 Rocky-10-GenericCloud-Base.latest.x86_64.qcow2 /var/lib/libvirt/images/rocky10.qcow2\n",
      "   ```\n",
      "\n",
      "3. Provision VM `rocky10` (1 vCPU core, 1024 MB RAM, UEFI boot mode):\n",
      "   ```bash\n",
      "   virt-install --name rocky10 --memory 1024 --vcpus 1 --os-variant rhel10-unknown --disk /var/lib/libvirt/images/rocky10.qcow2 --virt-type kvm --graphics none --import --network network=default,model=virtio --cloud-init --boot uefi\n",
      "   ```\n",
      "\n",
      "4. After the system boots up, disconnect from the console by pressing `Ctrl + ]`.\n",
      "\n",
      "5. Shut down the VM:\n",
      "   ```bash\n",
      "   virsh shutdown rocky10 \n",
      "   ```\n",
      "\n",
      "6. Ensure VM `rocky10` is powered off:\n",
      "   ```bash\n",
      "   virsh list --all\n",
      "   ```\n",
      "\n",
      "7. Set new root password directly using `virt-customize`:\n",
      "   ```bash\n",
      "   sudo virt-customize -a /var/lib/libvirt/images/rocky10.qcow2 --root-password password:coolpass\n",
      "   ```\n",
      "\n",
      "---\n",
      "\n",
      "## Customizing Rocky Linux Guest Installation\n",
      "\n",
      "1. Start `rocky10` instance and connect to guest console:\n",
      "   ```bash\n",
      "   virsh start rocky10\n",
      "   virsh console rocky10\n",
      "   ```\n",
      "   *Log in as user `root` using password `coolpass`.*\n",
      "\n",
      "   You will be prompted to reset the root password. Use `unisyscoolpass`.\n",
      "\n",
      "2. Install text editor utility `nano`:\n",
      "   ```bash\n",
      "   dnf install nano -y\n",
      "   ```\n",
      "\n",
      "3. Set local system hostname:\n",
      "   ```bash\n",
      "   nano /etc/hostname\n",
      "   ```\n",
      "   *Add `rocky10` to file contents and save.*\n",
      "\n",
      "4. Add administrative user `hostadm`:\n",
      "   ```bash\n",
      "   adduser hostadm\n",
      "   passwd hostadm\n",
      "   ```\n",
      "\n",
      "5. Grant administrative `sudo` rights by assigning `wheel` group membership:\n",
      "   ```bash\n",
      "   usermod -a -G wheel hostadm\n",
      "   ```\n",
      "\n",
      "6. Reboot system to confirm configuration persistence:\n",
      "   ```bash\n",
      "   reboot\n",
      "   ```\n",
      "\n",
      "7. After verifying system boot and `sudo` privilege escalation for user `hostadm`, issue clean console shutdown command:\n",
      "   ```bash\n",
      "   shutdown -h now\n",
      "   ```\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import time\n",
    "import base64 \n",
    "from google import genai\n",
    "from google.genai.errors import APIError\n",
    "\n",
    "os.environ[\"GEMINI_API_KEY\"] = \"AQ.Ab8RN6K9G5S-h6IWgpUgfmHbIk7b8iZlWTuYptMFqOT4Ph6FnQ\"\n",
    "client = genai.Client()\n",
    "\n",
    "prompt_instructions_detailed5 = (\n",
    "    \"You are processing raw course text meant for Jupyter Notebook markdown cells.\\n\\n\"\n",
    "    \n",
    "    \"CRITICAL RULE: Do NOT delete, alter, or remove any original image links, image paths, \"\n",
    "    \"or image tags (e.g., lines starting with `![...](...)` or containing `<img ...>`). \"\n",
    "    \"Leave them exactly where they are in the original text sequence.\\n\\n\"\n",
    "    \n",
    "    \"INSTRUCTION:\\n\"\n",
    "    \"Rewrite the text to make it clear and university-appropriate. Make it clear and write WCAG 2.1 AA compliant. Use the following strict sequence \"\n",
    "    \"when dealing with image placements:\\n\\n\"\n",
    "    \n",
    "    \"1. First, insert the NEW Text-Based Structured Alternative ABOVE the image:\\n\"\n",
    "    \"   - For numerical data charts or grid plots: Provide a clean Markdown Data Table.\\n\"\n",
    "    \"   - For hardware architecture, network layouts, or OS Process lifecycles (like fork(), process states, execution threads): \"\n",
    "    \"Provide a high-level text flowchart inside a preformatted ```text code block.\\n\"\n",
    "    \"   - For Terminal Commands or Pipelines (like stdin/stdout piping, shell interactions): \"\n",
    "    \"Provide a vertical box-and-arrow sequence map illustrating data flow from one utility to the next.\\n\"\n",
    "    \"   - Keep diagram characters perfectly aligned and compatible with static HTML page exports.\\n\\n\"\n",
    "    \"   - CRITICAL RULE FOR VIRTUAL DESKTOP LOGIN CONTEXT:\\n\"\n",
    "    \"     If the text or diagram is about logging into the Virtual Desktop or using Ravada, \"\n",
    "    \"the text diagram sequence MUST start exactly with this workflow path:\\n\"\n",
    "    \"     [Web access to Ravada portal with authentication] ===> [Start VM] ===> [Access VM via SSH with provided port]\\n\\n\"\n",
    "    \n",
    "    \"2. Second, place the ORIGINAL untouched image link exactly as it was.\\n\\n\"\n",
    "    \n",
    "    \"3. Third, immediately BELOW the original image link, add the Comprehensive Detailed Description:\\n\"\n",
    "    \"Inside this caption block, you MUST start with the exact text '**Accessible Image Description:**'. Place it always under the image.\"\n",
    "    \"   - Overview: One sentence describing what the visual shows at a high level.\\n\"\n",
    "    \"   - Setup: Explain the primary components, labels, axes, or code pathways without hyper-granular specs.\\n\"\n",
    "    \"   - Key Takeaways: Core conceptual trends or execution results shown.\\n\\n\"\n",
    "\n",
    "    f\"Course Text:\\n{my_notebook_text}\"\n",
    ")\n",
    "prompt_instructions = prompt_instructions_detailed5\n",
    "\n",
    "try:\n",
    "    response = client.models.generate_content(\n",
    "        model='models/gemini-3.6-flash',\n",
    "        contents= prompt_instructions,\n",
    "        )\n",
    "\n",
    "    #print(response.text)\n",
    "\n",
    "    output_text = response.text\n",
    "    print(output_text)\n",
    "    \n",
    "except APIError as e:\n",
    "    raise e"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "# Virtualization\n",
    "\n",
    "## Course Topics\n",
    "\n",
    "- Introduction to operating system virtualization\n",
    "- Modern virtualization types\n",
    "- Kernel-based Virtual Machine (KVM) virtualization\n",
    "- Virtual Machine CLI management tool: `virsh`\n",
    "- Virtual Machine deployment\n",
    "- Virtual Machine cloning\n",
    "- Virtual drive access and recovery\n",
    "- Virtual Machine disk resizing\n",
    "- Virtual Machine removal\n",
    "- Cloud image deployment and customization\n",
    "\n",
    "---\n",
    "\n",
    "## References\n",
    "\n",
    "- [KVM and QEMU Integration Guide](https://cloudbuilder.in/blogs/2014/03/09/kvm-and-qemu): Explains how the KVM kernel module and QEMU work together for hardware-accelerated virtualization.\n",
    "\n",
    "---\n",
    "\n",
    "## Prerequisites for Practical Exercises\n",
    "\n",
    "Create a working directory named `KVM` and navigate into it:\n",
    "\n",
    "```bash\n",
    "mkdir KVM\n",
    "cd KVM\n",
    "```\n",
    "\n",
    "Download the `kvm1` VM archive and the Rocky Linux 10 cloud image:\n",
    "\n",
    "```bash\n",
    "wget http://capone.rutgers.edu/coursefiles/kvm1.tgz\n",
    "wget https://dl.rockylinux.org/pub/rocky/10/images/x86_64/Rocky-10-GenericCloud-Base.latest.x86_64.qcow2\n",
    "```\n",
    "\n",
    "---\n",
    "\n",
    "## What is Virtualization?\n",
    "\n",
    "```text\n",
    "+-------------------------------------------------------------------+\n",
    "|  Virtual Machine 1   |  Virtual Machine 2   |  VM 3   |   VM 4    |\n",
    "|  (Guest OS / Apps)   |  (Guest OS / Apps)   | (Guest) |  (Guest)  |\n",
    "+-------------------------------------------------------------------+\n",
    "|              Hypervisor / Virtualization Layer                    |\n",
    "+-------------------------------------------------------------------+\n",
    "|                   Physical Hardware                               |\n",
    "|              (CPU, RAM, Storage, Network)                         |\n",
    "+-------------------------------------------------------------------+\n",
    "```\n",
    "\n",
    "![virtualization](img/virtualization.gif)\n",
    "\n",
    "**Accessible Image Description:**\n",
    "- **Overview:** An architectural diagram showing how a hypervisor layer abstracts physical host hardware to run multiple isolated virtual machines simultaneously.\n",
    "- **Setup:** The baseline consists of physical hardware components (CPU, RAM, Storage, Network Interface). Above the hardware is the Hypervisor/Virtualization Layer. Sitting on top are individual Virtual Machines (VM 1, VM 2, VM 3, VM 4), each operating with its own independent Guest OS and application stack.\n",
    "- **Key Takeaways:** Virtualization decouples software environments from physical hardware, enabling concurrent, isolated execution of multiple operating systems on a single physical host.\n",
    "\n",
    "In computing, **virtualization** is a technology that allows multiple operating systems (Virtual Machines or VMs) to run simultaneously, safely, and efficiently on a single computer system.\n",
    "\n",
    "### Primary Purposes of Virtualization\n",
    "- **Application Development & Testing:** Isolating environments from the host system.\n",
    "- **Server Consolidation:** Combining multiple virtual workloads onto a single physical server platform.\n",
    "- **Virtual Appliances:** Distributing pre-configured VMs for specific applications or services.\n",
    "- **Prototyping & Cloud Staging:** Building local VMs and migrating them to remote cloud providers.\n",
    "- **Multi-OS Desktop Workflows:** Running alternate operating systems alongside a primary host OS.\n",
    "\n",
    "---\n",
    "\n",
    "## OpenStack Computational Cloud: JetStream 2\n",
    "\n",
    "```text\n",
    "+---------------------------------------------------------+\n",
    "|                 JetStream 2 Cloud Cluster               |\n",
    "+---------------------------------------------------------+\n",
    "|  +--------------------------------------------------+   |\n",
    "|  |             OpenStack Management Layer           |   |\n",
    "|  +--------------------------------------------------+   |\n",
    "|  | Compute Node 1 | Compute Node 2 | Compute Node N |   |\n",
    "|  | (Ubuntu Host)  | (Ubuntu Host)  | (Ubuntu Host)  |   |\n",
    "|  |  +-----------+  |  +-----------+  |  +-----------+   |  \n",
    "|  |  | KVM VMs   |  |  | KVM VMs   |  |  | KVM VMs   |   |  \n",
    "|  |  +-----------+  |  +-----------+  |  +-----------+   |  \n",
    "|  +--------------------------------------------------+   |\n",
    "|  |                Physical Server Racks             |   |\n",
    "+---------------------------------------------------------+\n",
    "```\n",
    "\n",
    "![J2 cluster](img/J2.jpg)\n",
    "\n",
    "**Accessible Image Description:**\n",
    "- **Overview:** A photograph showing rows of physical rack-mounted server hardware that power the JetStream 2 cloud computing infrastructure at Indiana University.\n",
    "- **Setup:** High-density enterprise server racks contain multiple compute nodes. Each compute node runs an Ubuntu host operating system using KVM hypervisors, orchestrated centrally by OpenStack.\n",
    "- **Key Takeaways:** Illustrates production enterprise hardware configured to host large-scale, virtualized compute workloads for scientific research and cloud applications.\n",
    "\n",
    "---\n",
    "\n",
    "## Virtualization Challenges and Hardware Solutions\n",
    "\n",
    "Virtualization requires physical resources—CPU, RAM, and I/O devices—to be shared securely among multiple virtual machines. The traditional x86 architecture was not originally built to support full hardware virtualization. Modern processor technologies overcome these legacy limitations:\n",
    "\n",
    "1. **CPU Virtualization:**\n",
    "   - *Challenge:* Managing privileged CPU instructions (Ring 0 access across multiple guest kernels).\n",
    "   - *Solutions:* Binary translation/emulation and hardware-assisted virtualization.\n",
    "\n",
    "2. **Memory Virtualization:**\n",
    "   - *Challenge:* Safely partitioning physical system memory and dynamically allocating non-contiguous physical pages to virtual machines.\n",
    "   - *Solution:* Two distinct translations: Guest Virtual Address (GVA) to Guest Physical Address (GPA) (managed entirely by the guest OS page tables) and Guest Physical Address (GPA) to Host Physical Address (HPA) (managed by the hypervisor to isolate and map actual hardware RAM).\n",
    "\n",
    "3. **Device and I/O Virtualization:**\n",
    "   - *Challenge:* Routing input/output requests cleanly between virtual device controllers and shared physical hardware adapters.\n",
    "   - *Solution:* **SR-IOV (Single Root I/O Virtualization)** allows a single physical PCIe device to present itself as multiple separate virtual functions directly to guest VMs.\n",
    "\n",
    "4. **GPU/Video Virtualization:**\n",
    "   - *Challenge:* Concurrent video output from multiple VMs through one GPU. \n",
    "   - *Solution:* Direct GPU passthrough using **IOMMU**, **vGPU**, or **MIG (Multi-Instance GPU)** technology to assign dedicated hardware graphics slices to VMs.\n",
    "\n",
    "---\n",
    "\n",
    "## Common Virtualization Types on the x86 Platform\n",
    "\n",
    "1. **Operating System-Level Virtualization (Containers):**\n",
    "   - The system runs a single shared operating system kernel. Applications run inside isolated user-space environments (containers).\n",
    "   - *Examples:* Solaris Zones, LXC, Docker, Podman.\n",
    "\n",
    "2. **Hardware Emulation (Full Virtualization):**\n",
    "   - A hypervisor completely emulates a physical hardware environment, translating guest system calls into host execution calls for unmodified guest OSs.\n",
    "   - *Examples:* VMware Workstation/Player, VirtualBox, QEMU (in emulation mode).\n",
    "\n",
    "3. **Hardware-Assisted Virtualization:**\n",
    "   - Uses dedicated CPU hardware extensions (such as `Intel VT-x` or `AMD-V`) to execute guest code directly on the host processor with hypervisor privilege traps.\n",
    "   - *Examples:* KVM, VirtualBox, VMware ESXi, Microsoft Hyper-V.\n",
    "\n",
    "> **Note:** **Proxmox VE** is a popular Debian-based hypervisor platform that combines KVM hardware-assisted virtualization and LXC container management under a single administrative interface.\n",
    "\n",
    "---\n",
    "\n",
    "## Virtualization with KVM\n",
    "\n",
    "- **QEMU alone:** Provides full hardware emulation and software system-call trapping.\n",
    "- **QEMU + KVM:** Combines QEMU user-space device handling with the Linux kernel's KVM module (`kvm.ko`) to deliver near-native CPU execution speeds using `Intel VT-x` or `AMD-V` extensions.\n",
    "\n",
    "```text\n",
    "+-------------------------------------------------------+\n",
    "|                   User Space / Guest                  |\n",
    "|  +-------------------------------------------------+  |\n",
    "|  |               QEMU Process (User Mode)          |  |\n",
    "|  |     (Emulates I/O devices, PC hardware, BIOS)   |  |\n",
    "|  +-------------------------------------------------+  |\n",
    "+-------------------------------------------------------+\n",
    "|                   Host Linux Kernel                   |\n",
    "|  +-------------------------------------------------+  |\n",
    "|  |                   /dev/kvm                      |  |\n",
    "|  |            KVM Kernel Module (kvm.ko)           |  |\n",
    "|  |      (Schedules guest CPU execution via VT-x)   |  |\n",
    "|  +-------------------------------------------------+  |\n",
    "+-------------------------------------------------------+\n",
    "|           Hardware (CPU with Intel VT-x / AMD-V)      |\n",
    "+-------------------------------------------------------+\n",
    "```\n",
    "\n",
    "```{figure} img/KVM_QEMU.png\n",
    "---\n",
    "height: 300px\n",
    "---\n",
    "```\n",
    "\n",
    "**Accessible Image Description:**\n",
    "- **Overview:** An architectural diagram showing the layered relationship between user-space QEMU processes, the KVM Linux kernel module, and physical x86 CPU hardware extensions.\n",
    "- **Setup:** The architectural stack consists of three main tiers: User Space (where QEMU handles virtual machine creation, device emulation, and virtual storage/network calls), Host Kernel Space (where `/dev/kvm` and `kvm.ko` schedule guest thread execution directly on CPU hardware), and Physical Hardware (x86 CPU equipped with Intel VT-x or AMD-V extensions).\n",
    "- **Key Takeaways:** KVM turns the Linux kernel into a bare-metal hypervisor, allowing QEMU to offload core CPU and RAM execution to native physical hardware for maximum performance.\n",
    "\n",
    "---\n",
    "\n",
    "### Core Virtual Machine Files\n",
    "\n",
    "A standard KVM virtual machine consists of two primary files:\n",
    "1. **XML Configuration File:** Defines virtual hardware parameters (located at `/etc/libvirt/qemu/vm.xml`).\n",
    "2. **Disk Image File:** Holds the guest OS filesystems (typically stored at `/var/lib/libvirt/images/vm.qcow2`).\n",
    "\n",
    "---\n",
    "\n",
    "### Hardware Support Verification\n",
    "\n",
    "To verify if your CPU processor supports hardware virtualization:\n",
    "\n",
    "```bash\n",
    "egrep -c '(vmx|svm)' /proc/cpuinfo\n",
    "```\n",
    "\n",
    "- **Output `0`:** CPU lacks hardware virtualization support.\n",
    "- **Output `1` or greater:** CPU supports hardware virtualization extensions (ensure hardware virtualization is enabled in system BIOS/UEFI settings).\n",
    "\n",
    "To verify if hardware virtualization is active in BIOS/UEFI:\n",
    "\n",
    "```bash\n",
    "sudo apt update\n",
    "sudo apt install cpu-checker\n",
    "kvm-ok\n",
    "```\n",
    "\n",
    "- **If output reads:**\n",
    "  ```text\n",
    "  INFO: Your CPU supports KVM extensions\n",
    "  INFO: KVM is disabled by your BIOS\n",
    "  ```\n",
    "  *Action:* Reboot into BIOS settings and enable **Intel Virtualization Technology (VT-x)** or **AMD-V**.\n",
    "\n",
    "- **If output reads:**\n",
    "  ```text\n",
    "  INFO: Your CPU supports KVM extensions\n",
    "  INFO: /dev/kvm exists\n",
    "  KVM acceleration can be used\n",
    "  ```\n",
    "  *Action:* System is fully configured and ready to run accelerated VMs.\n",
    "\n",
    "---\n",
    "\n",
    "## KVM Setup (Hands-On Exercise)\n",
    "\n",
    "1. Open a system shell and switch to root:\n",
    "   ```bash\n",
    "   sudo -s\n",
    "   apt update\n",
    "   apt install virt-manager\n",
    "   ```\n",
    "\n",
    "2. Fix permissions on the default VM storage directory:\n",
    "   ```bash\n",
    "   chown hostadm:hostadm /var/lib/libvirt/images\n",
    "   ```\n",
    "\n",
    "3. Add user `hostadm` to the administrative `libvirt` groups:\n",
    "   ```bash\n",
    "   usermod -a -G libvirt hostadm\n",
    "   usermod -a -G libvirt-qemu hostadm\n",
    "   ```\n",
    "\n",
    "4. Install additional KVM packages:\n",
    "   ```bash\n",
    "   apt install qemu-kvm qemu-system\n",
    "   ```\n",
    "\n",
    "5. Restart the `libvirtd` service daemon:\n",
    "   ```bash\n",
    "   systemctl restart libvirtd \n",
    "   ```\n",
    "\n",
    "6. Log out of your desktop session and log back in to finalize group membership updates.\n",
    "\n",
    "---\n",
    "\n",
    "## Command Line Interface Management with `virsh`\n",
    "\n",
    "The `virsh` utility is a command-line interface alternative to the graphical `virt-manager` software. Both tools rely on the underlying `libvirt` API framework.\n",
    "\n",
    "To display all running VMs on the system default hypervisor connection:\n",
    "\n",
    "```bash\n",
    "virsh -c qemu:///system list\n",
    "```\n",
    "\n",
    "### Setting Default Connection URI\n",
    "\n",
    "To avoid appending `-c qemu:///system` to every command, configure the `LIBVIRT_DEFAULT_URI` environment variable.\n",
    "\n",
    "Edit `~/.bashrc` for user `hostadm` and add this line at the bottom:\n",
    "\n",
    "```bash\n",
    "export LIBVIRT_DEFAULT_URI='qemu:///system'\n",
    "```\n",
    "\n",
    "Apply the environment update:\n",
    "\n",
    "```bash\n",
    "source ~/.bashrc\n",
    "```\n",
    "\n",
    "### Enabling Default Virtual Network\n",
    "\n",
    "Enable and start the default libvirt network:\n",
    "\n",
    "```bash\n",
    "virsh net-autostart default\n",
    "virsh net-start default\n",
    "```\n",
    "\n",
    "Verify virtual network operational status:\n",
    "\n",
    "```bash\n",
    "virsh net-list --all\n",
    "```\n",
    "\n",
    "---\n",
    "\n",
    "## Deploying an Ubuntu 26.04 Virtual Appliance\n",
    "\n",
    "1. Navigate to your working directory and download the `kvm1` appliance tarball (if not previously downloaded):\n",
    "   ```bash\n",
    "   cd KVM\n",
    "   wget http://capone.rutgers.edu/coursefiles/kvm1.tgz\n",
    "   ```\n",
    "\n",
    "2. Extract the archive contents:\n",
    "   ```bash\n",
    "   tar -zxvf kvm1.tgz\n",
    "   ```\n",
    "\n",
    "3. Place configuration and disk files into system directories and adjust ownership settings:\n",
    "   ```bash\n",
    "   sudo cp kvm1.xml /etc/libvirt/qemu/\n",
    "   sudo mv kvm1.qcow2 /var/lib/libvirt/images/\n",
    "   sudo chown hostadm:hostadm /etc/libvirt/qemu/kvm1.xml\n",
    "   sudo chown hostadm:hostadm /var/lib/libvirt/images/kvm1.qcow2\n",
    "   ```\n",
    "\n",
    "4. Remove the tarball archive to free up disk space:\n",
    "   ```bash\n",
    "   rm kvm1.tgz\n",
    "   ```\n",
    "\n",
    "5. Register the new VM XML definition with KVM:\n",
    "   ```bash\n",
    "   sudo virsh define /etc/libvirt/qemu/kvm1.xml\n",
    "   ```\n",
    "\n",
    "6. Start the virtual machine:\n",
    "   ```bash\n",
    "   virsh start kvm1\n",
    "   ```\n",
    "\n",
    "7. Connect to the virtual console:\n",
    "   ```bash\n",
    "   virsh console kvm1\n",
    "   ```\n",
    "   *Login credentials:* Username `hostadm`, Password `unisys`.\n",
    "\n",
    "8. Perform a clean virtual machine shutdown from within the guest console:\n",
    "   ```bash\n",
    "   shutdown -h now\n",
    "   ```\n",
    "\n",
    "---\n",
    "\n",
    "## Essential `virsh` Control Commands\n",
    "\n",
    "- **List active VMs:**\n",
    "  ```bash\n",
    "  virsh list\n",
    "  ```\n",
    "\n",
    "- **List all registered VMs (active and inactive):**\n",
    "  ```bash\n",
    "  virsh list --all\n",
    "  ```\n",
    "\n",
    "- **Start a defined VM (`kvm1`):**\n",
    "  ```bash\n",
    "  virsh start kvm1\n",
    "  ```\n",
    "\n",
    "- **Gracefully shut down a VM (`kvm1`):**\n",
    "  ```bash\n",
    "  virsh shutdown kvm1\n",
    "  ```\n",
    "\n",
    "- **Get the IP address assigned to a running VM:**\n",
    "  ```bash\n",
    "  virsh domifaddr kvm1\n",
    "  ```\n",
    "\n",
    "- **Access guest serial console:**\n",
    "  ```bash\n",
    "  virsh console kvm1\n",
    "  ```\n",
    "\n",
    "- **Escape/Exit guest serial console back to host shell:**\n",
    "  Press key sequence: `Ctrl + ]` (or `^]`).\n",
    "\n",
    "---\n",
    "\n",
    "## Cloning Virtual Machines with `virt-clone`\n",
    "\n",
    "1. Clone existing VM `kvm1` to a new target named `kvm3`:\n",
    "   ```bash\n",
    "   virt-clone -o kvm1 -n kvm3 -f /home/hostadm/KVM/kvm3.qcow2\n",
    "   ```\n",
    "\n",
    "2. Verify registration, boot the cloned instance, and open its console session:\n",
    "   ```bash\n",
    "   virsh list --all\n",
    "   virsh start kvm3\n",
    "   virsh console kvm3\n",
    "   ```\n",
    "\n",
    "   > **Troubleshooting Permission Errors:**\n",
    "   > If starting `kvm3` displays:\n",
    "   > ```text\n",
    "   > error: Failed to start domain 'kvm3'\n",
    "   > error: Cannot access storage file '/home/hostadm/KVM/kvm3.qcow2' (as uid:64055, gid:64055): Permission denied\n",
    "   > ```\n",
    "   > Grant access permissions to directory `/home/hostadm`:\n",
    "   > ```bash\n",
    "   > sudo chmod 755 /home/hostadm\n",
    "   > ```\n",
    "\n",
    "3. Update guest Hostname inside `kvm3`:\n",
    "   Log in as `hostadm`, gain root privileges (`sudo -s`), edit `/etc/hostname`, and change `kvm1` to `kvm3`.\n",
    "\n",
    "4. Reset System Machine ID on `kvm3`:\n",
    "   ```bash\n",
    "   ./vm_id_reset.sh\n",
    "   ```\n",
    "\n",
    "5. Reboot `kvm3`:\n",
    "   ```bash\n",
    "   reboot\n",
    "   ```\n",
    "\n",
    "---\n",
    "\n",
    "## Accessing and Mounting Virtual Disk Files Offline\n",
    "\n",
    "If a VM experiences boot failures, you can mount its underlying `qcow2` virtual disk directly on the host to examine system logs and repair configuration files.\n",
    "\n",
    "1. Shut down target VM `kvm3`:\n",
    "   ```bash\n",
    "   virsh shutdown kvm3\n",
    "   ```\n",
    "\n",
    "2. Load host Network Block Device (`nbd`) kernel module:\n",
    "   ```bash\n",
    "   sudo -s\n",
    "   cd KVM\n",
    "   modprobe nbd max_part=2\n",
    "   qemu-nbd --connect=/dev/nbd0 /home/hostadm/KVM/kvm3.qcow2\n",
    "   ```\n",
    "\n",
    "3. Create a local mount directory:\n",
    "   ```bash\n",
    "   mkdir -p /mnt/vm\n",
    "   ```\n",
    "\n",
    "4. List partitions mapped onto device `/dev/nbd0`:\n",
    "   ```bash\n",
    "   fdisk -l /dev/nbd0\n",
    "   ```\n",
    "\n",
    "5. Mount target Linux filesystem partition (e.g., `/dev/nbd0p1`):\n",
    "   ```bash\n",
    "   mount /dev/nbd0p1 /mnt/vm\n",
    "   ```\n",
    "\n",
    "6. Inspect/modify files on the virtual disk:\n",
    "   ```bash\n",
    "   cd /mnt/vm\n",
    "   ls -la\n",
    "   cat etc/hostname\n",
    "   ```\n",
    "\n",
    "7. Safely unmount filesystem and disconnect block device:\n",
    "   ```bash\n",
    "   cd\n",
    "   umount /mnt/vm\n",
    "   qemu-nbd --disconnect /dev/nbd0\n",
    "   ```\n",
    "\n",
    "---\n",
    "\n",
    "## Expanding Virtual Machine Disk Capacity\n",
    "\n",
    "Expanding guest disk space involves three sequential steps:\n",
    "1. Increase physical capacity of the host `.qcow2` image file.\n",
    "2. Expand partition table boundaries inside the virtual machine.\n",
    "3. Resize internal guest filesystem structures.\n",
    "\n",
    "### Step 1: Expand Virtual Disk File Size\n",
    "\n",
    "1. Install `libguestfs-tools` on the host system:\n",
    "   ```bash\n",
    "   sudo apt install libguestfs-tools\n",
    "   ```\n",
    "\n",
    "2. Ensure `kvm3` is completely powered down:\n",
    "   ```bash\n",
    "   virsh shutdown kvm3\n",
    "   ```\n",
    "\n",
    "3. Inspect current image capacity and structural information:\n",
    "   ```bash\n",
    "   qemu-img info KVM/kvm3.qcow2\n",
    "   sudo virt-filesystems --long -h --all -a KVM/kvm3.qcow2\n",
    "   ```\n",
    "\n",
    "4. Resize virtual disk image to 10 GB:\n",
    "   ```bash\n",
    "   qemu-img resize KVM/kvm3.qcow2 10G\n",
    "   ```\n",
    "\n",
    "5. Verify updated virtual disk size:\n",
    "   ```bash\n",
    "   qemu-img info KVM/kvm3.qcow2\n",
    "   ```\n",
    "\n",
    "---\n",
    "\n",
    "### Step 2: Grow Partition and Filesystem inside Guest OS\n",
    "\n",
    "1. Start `kvm3` and connect to guest console:\n",
    "   ```bash\n",
    "   virsh start kvm3\n",
    "   virsh console kvm3\n",
    "   ```\n",
    "\n",
    "2. Inspect current block device geometry:\n",
    "   ```bash\n",
    "   lsblk\n",
    "   ```\n",
    "\n",
    "3. Check existing filesystem space utilization:\n",
    "   ```bash\n",
    "   df -h\n",
    "   ```\n",
    "\n",
    "4. Install guest cloud partitioning tools inside guest OS:\n",
    "   ```bash\n",
    "   sudo apt install cloud-guest-utils\n",
    "   ```\n",
    "\n",
    "5. Expand target root partition boundary (e.g., partition `1` on `/dev/vda`):\n",
    "   ```bash\n",
    "   sudo growpart /dev/vda 1\n",
    "   ```\n",
    "\n",
    "6. Resize filesystem (`ext4` format):\n",
    "   ```bash\n",
    "   sudo resize2fs /dev/vda1\n",
    "   ```\n",
    "\n",
    "   > **Note:** The `resize2fs` command specifically targets `ext4`/`ext3` filesystems. For `xfs` filesystems (commonly used in Red Hat/Rocky distributions), use `xfs_growfs` instead.\n",
    "\n",
    "7. Verify expanded filesystem capacity:\n",
    "   ```bash\n",
    "   df -h\n",
    "   ```\n",
    "\n",
    "---\n",
    "\n",
    "## Deleting a Virtual Machine\n",
    "\n",
    "1. Ensure VM `kvm3` is powered off:\n",
    "   ```bash\n",
    "   virsh shutdown kvm3\n",
    "   ```\n",
    "\n",
    "2. Unregister domain configuration from KVM:\n",
    "   ```bash\n",
    "   virsh undefine kvm3\n",
    "   ```\n",
    "   *If NVRAM error occurs during undefine, append `--nvram`:*\n",
    "   ```bash\n",
    "   virsh undefine kvm3 --nvram\n",
    "   ```\n",
    "\n",
    "3. Delete corresponding disk image file from host filesystem:\n",
    "   ```bash\n",
    "   rm KVM/kvm3.qcow2\n",
    "   ```\n",
    "\n",
    "4. Confirm deletion by checking hypervisor inventory:\n",
    "   ```bash\n",
    "   virsh list --all\n",
    "   ```\n",
    "\n",
    "---\n",
    "\n",
    "## Deploying a Rocky Linux Cloud Base Image\n",
    "\n",
    "1. Navigate to local `KVM` working directory:\n",
    "   ```bash\n",
    "   cd KVM\n",
    "   ```\n",
    "\n",
    "2. Convert and copy downloaded generic base image into system VM disk location:\n",
    "   ```bash\n",
    "   sudo qemu-img convert -f qcow2 -O qcow2 Rocky-10-GenericCloud-Base.latest.x86_64.qcow2 /var/lib/libvirt/images/rocky10.qcow2\n",
    "   ```\n",
    "\n",
    "3. Provision VM `rocky10` (1 vCPU core, 1024 MB RAM, UEFI boot mode):\n",
    "   ```bash\n",
    "   virt-install --name rocky10 --memory 1024 --vcpus 1 --os-variant rhel10-unknown --disk /var/lib/libvirt/images/rocky10.qcow2 --virt-type kvm --graphics none --import --network network=default,model=virtio --cloud-init --boot uefi\n",
    "   ```\n",
    "\n",
    "4. After the system boots up, disconnect from the console (`Ctrl + ]`).\n",
    "\n",
    "5. Shutdown the VM:\n",
    "   ```bash\n",
    "   virsh shutdown rocky10 \n",
    "   ```\n",
    "\n",
    "6. Ensure VM `rocky10` is powered off:\n",
    "   ```bash\n",
    "   virsh list --all\n",
    "   ```\n",
    "\n",
    "7. Set new root password directly using `virt-customize`:\n",
    "   ```bash\n",
    "   sudo virt-customize -a /var/lib/libvirt/images/rocky10.qcow2 --root-password password:coolpass\n",
    "   ```\n",
    "\n",
    "---\n",
    "\n",
    "## Customizing Rocky Linux Guest Installation\n",
    "\n",
    "1. Start `rocky10` instance and connect to guest console:\n",
    "   ```bash\n",
    "   virsh start rocky10\n",
    "   virsh console rocky10\n",
    "   ```\n",
    "   *Log in as user `root` using password `coolpass`.*\n",
    "\n",
    "   You will be prompted to reset the root password. Use `unisyscoolpass`.\n",
    "\n",
    "2. Install text editor utility `nano`:\n",
    "   ```bash\n",
    "   dnf install nano -y\n",
    "   ```\n",
    "\n",
    "3. Set local system hostname:\n",
    "   ```bash\n",
    "   nano /etc/hostname\n",
    "   ```\n",
    "   *Add `rocky10` to file contents and save.*\n",
    "\n",
    "4. Add administrative user `hostadm`:\n",
    "   ```bash\n",
    "   adduser hostadm\n",
    "   passwd hostadm\n",
    "   ```\n",
    "\n",
    "5. Grant administrative `sudo` rights by assigning `wheel` group membership:\n",
    "   ```bash\n",
    "   usermod -a -G wheel hostadm\n",
    "   ```\n",
    "\n",
    "6. Reboot system to confirm configuration persistence:\n",
    "   ```bash\n",
    "   reboot\n",
    "   ```\n",
    "\n",
    "7. After verifying system boot and `sudo` privilege escalation for user `hostadm`, issue clean console shutdown command:\n",
    "   ```bash\n",
    "   shutdown -h now\n",
    "   ```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
