-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.ps1
158 lines (139 loc) · 5.06 KB
/
build.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]
$Name
,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]
$Path
,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]
$IsoPath
,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]
$EditionIndex
,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]
$UpdatePath
,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]
$MountPath
,
[Parameter()]
[ValidateSet('CE', 'EE')]
[string]
$DockerEdition = 'CE'
,
[Parameter()]
[ValidateSet('stable', 'edge', 'testing')]
[string]
$DockerChannel = 'stable'
,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]
$DockerVersion
)
#$IsoPath = "$PSScriptRoot\en_windows_server_version_1709_x64_dvd_100090904.iso"
#$EditionIndex = 2
#$Name = 'Server1709'
#$IsoPath = "$PSScriptRoot\14393.0.161119-1705.RS1_REFRESH_SERVER_EVAL_X64FRE_EN-US.ISO"
#$EditionIndex = 3
#$Name = 'ws16eval'
$BaseImagePath = "$Path\$Name.vhdx"
$PatchedImagePath = "$Path\$Name-patch.vhdx"
$DockerImagePath = "$Path\$Name-docker.vhdx"
$MergedImagePath = "$Path\$Name-final.vhdx"
$DiffImagePath = "$Path\$Name-diff.vhdx"
if (-not (Test-Path -Path .\Convert-WindowsImage.ps1)) {
Write-Error 'Please obtain Convert-WindowsImage.ps1 from ISO of Windows Server 2016 and place in save directory.'
return
}
. .\Convert-WindowsImage.ps1
. "$PSScriptRoot\functions.ps1"
if (-not $DockerVersion) {
$DockerVersion = Get-DockerVersion -Edition $DockerEdition -Channel $DockerChannel
}
"https://download.docker.com/win/static/$DockerChannel/x86_64/docker-$DockerVersion-$($DockerEdition.ToLower()).zip" | Set-Content -Path "$PSScriptRoot\c\docker_url.txt"
if (-not (Test-Path -Path $BaseImagePath)) {
$IsoPath = Get-Item -Path $IsoPath
if (-not (Test-Path -Path $IsoPath)) {
Write-Warning 'Specified ISO does not exist'
return
}
$DriveLetter = Mount-Image -Path $IsoPath
if (-not $DriveLetter) {
Write-Warning 'Unable to determine drive letter'
return
}
if (-not $EditionIndex) {
Write-Warning 'EditionIndex not specified'
dism /Get-WimInfo /WimFile:"$($DriveLetter):\sources\install.wim"
return
}
Convert-WindowsImage -SourcePath "$($DriveLetter):\sources\install.wim" -Edition $EditionIndex -VHDPath $BaseImagePath -SizeBytes 128GB -VHDFormat VHDX -DiskLayout UEFI
Dismount-DiskImage -ImagePath $IsoPath
}
if (-not $UpdatePath) {
$null = New-VHD -Path $PatchedImagePath -ParentPath $BaseImagePath -Differencing -ErrorAction Stop
}
if (-not (Test-Path -Path $PatchedImagePath)) {
$null = New-VHD -Path $PatchedImagePath -ParentPath $BaseImagePath -Differencing -ErrorAction Stop
$PatchedImagePath = Get-Item -Path $PatchedImagePath
$Data = dism /Get-ImageInfo /ImageFile:$PatchedImagePath /Index:1
$Line = $Data | Where-Object { $_ -like 'Version : *' }
if ($Line -match '10.0.(.+)$') {
$Build = $Matches[1]
}
if (-not $Build) {
Write-Warning 'Unable to determine build version'
return
}
$Files = Get-ChildItem -Path $UpdatePath | Select-Object -ExpandProperty FullName
$UpdateDefinition = Get-UpdateDefinition -Build $Build
$LatestUpdate = Find-Update -UpdateDefinition $UpdateDefinition -Files $Files
if ($LatestUpdate) {
Mount-ImageUsingDism -ImagePath $PatchedImagePath -MountPath $MountPath
if (Test-ImageUsingDism -ImagePath $PatchedImagePath) {
Add-UpdateUsingDism -MountPath $MountPath -UpdatePath $LatestUpdate
$Files | Where-Object { $_ -ne $LatestUpdate } | ForEach-Object {
Add-UpdateUsingDism -MountPath $MountPath -UpdatePath $_
}
Unmount-ImageUsingDism -MountPath $MountPath
} else {
Write-Warning 'Unable to mount for patching'
return
}
}
}
if (-not (Test-Path -Path $DockerImagePath)) {
$null = New-VHD -Path $DockerImagePath -ParentPath $PatchedImagePath -Differencing -ErrorAction Stop
$DockerImagePath = Get-Item -Path $DockerImagePath
# TODO add admin password from parameter
Mount-ImageUsingDism -ImagePath $DockerImagePath -MountPath $MountPath
if (Test-ImageUsingDism -ImagePath $DockerImagePath) {
Add-FeatureUsingDism -MountPath $MountPath -FeatureName 'Containers'
Copy-Item -Path "$PSScriptRoot\c\*" -Destination $MountPath -Recurse -Force
Unmount-ImageUsingDism -MountPath $MountPath
}
}
if (-not (Test-Path -Path $MergedImagePath)) {
Convert-VHD -Path $DockerImagePath -DestinationPath $MergedImagePath -VHDType Dynamic -ErrorAction Stop
}
if (-not (Test-Path -Path $DiffImagePath)) {
$null = New-VHD -Path $DiffImagePath -ParentPath $MergedImagePath -Differencing -ErrorAction Stop
Get-VM -Name test | Get-VMHardDiskDrive | Remove-VMHardDiskDrive
$HardDrive = Get-VM -Name test | Add-VMHardDiskDrive -Path $DiffImagePath -Passthru
Set-VMFirmware -VMName test -FirstBootDevice $HardDrive
}