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/

{411871B7-A7A1-4195-9034-042F699AB8DF}

Now in Hyper-V Manager, create a New Virtual Machine

Name it "Ubuntu24-base"

{9904112C-60B7-4666-AEAE-E40B6A8B16A3}

Pick "Generation 2"

{C2C02888-629B-4A06-9293-444F0DA75962}

Bump it up to 4GB RAM

{2D65077C-93B9-413B-940A-FF9C468C43F6}

Put it on HYPER-V WAN

{9127EC04-8A5C-4DA9-A78C-AE87FBB9D763}

20GB is enough storage

{0EAC5D23-FDA1-4943-B0CE-F3598230F689}

Make sure to set the the installer to the iso we downloaded a second ago

{37DCABDF-F099-4640-8C92-790F715B1A7B}

Then in settings, make sure to disable Secure Boot, and Add another vCPU while you're at it

{5AE03EFD-74C5-4D8A-A9AB-AA17832A71ED}

Turn on, and go ahead with the initial setup

{26EC16C4-0CFF-4CC8-82E2-CDAC8399EDD4}

{9020AA07-228D-4363-9D7C-200374029EEE}

When it wraps up, go ahead and restart

{DA0EEB87-9CF1-4478-BF15-AB3401CFA931}

When its back up, download the baseline script from https://github.com/gmcyber/480share/blob/master/hyperv-ubuntu-sealer.sh and run it

image

image

I decided to install a couple more programs just for variety:

  • Audacity

  • VLC Media Player

  • GIMP

image

image

image

image

And we will also create a folder "test" on desktop before powering off

image

image

Try taking a snapshot

image

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

image

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...

image

VHDX is fine

image

IMPORTANT: Select Differencing

image

Name it "sonofubuntu", default location is fine

image

And select Ubuntu24-base.vhdx as the Parent Disk

image

It should look like this.

image

Now we will create a new VM with the same specs as Ubuntu24-base, but using sonofubuntu.vhdx as its storage

image

image

image

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.

image

Go ahead and make a very simple change, like drawing a picture and saving it to desktop

image

Deliverable 2. Provide a screenshot that shows ‘sonofubuntu’ running as well as the very tiny difference in disk attributes.

image

Automation

We will be automating Hyper-V with Powershell

Deliverable 3. stop sonofubuntu

Stop-VM -Name "sonofubuntu" -Force

image

image

Deliverable 4. take a checkpoint of sonofubuntu called snapshot1

Checkpoint-VM -Name "sonofubuntu" -SnapshotName "snapshot1"

image

image

Deliverable 5. start sonofubuntu

Start-VM -Name "sonofubuntu"

image

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

image

As we can see in the Settings, sonofubuntu is now on LAN-INTERNAL

image

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

image

image

Prepping install for Hyper-V

image

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: $_"
}

image

It sets the Parent Disk to Read-Only

image

It creates a Linked Clone of the Parent Disk (notice the small file size)

image

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.

image

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.