Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added secure delete for the image partition #140

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Tests/WinImageBuilder.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ Describe "Test Resize-VHDImage" {
Mock Optimize-Volume -Verifiable -ModuleName $moduleName { return }
Mock Get-PartitionSupportedSize -Verifiable -ModuleName $moduleName { return @{"SizeMin" = 100} }
Mock Resize-Partition -Verifiable -ModuleName $moduleName { return 0 }
Mock Run-SecureDelete -Verifiable -ModuleName $moduleName { return 0 }
Mock Resize-VHD -Verifiable -ModuleName $moduleName { return 0 }
Mock Dismount-VHD -Verifiable -ModuleName $moduleName { return 0 }

Expand Down
33 changes: 29 additions & 4 deletions WinImageBuilder.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -791,9 +791,12 @@ function Resize-VHDImage {
Resize-Partition -DriveLetter $Drive -Size $sizeIncreased -ErrorAction "Stop"
}
}
}
finally
{
try {
Run-SecureDelete $Drive
} catch {
Write-Host $_
}
} finally {
Dismount-VHD -Path $VirtualDiskPath
}

Expand Down Expand Up @@ -850,6 +853,28 @@ function Wait-ForVMShutdown {
}
}

function Run-SecureDelete {
Param(
[Parameter(Mandatory=$true)]
[string]$DriveLetter,
[PathShouldExistAttribute()]
[string]$ddPath="${env:ProgramFiles}\Git\usr\bin\dd.exe"
)
$sdeleteTempFile = "${DriveLetter}:\sdelete-cache"
if (Test-Path $sdeleteTempFile) {
Remove-Item $sdeleteTempFile -Force
}
$cmd = "if=/dev/zero of='${sdeleteTempFile}'"
$p = Start-Process -FilePath $ddPath -ArgumentList $cmd -NoNewWindow -PassThru -Wait
$exitCode= $p.ExitCode
if (($exitCode -eq 1) -and (Test-Path $sdeleteTempFile)) {
Remove-Item $sdeleteTempFile -Force
Write-Host "Executed secure delete successfully"
} else {
throw "Failed to run secure delete using ``dd.exe $cmd`` with $exitCode"
}
}

function Run-Sysprep {
Param(
[Parameter(Mandatory=$true)]
Expand Down Expand Up @@ -1287,4 +1312,4 @@ function New-WindowsFromGoldenImage {
}

Export-ModuleMember New-WindowsCloudImage, Get-WimFileImagesInfo, New-MaaSImage, Resize-VHDImage,
New-WindowsOnlineImage, Add-VirtIODriversFromISO, New-WindowsFromGoldenImage
New-WindowsOnlineImage, Add-VirtIODriversFromISO, New-WindowsFromGoldenImage, Run-SecureDelete