-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathelemental.go
619 lines (551 loc) · 19.5 KB
/
elemental.go
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/*
Copyright © 2022 - 2023 SUSE LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package elemental
import (
"errors"
"fmt"
"path/filepath"
"regexp"
"strings"
cnst "github.com/rancher/elemental-toolkit/pkg/constants"
"github.com/rancher/elemental-toolkit/pkg/partitioner"
v1 "github.com/rancher/elemental-toolkit/pkg/types/v1"
"github.com/rancher/elemental-toolkit/pkg/utils"
)
// Elemental is the struct meant to self-contain most utils and actions related to Elemental, like installing or applying selinux
type Elemental struct {
config *v1.Config
}
func NewElemental(config *v1.Config) *Elemental {
return &Elemental{
config: config,
}
}
// FormatPartition will format an already existing partition
func (e *Elemental) FormatPartition(part *v1.Partition, opts ...string) error {
e.config.Logger.Infof("Formatting '%s' partition", part.Name)
return partitioner.FormatDevice(e.config.Runner, part.Path, part.FS, part.FilesystemLabel, opts...)
}
// PartitionAndFormatDevice creates a new empty partition table on target disk
// and applies the configured disk layout by creating and formatting all
// required partitions
func (e *Elemental) PartitionAndFormatDevice(i *v1.InstallSpec) error {
disk := partitioner.NewDisk(
i.Target,
partitioner.WithRunner(e.config.Runner),
partitioner.WithFS(e.config.Fs),
partitioner.WithLogger(e.config.Logger),
)
if !disk.Exists() {
e.config.Logger.Errorf("Disk %s does not exist", i.Target)
return fmt.Errorf("disk %s does not exist", i.Target)
}
e.config.Logger.Infof("Partitioning device...")
out, err := disk.NewPartitionTable(i.PartTable)
if err != nil {
e.config.Logger.Errorf("Failed creating new partition table: %s", out)
return err
}
parts := i.Partitions.PartitionsByInstallOrder(i.ExtraPartitions)
return e.createPartitions(disk, parts)
}
func (e *Elemental) createAndFormatPartition(disk *partitioner.Disk, part *v1.Partition) error {
e.config.Logger.Debugf("Adding partition %s", part.Name)
num, err := disk.AddPartition(part.Size, part.FS, part.Name, part.Flags...)
if err != nil {
e.config.Logger.Errorf("Failed creating %s partition", part.Name)
return err
}
partDev, err := disk.FindPartitionDevice(num)
if err != nil {
return err
}
if part.FS != "" {
e.config.Logger.Debugf("Formatting partition with label %s", part.FilesystemLabel)
err = partitioner.FormatDevice(e.config.Runner, partDev, part.FS, part.FilesystemLabel)
if err != nil {
e.config.Logger.Errorf("Failed formatting partition %s", part.Name)
return err
}
} else {
e.config.Logger.Debugf("Wipe file system on %s", part.Name)
err = disk.WipeFsOnPartition(partDev)
if err != nil {
e.config.Logger.Errorf("Failed to wipe filesystem of partition %s", partDev)
return err
}
}
part.Path = partDev
return nil
}
func (e *Elemental) createPartitions(disk *partitioner.Disk, parts v1.PartitionList) error {
for _, part := range parts {
err := e.createAndFormatPartition(disk, part)
if err != nil {
return err
}
}
return nil
}
// MountPartitions mounts configured partitions. Partitions with an unset mountpoint are not mounted.
// Note umounts must be handled by caller logic.
func (e Elemental) MountPartitions(parts v1.PartitionList) error {
e.config.Logger.Infof("Mounting disk partitions")
var err error
for _, part := range parts {
if part.MountPoint != "" {
err = e.MountPartition(part, "rw")
if err != nil {
_ = e.UnmountPartitions(parts)
return err
}
}
}
return err
}
// UnmountPartitions unmounts configured partitiosn. Partitions with an unset mountpoint are not unmounted.
func (e Elemental) UnmountPartitions(parts v1.PartitionList) error {
e.config.Logger.Infof("Unmounting disk partitions")
var err error
errMsg := ""
failure := false
// If there is an early error we still try to unmount other partitions
for _, part := range parts {
if part.MountPoint != "" {
err = e.UnmountPartition(part)
if err != nil {
errMsg += fmt.Sprintf("Failed to unmount %s\n", part.MountPoint)
failure = true
}
}
}
if failure {
return errors.New(errMsg)
}
return nil
}
// MountRWPartition mounts, or remounts if needed, a partition with RW permissions
func (e Elemental) MountRWPartition(part *v1.Partition) (umount func() error, err error) {
if mnt, _ := utils.IsMounted(e.config, part); mnt {
err = e.MountPartition(part, "remount", "rw")
if err != nil {
e.config.Logger.Errorf("failed mounting %s partition: %v", part.Name, err)
return nil, err
}
umount = func() error { return e.MountPartition(part, "remount", "ro") }
} else {
err = e.MountPartition(part, "rw")
if err != nil {
e.config.Logger.Error("failed mounting %s partition: %v", part.Name, err)
return nil, err
}
umount = func() error { return e.UnmountPartition(part) }
}
return umount, nil
}
// MountPartition mounts a partition with the given mount options
func (e Elemental) MountPartition(part *v1.Partition, opts ...string) error {
e.config.Logger.Debugf("Mounting partition %s", part.FilesystemLabel)
err := utils.MkdirAll(e.config.Fs, part.MountPoint, cnst.DirPerm)
if err != nil {
return err
}
if part.Path == "" {
// Lets error out only after 10 attempts to find the device
device, err := utils.GetDeviceByLabel(e.config.Runner, part.FilesystemLabel, 10)
if err != nil {
e.config.Logger.Errorf("Could not find a device with label %s", part.FilesystemLabel)
return err
}
part.Path = device
}
err = e.config.Mounter.Mount(part.Path, part.MountPoint, "auto", opts)
if err != nil {
e.config.Logger.Errorf("Failed mounting device %s with label %s", part.Path, part.FilesystemLabel)
return err
}
return nil
}
// UnmountPartition unmounts the given partition or does nothing if not mounted
func (e Elemental) UnmountPartition(part *v1.Partition) error {
if mnt, _ := utils.IsMounted(e.config, part); !mnt {
e.config.Logger.Debugf("Not unmounting partition, %s doesn't look like mountpoint", part.MountPoint)
return nil
}
e.config.Logger.Debugf("Unmounting partition %s", part.FilesystemLabel)
return e.config.Mounter.Unmount(part.MountPoint)
}
// MountImage mounts an image with the given mount options
func (e Elemental) MountImage(img *v1.Image, opts ...string) error {
e.config.Logger.Debugf("Mounting image %s to %s", img.Label, img.MountPoint)
err := utils.MkdirAll(e.config.Fs, img.MountPoint, cnst.DirPerm)
if err != nil {
e.config.Logger.Errorf("Failed creating mountpoint %s", img.MountPoint)
return err
}
out, err := e.config.Runner.Run("losetup", "--show", "-f", img.File)
if err != nil {
e.config.Logger.Errorf("Failed setting a loop device for %s", img.File)
return err
}
loop := strings.TrimSpace(string(out))
err = e.config.Mounter.Mount(loop, img.MountPoint, "auto", opts)
if err != nil {
e.config.Logger.Errorf("Failed to mount %s", loop)
_, _ = e.config.Runner.Run("losetup", "-d", loop)
return err
}
img.LoopDevice = loop
return nil
}
// UnmountImage unmounts the given image or does nothing if not mounted
func (e Elemental) UnmountImage(img *v1.Image) error {
// Using IsLikelyNotMountPoint seams to be safe as we are not checking
// for bind mounts here
if notMnt, _ := e.config.Mounter.IsLikelyNotMountPoint(img.MountPoint); notMnt {
e.config.Logger.Debugf("Not unmounting image, %s doesn't look like mountpoint", img.MountPoint)
return nil
}
e.config.Logger.Debugf("Unmounting image %s from %s", img.Label, img.MountPoint)
err := e.config.Mounter.Unmount(img.MountPoint)
if err != nil {
return err
}
_, err = e.config.Runner.Run("losetup", "-d", img.LoopDevice)
img.LoopDevice = ""
return err
}
// CreateFileSystemImage creates the image file for the given image
func (e Elemental) CreateFileSystemImage(img *v1.Image) error {
return e.CreatePreLoadedFileSystemImage(img, "")
}
// CreatePreLoadedFileSystemImage creates the image file for the given image including the contents of the rootDir.
// If rootDir is empty it simply creates an empty filesystem image
func (e Elemental) CreatePreLoadedFileSystemImage(img *v1.Image, rootDir string) error {
e.config.Logger.Infof("Creating filesystem image %s with size: %d", img.File, img.Size)
err := utils.MkdirAll(e.config.Fs, filepath.Dir(img.File), cnst.DirPerm)
if err != nil {
return err
}
err = utils.CreateRAWFile(e.config.Fs, img.File, img.Size)
if err != nil {
return err
}
var extraOpts []string
// Only add the rootDir if it's not empty
match, _ := regexp.MatchString("ext[2-4]", img.FS)
exists, _ := utils.Exists(e.config.Fs, rootDir)
if !match && exists {
e.config.Logger.Infof("Pre-loaded image creation is only available for ext[2-4] filesystems, ignoring options for %s", img.FS)
}
if exists && match {
extraOpts = []string{"-d", rootDir}
}
mkfs := partitioner.NewMkfsCall(img.File, img.FS, img.Label, e.config.Runner, extraOpts...)
_, err = mkfs.Apply()
if err != nil {
_ = e.config.Fs.RemoveAll(img.File)
return err
}
return nil
}
// DeployImgTree will deploy the given image into the given root tree. Returns source metadata in info,
// a tree cleaner function and error. The given root will be a bind mount of a temporary directory into the same
// filesystem of img.File, this is helpful to make the deployment easily accessible in after-* hooks.
func (e *Elemental) DeployImgTree(img *v1.Image, root string) (info interface{}, cleaner func() error, err error) {
// We prepare the rootTree next to the target image file, in the same base path
e.config.Logger.Infof("Preparing root tree for image: %s", img.File)
tmp := strings.TrimSuffix(img.File, filepath.Ext(img.File))
tmp += ".imgTree"
err = utils.MkdirAll(e.config.Fs, tmp, cnst.DirPerm)
if err != nil {
return nil, nil, err
}
err = utils.MkdirAll(e.config.Fs, root, cnst.DirPerm)
if err != nil {
_ = e.config.Fs.RemoveAll(tmp)
return nil, nil, err
}
err = e.config.Mounter.Mount(tmp, root, "bind", []string{"bind"})
if err != nil {
_ = e.config.Fs.RemoveAll(tmp)
_ = e.config.Fs.RemoveAll(root)
return nil, nil, err
}
cleaner = func() error {
_ = e.config.Mounter.Unmount(root)
err := e.config.Fs.RemoveAll(root)
if err != nil {
return err
}
return e.config.Fs.RemoveAll(tmp)
}
info, err = e.DumpSource(root, img.Source)
if err != nil {
_ = cleaner()
return nil, nil, err
}
err = utils.CreateDirStructure(e.config.Fs, root)
if err != nil {
_ = cleaner()
return nil, nil, err
}
return info, cleaner, err
}
// CreateImgFromTree creates the given image from with the contents of the tree for the given root.
// NoMount flag allows formatting an image including its contents (experimental and ext* specific)
func (e *Elemental) CreateImgFromTree(root string, img *v1.Image, noMount bool, cleaner func() error) (err error) {
if cleaner != nil {
defer func() {
cErr := cleaner()
if cErr != nil && err == nil {
err = cErr
}
}()
}
var preLoadRoot string
if noMount {
preLoadRoot = root
}
if img.FS == cnst.SquashFs {
e.config.Logger.Infof("Creating squashed image: %s", img.File)
err = utils.MkdirAll(e.config.Fs, filepath.Dir(img.File), cnst.DirPerm)
if err != nil {
e.config.Logger.Errorf("failed creating destination folder: %s", err.Error())
return err
}
squashOptions := append(cnst.GetDefaultSquashfsOptions(), e.config.SquashFsCompressionConfig...)
err = utils.CreateSquashFS(e.config.Runner, e.config.Logger, root, img.File, squashOptions)
if err != nil {
return err
}
} else {
if img.Size == 0 {
size, err := utils.DirSizeMB(e.config.Fs, root)
if err != nil {
return err
}
img.Size = size + cnst.ImgOverhead
}
err = e.CreatePreLoadedFileSystemImage(img, preLoadRoot)
if err != nil {
return err
}
if !noMount {
err = e.MountImage(img, "rw")
if err != nil {
return err
}
defer func() {
mErr := e.UnmountImage(img)
if err == nil && mErr != nil {
err = mErr
}
}()
e.config.Logger.Infof("Sync %s to %s", root, img.MountPoint)
err = utils.SyncData(e.config.Logger, e.config.Runner, e.config.Fs, root, img.MountPoint)
if err != nil {
return err
}
}
}
return err
}
// CopyFileImg copies the files target as the source of this image. It also applies the img label over the copied image.
func (e *Elemental) CopyFileImg(img *v1.Image) error {
if !img.Source.IsFile() {
return fmt.Errorf("Copying a file image requires an image source of file type")
}
err := utils.MkdirAll(e.config.Fs, filepath.Dir(img.File), cnst.DirPerm)
if err != nil {
return err
}
e.config.Logger.Infof("Copying image %s to %s", img.Source.Value(), img.File)
err = utils.CopyFile(e.config.Fs, img.Source.Value(), img.File)
if err != nil {
return err
}
if img.FS != cnst.SquashFs && img.Label != "" {
e.config.Logger.Infof("Setting label: %s ", img.Label)
_, err = e.config.Runner.Run("tune2fs", "-L", img.Label, img.File)
}
return err
}
// DeployImage will deploy the given image into the target. This method
// creates the filesystem image file and fills it with the correspondant data
func (e *Elemental) DeployImage(img *v1.Image) (interface{}, error) {
e.config.Logger.Infof("Deploying image: %s", img.File)
info, cleaner, err := e.DeployImgTree(img, cnst.WorkingImgDir)
if err != nil {
return nil, err
}
err = e.CreateImgFromTree(cnst.WorkingImgDir, img, false, cleaner)
if err != nil {
return nil, err
}
return info, nil
}
// DumpSource sets the image data according to the image source type
func (e *Elemental) DumpSource(target string, imgSrc *v1.ImageSource) (info interface{}, err error) { // nolint:gocyclo
e.config.Logger.Infof("Copying %s source...", imgSrc.Value())
err = utils.MkdirAll(e.config.Fs, target, cnst.DirPerm)
if err != nil {
e.config.Logger.Errorf("failed to create target directory %s", target)
return nil, err
}
if imgSrc.IsImage() {
if e.config.Cosign {
e.config.Logger.Infof("Running cosing verification for %s", imgSrc.Value())
out, err := utils.CosignVerify(
e.config.Fs, e.config.Runner, imgSrc.Value(),
e.config.CosignPubKey, v1.IsDebugLevel(e.config.Logger),
)
if err != nil {
e.config.Logger.Errorf("Cosign verification failed: %s", out)
return nil, err
}
}
err = e.config.ImageExtractor.ExtractImage(imgSrc.Value(), target, e.config.Platform.String(), e.config.LocalImage)
if err != nil {
return nil, err
}
} else if imgSrc.IsDir() {
excludes := []string{"/mnt", "/proc", "/sys", "/dev", "/tmp", "/host", "/run"}
err = utils.SyncData(e.config.Logger, e.config.Runner, e.config.Fs, imgSrc.Value(), target, excludes...)
if err != nil {
return nil, err
}
} else if imgSrc.IsFile() {
err = utils.MkdirAll(e.config.Fs, cnst.ImgSrcDir, cnst.DirPerm)
if err != nil {
return nil, err
}
img := &v1.Image{File: imgSrc.Value(), MountPoint: cnst.ImgSrcDir}
err = e.MountImage(img, "auto", "ro")
if err != nil {
return nil, err
}
defer e.UnmountImage(img) // nolint:errcheck
excludes := []string{"/mnt", "/proc", "/sys", "/dev", "/tmp", "/host", "/run"}
err = utils.SyncData(e.config.Logger, e.config.Runner, e.config.Fs, cnst.ImgSrcDir, target, excludes...)
if err != nil {
return nil, err
}
} else {
return nil, fmt.Errorf("unknown image source type")
}
e.config.Logger.Infof("Finished copying %s into %s", imgSrc.Value(), target)
return info, nil
}
// CopyCloudConfig will check if there is a cloud init in the config and store it on the target
func (e *Elemental) CopyCloudConfig(path string, cloudInit []string) (err error) {
if path == "" {
e.config.Logger.Warnf("empty path. Will not copy cloud config files.")
return nil
}
for i, ci := range cloudInit {
customConfig := filepath.Join(path, fmt.Sprintf("9%d_custom.yaml", i))
err = utils.GetSource(e.config, ci, customConfig)
if err != nil {
return err
}
if err = e.config.Fs.Chmod(customConfig, cnst.FilePerm); err != nil {
return err
}
e.config.Logger.Infof("Finished copying cloud config file %s to %s", cloudInit, customConfig)
}
return nil
}
// SelinuxRelabel will relabel the system if it finds the binary and the context
func (e *Elemental) SelinuxRelabel(rootDir string, raiseError bool) error {
policyFile, err := utils.FindFile(e.config.Fs, rootDir, filepath.Join(cnst.SELinuxTargetedPolicyPath, "policy.*"))
contextFile := filepath.Join(rootDir, cnst.SELinuxTargetedContextFile)
contextExists, _ := utils.Exists(e.config.Fs, contextFile)
if err == nil && contextExists && e.config.Runner.CommandExists("setfiles") {
var out []byte
var err error
if rootDir == "/" || rootDir == "" {
out, err = e.config.Runner.Run("setfiles", "-c", policyFile, "-e", "/dev", "-e", "/proc", "-e", "/sys", "-F", contextFile, "/")
} else {
out, err = e.config.Runner.Run("setfiles", "-c", policyFile, "-F", "-r", rootDir, contextFile, rootDir)
}
e.config.Logger.Debugf("SELinux setfiles output: %s", string(out))
if err != nil && raiseError {
return err
}
} else {
e.config.Logger.Debugf("No files relabelling as SELinux utilities are not found")
}
return nil
}
// CheckActiveDeployment returns true if at least one of the provided filesystem labels is found within the system
func (e *Elemental) CheckActiveDeployment(labels []string) bool {
e.config.Logger.Infof("Checking for active deployment")
for _, label := range labels {
found, _ := utils.GetDeviceByLabel(e.config.Runner, label, 1)
if found != "" {
e.config.Logger.Debug("there is already an active deployment in the system")
return true
}
}
return false
}
// UpdateSourceISO downloads an ISO in a temporary folder, mounts it and updates active image to use the ISO squashfs image as
// source. Returns a cleaner method to unmount and remove the temporary folder afterwards.
func (e Elemental) UpdateSourceFormISO(iso string, activeImg *v1.Image) (func() error, error) {
nilErr := func() error { return nil }
tmpDir, err := utils.TempDir(e.config.Fs, "", "elemental")
if err != nil {
return nilErr, err
}
cleanTmpDir := func() error { return e.config.Fs.RemoveAll(tmpDir) }
tmpFile := filepath.Join(tmpDir, "elemental.iso")
err = utils.GetSource(e.config, iso, tmpFile)
if err != nil {
return cleanTmpDir, err
}
isoMnt := filepath.Join(tmpDir, "iso")
err = utils.MkdirAll(e.config.Fs, isoMnt, cnst.DirPerm)
if err != nil {
return cleanTmpDir, err
}
e.config.Logger.Infof("Mounting iso %s into %s", tmpFile, isoMnt)
err = e.config.Mounter.Mount(tmpFile, isoMnt, "auto", []string{"loop"})
if err != nil {
return cleanTmpDir, err
}
cleanAll := func() error {
cErr := e.config.Mounter.Unmount(isoMnt)
if cErr != nil {
return cErr
}
return cleanTmpDir()
}
squashfsImg := filepath.Join(isoMnt, cnst.ISORootFile)
ok, _ := utils.Exists(e.config.Fs, squashfsImg)
if !ok {
return cleanAll, fmt.Errorf("squashfs image not found in ISO: %s", squashfsImg)
}
activeImg.Source = v1.NewFileSrc(squashfsImg)
return cleanAll, nil
}
// DeactivateDevice deactivates unmounted the block devices present within the system.
// Useful to deactivate LVM volumes, if any, related to the target device.
func (e Elemental) DeactivateDevices() error {
out, err := e.config.Runner.Run(
"blkdeactivate", "--lvmoptions", "retry,wholevg",
"--dmoptions", "force,retry", "--errors",
)
e.config.Logger.Debugf("blkdeactivate command output: %s", string(out))
return err
}