Skip to content

Commit

Permalink
Remove Ubuntu install disk after install seems done
Browse files Browse the repository at this point in the history
  • Loading branch information
nyanhp committed Feb 21, 2024
1 parent 0083235 commit 870387c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
23 changes: 0 additions & 23 deletions AutomatedLabWorker/functions/VirtualMachines/New-LWHypervVM.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -561,29 +561,6 @@ rm -rf /etc/cron.d/postconf
$systemDisk = $vm | Get-VMHardDiskDrive
$ubuntuSpecialDisk = $vm | Add-VMHardDiskDrive -Path "$vmPath\$($Machine.ResourceName)_INSTALL.vhdx" -Passthru
$vm | Set-VMFirmware -BootOrder $systemDisk, $ubuntuSpecialDisk

Start-Job -Name "Ensure Ubuntu Boot Order $($Machine.ResourceName)" -ScriptBlock {
param
(
$Name
)

# Boot order of Ubuntu system changes during installation. We need to ensure the OS
# is booted, lest it is stuck in an installation loop.
# No cancellation token, we'll remove the job if the lab installation has finished.
while ($true)
{
Write-Verbose "Reordering boot order for Ubuntu VM '$Name'"
$linvm = Get-LWHypervVM -Name $Name
$order = ($linvm | Get-VMFirmware).BootOrder
if ($order[0].BootType -eq 'Drive' -and $order[0].Device.Path -like "*_INSTALL*")
{
$newOrder = $order[1..$order.Count] + $order[0]
$linvm | Set-VMFirmware -BootOrder $newOrder
}
Start-Sleep -Seconds 1
}
} -ArgumentList $Machine.ResourceName
}

Set-LWHypervVMDescription -ComputerName $Machine.ResourceName -Hashtable @{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,27 @@
foreach ($Name in $(Get-LabVM -ComputerName $ComputerName -IncludeLinux | Where-Object SkipDeployment -eq $false))
{
$machine = Get-LabVM -ComputerName $Name -IncludeLinux
$hvMachine = Get-LWHypervVM -Name $Name.ResourceName

<#
Remove _INSTALL.vhdx on Ubuntu if the VM has been shut down once - indicating that the
cloudinit/subiquity phase was successfully finished.
We compare GuestStatePath LastWriteTime as a simple and quick way to check if
the VM's status has changed i.e. when it was stopped.
One minute seems like a sane interval, we might need to increase it in the future.
#>
if ($hvMachine.State -ne 'Running' -and ((Get-Item -Path $hvMachine.GuestStatePath).LastWriteTime - $hvMachine.CreationTime) -gt '00:01:00')
{
Write-ScreenInfo -Type Verbose "Removing installation disk '$Name'"
$disk = $hvMachine | Get-VMHardDiskDrive | Where-Object Path -like "*_INSTALL*"
$diskPath = $disk.Path # Otherwise $disk will be update after remove-vmharddiskdrive was called
$disk | Remove-VMHardDiskDrive
Remove-Item -Path $diskPath -Force
}

try
{
Get-LWHypervVM -Name $Name.ResourceName | Hyper-V\Start-VM -ErrorAction Stop
$hvMachine | Hyper-V\Start-VM -ErrorAction Stop
}
catch
{
Expand Down

0 comments on commit 870387c

Please sign in to comment.