-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathspec.go
1106 lines (971 loc) · 37.1 KB
/
spec.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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright © 2022 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 config
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"reflect"
"strings"
"github.com/kairos-io/kairos-agent/v2/pkg/constants"
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
fsutils "github.com/kairos-io/kairos-agent/v2/pkg/utils/fs"
k8sutils "github.com/kairos-io/kairos-agent/v2/pkg/utils/k8s"
"github.com/kairos-io/kairos-agent/v2/pkg/utils/partitions"
"github.com/kairos-io/kairos-sdk/collector"
"github.com/kairos-io/kairos-sdk/ghw"
"github.com/kairos-io/kairos-sdk/types"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/mitchellh/mapstructure"
"github.com/sanity-io/litter"
"github.com/spf13/viper"
"golang.org/x/sys/unix"
)
const (
_ = 1 << (10 * iota)
KiB
MiB
GiB
TiB
)
// resolveTarget will try to resovle a /dev/disk/by-X disk into the final real disk under /dev/X
// We use it to calculate the device on the fly for the Config and the InstallSpec but we leave
// the original value in teh config.Collector so its written down in the final cloud config in the
// installed system, so users can know what parameters it was installed with in case they need to refer
// to it down the line to know what was the original parametes
// If the target is a normal /dev/X we dont do anything and return the original value so normal installs
// should not be affected
func resolveTarget(target string) (string, error) {
// Accept that the target can be a /dev/disk/by-{label,uuid,path,etc..} and resolve it into a /dev/device
if strings.HasPrefix(target, "/dev/disk/by-") {
// we dont accept partitions as target so check and fail earlier for those that are partuuid or parlabel
if strings.Contains(target, "partlabel") || strings.Contains(target, "partuuid") {
return "", fmt.Errorf("target contains 'parlabel' or 'partuuid', looks like its a partition instead of a disk: %s", target)
}
// Use EvanSymlinks to properly resolve the full path to the target
device, err := filepath.EvalSymlinks(target)
if err != nil {
return "", fmt.Errorf("failed to read device link for %s: %w", target, err)
}
if !strings.HasPrefix(device, "/dev/") {
return "", fmt.Errorf("device %s is not a valid device path", device)
}
return device, nil
}
// If we don't resolve and don't fail, just return the original target
return target, nil
}
// NewInstallSpec returns an InstallSpec struct all based on defaults and basic host checks (e.g. EFI vs BIOS)
func NewInstallSpec(cfg *Config) (*v1.InstallSpec, error) {
var firmware string
var recoveryImg, activeImg, passiveImg v1.Image
recoveryImgFile := filepath.Join(constants.LiveDir, constants.RecoverySquashFile)
// Check if current host has EFI firmware
efiExists, _ := fsutils.Exists(cfg.Fs, constants.EfiDevice)
// Check the default ISO installation media is available
isoRootExists, _ := fsutils.Exists(cfg.Fs, constants.IsoBaseTree)
// Check the default ISO recovery installation media is available)
recoveryExists, _ := fsutils.Exists(cfg.Fs, recoveryImgFile)
if efiExists {
firmware = v1.EFI
} else {
firmware = v1.BIOS
}
// Resolve the install target
dev, err := resolveTarget(cfg.Install.Device)
if err != nil {
return nil, err
}
cfg.Install.Device = dev
activeImg.Label = constants.ActiveLabel
activeImg.Size = constants.ImgSize
activeImg.File = filepath.Join(constants.StateDir, "cOS", constants.ActiveImgFile)
activeImg.FS = constants.LinuxImgFs
activeImg.MountPoint = constants.ActiveDir
// First try to use the install media source
if isoRootExists {
activeImg.Source = v1.NewDirSrc(constants.IsoBaseTree)
}
// Then any user provided source
if cfg.Install.Source != "" {
activeImg.Source, _ = v1.NewSrcFromURI(cfg.Install.Source)
}
// If we dont have any just an empty source so the sanitation fails
// TODO: Should we directly fail here if we got no source instead of waiting for the Sanitize() to fail?
if !isoRootExists && cfg.Install.Source == "" {
activeImg.Source = v1.NewEmptySrc()
}
if recoveryExists {
recoveryImg.Source = v1.NewFileSrc(recoveryImgFile)
recoveryImg.FS = constants.SquashFs
recoveryImg.File = filepath.Join(constants.RecoveryDir, "cOS", constants.RecoverySquashFile)
recoveryImg.Size = constants.ImgSize
} else {
recoveryImg.Source = v1.NewFileSrc(activeImg.File)
recoveryImg.FS = constants.LinuxImgFs
recoveryImg.Label = constants.SystemLabel
recoveryImg.File = filepath.Join(constants.RecoveryDir, "cOS", constants.RecoveryImgFile)
recoveryImg.Size = constants.ImgSize
}
passiveImg = v1.Image{
File: filepath.Join(constants.StateDir, "cOS", constants.PassiveImgFile),
Label: constants.PassiveLabel,
Source: v1.NewFileSrc(activeImg.File),
FS: constants.LinuxImgFs,
Size: constants.ImgSize,
}
spec := &v1.InstallSpec{
Target: cfg.Install.Device,
Firmware: firmware,
PartTable: v1.GPT,
GrubConf: constants.GrubConf,
Tty: constants.DefaultTty,
Active: activeImg,
Recovery: recoveryImg,
Passive: passiveImg,
NoFormat: cfg.Install.NoFormat,
}
// Get the actual source size to calculate the image size and partitions size
size, err := GetSourceSize(cfg, spec.Active.Source)
if err != nil {
cfg.Logger.Warnf("Failed to infer size for images: %s", err.Error())
} else {
cfg.Logger.Infof("Setting image size to %dMb", size)
spec.Active.Size = uint(size)
spec.Passive.Size = uint(size)
spec.Recovery.Size = uint(size)
}
err = unmarshallFullSpec(cfg, "install", spec)
if err != nil {
return nil, fmt.Errorf("failed unmarshalling the full spec: %w", err)
}
// resolve also the target of the spec so we can partition properly
spec.Target, err = resolveTarget(spec.Target)
if err != nil {
return nil, err
}
// Calculate the partitions afterwards so they use the image sizes for the final partition sizes
spec.Partitions = NewInstallElementalPartitions(cfg.Logger, spec)
return spec, nil
}
func NewInstallElementalPartitions(log types.KairosLogger, spec *v1.InstallSpec) v1.ElementalPartitions {
pt := v1.ElementalPartitions{}
var oemSize uint
if spec.Partitions.OEM != nil && spec.Partitions.OEM.Size != 0 {
oemSize = spec.Partitions.OEM.Size
} else {
oemSize = constants.OEMSize
}
pt.OEM = &types.Partition{
FilesystemLabel: constants.OEMLabel,
Size: oemSize,
Name: constants.OEMPartName,
FS: constants.LinuxFs,
MountPoint: constants.OEMDir,
Flags: []string{},
}
log.Infof("Setting OEM partition size to %dMb", oemSize)
// Double the space for recovery, as upgrades use the recovery partition to create the transition image for upgrades
// so we need twice the space to do a proper upgrade
// Check if the default/user provided values are enough to fit the images sizes
var recoverySize uint
if spec.Partitions.Recovery == nil { // This means its not configured by user so use the default
recoverySize = (spec.Recovery.Size * 2) + 200
} else {
if spec.Partitions.Recovery.Size < (spec.Recovery.Size*2)+200 { // Configured by user but not enough space
// If we had the logger here we could log a message saying that space is not enough and we are auto increasing it
recoverySize = (spec.Recovery.Size * 2) + 200
log.Warnf("Not enough space set for recovery partition(%dMb), increasing it to fit the recovery images(%dMb)", spec.Partitions.Recovery.Size, recoverySize)
} else {
recoverySize = spec.Partitions.Recovery.Size
}
}
log.Infof("Setting recovery partition size to %dMb", recoverySize)
pt.Recovery = &types.Partition{
FilesystemLabel: constants.RecoveryLabel,
Size: recoverySize,
Name: constants.RecoveryPartName,
FS: constants.LinuxFs,
MountPoint: constants.RecoveryDir,
Flags: []string{},
}
// Add 1 Gb to the partition so images can grow a bit, otherwise you are stuck with the smallest space possible and
// there is no coming back from that
// Also multiply the space for active, as upgrades use the state partition to create the transition image for upgrades
// so we need twice the space to do a proper upgrade
// Check if the default/user provided values are enough to fit the images sizes
var stateSize uint
if spec.Partitions.State == nil { // This means its not configured by user so use the default
stateSize = (spec.Active.Size * 2) + spec.Passive.Size + 1000
} else {
if spec.Partitions.State.Size < (spec.Active.Size*2)+spec.Passive.Size+1000 { // Configured by user but not enough space
stateSize = (spec.Active.Size * 2) + spec.Passive.Size + 1000
log.Warnf("Not enough space set for state partition(%dMb), increasing it to fit the state images(%dMb)", spec.Partitions.State.Size, stateSize)
} else {
stateSize = spec.Partitions.State.Size
}
}
log.Infof("Setting state partition size to %dMb", stateSize)
pt.State = &types.Partition{
FilesystemLabel: constants.StateLabel,
Size: stateSize,
Name: constants.StatePartName,
FS: constants.LinuxFs,
MountPoint: constants.StateDir,
Flags: []string{},
}
var persistentSize uint
if spec.Partitions.Persistent == nil { // This means its not configured by user so use the default
persistentSize = constants.PersistentSize
} else {
persistentSize = spec.Partitions.Persistent.Size
}
pt.Persistent = &types.Partition{
FilesystemLabel: constants.PersistentLabel,
Size: persistentSize,
Name: constants.PersistentPartName,
FS: constants.LinuxFs,
MountPoint: constants.PersistentDir,
Flags: []string{},
}
log.Infof("Setting persistent partition size to %dMb", persistentSize)
return pt
}
// NewUpgradeSpec returns an UpgradeSpec struct all based on defaults and current host state
func NewUpgradeSpec(cfg *Config) (*v1.UpgradeSpec, error) {
var recLabel, recFs, recMnt string
var active, passive, recovery v1.Image
installState, err := cfg.LoadInstallState()
if err != nil {
cfg.Logger.Warnf("failed reading installation state: %s", err.Error())
}
parts, err := partitions.GetAllPartitions(&cfg.Logger)
if err != nil {
return nil, fmt.Errorf("could not read host partitions")
}
ep := v1.NewElementalPartitionsFromList(parts)
if ep.Recovery == nil {
// We could have recovery in lvm which won't appear in ghw list
ep.Recovery = partitions.GetPartitionViaDM(cfg.Fs, constants.RecoveryLabel)
}
if ep.OEM == nil {
// We could have OEM in lvm which won't appear in ghw list
ep.OEM = partitions.GetPartitionViaDM(cfg.Fs, constants.OEMLabel)
}
if ep.Persistent == nil {
// We could have persistent encrypted or in lvm which won't appear in ghw list
ep.Persistent = partitions.GetPartitionViaDM(cfg.Fs, constants.PersistentLabel)
}
if ep.Recovery != nil {
if ep.Recovery.MountPoint == "" {
ep.Recovery.MountPoint = constants.RecoveryDir
}
squashedRec, err := hasSquashedRecovery(cfg, ep.Recovery)
if err != nil {
return nil, fmt.Errorf("failed checking for squashed recovery: %w", err)
}
if squashedRec {
recFs = constants.SquashFs
} else {
recLabel = constants.SystemLabel
recFs = constants.LinuxImgFs
recMnt = constants.TransitionDir
}
recovery = v1.Image{
File: filepath.Join(ep.Recovery.MountPoint, "cOS", constants.TransitionImgFile),
Size: constants.ImgSize,
Label: recLabel,
FS: recFs,
MountPoint: recMnt,
Source: v1.NewEmptySrc(),
}
}
if ep.State != nil {
if ep.State.MountPoint == "" {
ep.State.MountPoint = constants.StateDir
}
active = v1.Image{
File: filepath.Join(ep.State.MountPoint, "cOS", constants.TransitionImgFile),
Size: constants.ImgSize,
Label: constants.ActiveLabel,
FS: constants.LinuxImgFs,
MountPoint: constants.TransitionDir,
Source: v1.NewEmptySrc(),
}
passive = v1.Image{
File: filepath.Join(ep.State.MountPoint, "cOS", constants.PassiveImgFile),
Label: constants.PassiveLabel,
Size: constants.ImgSize,
Source: v1.NewFileSrc(active.File),
FS: active.FS,
}
}
// If we have oem in the system, but it has no mountpoint
if ep.OEM != nil && ep.OEM.MountPoint == "" {
// Add the default mountpoint for it in case the chroot stages want to bind mount it
ep.OEM.MountPoint = constants.OEMPath
}
// This is needed if we want to use the persistent as tmpdir for the upgrade images
// as tmpfs is 25% of the total RAM, we cannot rely on the tmp dir having enough space for our image
// This enables upgrades on low ram devices
if ep.Persistent != nil {
if ep.Persistent.MountPoint == "" {
ep.Persistent.MountPoint = constants.PersistentDir
}
}
// Deep look to see if upgrade.recovery == true in the config
// if yes, we set the upgrade spec "Entry" to "recovery"
entry := ""
_, ok := cfg.Config.Values["upgrade"]
if ok {
_, ok = cfg.Config.Values["upgrade"].(collector.ConfigValues)["recovery"]
if ok {
if cfg.Config.Values["upgrade"].(collector.ConfigValues)["recovery"].(bool) {
entry = constants.BootEntryRecovery
}
}
}
spec := &v1.UpgradeSpec{
Entry: entry,
Active: active,
Recovery: recovery,
Passive: passive,
Partitions: ep,
State: installState,
}
// Unmarshall the config into the spec first so the active/recovery gets filled properly from all sources
err = unmarshallFullSpec(cfg, "upgrade", spec)
if err != nil {
return nil, fmt.Errorf("failed unmarshalling the full spec: %w", err)
}
err = setUpgradeSourceSize(cfg, spec)
if err != nil {
return nil, fmt.Errorf("failed calculating size: %w", err)
}
if spec.Active.Source.IsDocker() {
cfg.Logger.Infof("Checking if OCI image %s exists", spec.Active.Source.Value())
_, err := crane.Manifest(spec.Active.Source.Value())
if err != nil {
if strings.Contains(err.Error(), "MANIFEST_UNKNOWN") {
return nil, fmt.Errorf("oci image %s does not exist", spec.Active.Source.Value())
}
return nil, err
}
}
return spec, nil
}
func setUpgradeSourceSize(cfg *Config, spec *v1.UpgradeSpec) error {
var size int64
var err error
var originalSize uint
// Store the default given size in the spec. This includes the user specified values which have already been marshalled in the spec
if spec.RecoveryUpgrade() {
originalSize = spec.Recovery.Size
} else {
originalSize = spec.Active.Size
}
var targetSpec *v1.Image
if spec.RecoveryUpgrade() {
targetSpec = &(spec.Recovery)
} else {
targetSpec = &(spec.Active)
}
if targetSpec.Source != nil && targetSpec.Source.IsEmpty() {
cfg.Logger.Debugf("No source specified for image, skipping size calculation")
return nil
}
size, err = GetSourceSize(cfg, targetSpec.Source)
if err != nil {
cfg.Logger.Warnf("Failed to infer size for images: %s", err.Error())
return err
}
if uint(size) < originalSize {
cfg.Logger.Debugf("Calculated size (%dMB) is less than specified/default size (%dMB)", size, originalSize)
targetSpec.Size = originalSize
} else {
cfg.Logger.Debugf("Calculated size (%dMB) is higher than specified/default size (%dMB)", size, originalSize)
targetSpec.Size = uint(size)
}
cfg.Logger.Infof("Setting image size to %dMB", targetSpec.Size)
return nil
}
// NewResetSpec returns a ResetSpec struct all based on defaults and current host state
func NewResetSpec(cfg *Config) (*v1.ResetSpec, error) {
var imgSource *v1.ImageSource
//TODO find a way to pre-load current state values such as labels
if !BootedFrom(cfg.Runner, constants.RecoverySquashFile) &&
!BootedFrom(cfg.Runner, constants.SystemLabel) {
return nil, fmt.Errorf("reset can only be called from the recovery system")
}
efiExists, _ := fsutils.Exists(cfg.Fs, constants.EfiDevice)
installState, err := cfg.LoadInstallState()
if err != nil {
cfg.Logger.Warnf("failed reading installation state: %s", err.Error())
}
parts, err := partitions.GetAllPartitions(&cfg.Logger)
if err != nil {
return nil, fmt.Errorf("could not read host partitions")
}
ep := v1.NewElementalPartitionsFromList(parts)
if efiExists {
if ep.EFI == nil {
return nil, fmt.Errorf("EFI partition not found")
}
if ep.EFI.MountPoint == "" {
ep.EFI.MountPoint = constants.EfiDir
}
ep.EFI.Name = constants.EfiPartName
}
if ep.State == nil {
return nil, fmt.Errorf("state partition not found")
}
if ep.State.MountPoint == "" {
ep.State.MountPoint = constants.StateDir
}
ep.State.Name = constants.StatePartName
if ep.Recovery == nil {
// We could have recovery in lvm which won't appear in ghw list
ep.Recovery = partitions.GetPartitionViaDM(cfg.Fs, constants.RecoveryLabel)
if ep.Recovery == nil {
return nil, fmt.Errorf("recovery partition not found")
}
}
if ep.Recovery.MountPoint == "" {
ep.Recovery.MountPoint = constants.RecoveryDir
}
target := ep.State.Disk
// OEM partition is not a hard requirement for reset unless we have the reset oem flag
if ep.OEM == nil {
// We could have oem in lvm which won't appear in ghw list
ep.OEM = partitions.GetPartitionViaDM(cfg.Fs, constants.OEMLabel)
}
// Persistent partition is not a hard requirement
if ep.Persistent == nil {
// We could have persistent encrypted or in lvm which won't appear in ghw list
ep.Persistent = partitions.GetPartitionViaDM(cfg.Fs, constants.PersistentLabel)
}
recoveryImg := filepath.Join(constants.RunningStateDir, "cOS", constants.RecoveryImgFile)
recoveryImg2 := filepath.Join(constants.RunningRecoveryStateDir, "cOS", constants.RecoveryImgFile)
if exists, _ := fsutils.Exists(cfg.Fs, recoveryImg); exists {
imgSource = v1.NewFileSrc(recoveryImg)
} else if exists, _ = fsutils.Exists(cfg.Fs, recoveryImg2); exists {
imgSource = v1.NewFileSrc(recoveryImg2)
} else if exists, _ = fsutils.Exists(cfg.Fs, constants.IsoBaseTree); exists {
imgSource = v1.NewDirSrc(constants.IsoBaseTree)
} else {
imgSource = v1.NewEmptySrc()
}
activeFile := filepath.Join(ep.State.MountPoint, "cOS", constants.ActiveImgFile)
spec := &v1.ResetSpec{
Target: target,
Partitions: ep,
Efi: efiExists,
GrubDefEntry: constants.GrubDefEntry,
GrubConf: constants.GrubConf,
Tty: constants.DefaultTty,
FormatPersistent: true,
Active: v1.Image{
Label: constants.ActiveLabel,
Size: constants.ImgSize,
File: activeFile,
FS: constants.LinuxImgFs,
Source: imgSource,
MountPoint: constants.ActiveDir,
},
Passive: v1.Image{
File: filepath.Join(ep.State.MountPoint, "cOS", constants.PassiveImgFile),
Label: constants.PassiveLabel,
Size: constants.ImgSize,
Source: v1.NewFileSrc(activeFile),
FS: constants.LinuxImgFs,
},
State: installState,
}
// Get the actual source size to calculate the image size and partitions size
size, err := GetSourceSize(cfg, spec.Active.Source)
if err != nil {
cfg.Logger.Warnf("Failed to infer size for images: %s", err.Error())
} else {
cfg.Logger.Infof("Setting image size to %dMb", size)
spec.Active.Size = uint(size)
spec.Passive.Size = uint(size)
}
err = unmarshallFullSpec(cfg, "reset", spec)
if err != nil {
return nil, fmt.Errorf("failed unmarshalling the full spec: %w", err)
}
if ep.OEM == nil && spec.FormatOEM {
cfg.Logger.Warnf("no OEM partition found, won't format it")
}
if ep.Persistent == nil && spec.FormatPersistent {
cfg.Logger.Warnf("no Persistent partition found, won't format it")
}
// If we mount partitions by the /dev/disk/by-label stanza, their mountpoints wont show up due to ghw not
// accounting for them. Normally this is not an issue but in this case, if we are formatting a given partition we
// want to unmount it first. Thus we need to make sure that if its mounted we have the mountpoint info
if ep.Persistent.MountPoint == "" {
ep.Persistent.MountPoint = partitions.GetMountPointByLabel(ep.Persistent.FilesystemLabel)
}
if ep.OEM.MountPoint == "" {
ep.OEM.MountPoint = partitions.GetMountPointByLabel(ep.OEM.FilesystemLabel)
}
return spec, nil
}
// ReadResetSpecFromConfig will return a proper v1.ResetSpec based on an agent Config
func ReadResetSpecFromConfig(c *Config) (*v1.ResetSpec, error) {
sp, err := ReadSpecFromCloudConfig(c, "reset")
if err != nil {
return &v1.ResetSpec{}, err
}
resetSpec := sp.(*v1.ResetSpec)
return resetSpec, nil
}
func NewUkiResetSpec(cfg *Config) (spec *v1.ResetUkiSpec, err error) {
spec = &v1.ResetUkiSpec{
FormatPersistent: true, // Persistent is formatted by default
Partitions: v1.ElementalPartitions{},
}
_, ukiBootMode := cfg.Fs.Stat("/run/cos/uki_boot_mode")
if !BootedFrom(cfg.Runner, "rd.immucore.uki") && ukiBootMode == nil {
return spec, fmt.Errorf("uki reset can only be called from the recovery installed system")
}
// Fill persistent partition
spec.Partitions.Persistent = partitions.GetPartitionViaDM(cfg.Fs, constants.PersistentLabel)
spec.Partitions.OEM = partitions.GetPartitionViaDM(cfg.Fs, constants.OEMLabel)
// Get EFI partition
parts, err := partitions.GetAllPartitions(&cfg.Logger)
if err != nil {
return spec, fmt.Errorf("could not read host partitions")
}
for _, p := range parts {
if p.FilesystemLabel == constants.EfiLabel {
spec.Partitions.EFI = p
break
}
}
if spec.Partitions.Persistent == nil {
return spec, fmt.Errorf("persistent partition not found")
}
if spec.Partitions.OEM == nil {
return spec, fmt.Errorf("oem partition not found")
}
if spec.Partitions.EFI == nil {
return spec, fmt.Errorf("efi partition not found")
}
// Fill oem partition
err = unmarshallFullSpec(cfg, "reset", spec)
return spec, err
}
// ReadInstallSpecFromConfig will return a proper v1.InstallSpec based on an agent Config
func ReadInstallSpecFromConfig(c *Config) (*v1.InstallSpec, error) {
sp, err := ReadSpecFromCloudConfig(c, "install")
if err != nil {
return &v1.InstallSpec{}, err
}
installSpec := sp.(*v1.InstallSpec)
if (installSpec.Target == "" || installSpec.Target == "auto") && !installSpec.NoFormat {
installSpec.Target = detectLargestDevice()
}
return installSpec, nil
}
// ReadUkiResetSpecFromConfig will return a proper v1.ResetUkiSpec based on an agent Config
func ReadUkiResetSpecFromConfig(c *Config) (*v1.ResetUkiSpec, error) {
sp, err := ReadSpecFromCloudConfig(c, "reset-uki")
if err != nil {
return &v1.ResetUkiSpec{}, err
}
resetSpec := sp.(*v1.ResetUkiSpec)
return resetSpec, nil
}
func NewUkiInstallSpec(cfg *Config) (*v1.InstallUkiSpec, error) {
var activeImg v1.Image
isoRootExists, _ := fsutils.Exists(cfg.Fs, constants.IsoBaseTree)
// First try to use the install media source
if isoRootExists {
activeImg.Source = v1.NewDirSrc(constants.IsoBaseTree)
}
// Then any user provided source
if cfg.Install.Source != "" {
activeImg.Source, _ = v1.NewSrcFromURI(cfg.Install.Source)
}
// If we dont have any just an empty source so the sanitation fails
// TODO: Should we directly fail here if we got no source instead of waiting for the Sanitize() to fail?
if !isoRootExists && cfg.Install.Source == "" {
activeImg.Source = v1.NewEmptySrc()
}
spec := &v1.InstallUkiSpec{
Target: cfg.Install.Device,
Active: activeImg,
}
// Calculate the partitions afterwards so they use the image sizes for the final partition sizes
spec.Partitions.EFI = &types.Partition{
FilesystemLabel: constants.EfiLabel,
Size: constants.ImgSize * 5, // 15Gb for the EFI partition as default
Name: constants.EfiPartName,
FS: constants.EfiFs,
MountPoint: constants.EfiDir,
Flags: []string{"esp"},
}
spec.Partitions.OEM = &types.Partition{
FilesystemLabel: constants.OEMLabel,
Size: constants.OEMSize,
Name: constants.OEMPartName,
FS: constants.LinuxFs,
MountPoint: constants.OEMDir,
Flags: []string{},
}
spec.Partitions.Persistent = &types.Partition{
FilesystemLabel: constants.PersistentLabel,
Size: constants.PersistentSize,
Name: constants.PersistentPartName,
FS: constants.LinuxFs,
MountPoint: constants.PersistentDir,
Flags: []string{},
}
// Get the actual source size to calculate the image size and partitions size
size, err := GetSourceSize(cfg, spec.Active.Source)
if err != nil {
cfg.Logger.Warnf("Failed to infer size for images, leaving it as default size (%dMb): %s", spec.Partitions.EFI.Size, err.Error())
} else {
// Only override if the calculated size is bigger than the default size, otherwise stay with 15Gb minimum
if uint(size*3) > spec.Partitions.EFI.Size {
spec.Partitions.EFI.Size = uint(size * 3)
}
}
cfg.Logger.Infof("Setting image size to %dMb", spec.Partitions.EFI.Size)
err = unmarshallFullSpec(cfg, "install", spec)
// Add default values for the skip partitions for our default entries
spec.SkipEntries = append(spec.SkipEntries, constants.UkiDefaultSkipEntries()...)
return spec, err
}
// ReadUkiInstallSpecFromConfig will return a proper v1.InstallUkiSpec based on an agent Config
func ReadUkiInstallSpecFromConfig(c *Config) (*v1.InstallUkiSpec, error) {
sp, err := ReadSpecFromCloudConfig(c, "install-uki")
if err != nil {
return &v1.InstallUkiSpec{}, err
}
installSpec := sp.(*v1.InstallUkiSpec)
if (installSpec.Target == "" || installSpec.Target == "auto") && !installSpec.NoFormat {
installSpec.Target = detectLargestDevice()
}
return installSpec, nil
}
func NewUkiUpgradeSpec(cfg *Config) (*v1.UpgradeUkiSpec, error) {
spec := &v1.UpgradeUkiSpec{}
if err := unmarshallFullSpec(cfg, "upgrade", spec); err != nil {
return nil, fmt.Errorf("failed unmarshalling full spec: %w", err)
}
// TODO: Use this everywhere?
cfg.Logger.Infof("Checking if OCI image %s exists", spec.Active.Source.Value())
if spec.Active.Source.IsDocker() {
_, err := crane.Manifest(spec.Active.Source.Value())
if err != nil {
if strings.Contains(err.Error(), "MANIFEST_UNKNOWN") {
return nil, fmt.Errorf("oci image %s does not exist", spec.Active.Source.Value())
}
return nil, err
}
}
// Get the actual source size to calculate the image size and partitions size
size, err := GetSourceSize(cfg, spec.Active.Source)
if err != nil {
cfg.Logger.Warnf("Failed to infer size for images: %s", err.Error())
spec.Active.Size = constants.ImgSize
} else {
cfg.Logger.Infof("Setting image size to %dMb", size)
spec.Active.Size = uint(size)
}
// Get EFI partition
spec.EfiPartition, err = partitions.GetEfiPartition(&cfg.Logger)
if err != nil {
return spec, fmt.Errorf("could not read host partitions")
}
// Get free size of partition
var stat unix.Statfs_t
_ = unix.Statfs(spec.EfiPartition.MountPoint, &stat)
freeSize := stat.Bfree * uint64(stat.Bsize) / 1000 / 1000
cfg.Logger.Debugf("Partition on mountpoint %s has %dMb free", spec.EfiPartition.MountPoint, freeSize)
// Check if the source is over the free size
if spec.Active.Size > uint(freeSize) {
return spec, fmt.Errorf("source size(%d) is bigger than the free space(%d) on the EFI partition(%s)", spec.Active.Size, freeSize, spec.EfiPartition.MountPoint)
}
return spec, err
}
// ReadUkiUpgradeSpecFromConfig will return a proper v1.UpgradeUkiSpec based on an agent Config
func ReadUkiUpgradeSpecFromConfig(c *Config) (*v1.UpgradeUkiSpec, error) {
sp, err := ReadSpecFromCloudConfig(c, "upgrade-uki")
if err != nil {
return &v1.UpgradeUkiSpec{}, err
}
upgradeSpec := sp.(*v1.UpgradeUkiSpec)
return upgradeSpec, nil
}
// getSize will calculate the size of a file or symlink and will do nothing with directories
// fileList: keeps track of the files visited to avoid counting a file more than once if it's a symlink. It could also be used as a way to filter some files
// size: will be the memory that adds up all the files sizes. Meaning it could be initialized with a value greater than 0 if needed.
func getSize(vfs v1.FS, size *int64, fileList map[string]bool, path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
actualFilePath := path
if d.Type()&fs.ModeSymlink != 0 {
// If it's a symlink, get its target and calculate its size.
var err error
actualFilePath, err = vfs.Readlink(path)
if err != nil {
return err
}
if !filepath.IsAbs(actualFilePath) {
// If it's a relative path, join it with the base directory path.
actualFilePath = filepath.Join(filepath.Dir(path), actualFilePath)
}
}
fileInfo, err := vfs.Stat(actualFilePath)
if os.IsNotExist(err) || fileList[actualFilePath] {
return nil
}
if err != nil {
return err
}
*size += fileInfo.Size()
fileList[actualFilePath] = true
return nil
}
// GetSourceSize will try to gather the actual size of the source
// Useful to create the exact size of images and by side effect the partition size
// This helps adjust the size to be juuuuust right.
// It can still be manually override from the cloud config by setting all values manually
// But by default it should adjust the sizes properly
func GetSourceSize(config *Config, source *v1.ImageSource) (int64, error) {
var size int64
var err error
var filesVisited map[string]bool
switch {
case source.IsDocker():
// Docker size is uncompressed! So we double it to be sure that we have enough space
// Otherwise we would need to READ all the layers uncompressed to calculate the image size which would
// double the download size and slow down everything
size, err = config.ImageExtractor.GetOCIImageSize(source.Value(), config.Platform.String())
size = int64(float64(size) * 2.5)
case source.IsDir():
filesVisited = make(map[string]bool, 30000) // An Ubuntu system has around 27k files. This improves performance by not having to resize the map for every file visited
// In kubernetes we use the suc script to upgrade (https://github.com/kairos-io/packages/blob/main/packages/system/suc-upgrade/suc-upgrade.sh)
// , which mounts the host root into $HOST_DIR
// we should skip that dir when calculating the size as we would be doubling the calculated size
// Plus we will hit the usual things when checking a running system. Processes that go away, tmpfiles, etc...
// This is always set for pods running under kubernetes
_, underKubernetes := os.LookupEnv("KUBERNETES_SERVICE_HOST")
// Try to get the HOST_DIR in case we are not using the default one
hostDir := k8sutils.GetHostDirForK8s()
config.Logger.Logger.Debug().Bool("status", underKubernetes).Str("hostdir", hostDir).Msg("Kubernetes check")
err = fsutils.WalkDirFs(config.Fs, source.Value(), func(path string, d fs.DirEntry, err error) error {
// If its empty we are just not setting it, so probably out of the k8s upgrade path
if hostDir != "" && strings.HasPrefix(path, hostDir) {
config.Logger.Logger.Debug().Str("path", path).Str("hostDir", hostDir).Msg("Skipping file as it is a host directory")
} else if underKubernetes && (strings.HasPrefix(path, "/proc") || strings.HasPrefix(path, "/dev") || strings.HasPrefix(path, "/run")) {
// If under kubernetes, the upgrade will check the size of / which includes the host dir mounted under /host
// But it also can bind the host mounts into / so we want to skip those runtime dirs
// During install or upgrade outside kubernetes, we dont care about those dirs as they are not expected to be in the source dir
config.Logger.Logger.Debug().Str("path", path).Str("hostDir", hostDir).Msg("Skipping dir as it is a runtime directory under kubernetes (/proc, /dev or /run)")
} else {
v := getSize(config.Fs, &size, filesVisited, path, d, err)
return v
}
return nil
})
case source.IsFile():
file, err := config.Fs.Stat(source.Value())
if err != nil {
return size, err
}
size = file.Size()
}
// Normalize size to Mb before returning and add 100Mb to round the size from bytes to mb+extra files like grub stuff
if size != 0 {
size = (size / 1000 / 1000) + 100
}
return size, err
}
// ReadUpgradeSpecFromConfig will return a proper v1.UpgradeSpec based on an agent Config
func ReadUpgradeSpecFromConfig(c *Config) (*v1.UpgradeSpec, error) {
sp, err := ReadSpecFromCloudConfig(c, "upgrade")
if err != nil {
return &v1.UpgradeSpec{}, err
}
upgradeSpec := sp.(*v1.UpgradeSpec)
return upgradeSpec, nil
}
// ReadSpecFromCloudConfig returns a v1.Spec for the given spec
func ReadSpecFromCloudConfig(r *Config, spec string) (v1.Spec, error) {
var sp v1.Spec
var err error
switch spec {
case "install":
sp, err = NewInstallSpec(r)
case "upgrade":
sp, err = NewUpgradeSpec(r)
case "reset":
sp, err = NewResetSpec(r)
case "install-uki":
sp, err = NewUkiInstallSpec(r)
case "reset-uki":
sp, err = NewUkiResetSpec(r)
case "upgrade-uki":
sp, err = NewUkiUpgradeSpec(r)
default:
return nil, fmt.Errorf("spec not valid: %s", spec)
}
if err != nil {
return nil, fmt.Errorf("failed initializing spec: %v", err)
}
err = sp.Sanitize()
if err != nil {
return sp, fmt.Errorf("sanitizing the %s spec: %w", spec, err)
}
r.Logger.Debugf("Loaded %s spec: %s", spec, litter.Sdump(sp))
return sp, nil
}
var decodeHook = viper.DecodeHook(
mapstructure.ComposeDecodeHookFunc(
UnmarshalerHook(),
mapstructure.StringToTimeDurationHookFunc(),
mapstructure.StringToSliceHookFunc(","),
),
)
type Unmarshaler interface {
CustomUnmarshal(interface{}) (bool, error)
}
func UnmarshalerHook() mapstructure.DecodeHookFunc {
return func(from reflect.Value, to reflect.Value) (interface{}, error) {
// get the destination object address if it is not passed by reference
if to.CanAddr() {
to = to.Addr()
}
// If the destination implements the unmarshaling interface
u, ok := to.Interface().(Unmarshaler)
if !ok {
return from.Interface(), nil
}
// If it is nil and a pointer, create and assign the target value first
if to.IsNil() && to.Type().Kind() == reflect.Ptr {
to.Set(reflect.New(to.Type().Elem()))
u = to.Interface().(Unmarshaler)
}
// Call the custom unmarshaling method
cont, err := u.CustomUnmarshal(from.Interface())
if cont {
// Continue with the decoding stack
return from.Interface(), err
}
// Decoding finalized
return to.Interface(), err
}
}