Milestone 12 ‐ HyperV ‐ Linked Clones and Automation - jacobwilliams100/sys-350 GitHub Wiki
Parent Disk
Start by downloading ubuntu-24.04.1-desktop-amd64.iso
from 192.168.3.185/files/
Now in Hyper-V Manager, create a New Virtual Machine
Name it "Ubuntu24-base"
Pick "Generation 2"
Bump it up to 4GB RAM
Put it on HYPER-V WAN
20GB is enough storage
Make sure to set the the installer to the iso we downloaded a second ago
Then in settings, make sure to disable Secure Boot, and Add another vCPU while you're at it
Turn on, and go ahead with the initial setup
When it wraps up, go ahead and restart
When its back up, download the baseline script from https://github.com/gmcyber/480share/blob/master/hyperv-ubuntu-sealer.sh and run it
I decided to install a couple more programs just for variety:
-
Audacity
-
VLC Media Player
-
GIMP
And we will also create a folder "test" on desktop before powering off
Try taking a snapshot
Deliverable 1. Hunt down the VHD file associated with your Ubuntu Base Image and give that file read only permissions. Provide a screenshot.
In my case, Ubuntu24-base.vhdx was located at C:\Users\Public\Documents\Hyper-V\Virtual hard disks
Read-Only is important for this Disk to make sure it remains static. The plan isn't to actually use Ubuntu24-base, but to use it as a basis for Linked Clones to save on space. Because of this, the base really should not change.
Child Disk
Back in Hyper-V Manager deselect Ubuntu24-base and Go to Actions->Hard Disk...
VHDX is fine
IMPORTANT: Select Differencing
Name it "sonofubuntu", default location is fine
And select Ubuntu24-base.vhdx as the Parent Disk
It should look like this.
Now we will create a new VM with the same specs as Ubuntu24-base, but using sonofubuntu.vhdx as its storage
You should now be able to boot into sonofubuntu, and log in with the same credentials as the base image. It has the same software we installed earlier.
Go ahead and make a very simple change, like drawing a picture and saving it to desktop
Deliverable 2. Provide a screenshot that shows ‘sonofubuntu’ running as well as the very tiny difference in disk attributes.
Automation
We will be automating Hyper-V with Powershell
Deliverable 3. stop sonofubuntu
Stop-VM -Name "sonofubuntu" -Force
Deliverable 4. take a checkpoint of sonofubuntu called snapshot1
Checkpoint-VM -Name "sonofubuntu" -SnapshotName "snapshot1"
Deliverable 5. start sonofubuntu
Start-VM -Name "sonofubuntu"
Deliverable 6. switch sonofubuntu to another network
sonofubuntu must be off first.
To move sonofubuntu to LAN-INTERNAL:
Set-VMNetworkAdapter -VMName "sonofubuntu" -Name "Network Adapter" -SwitchName "LAN-INTERNAL"
Then use Get-VMNetworkAdapter -VMName "sonofubuntu"
to see if it changed
As we can see in the Settings, sonofubuntu is now on LAN-INTERNAL
Deliverable 7. Create a new Base VM using an OS that is not Ubuntu.
Research & write a script to automate the creation of a Linked Clone of your new OS base image using Powershell. Provide a screenshot of your successful script/command(s) and a screenshot of your running OS and the virtual properties of your child disk.
I have decided to create a Manjaro VM
Prepping install for Hyper-V
Once this is done, shut down.
With the assistance of ChatGPT, I automated the process of creating a Linked Clone of Manjaro-base.vhdx called sonofmanjaro.vhdx using the Powershell script LinkedClone.ps1
https://github.com/jacobwilliams100/sys-350/blob/main/LinkedClone.ps1
# Drafted using ChatGPT
# Asks user to specify parent filepath and child filepath,
# Sets parent filepath to "read only"
# Creates a linked clone at the child filepath using the parent filepath as parent
# Then creates a new VM using the child filepath as storage, effectively creating a cloned VM
# Getting information from user
$ParentPath = Read-Host "Enter the full path to the parent .vhdx file"
$ChildPath = Read-Host "Enter the full path to the child .vhdx file"
$VMName = Read-Host "Enter the name for the cloned VM"
# Check if ParentPath exists
if (-Not (Test-Path -Path $ParentPath)) {
Write-Error "Parent VHDX file not found at $ParentPath"
exit
}
# Set parent disk to read-only if it isn't yet
try {
$ParentFile = Get-Item -Path $ParentPath
Set-ItemProperty -Path $ParentFile.FullName -Name IsReadOnly -Value $true
Write-Host "Parent VHDX file has been set to read-only."
} catch {
Write-Error: "Failed to set the parent VHDX file to read only: $_"
exit
}
# create a differencing disk (linked clone)
try {
New-VHD -Path $ChildPath -ParentPath $ParentPath -Differencing
Write-Host "Linked clone created successfully at $ChildPath"
} catch {
Write-Error "Failed to create linked clone: $_"
}
# create a new VM using new differencing disk
try {
# 4GB RAM, 2VCPU, Gen 2, Secure Boot Off, Hyper-V WAN Network, linked clone as storage
$VM = New-VM -Name $VMName -MemoryStartupBytes 4GB -Generation 2
Add-VMHardDiskDrive -VMName $VMName -Path $ChildPath
Set-VMProcessor -VMName $VMName -Count 2
Set-VMFirmware -VMName $VMName -EnableSecureBoot Off
Add-VMNetworkAdapter -VMName $VMName -SwitchName "Hyper-V WAN"
Write-Host "Virtual Machine '$VMName' created using '$ChildPath' as storage."
} catch {
Write-Error "Failed to create or configure the VM: $_"
}
It sets the Parent Disk to Read-Only
It creates a Linked Clone of the Parent Disk (notice the small file size)
And it creates a VM, with the correct specs, and using sonofmanjaro.vhdx as storage. So when it is turned on and accessed, it will appear identical to Manjaro-base which is the VHDX it was linked from.
Reflection
I really enjoyed this lab. It was a bit more difficult than I expected, but still much easier than the PyVmomi labs for VMWare. One of the nice things about Windows is the software integration; Powershell communicates with Hyper-V seamlessly without having to import a bunch of Python dependencies. The hardest part was looking up which commands to use, which was not even that hard. The script processing was very responsive and gave quite useful feedback when something failed. I was also a bit suprised by how well Hyper-V handled a non-Debian Linux distribution like Majaro. I didn't run into any serious issues with installing or running it. After doing these labs, I'm suprised Hyper-V isn't actually more popular, as it "just works" (and a lot better than my experience with VMWare for sure). I suspect it might not scale as well as VMWare or may not work as well for larger, distributed virtualization applications. For example, I did not notice any way to manage multiple bare-metal servers like you can with hosts on VCenter.