forked from kubevirt/kubevirt
/
converter.go
1391 lines (1217 loc) · 40.1 KB
/
converter.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
/*
* This file is part of the KubeVirt project
*
* 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.
*
* Copyright 2017, 2018 Red Hat, Inc.
*
*/
package api
import (
"bytes"
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
"strconv"
"strings"
"syscall"
k8sv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
v1 "kubevirt.io/kubevirt/pkg/api/v1"
cloudinit "kubevirt.io/kubevirt/pkg/cloud-init"
"kubevirt.io/kubevirt/pkg/config"
containerdisk "kubevirt.io/kubevirt/pkg/container-disk"
"kubevirt.io/kubevirt/pkg/emptydisk"
ephemeraldisk "kubevirt.io/kubevirt/pkg/ephemeral-disk"
"kubevirt.io/kubevirt/pkg/ignition"
"kubevirt.io/kubevirt/pkg/log"
"kubevirt.io/kubevirt/pkg/precond"
"kubevirt.io/kubevirt/pkg/util"
"kubevirt.io/kubevirt/pkg/util/net/dns"
)
const (
CPUModeHostPassthrough = "host-passthrough"
CPUModeHostModel = "host-model"
defaultIOThread = uint(1)
EFIPath = "/usr/share/OVMF/OVMF_CODE.fd"
EFIVarsPath = "/usr/share/OVMF/OVMF_VARS.fd"
)
type ConverterContext struct {
UseEmulation bool
Secrets map[string]*k8sv1.Secret
VirtualMachine *v1.VirtualMachineInstance
CPUSet []int
IsBlockPVC map[string]bool
SRIOVDevices map[string][]string
}
func Convert_v1_Disk_To_api_Disk(diskDevice *v1.Disk, disk *Disk, devicePerBus map[string]int, numQueues *uint) error {
if diskDevice.Disk != nil {
disk.Device = "disk"
disk.Target.Bus = diskDevice.Disk.Bus
disk.Target.Device = makeDeviceName(diskDevice.Disk.Bus, devicePerBus)
if diskDevice.Disk.PciAddress != "" {
if diskDevice.Disk.Bus != "virtio" {
return fmt.Errorf("setting a pci address is not allowed for non-virtio bus types, for disk %s", diskDevice.Name)
}
addr, err := decoratePciAddressField(diskDevice.Disk.PciAddress)
if err != nil {
return fmt.Errorf("failed to configure disk %s: %v", diskDevice.Name, err)
}
disk.Address = addr
}
disk.ReadOnly = toApiReadOnly(diskDevice.Disk.ReadOnly)
disk.Serial = diskDevice.Serial
} else if diskDevice.LUN != nil {
disk.Device = "lun"
disk.Target.Bus = diskDevice.LUN.Bus
disk.Target.Device = makeDeviceName(diskDevice.LUN.Bus, devicePerBus)
disk.ReadOnly = toApiReadOnly(diskDevice.LUN.ReadOnly)
} else if diskDevice.Floppy != nil {
disk.Device = "floppy"
disk.Target.Bus = "fdc"
disk.Target.Tray = string(diskDevice.Floppy.Tray)
disk.Target.Device = makeDeviceName(disk.Target.Bus, devicePerBus)
disk.ReadOnly = toApiReadOnly(diskDevice.Floppy.ReadOnly)
} else if diskDevice.CDRom != nil {
disk.Device = "cdrom"
disk.Target.Tray = string(diskDevice.CDRom.Tray)
disk.Target.Bus = diskDevice.CDRom.Bus
disk.Target.Device = makeDeviceName(diskDevice.CDRom.Bus, devicePerBus)
if diskDevice.CDRom.ReadOnly != nil {
disk.ReadOnly = toApiReadOnly(*diskDevice.CDRom.ReadOnly)
} else {
disk.ReadOnly = toApiReadOnly(true)
}
}
disk.Driver = &DiskDriver{
Name: "qemu",
Cache: string(diskDevice.Cache),
}
if numQueues != nil {
disk.Driver.Queues = numQueues
}
disk.Alias = &Alias{Name: diskDevice.Name}
if diskDevice.BootOrder != nil {
disk.BootOrder = &BootOrder{Order: *diskDevice.BootOrder}
}
return nil
}
func checkDirectIOFlag(path string) bool {
// check if fs where disk.img file is located or block device
// support direct i/o
f, err := os.OpenFile(path, syscall.O_RDONLY|syscall.O_DIRECT, 0)
defer f.Close()
if err != nil && !os.IsNotExist(err) {
return false
}
return true
}
func SetDriverCacheMode(disk *Disk) error {
var path string
supportDirectIO := true
mode := v1.DriverCache(disk.Driver.Cache)
if disk.Source.File != "" {
path = disk.Source.File
} else if disk.Source.Dev != "" {
path = disk.Source.Dev
} else {
return fmt.Errorf("Unable to set a driver cache mode, disk is neither a block device nor a file")
}
if mode == "" || mode == v1.CacheNone {
supportDirectIO = checkDirectIOFlag(path)
if !supportDirectIO {
log.Log.Infof("%s file system does not support direct I/O", path)
}
}
// if user set a cache mode = 'none' and fs does not support direct I/O then return an error
if mode == v1.CacheNone && !supportDirectIO {
return fmt.Errorf("Unable to use '%s' cache mode, file system where %s is stored does not support direct I/O", mode, path)
}
// if user did not set a cache mode and fs supports direct I/O then set cache = 'none'
// else set cache = 'writethrough
if mode == "" && supportDirectIO {
mode = v1.CacheNone
} else if mode == "" && !supportDirectIO {
mode = v1.CacheWriteThrough
}
disk.Driver.Cache = string(mode)
log.Log.Infof("Driver cache mode for %s set to %s", path, mode)
return nil
}
func makeDeviceName(bus string, devicePerBus map[string]int) string {
index := devicePerBus[bus]
devicePerBus[bus] += 1
prefix := ""
switch bus {
case "virtio":
prefix = "vd"
case "sata", "scsi":
prefix = "sd"
case "fdc":
prefix = "fd"
default:
log.Log.Errorf("Unrecognized bus '%s'", bus)
return ""
}
return formatDeviceName(prefix, index)
}
// port of http://elixir.free-electrons.com/linux/v4.15/source/drivers/scsi/sd.c#L3211
func formatDeviceName(prefix string, index int) string {
base := int('z' - 'a' + 1)
name := ""
for index >= 0 {
name = string('a'+(index%base)) + name
index = (index / base) - 1
}
return prefix + name
}
func toApiReadOnly(src bool) *ReadOnly {
if src {
return &ReadOnly{}
}
return nil
}
// Add_Agent_To_api_Channel creates the channel for guest agent communication
func Add_Agent_To_api_Channel() (channel Channel) {
channel.Type = "unix"
// let libvirt decide which path to use
channel.Source = nil
channel.Target = &ChannelTarget{
Name: "org.qemu.guest_agent.0",
Type: "virtio",
}
return
}
func Convert_v1_Volume_To_api_Disk(source *v1.Volume, disk *Disk, c *ConverterContext) error {
if source.ContainerDisk != nil {
return Convert_v1_ContainerDiskSource_To_api_Disk(source.Name, source.ContainerDisk, disk, c)
}
if source.CloudInitNoCloud != nil {
return Convert_v1_CloudInitNoCloudSource_To_api_Disk(source.CloudInitNoCloud, disk, c)
}
if source.HostDisk != nil {
return Convert_v1_HostDisk_To_api_Disk(source.HostDisk.Path, disk, c)
}
if source.PersistentVolumeClaim != nil {
return Convert_v1_PersistentVolumeClaim_To_api_Disk(source.Name, disk, c)
}
if source.DataVolume != nil {
return Convert_v1_FilesystemVolumeSource_To_api_Disk(source.Name, disk, c)
}
if source.Ephemeral != nil {
return Convert_v1_EphemeralVolumeSource_To_api_Disk(source.Name, source.Ephemeral, disk, c)
}
if source.EmptyDisk != nil {
return Convert_v1_EmptyDiskSource_To_api_Disk(source.Name, source.EmptyDisk, disk, c)
}
if source.ConfigMap != nil {
return Convert_v1_Config_To_api_Disk(source.Name, disk, config.ConfigMap)
}
if source.Secret != nil {
return Convert_v1_Config_To_api_Disk(source.Name, disk, config.Secret)
}
if source.ServiceAccount != nil {
return Convert_v1_Config_To_api_Disk(source.Name, disk, config.ServiceAccount)
}
return fmt.Errorf("disk %s references an unsupported source", disk.Alias.Name)
}
func Convert_v1_Config_To_api_Disk(volumeName string, disk *Disk, configType config.Type) error {
disk.Type = "file"
disk.Driver.Type = "raw"
switch configType {
case config.ConfigMap:
disk.Source.File = config.GetConfigMapDiskPath(volumeName)
break
case config.Secret:
disk.Source.File = config.GetSecretDiskPath(volumeName)
break
case config.ServiceAccount:
disk.Source.File = config.GetServiceAccountDiskPath()
break
default:
return fmt.Errorf("Cannot convert config '%s' to disk, unrecognized type", configType)
}
return nil
}
func GetFilesystemVolumePath(volumeName string) string {
return filepath.Join(string(filepath.Separator), "var", "run", "kubevirt-private", "vmi-disks", volumeName, "disk.img")
}
func GetBlockDeviceVolumePath(volumeName string) string {
return filepath.Join(string(filepath.Separator), "dev", volumeName)
}
func Convert_v1_PersistentVolumeClaim_To_api_Disk(name string, disk *Disk, c *ConverterContext) error {
if c.IsBlockPVC[name] {
return Convert_v1_BlockVolumeSource_To_api_Disk(name, disk, c)
}
return Convert_v1_FilesystemVolumeSource_To_api_Disk(name, disk, c)
}
// Convert_v1_FilesystemVolumeSource_To_api_Disk takes a FS source and builds the KVM Disk representation
func Convert_v1_FilesystemVolumeSource_To_api_Disk(volumeName string, disk *Disk, c *ConverterContext) error {
disk.Type = "file"
disk.Driver.Type = "raw"
disk.Source.File = GetFilesystemVolumePath(volumeName)
return nil
}
func Convert_v1_BlockVolumeSource_To_api_Disk(volumeName string, disk *Disk, c *ConverterContext) error {
disk.Type = "block"
disk.Driver.Type = "raw"
disk.Source.Dev = GetBlockDeviceVolumePath(volumeName)
return nil
}
func Convert_v1_HostDisk_To_api_Disk(path string, disk *Disk, c *ConverterContext) error {
disk.Type = "file"
disk.Driver.Type = "raw"
disk.Source.File = path
return nil
}
func Convert_v1_CloudInitNoCloudSource_To_api_Disk(source *v1.CloudInitNoCloudSource, disk *Disk, c *ConverterContext) error {
if disk.Type == "lun" {
return fmt.Errorf("device %s is of type lun. Not compatible with a file based disk", disk.Alias.Name)
}
disk.Source.File = fmt.Sprintf("%s/%s", cloudinit.GetDomainBasePath(c.VirtualMachine.Name, c.VirtualMachine.Namespace), cloudinit.NoCloudFile)
disk.Type = "file"
disk.Driver.Type = "raw"
return nil
}
func Convert_v1_IgnitionData_To_api_Disk(disk *Disk, c *ConverterContext) error {
disk.Source.File = fmt.Sprintf("%s/%s", ignition.GetDomainBasePath(c.VirtualMachine.Name, c.VirtualMachine.Namespace), c.VirtualMachine.Annotations[v1.IgnitionAnnotation])
disk.Type = "file"
disk.Driver.Type = "raw"
return nil
}
func Convert_v1_EmptyDiskSource_To_api_Disk(volumeName string, _ *v1.EmptyDiskSource, disk *Disk, c *ConverterContext) error {
if disk.Type == "lun" {
return fmt.Errorf("device %s is of type lun. Not compatible with a file based disk", disk.Alias.Name)
}
disk.Type = "file"
disk.Driver.Type = "qcow2"
disk.Source.File = emptydisk.FilePathForVolumeName(volumeName)
return nil
}
func Convert_v1_ContainerDiskSource_To_api_Disk(volumeName string, _ *v1.ContainerDiskSource, disk *Disk, c *ConverterContext) error {
if disk.Type == "lun" {
return fmt.Errorf("device %s is of type lun. Not compatible with a file based disk", disk.Alias.Name)
}
disk.Type = "file"
diskPath, diskType, err := containerdisk.GetFilePath(c.VirtualMachine, volumeName)
if err != nil {
return err
}
disk.Driver.Type = diskType
disk.Source.File = diskPath
return nil
}
func Convert_v1_EphemeralVolumeSource_To_api_Disk(volumeName string, source *v1.EphemeralVolumeSource, disk *Disk, c *ConverterContext) error {
disk.Type = "file"
disk.Driver.Type = "qcow2"
disk.Source.File = ephemeraldisk.GetFilePath(volumeName)
disk.BackingStore = &BackingStore{
Format: &BackingStoreFormat{},
Source: &DiskSource{},
}
backingDisk := &Disk{Driver: &DiskDriver{}}
err := Convert_v1_FilesystemVolumeSource_To_api_Disk(volumeName, backingDisk, c)
if err != nil {
return err
}
disk.BackingStore.Format.Type = backingDisk.Driver.Type
disk.BackingStore.Source = &backingDisk.Source
disk.BackingStore.Type = backingDisk.Type
return nil
}
func Convert_v1_Watchdog_To_api_Watchdog(source *v1.Watchdog, watchdog *Watchdog, _ *ConverterContext) error {
watchdog.Alias = &Alias{
Name: source.Name,
}
if source.I6300ESB != nil {
watchdog.Model = "i6300esb"
watchdog.Action = string(source.I6300ESB.Action)
return nil
}
return fmt.Errorf("watchdog %s can't be mapped, no watchdog type specified", source.Name)
}
func Convert_v1_Rng_To_api_Rng(source *v1.Rng, rng *Rng, _ *ConverterContext) error {
// default rng model for KVM/QEMU virtualization
rng.Model = "virtio"
// default backend model, random
rng.Backend = &RngBackend{
Model: "random",
}
// the default source for rng is dev urandom
rng.Backend.Source = "/dev/urandom"
return nil
}
func Convert_v1_Input_To_api_InputDevice(input *v1.Input, inputDevice *Input, _ *ConverterContext) error {
if input.Bus != "virtio" && input.Bus != "usb" && input.Bus != "" {
return fmt.Errorf("input contains unsupported bus %s", input.Bus)
}
if input.Bus != "virtio" && input.Bus != "usb" {
input.Bus = "usb"
}
if input.Type != "tablet" {
return fmt.Errorf("input contains unsupported type %s", input.Type)
}
inputDevice.Bus = input.Bus
inputDevice.Type = input.Type
inputDevice.Alias = &Alias{Name: input.Name}
return nil
}
func Convert_v1_Clock_To_api_Clock(source *v1.Clock, clock *Clock, c *ConverterContext) error {
if source.UTC != nil {
clock.Offset = "utc"
if source.UTC.OffsetSeconds != nil {
clock.Adjustment = strconv.Itoa(*source.UTC.OffsetSeconds)
} else {
clock.Adjustment = "reset"
}
} else if source.Timezone != nil {
clock.Offset = "timezone"
}
if source.Timer != nil {
if source.Timer.RTC != nil {
newTimer := Timer{Name: "rtc"}
newTimer.Track = string(source.Timer.RTC.Track)
newTimer.TickPolicy = string(source.Timer.RTC.TickPolicy)
newTimer.Present = boolToYesNo(source.Timer.RTC.Enabled, true)
clock.Timer = append(clock.Timer, newTimer)
}
if source.Timer.PIT != nil {
newTimer := Timer{Name: "pit"}
newTimer.Present = boolToYesNo(source.Timer.PIT.Enabled, true)
newTimer.TickPolicy = string(source.Timer.PIT.TickPolicy)
clock.Timer = append(clock.Timer, newTimer)
}
if source.Timer.KVM != nil {
newTimer := Timer{Name: "kvmclock"}
newTimer.Present = boolToYesNo(source.Timer.KVM.Enabled, true)
clock.Timer = append(clock.Timer, newTimer)
}
if source.Timer.HPET != nil {
newTimer := Timer{Name: "hpet"}
newTimer.Present = boolToYesNo(source.Timer.HPET.Enabled, true)
newTimer.TickPolicy = string(source.Timer.HPET.TickPolicy)
clock.Timer = append(clock.Timer, newTimer)
}
if source.Timer.Hyperv != nil {
newTimer := Timer{Name: "hypervclock"}
newTimer.Present = boolToYesNo(source.Timer.Hyperv.Enabled, true)
clock.Timer = append(clock.Timer, newTimer)
}
}
return nil
}
func convertFeatureState(source *v1.FeatureState) *FeatureState {
if source != nil {
return &FeatureState{
State: boolToOnOff(source.Enabled, true),
}
}
return nil
}
//isUSBDevicePresent checks if exists device with usb bus in vmi
func isUSBDevicePresent(vmi *v1.VirtualMachineInstance) bool {
usbDeviceExists := false
for _, input := range vmi.Spec.Domain.Devices.Inputs {
if input.Bus == "usb" {
usbDeviceExists = true
return usbDeviceExists
}
}
return usbDeviceExists
}
func Convert_v1_Features_To_api_Features(source *v1.Features, features *Features, c *ConverterContext) error {
if source.ACPI.Enabled == nil || *source.ACPI.Enabled {
features.ACPI = &FeatureEnabled{}
}
if source.SMM != nil {
if source.SMM.Enabled == nil || *source.SMM.Enabled {
features.SMM = &FeatureEnabled{}
}
}
if source.APIC != nil {
if source.APIC.Enabled == nil || *source.APIC.Enabled {
features.APIC = &FeatureEnabled{}
}
}
if source.Hyperv != nil {
features.Hyperv = &FeatureHyperv{}
err := Convert_v1_FeatureHyperv_To_api_FeatureHyperv(source.Hyperv, features.Hyperv, c)
if err != nil {
return nil
}
}
return nil
}
func Convert_v1_Machine_To_api_OSType(source *v1.Machine, ost *OSType, c *ConverterContext) error {
ost.Machine = source.Type
return nil
}
func Convert_v1_FeatureHyperv_To_api_FeatureHyperv(source *v1.FeatureHyperv, hyperv *FeatureHyperv, c *ConverterContext) error {
if source.Spinlocks != nil {
hyperv.Spinlocks = &FeatureSpinlocks{
State: boolToOnOff(source.Spinlocks.Enabled, true),
Retries: source.Spinlocks.Retries,
}
}
if source.VendorID != nil {
hyperv.VendorID = &FeatureVendorID{
State: boolToOnOff(source.VendorID.Enabled, true),
Value: source.VendorID.VendorID,
}
}
hyperv.Relaxed = convertFeatureState(source.Relaxed)
hyperv.Reset = convertFeatureState(source.Reset)
hyperv.Runtime = convertFeatureState(source.Runtime)
hyperv.SyNIC = convertFeatureState(source.SyNIC)
hyperv.SyNICTimer = convertFeatureState(source.SyNICTimer)
hyperv.VAPIC = convertFeatureState(source.VAPIC)
hyperv.VPIndex = convertFeatureState(source.VPIndex)
return nil
}
func filterAddress(addrs []string, addr string) []string {
var res []string
for _, a := range addrs {
if a != addr {
res = append(res, a)
}
}
return res
}
func reserveAddress(addrsMap map[string][]string, addr string) {
// Sometimes the same address is available to multiple networks,
// specifically when two networks refer to the same resourceName. In this
// case, we should make sure that a reserved address is removed from *all*
// per-network lists of available devices, to avoid configuring the same
// device ID for multiple interfaces.
for networkName, addrs := range addrsMap {
addrsMap[networkName] = filterAddress(addrs, addr)
}
return
}
// Get the next PCI address available to a particular SR-IOV network. The
// function makes sure that the allocated address is not allocated to next
// callers, whether they request an address for the same network or another
// network that is backed by the same resourceName.
func popSRIOVPCIAddress(networkName string, addrsMap map[string][]string) (string, map[string][]string, error) {
if len(addrsMap[networkName]) > 0 {
addr := addrsMap[networkName][0]
reserveAddress(addrsMap, addr)
return addr, addrsMap, nil
}
return "", addrsMap, fmt.Errorf("no more SR-IOV PCI addresses to allocate")
}
func Convert_v1_VirtualMachine_To_api_Domain(vmi *v1.VirtualMachineInstance, domain *Domain, c *ConverterContext) (err error) {
precond.MustNotBeNil(vmi)
precond.MustNotBeNil(domain)
precond.MustNotBeNil(c)
domain.Spec.Name = VMINamespaceKeyFunc(vmi)
domain.ObjectMeta.Name = vmi.ObjectMeta.Name
domain.ObjectMeta.Namespace = vmi.ObjectMeta.Namespace
if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) {
if c.UseEmulation {
logger := log.DefaultLogger()
logger.Infof("Hardware emulation device '/dev/kvm' not present. Using software emulation.")
domain.Spec.Type = "qemu"
} else {
return fmt.Errorf("hardware emulation device '/dev/kvm' not present")
}
} else if err != nil {
return err
}
virtioNetProhibited := false
if _, err := os.Stat("/dev/vhost-net"); os.IsNotExist(err) {
if c.UseEmulation {
logger := log.DefaultLogger()
logger.Infof("In-kernel virtio-net device emulation '/dev/vhost-net' not present. Falling back to QEMU userland emulation.")
} else {
virtioNetProhibited = true
}
} else if err != nil {
return err
}
// Spec metadata
newChannel := Add_Agent_To_api_Channel()
domain.Spec.Devices.Channels = append(domain.Spec.Devices.Channels, newChannel)
domain.Spec.Metadata.KubeVirt.UID = vmi.UID
gracePeriodSeconds := v1.DefaultGracePeriodSeconds
if vmi.Spec.TerminationGracePeriodSeconds != nil {
gracePeriodSeconds = *vmi.Spec.TerminationGracePeriodSeconds
}
domain.Spec.Metadata.KubeVirt.GracePeriod = &GracePeriodMetadata{
DeletionGracePeriodSeconds: gracePeriodSeconds,
}
domain.Spec.SysInfo = &SysInfo{}
if vmi.Spec.Domain.Firmware != nil {
domain.Spec.SysInfo.System = []Entry{
{
Name: "uuid",
Value: string(vmi.Spec.Domain.Firmware.UUID),
},
}
if vmi.Spec.Domain.Firmware.Bootloader != nil && vmi.Spec.Domain.Firmware.Bootloader.EFI != nil {
domain.Spec.OS.BootLoader = &Loader{
Path: EFIPath,
ReadOnly: "yes",
Secure: "no",
Type: "pflash",
}
domain.Spec.OS.NVRam = &NVRam{
NVRam: filepath.Join("/tmp", domain.Spec.Name),
Template: EFIVarsPath,
}
}
if len(vmi.Spec.Domain.Firmware.Serial) > 0 {
domain.Spec.SysInfo.System = append(domain.Spec.SysInfo.System, Entry{Name: "serial", Value: string(vmi.Spec.Domain.Firmware.Serial)})
}
}
// Take memory from the requested memory
if v, ok := vmi.Spec.Domain.Resources.Requests[k8sv1.ResourceMemory]; ok {
if domain.Spec.Memory, err = QuantityToByte(v); err != nil {
return err
}
}
// In case that guest memory is explicitly set, override it
if vmi.Spec.Domain.Memory != nil && vmi.Spec.Domain.Memory.Guest != nil {
if domain.Spec.Memory, err = QuantityToByte(*vmi.Spec.Domain.Memory.Guest); err != nil {
return err
}
}
if vmi.Spec.Domain.Memory != nil && vmi.Spec.Domain.Memory.Hugepages != nil {
domain.Spec.MemoryBacking = &MemoryBacking{
HugePages: &HugePages{},
}
}
volumes := map[string]*v1.Volume{}
for _, volume := range vmi.Spec.Volumes {
volumes[volume.Name] = volume.DeepCopy()
}
dedicatedThreads := 0
autoThreads := 0
useIOThreads := false
threadPoolLimit := 1
if vmi.Spec.Domain.IOThreadsPolicy != nil {
useIOThreads = true
if (*vmi.Spec.Domain.IOThreadsPolicy) == v1.IOThreadsPolicyAuto {
numCPUs := 1
// Requested CPU's is guaranteed to be no greater than the limit
if cpuRequests, ok := vmi.Spec.Domain.Resources.Requests[k8sv1.ResourceCPU]; ok {
numCPUs = int(cpuRequests.Value())
} else if cpuLimit, ok := vmi.Spec.Domain.Resources.Limits[k8sv1.ResourceCPU]; ok {
numCPUs = int(cpuLimit.Value())
}
threadPoolLimit = numCPUs * 2
}
}
for _, diskDevice := range vmi.Spec.Domain.Devices.Disks {
dedicatedThread := false
if diskDevice.DedicatedIOThread != nil {
dedicatedThread = *diskDevice.DedicatedIOThread
}
if dedicatedThread {
useIOThreads = true
dedicatedThreads += 1
} else {
autoThreads += 1
}
}
if (autoThreads + dedicatedThreads) > threadPoolLimit {
autoThreads = threadPoolLimit - dedicatedThreads
// We need at least one shared thread
if autoThreads < 1 {
autoThreads = 1
}
}
ioThreadCount := (autoThreads + dedicatedThreads)
if ioThreadCount != 0 {
if domain.Spec.IOThreads == nil {
domain.Spec.IOThreads = &IOThreads{}
}
domain.Spec.IOThreads.IOThreads = uint(ioThreadCount)
}
currentAutoThread := defaultIOThread
currentDedicatedThread := uint(autoThreads + 1)
var numQueues *uint
virtioBlkMQRequested := (vmi.Spec.Domain.Devices.BlockMultiQueue != nil) && (*vmi.Spec.Domain.Devices.BlockMultiQueue)
virtioNetMQRequested := (vmi.Spec.Domain.Devices.NetworkInterfaceMultiQueue != nil) && (*vmi.Spec.Domain.Devices.NetworkInterfaceMultiQueue)
if virtioBlkMQRequested || virtioNetMQRequested {
// Requested CPU's is guaranteed to be no greater than the limit
if cpuRequests, ok := vmi.Spec.Domain.Resources.Requests[k8sv1.ResourceCPU]; ok {
numCPUs := uint(cpuRequests.Value())
numQueues = &numCPUs
} else if cpuLimit, ok := vmi.Spec.Domain.Resources.Limits[k8sv1.ResourceCPU]; ok {
numCPUs := uint(cpuLimit.Value())
numQueues = &numCPUs
}
}
devicePerBus := make(map[string]int)
for _, disk := range vmi.Spec.Domain.Devices.Disks {
newDisk := Disk{}
err := Convert_v1_Disk_To_api_Disk(&disk, &newDisk, devicePerBus, numQueues)
if err != nil {
return err
}
volume := volumes[disk.Name]
if volume == nil {
return fmt.Errorf("No matching volume with name %s found", disk.Name)
}
err = Convert_v1_Volume_To_api_Disk(volume, &newDisk, c)
if err != nil {
return err
}
if useIOThreads {
ioThreadId := defaultIOThread
dedicatedThread := false
if disk.DedicatedIOThread != nil {
dedicatedThread = *disk.DedicatedIOThread
}
if dedicatedThread {
ioThreadId = currentDedicatedThread
currentDedicatedThread += 1
} else {
ioThreadId = currentAutoThread
// increment the threadId to be used next but wrap around at the thread limit
// the odd math here is because thread ID's start at 1, not 0
currentAutoThread = (currentAutoThread % uint(autoThreads)) + 1
}
newDisk.Driver.IOThread = &ioThreadId
}
domain.Spec.Devices.Disks = append(domain.Spec.Devices.Disks, newDisk)
}
if vmi.Spec.Domain.Devices.Watchdog != nil {
newWatchdog := &Watchdog{}
err := Convert_v1_Watchdog_To_api_Watchdog(vmi.Spec.Domain.Devices.Watchdog, newWatchdog, c)
if err != nil {
return err
}
domain.Spec.Devices.Watchdog = newWatchdog
}
if vmi.Spec.Domain.Devices.Rng != nil {
newRng := &Rng{}
err := Convert_v1_Rng_To_api_Rng(vmi.Spec.Domain.Devices.Rng, newRng, c)
if err != nil {
return err
}
domain.Spec.Devices.Rng = newRng
}
//usb controller is turned on, only when user specify input device with usb bus,
//otherwise it is turned off
if usbDeviceExists := isUSBDevicePresent(vmi); !usbDeviceExists {
// disable usb controller
domain.Spec.Devices.Controllers = append(domain.Spec.Devices.Controllers, Controller{
Type: "usb",
Index: "0",
Model: "none",
})
}
if vmi.Spec.Domain.Devices.Inputs != nil {
inputDevices := make([]Input, 0)
for _, input := range vmi.Spec.Domain.Devices.Inputs {
inputDevice := Input{}
err := Convert_v1_Input_To_api_InputDevice(&input, &inputDevice, c)
inputDevices = append(inputDevices, inputDevice)
if err != nil {
return err
}
}
domain.Spec.Devices.Inputs = inputDevices
}
if vmi.Spec.Domain.Clock != nil {
clock := vmi.Spec.Domain.Clock
newClock := &Clock{}
err := Convert_v1_Clock_To_api_Clock(clock, newClock, c)
if err != nil {
return err
}
domain.Spec.Clock = newClock
}
if vmi.Spec.Domain.Features != nil {
domain.Spec.Features = &Features{}
err := Convert_v1_Features_To_api_Features(vmi.Spec.Domain.Features, domain.Spec.Features, c)
if err != nil {
return err
}
}
apiOst := &vmi.Spec.Domain.Machine
err = Convert_v1_Machine_To_api_OSType(apiOst, &domain.Spec.OS.Type, c)
if err != nil {
return err
}
// Set VM CPU cores
// CPU topology will be created everytime, because user can specify
// number of cores in vmi.Spec.Domain.Resources.Requests/Limits, not only
// in vmi.Spec.Domain.CPU
domain.Spec.CPU.Topology = getCPUTopology(vmi)
domain.Spec.VCPU = &VCPU{
Placement: "static",
CPUs: calculateRequestedVCPUs(domain.Spec.CPU.Topology),
}
if vmi.Spec.Domain.CPU != nil {
// Set VM CPU model and vendor
if vmi.Spec.Domain.CPU.Model != "" {
if vmi.Spec.Domain.CPU.Model == v1.CPUModeHostModel || vmi.Spec.Domain.CPU.Model == v1.CPUModeHostPassthrough {
domain.Spec.CPU.Mode = vmi.Spec.Domain.CPU.Model
} else {
domain.Spec.CPU.Mode = "custom"
domain.Spec.CPU.Model = vmi.Spec.Domain.CPU.Model
}
}
// Set VM CPU features
if vmi.Spec.Domain.CPU.Features != nil {
for _, feature := range vmi.Spec.Domain.CPU.Features {
domain.Spec.CPU.Features = append(domain.Spec.CPU.Features, CPUFeature{
Name: feature.Name,
Policy: feature.Policy,
})
}
}
// Adjust guest vcpu config. Currenty will handle vCPUs to pCPUs pinning
if vmi.IsCPUDedicated() {
if err := formatDomainCPUTune(vmi, domain, c); err != nil {
log.Log.Reason(err).Error("failed to format domain cputune.")
return err
}
if useIOThreads {
if err := formatDomainIOThreadPin(vmi, domain, c); err != nil {
log.Log.Reason(err).Error("failed to format domain iothread pinning.")
return err
}
}
}
}
if vmi.Spec.Domain.CPU == nil || vmi.Spec.Domain.CPU.Model == "" {
domain.Spec.CPU.Mode = v1.CPUModeHostModel
}
// Add mandatory console device
var serialPort uint = 0
var serialType string = "serial"
domain.Spec.Devices.Consoles = []Console{
{
Type: "pty",
Target: &ConsoleTarget{
Type: &serialType,
Port: &serialPort,
},
},
}
domain.Spec.Devices.Serials = []Serial{
{
Type: "unix",
Target: &SerialTarget{
Port: &serialPort,
},
Source: &SerialSource{
Mode: "bind",
Path: fmt.Sprintf("/var/run/kubevirt-private/%s/virt-serial%d", vmi.ObjectMeta.UID, serialPort),
},
},
}
if vmi.Spec.Domain.Devices.AutoattachGraphicsDevice == nil || *vmi.Spec.Domain.Devices.AutoattachGraphicsDevice == true {
var heads uint = 1
var vram uint = 16384
domain.Spec.Devices.Video = []Video{
{
Model: VideoModel{
Type: "vga",
Heads: &heads,
VRam: &vram,
},
},
}
domain.Spec.Devices.Graphics = []Graphics{
{
Listen: &GraphicsListen{
Type: "socket",
Socket: fmt.Sprintf("/var/run/kubevirt-private/%s/virt-vnc", vmi.ObjectMeta.UID),
},
Type: "vnc",
},
}
}
getInterfaceType := func(iface *v1.Interface) string {
if iface.Slirp != nil {
// Slirp configuration works only with e1000 or rtl8139
if iface.Model != "e1000" && iface.Model != "rtl8139" {
log.Log.Infof("The network interface type of %s was changed to e1000 due to unsupported interface type by qemu slirp network", iface.Name)
return "e1000"
}
return iface.Model
}
if iface.Model != "" {
return iface.Model
}
return "virtio"
}
networks := map[string]*v1.Network{}
cniNetworks := map[string]int{}
for _, network := range vmi.Spec.Networks {
numberOfSources := 0
if network.Pod != nil {
numberOfSources++
}
if network.Multus != nil {
cniNetworks[network.Name] = len(cniNetworks) + 1
numberOfSources++
}
if network.Genie != nil {
cniNetworks[network.Name] = len(cniNetworks)
numberOfSources++
}
if numberOfSources == 0 {
return fmt.Errorf("fail network %s must have a network type", network.Name)
} else if numberOfSources > 1 {
return fmt.Errorf("fail network %s must have only one network type", network.Name)
}
networks[network.Name] = network.DeepCopy()
}
sriovPciAddresses := make(map[string][]string)
for key, value := range c.SRIOVDevices {