Skip to content

Commit 6682c22

Browse files
committed
Remove Ubuntu install disk after install seems done
1 parent 0083235 commit 6682c22

File tree

2 files changed

+18
-31
lines changed

2 files changed

+18
-31
lines changed

AutomatedLabWorker/functions/VirtualMachines/New-LWHypervVM.ps1

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -556,36 +556,6 @@ rm -rf /etc/cron.d/postconf
556556

557557
$vm = Hyper-V\New-VM @vmParameter
558558

559-
if ($vm.Generation -ge 2 -and $Machine.OperatingSystemType -eq 'Linux' -and $Machine.LinuxType -eq 'Ubuntu')
560-
{
561-
$systemDisk = $vm | Get-VMHardDiskDrive
562-
$ubuntuSpecialDisk = $vm | Add-VMHardDiskDrive -Path "$vmPath\$($Machine.ResourceName)_INSTALL.vhdx" -Passthru
563-
$vm | Set-VMFirmware -BootOrder $systemDisk, $ubuntuSpecialDisk
564-
565-
Start-Job -Name "Ensure Ubuntu Boot Order $($Machine.ResourceName)" -ScriptBlock {
566-
param
567-
(
568-
$Name
569-
)
570-
571-
# Boot order of Ubuntu system changes during installation. We need to ensure the OS
572-
# is booted, lest it is stuck in an installation loop.
573-
# No cancellation token, we'll remove the job if the lab installation has finished.
574-
while ($true)
575-
{
576-
Write-Verbose "Reordering boot order for Ubuntu VM '$Name'"
577-
$linvm = Get-LWHypervVM -Name $Name
578-
$order = ($linvm | Get-VMFirmware).BootOrder
579-
if ($order[0].BootType -eq 'Drive' -and $order[0].Device.Path -like "*_INSTALL*")
580-
{
581-
$newOrder = $order[1..$order.Count] + $order[0]
582-
$linvm | Set-VMFirmware -BootOrder $newOrder
583-
}
584-
Start-Sleep -Seconds 1
585-
}
586-
} -ArgumentList $Machine.ResourceName
587-
}
588-
589559
Set-LWHypervVMDescription -ComputerName $Machine.ResourceName -Hashtable @{
590560
CreatedBy = '{0} ({1})' -f $PSCmdlet.MyInvocation.MyCommand.Module.Name, $PSCmdlet.MyInvocation.MyCommand.Module.Version
591561
CreationTime = Get-Date

AutomatedLabWorker/functions/VirtualMachines/Start-LWHypervVM.ps1

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,27 @@
2424
foreach ($Name in $(Get-LabVM -ComputerName $ComputerName -IncludeLinux | Where-Object SkipDeployment -eq $false))
2525
{
2626
$machine = Get-LabVM -ComputerName $Name -IncludeLinux
27+
$hvMachine = Get-LWHypervVM -Name $Name.ResourceName
28+
29+
<#
30+
Remove _INSTALL.vhdx on Ubuntu if the VM has been shut down once - indicating that the
31+
cloudinit/subiquity phase was successfully finished.
32+
We compare GuestStatePath LastWriteTime as a simple and quick way to check if
33+
the VM's status has changed i.e. when it was stopped.
34+
One minute seems like a sane interval, we might need to increase it in the future.
35+
#>
36+
if ($hvMachine.State -ne 'Running' -and ((Get-Item -Path $hvMachine.GuestStatePath).LastWriteTime - $hvMachine.CreationTime) -gt '00:01:00')
37+
{
38+
Write-ScreenInfo -Type Verbose "Removing installation disk '$Name'"
39+
$disk = $hvMachine | Get-VMHardDiskDrive | Where-Object Path -like "*_INSTALL*"
40+
$diskPath = $disk.Path # Otherwise $disk will be update after remove-vmharddiskdrive was called
41+
$disk | Remove-VMHardDiskDrive
42+
Remove-Item -Path $diskPath -Force
43+
}
2744

2845
try
2946
{
30-
Get-LWHypervVM -Name $Name.ResourceName | Hyper-V\Start-VM -ErrorAction Stop
47+
$hvMachine | Hyper-V\Start-VM -ErrorAction Stop
3148
}
3249
catch
3350
{

0 commit comments

Comments
 (0)