-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathliveosisobuilder.go
More file actions
291 lines (241 loc) · 11.3 KB
/
liveosisobuilder.go
File metadata and controls
291 lines (241 loc) · 11.3 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package imagecustomizerlib
import (
"fmt"
"os"
"path/filepath"
"github.com/microsoft/azurelinux/toolkit/tools/imagecustomizerapi"
"github.com/microsoft/azurelinux/toolkit/tools/internal/file"
"github.com/microsoft/azurelinux/toolkit/tools/internal/isogenerator"
"github.com/microsoft/azurelinux/toolkit/tools/internal/logger"
)
func populateWriteableRootfsDir(sourceDir, writeableRootfsDir string) error {
logger.Log.Debugf("Creating writeable rootfs")
err := os.MkdirAll(writeableRootfsDir, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create folder %s:\n%w", writeableRootfsDir, err)
}
err = copyPartitionFiles(sourceDir+"/.", writeableRootfsDir)
if err != nil {
return fmt.Errorf("failed to copy rootfs contents to a writeable folder (%s):\n%w", writeableRootfsDir, err)
}
return nil
}
func createLiveOSIsoImage(buildDir, baseConfigPath string, inputArtifactsStore *IsoArtifactsStore, requestedSelinuxMode imagecustomizerapi.SELinuxMode,
isoConfig *imagecustomizerapi.Iso, pxeConfig *imagecustomizerapi.Pxe, rawImageFile, outputImagePath string,
outputPXEArtifactsDir string) (err error) {
var extraCommandLine []string
var additionalIsoFiles imagecustomizerapi.AdditionalFileList
if isoConfig != nil {
extraCommandLine = isoConfig.KernelCommandLine.ExtraCommandLine
additionalIsoFiles = isoConfig.AdditionalFiles
}
pxeIsoImageBaseUrl := ""
if pxeConfig != nil {
pxeIsoImageBaseUrl = pxeConfig.IsoImageBaseUrl
}
pxeIsoImageFileUrl := ""
if pxeConfig != nil {
pxeIsoImageFileUrl = pxeConfig.IsoImageFileUrl
}
isoBuildDir := filepath.Join(buildDir, "liveosbuild")
defer func() {
cleanupErr := os.RemoveAll(isoBuildDir)
if cleanupErr != nil {
if err != nil {
err = fmt.Errorf("%w:\nfailed to clean-up (%s): %w", err, isoBuildDir, cleanupErr)
} else {
err = fmt.Errorf("failed to clean-up (%s): %w", isoBuildDir, cleanupErr)
}
}
}()
logger.Log.Debugf("Connecting to raw image (%s)", rawImageFile)
rawImageConnection, _, _, _, err := connectToExistingImage(rawImageFile, isoBuildDir, "readonly-rootfs-mount", false /*includeDefaultMounts*/)
if err != nil {
return err
}
defer rawImageConnection.Close()
// From raw image to a writeable folder
writeableRootfsDir := filepath.Join(isoBuildDir, "writeable-rootfs")
err = populateWriteableRootfsDir(rawImageConnection.Chroot().RootDir(), writeableRootfsDir)
if err != nil {
return fmt.Errorf("failed to copy the contents of rootfs from image (%s) to local folder (%s):\n%w", rawImageFile, writeableRootfsDir, err)
}
// Create the ISO artifacts store
storeFolder := filepath.Join(isoBuildDir, "from-iso-and-raw")
artifactsStore, err := createIsoArtifactStoreFromMountedImage(inputArtifactsStore, writeableRootfsDir, storeFolder)
if err != nil {
return err
}
// Combine the current configuration with the saved configuration
updatedSavedConfigs, err := updateSavedConfigs(artifactsStore.files.savedConfigsFilePath, extraCommandLine, pxeIsoImageBaseUrl,
pxeIsoImageFileUrl, artifactsStore.info.dracutPackageInfo, requestedSelinuxMode, artifactsStore.info.selinuxPolicyPackageInfo)
if err != nil {
return fmt.Errorf("failed to combine saved configurations with new configuration:\n%w", err)
}
// Figure out the selinux situation
// Note that by now, the user selinux config has been applied to the image,
// so checking only 'imageSELinuxMode' is sufficient to determine whether
// selinux is enabled or not for this image (regardless of the source of
// that configuration).
disableSELinux := false
if artifactsStore.info.seLinuxMode != imagecustomizerapi.SELinuxModeDisabled {
// SELinux is enabled (either in the base image, or requested by the user)
err = verifyNoLiveOsSelinuxBlockers(updatedSavedConfigs.OS.DracutPackageInfo, updatedSavedConfigs.OS.SELinuxPolicyPackageInfo)
if err != nil {
// We need to determine whether the source of enablment is user
// explicit configuration or the base image.
if updatedSavedConfigs.OS.RequestedSELinuxMode != imagecustomizerapi.SELinuxModeDisabled &&
updatedSavedConfigs.OS.RequestedSELinuxMode != imagecustomizerapi.SELinuxModeDefault {
return fmt.Errorf("SELinux cannot be enabled due to older dracut and selinux-policy package versions:\n%w", err)
} else {
logger.Log.Infof("SELinux disabled due to older dracut and selinux-policy package versions:\n%s", err)
}
disableSELinux = true
}
}
// Update grub.cfg
err = updateGrubCfg(artifactsStore.files.isoGrubCfgPath, artifactsStore.files.pxeGrubCfgPath, disableSELinux,
updatedSavedConfigs, filepath.Base(outputImagePath))
if err != nil {
return fmt.Errorf("failed to update grub.cfg:\n%w", err)
}
// Generate the ISO bootimage (/boot/grub2/efiboot.img)
artifactsStore.files.isoBootImagePath = filepath.Join(artifactsStore.files.artifactsDir, isoBootImagePath)
err = isogenerator.BuildIsoBootImage(isoBuildDir, artifactsStore.files.bootEfiPath,
artifactsStore.files.grubEfiPath, artifactsStore.files.isoBootImagePath)
if err != nil {
return fmt.Errorf("failed to build iso boot image:\n%w", err)
}
// Generate the initrd image
outputInitrdPath := filepath.Join(artifactsStore.files.artifactsDir, initrdImage)
err = createInitrdImage(writeableRootfsDir, artifactsStore.info.kernelVersion, outputInitrdPath)
if err != nil {
return fmt.Errorf("failed to create initrd image:\n%w", err)
}
artifactsStore.files.initrdImagePath = outputInitrdPath
// Generate the squashfs image
outputSquashfsPath := filepath.Join(artifactsStore.files.artifactsDir, liveOSImage)
err = createSquashfsImage(writeableRootfsDir, outputSquashfsPath)
if err != nil {
return fmt.Errorf("failed to create squashfs image:\n%w", err)
}
artifactsStore.files.squashfsImagePath = outputSquashfsPath
// Generate the final iso image
err = createIsoImageAndPXEFolder(isoBuildDir, baseConfigPath, additionalIsoFiles, artifactsStore, outputImagePath, outputPXEArtifactsDir)
if err != nil {
return fmt.Errorf("failed to generate iso image and/or PXE artifacts folder\n%w", err)
}
return nil
}
func createImageFromUnchangedOS(isoBuildDir string, baseConfigPath string, isoConfig *imagecustomizerapi.Iso,
pxeConfig *imagecustomizerapi.Pxe, inputArtifactsStore *IsoArtifactsStore, outputImagePath string, outputPXEArtifactsDir string) error {
logger.Log.Infof("Creating LiveOS iso image using unchanged OS partitions")
var extraCommandLine []string
var additionalIsoFiles imagecustomizerapi.AdditionalFileList
if isoConfig != nil {
extraCommandLine = isoConfig.KernelCommandLine.ExtraCommandLine
additionalIsoFiles = isoConfig.AdditionalFiles
}
pxeIsoImageBaseUrl := ""
if pxeConfig != nil {
pxeIsoImageBaseUrl = pxeConfig.IsoImageBaseUrl
}
pxeIsoImageFileUrl := ""
if pxeConfig != nil {
pxeIsoImageFileUrl = pxeConfig.IsoImageFileUrl
}
// Note that in this ISO build flow, there is no os configuration, and hence
// no selinux configuration. So, we will set it to default (i.e. unspecified)
// and let any saved data override if present.
requestedSelinuxMode := imagecustomizerapi.SELinuxModeDefault
updatedSavedConfigs, err := updateSavedConfigs(inputArtifactsStore.files.savedConfigsFilePath, extraCommandLine, pxeIsoImageBaseUrl,
pxeIsoImageFileUrl, nil /*dracut pkg info*/, requestedSelinuxMode, nil /*selinux policy pkg info*/)
if err != nil {
return fmt.Errorf("failed to combine saved configurations with new configuration:\n%w", err)
}
// SELinux cannot be enabled/disabled in this flow since, by definition,
// the config os.selinux is not present. As a result, we will just keep
// SELinux configuration unchanged.
// Setting disableSELinux to false tells updateGrubCfg to, well, not disable
// selinux and not enable it either.
disableSELinux := false
// Update grub.cfg
err = updateGrubCfg(inputArtifactsStore.files.isoGrubCfgPath, inputArtifactsStore.files.pxeGrubCfgPath, disableSELinux, updatedSavedConfigs, filepath.Base(outputImagePath))
if err != nil {
return fmt.Errorf("failed to update grub.cfg:\n%w", err)
}
// Generate the final iso image
err = createIsoImageAndPXEFolder(isoBuildDir, baseConfigPath, additionalIsoFiles, inputArtifactsStore, outputImagePath, outputPXEArtifactsDir)
if err != nil {
return fmt.Errorf("failed to generate iso image and/or PXE artifacts folder\n%w", err)
}
return nil
}
func createIsoImageAndPXEFolder(buildDir string, baseConfigPath string, additionalIsoFiles imagecustomizerapi.AdditionalFileList, artifactsStore *IsoArtifactsStore, outputImagePath string,
outputPXEArtifactsDir string) error {
err := createIsoImage(buildDir, artifactsStore.files, baseConfigPath, additionalIsoFiles, outputImagePath)
if err != nil {
return fmt.Errorf("failed to create the Iso image.\n%w", err)
}
if outputPXEArtifactsDir != "" {
err = verifyDracutPXESupport(artifactsStore.info.dracutPackageInfo)
if err != nil {
return fmt.Errorf("failed to verify Dracut's PXE support.\n%w", err)
}
err = populatePXEArtifactsDir(outputImagePath, buildDir, outputPXEArtifactsDir)
if err != nil {
return fmt.Errorf("failed to populate the PXE artifacts folder.\n%w", err)
}
}
return nil
}
func populatePXEArtifactsDir(isoImagePath string, buildDir string, outputPXEArtifactsDir string) error {
logger.Log.Infof("Copying PXE artifacts to (%s)", outputPXEArtifactsDir)
// Extract all files from the iso image file.
err := extractIsoImageContents(buildDir, isoImagePath, outputPXEArtifactsDir)
if err != nil {
return err
}
// Replace the iso grub.cfg with the PXE grub.cfg
isoGrubCfgPath := filepath.Join(outputPXEArtifactsDir, grubCfgDir, isoGrubCfg)
pxeGrubCfgPath := filepath.Join(outputPXEArtifactsDir, grubCfgDir, pxeGrubCfg)
err = file.Copy(pxeGrubCfgPath, isoGrubCfgPath)
if err != nil {
return fmt.Errorf("failed to copy (%s) to (%s) while populating the PXE artifacts directory:\n%w", pxeGrubCfgPath, isoGrubCfgPath, err)
}
err = os.RemoveAll(pxeGrubCfgPath)
if err != nil {
return fmt.Errorf("failed to remove file (%s):\n%w", pxeGrubCfgPath, err)
}
_, bootFilesConfig, err := getBootArchConfig()
if err != nil {
return err
}
// Move bootloader files from under '<pxe-folder>/efi/boot' to '<pxe-folder>/'
bootloaderSrcDir := filepath.Join(outputPXEArtifactsDir, isoBootloadersDir)
bootloaderFiles := []string{bootFilesConfig.bootBinary, bootFilesConfig.grubBinary}
for _, bootloaderFile := range bootloaderFiles {
sourcePath := filepath.Join(bootloaderSrcDir, bootloaderFile)
targetPath := filepath.Join(outputPXEArtifactsDir, bootloaderFile)
err = file.Move(sourcePath, targetPath)
if err != nil {
return fmt.Errorf("failed to move boot loader file from (%s) to (%s) while generated the PXE artifacts folder:\n%w", sourcePath, targetPath, err)
}
}
// Remove the empty 'pxe-folder>/efi' folder.
isoEFIDir := filepath.Join(outputPXEArtifactsDir, "efi")
err = os.RemoveAll(isoEFIDir)
if err != nil {
return fmt.Errorf("failed to remove folder (%s):\n%w", isoEFIDir, err)
}
// The iso image file itself must be placed in the PXE folder because
// dracut livenet module will download it.
artifactsIsoImagePath := filepath.Join(outputPXEArtifactsDir, filepath.Base(isoImagePath))
err = file.Copy(isoImagePath, artifactsIsoImagePath)
if err != nil {
return fmt.Errorf("failed to copy (%s) while populating the PXE artifacts directory:\n%w", isoImagePath, err)
}
return nil
}