/
driver_qemu_templates.go
905 lines (789 loc) · 21.8 KB
/
driver_qemu_templates.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
package drivers
import (
"fmt"
"strings"
"github.com/canonical/lxd/lxd/resources"
"github.com/canonical/lxd/shared/osarch"
)
type cfgEntry struct {
key string
value string
}
type cfgSection struct {
name string
comment string
entries []cfgEntry
}
func qemuStringifyCfg(cfg ...cfgSection) *strings.Builder {
sb := &strings.Builder{}
for _, section := range cfg {
if section.comment != "" {
sb.WriteString(fmt.Sprintf("# %s\n", section.comment))
}
sb.WriteString(fmt.Sprintf("[%s]\n", section.name))
for _, entry := range section.entries {
value := entry.value
if value != "" {
sb.WriteString(fmt.Sprintf("%s = \"%s\"\n", entry.key, value))
}
}
sb.WriteString("\n")
}
return sb
}
func qemuMachineType(architecture int) string {
var machineType string
switch architecture {
case osarch.ARCH_64BIT_INTEL_X86:
machineType = "q35"
case osarch.ARCH_64BIT_ARMV8_LITTLE_ENDIAN:
machineType = "virt"
case osarch.ARCH_64BIT_POWERPC_LITTLE_ENDIAN:
machineType = "pseries"
case osarch.ARCH_64BIT_S390_BIG_ENDIAN:
machineType = "s390-ccw-virtio"
}
return machineType
}
type qemuBaseOpts struct {
architecture int
}
func qemuBase(opts *qemuBaseOpts) []cfgSection {
machineType := qemuMachineType(opts.architecture)
gicVersion := ""
capLargeDecr := ""
switch opts.architecture {
case osarch.ARCH_64BIT_ARMV8_LITTLE_ENDIAN:
gicVersion = "max"
case osarch.ARCH_64BIT_POWERPC_LITTLE_ENDIAN:
capLargeDecr = "off"
}
sections := []cfgSection{{
name: "machine",
comment: "Machine",
entries: []cfgEntry{
{key: "graphics", value: "off"},
{key: "type", value: machineType},
{key: "gic-version", value: gicVersion},
{key: "cap-large-decr", value: capLargeDecr},
{key: "accel", value: "kvm"},
{key: "usb", value: "off"},
},
}}
if opts.architecture == osarch.ARCH_64BIT_INTEL_X86 {
sections = append(sections, []cfgSection{{
name: "global",
entries: []cfgEntry{
{key: "driver", value: "ICH9-LPC"},
{key: "property", value: "disable_s3"},
{key: "value", value: "1"},
},
}, {
name: "global",
entries: []cfgEntry{
{key: "driver", value: "ICH9-LPC"},
{key: "property", value: "disable_s4"},
{key: "value", value: "1"},
},
}}...)
}
return append(
sections,
cfgSection{
name: "boot-opts",
entries: []cfgEntry{{key: "strict", value: "on"}},
})
}
type qemuMemoryOpts struct {
memSizeMB int64
}
func qemuMemory(opts *qemuMemoryOpts) []cfgSection {
return []cfgSection{{
name: "memory",
comment: "Memory",
entries: []cfgEntry{{key: "size", value: fmt.Sprintf("%dM", opts.memSizeMB)}},
}}
}
type qemuDevOpts struct {
busName string
devBus string
devAddr string
multifunction bool
}
type qemuDevEntriesOpts struct {
dev qemuDevOpts
pciName string
ccwName string
}
func qemuDeviceEntries(opts *qemuDevEntriesOpts) []cfgEntry {
entries := []cfgEntry{}
if opts.dev.busName == "pci" || opts.dev.busName == "pcie" {
entries = append(entries, []cfgEntry{
{key: "driver", value: opts.pciName},
{key: "bus", value: opts.dev.devBus},
{key: "addr", value: opts.dev.devAddr},
}...)
} else if opts.dev.busName == "ccw" {
entries = append(entries, cfgEntry{key: "driver", value: opts.ccwName})
}
if opts.dev.multifunction {
entries = append(entries, cfgEntry{key: "multifunction", value: "on"})
}
return entries
}
type qemuSerialOpts struct {
dev qemuDevOpts
charDevName string
ringbufSizeBytes int
}
func qemuSerial(opts *qemuSerialOpts) []cfgSection {
entriesOpts := qemuDevEntriesOpts{
dev: opts.dev,
pciName: "virtio-serial-pci",
ccwName: "virtio-serial-ccw",
}
return []cfgSection{{
name: `device "dev-qemu_serial"`,
comment: "Virtual serial bus",
entries: qemuDeviceEntries(&entriesOpts),
}, {
// Ring buffer used by the lxd agent to report (write) its status to. LXD server will read
// its content via QMP using "ringbuf-read" command.
name: fmt.Sprintf(`chardev "%s"`, opts.charDevName),
comment: "LXD serial identifier",
entries: []cfgEntry{
{key: "backend", value: "ringbuf"},
{key: "size", value: fmt.Sprintf("%dB", opts.ringbufSizeBytes)}},
}, {
// QEMU serial device connected to the above ring buffer.
name: `device "qemu_serial"`,
entries: []cfgEntry{
{key: "driver", value: "virtserialport"},
{key: "name", value: "com.canonical.lxd"},
{key: "chardev", value: opts.charDevName},
{key: "bus", value: "dev-qemu_serial.0"},
},
}, {
// Legacy QEMU serial device, not connected to any ring buffer. Its purpose is to
// create a symlink in /dev/virtio-ports/, triggering a udev rule to start the lxd-agent.
// This is necessary for backward compatibility with virtual machines lacking the
// updated lxd-agent-loader package, which includes updated udev rules and a systemd unit.
name: `device "qemu_serial_legacy"`,
entries: []cfgEntry{
{key: "driver", value: "virtserialport"},
{key: "name", value: "org.linuxcontainers.lxd"},
{key: "bus", value: "dev-qemu_serial.0"},
},
}, {
name: `chardev "qemu_spice-chardev"`,
comment: "Spice agent",
entries: []cfgEntry{
{key: "backend", value: "spicevmc"},
{key: "name", value: "vdagent"},
},
}, {
name: `device "qemu_spice"`,
entries: []cfgEntry{
{key: "driver", value: "virtserialport"},
{key: "name", value: "com.redhat.spice.0"},
{key: "chardev", value: "qemu_spice-chardev"},
{key: "bus", value: "dev-qemu_serial.0"},
},
}, {
name: `chardev "qemu_spicedir-chardev"`,
comment: "Spice folder",
entries: []cfgEntry{
{key: "backend", value: "spiceport"},
{key: "name", value: "org.spice-space.webdav.0"},
},
}, {
name: `device "qemu_spicedir"`,
entries: []cfgEntry{
{key: "driver", value: "virtserialport"},
{key: "name", value: "org.spice-space.webdav.0"},
{key: "chardev", value: "qemu_spicedir-chardev"},
{key: "bus", value: "dev-qemu_serial.0"},
},
}}
}
type qemuPCIeOpts struct {
portName string
index int
devAddr string
multifunction bool
}
func qemuPCIe(opts *qemuPCIeOpts) []cfgSection {
entries := []cfgEntry{
{key: "driver", value: "pcie-root-port"},
{key: "bus", value: "pcie.0"},
{key: "addr", value: opts.devAddr},
{key: "chassis", value: fmt.Sprintf("%d", opts.index)},
}
if opts.multifunction {
entries = append(entries, cfgEntry{key: "multifunction", value: "on"})
}
return []cfgSection{{
name: fmt.Sprintf(`device "%s"`, opts.portName),
entries: entries,
}}
}
func qemuSCSI(opts *qemuDevOpts) []cfgSection {
entriesOpts := qemuDevEntriesOpts{
dev: *opts,
pciName: "virtio-scsi-pci",
ccwName: "virtio-scsi-ccw",
}
return []cfgSection{{
name: `device "qemu_scsi"`,
comment: "SCSI controller",
entries: qemuDeviceEntries(&entriesOpts),
}}
}
func qemuBalloon(opts *qemuDevOpts) []cfgSection {
entriesOpts := qemuDevEntriesOpts{
dev: *opts,
pciName: "virtio-balloon-pci",
ccwName: "virtio-balloon-ccw",
}
return []cfgSection{{
name: `device "qemu_balloon"`,
comment: "Balloon driver",
entries: qemuDeviceEntries(&entriesOpts),
}}
}
func qemuRNG(opts *qemuDevOpts) []cfgSection {
entriesOpts := qemuDevEntriesOpts{
dev: *opts,
pciName: "virtio-rng-pci",
ccwName: "virtio-rng-ccw",
}
return []cfgSection{{
name: `object "qemu_rng"`,
comment: "Random number generator",
entries: []cfgEntry{
{key: "qom-type", value: "rng-random"},
{key: "filename", value: "/dev/urandom"},
},
}, {
name: `device "dev-qemu_rng"`,
entries: append(qemuDeviceEntries(&entriesOpts),
cfgEntry{key: "rng", value: "qemu_rng"}),
}}
}
type qemuSevOpts struct {
cbitpos int
reducedPhysBits int
policy string
dhCertFD string
sessionDataFD string
}
func qemuSEV(opts *qemuSevOpts) []cfgSection {
entries := []cfgEntry{
{key: "qom-type", value: "sev-guest"},
{key: "cbitpos", value: fmt.Sprintf("%d", opts.cbitpos)},
{key: "reduced-phys-bits", value: fmt.Sprintf("%d", opts.reducedPhysBits)},
{key: "policy", value: opts.policy},
}
if opts.dhCertFD != "" && opts.sessionDataFD != "" {
entries = append(entries, cfgEntry{key: "dh-cert-file", value: opts.dhCertFD}, cfgEntry{key: "session-file", value: opts.sessionDataFD})
}
return []cfgSection{{
name: `object "sev0"`,
comment: "Secure Encrypted Virtualization",
entries: entries,
}}
}
type qemuVsockOpts struct {
dev qemuDevOpts
vsockFD int
vsockID uint32
}
func qemuVsock(opts *qemuVsockOpts) []cfgSection {
entriesOpts := qemuDevEntriesOpts{
dev: opts.dev,
pciName: "vhost-vsock-pci",
ccwName: "vhost-vsock-ccw",
}
return []cfgSection{{
name: `device "qemu_vsock"`,
comment: "Vsock",
entries: append(qemuDeviceEntries(&entriesOpts),
cfgEntry{key: "guest-cid", value: fmt.Sprintf("%d", opts.vsockID)},
cfgEntry{key: "vhostfd", value: fmt.Sprintf("%d", opts.vsockFD)}),
}}
}
type qemuGpuOpts struct {
dev qemuDevOpts
architecture int
}
func qemuGPU(opts *qemuGpuOpts) []cfgSection {
var pciName string
if opts.architecture == osarch.ARCH_64BIT_INTEL_X86 {
pciName = "virtio-vga"
} else {
pciName = "virtio-gpu-pci"
}
entriesOpts := qemuDevEntriesOpts{
dev: opts.dev,
pciName: pciName,
ccwName: "virtio-gpu-ccw",
}
return []cfgSection{{
name: `device "qemu_gpu"`,
comment: "GPU",
entries: qemuDeviceEntries(&entriesOpts),
}}
}
func qemuKeyboard(opts *qemuDevOpts) []cfgSection {
entriesOpts := qemuDevEntriesOpts{
dev: *opts,
pciName: "virtio-keyboard-pci",
ccwName: "virtio-keyboard-ccw",
}
return []cfgSection{{
name: `device "qemu_keyboard"`,
comment: "Input",
entries: qemuDeviceEntries(&entriesOpts),
}}
}
func qemuTablet(opts *qemuDevOpts) []cfgSection {
entriesOpts := qemuDevEntriesOpts{
dev: *opts,
pciName: "virtio-tablet-pci",
ccwName: "virtio-tablet-ccw",
}
return []cfgSection{{
name: `device "qemu_tablet"`,
comment: "Input",
entries: qemuDeviceEntries(&entriesOpts),
}}
}
type qemuNumaEntry struct {
node uint64
socket uint64
core uint64
thread uint64
}
type qemuCPUOpts struct {
architecture string
cpuCount int
cpuRequested int
cpuSockets int
cpuCores int
cpuThreads int
cpuNumaNodes []uint64
cpuNumaMapping []qemuNumaEntry
cpuNumaHostNodes []uint64
hugepages string
memory int64
qemuMemObjectFormat string
}
func qemuCPUNumaHostNode(opts *qemuCPUOpts, index int) []cfgSection {
entries := []cfgEntry{}
if opts.hugepages != "" {
entries = append(entries, []cfgEntry{
{key: "qom-type", value: "memory-backend-file"},
{key: "mem-path", value: opts.hugepages},
{key: "prealloc", value: "on"},
{key: "discard-data", value: "on"},
}...)
} else {
entries = append(entries, cfgEntry{key: "qom-type", value: "memory-backend-memfd"})
}
entries = append(entries, cfgEntry{key: "size", value: fmt.Sprintf("%dM", opts.memory)})
return []cfgSection{{
name: fmt.Sprintf("object \"mem%d\"", index),
entries: entries,
}, {
name: "numa",
entries: []cfgEntry{
{key: "type", value: "node"},
{key: "nodeid", value: fmt.Sprintf("%d", index)},
{key: "memdev", value: fmt.Sprintf("mem%d", index)},
},
}}
}
func qemuCPU(opts *qemuCPUOpts, pinning bool) []cfgSection {
entries := []cfgEntry{
{key: "cpus", value: fmt.Sprintf("%d", opts.cpuCount)},
}
if pinning {
entries = append(entries, cfgEntry{
key: "sockets", value: fmt.Sprintf("%d", opts.cpuSockets),
}, cfgEntry{
key: "cores", value: fmt.Sprintf("%d", opts.cpuCores),
}, cfgEntry{
key: "threads", value: fmt.Sprintf("%d", opts.cpuThreads),
})
} else {
cpu, err := resources.GetCPU()
if err != nil {
return nil
}
// Cap the max number of CPUs to 64 unless directly assigned more.
max := 64
if int(cpu.Total) < max {
max = int(cpu.Total)
} else if opts.cpuRequested > max {
max = opts.cpuRequested
} else if opts.cpuCount > max {
max = opts.cpuCount
}
entries = append(entries, cfgEntry{
key: "maxcpus", value: fmt.Sprintf("%d", max),
})
}
sections := []cfgSection{{
name: "smp-opts",
comment: "CPU",
entries: entries,
}}
if opts.architecture != "x86_64" {
return sections
}
share := cfgEntry{key: "share", value: "on"}
if len(opts.cpuNumaHostNodes) == 0 {
// add one mem and one numa sections with index 0
numaHostNode := qemuCPUNumaHostNode(opts, 0)
// unconditionally append "share = "on" to the [object "mem0"] section
numaHostNode[0].entries = append(numaHostNode[0].entries, share)
return append(sections, numaHostNode...)
}
for index, element := range opts.cpuNumaHostNodes {
numaHostNode := qemuCPUNumaHostNode(opts, index)
extraMemEntries := []cfgEntry{{key: "policy", value: "bind"}}
if opts.hugepages != "" {
// append share = "on" only if hugepages is set
extraMemEntries = append(extraMemEntries, share)
}
var hostNodesKey string
if opts.qemuMemObjectFormat == "indexed" {
hostNodesKey = "host-nodes.0"
} else {
hostNodesKey = "host-nodes"
}
hostNode := cfgEntry{key: hostNodesKey, value: fmt.Sprintf("%d", element)}
extraMemEntries = append(extraMemEntries, hostNode)
// append the extra entries to the [object "mem{{idx}}"] section
numaHostNode[0].entries = append(numaHostNode[0].entries, extraMemEntries...)
sections = append(sections, numaHostNode...)
}
for _, numa := range opts.cpuNumaMapping {
sections = append(sections, cfgSection{
name: "numa",
entries: []cfgEntry{
{key: "type", value: "cpu"},
{key: "node-id", value: fmt.Sprintf("%d", numa.node)},
{key: "socket-id", value: fmt.Sprintf("%d", numa.socket)},
{key: "core-id", value: fmt.Sprintf("%d", numa.core)},
{key: "thread-id", value: fmt.Sprintf("%d", numa.thread)},
},
})
}
return sections
}
type qemuControlSocketOpts struct {
path string
}
func qemuControlSocket(opts *qemuControlSocketOpts) []cfgSection {
return []cfgSection{{
name: `chardev "monitor"`,
comment: "Qemu control",
entries: []cfgEntry{
{key: "backend", value: "socket"},
{key: "path", value: opts.path},
{key: "server", value: "on"},
{key: "wait", value: "off"},
},
}, {
name: "mon",
entries: []cfgEntry{
{key: "chardev", value: "monitor"},
{key: "mode", value: "control"},
},
}}
}
type qemuConsoleOpts struct {
path string
}
func qemuConsole(opts *qemuConsoleOpts) []cfgSection {
return []cfgSection{{
name: `chardev "console"`,
comment: "Console",
entries: []cfgEntry{
{key: "backend", value: "socket"},
{key: "path", value: opts.path},
{key: "server", value: "on"},
{key: "wait", value: "off"},
},
}}
}
type qemuDriveFirmwareOpts struct {
roPath string
nvramPath string
}
func qemuDriveFirmware(opts *qemuDriveFirmwareOpts) []cfgSection {
return []cfgSection{{
name: "drive",
comment: "Firmware (read only)",
entries: []cfgEntry{
{key: "file", value: opts.roPath},
{key: "if", value: "pflash"},
{key: "format", value: "raw"},
{key: "unit", value: "0"},
{key: "readonly", value: "on"},
},
}, {
name: "drive",
comment: "Firmware settings (writable)",
entries: []cfgEntry{
{key: "file", value: opts.nvramPath},
{key: "if", value: "pflash"},
{key: "format", value: "raw"},
{key: "unit", value: "1"},
},
}}
}
type qemuHostDriveOpts struct {
dev qemuDevOpts
name string
nameSuffix string
comment string
fsdriver string
mountTag string
securityModel string
path string
sockFd string
readonly bool
protocol string
}
func qemuHostDrive(opts *qemuHostDriveOpts) []cfgSection {
var extraDeviceEntries []cfgEntry
var driveSection cfgSection
deviceOpts := qemuDevEntriesOpts{dev: opts.dev}
if opts.protocol == "9p" {
var readonly string
if opts.readonly {
readonly = "on"
} else {
readonly = "off"
}
driveSection = cfgSection{
name: fmt.Sprintf(`fsdev "%s"`, opts.name),
comment: opts.comment,
entries: []cfgEntry{
{key: "fsdriver", value: opts.fsdriver},
{key: "sock_fd", value: opts.sockFd},
{key: "security_model", value: opts.securityModel},
{key: "readonly", value: readonly},
{key: "path", value: opts.path},
},
}
deviceOpts.pciName = "virtio-9p-pci"
deviceOpts.ccwName = "virtio-9p-ccw"
extraDeviceEntries = []cfgEntry{
{key: "mount_tag", value: opts.mountTag},
{key: "fsdev", value: opts.name},
}
} else if opts.protocol == "virtio-fs" {
driveSection = cfgSection{
name: fmt.Sprintf(`chardev "%s"`, opts.name),
comment: opts.comment,
entries: []cfgEntry{
{key: "backend", value: "socket"},
{key: "path", value: opts.path},
},
}
deviceOpts.pciName = "vhost-user-fs-pci"
deviceOpts.ccwName = "vhost-user-fs-ccw"
extraDeviceEntries = []cfgEntry{
{key: "tag", value: opts.mountTag},
{key: "chardev", value: opts.name},
}
} else {
return []cfgSection{}
}
return []cfgSection{
driveSection,
{
name: fmt.Sprintf(`device "dev-%s%s-%s"`, opts.name, opts.nameSuffix, opts.protocol),
entries: append(qemuDeviceEntries(&deviceOpts), extraDeviceEntries...),
},
}
}
type qemuDriveConfigOpts struct {
dev qemuDevOpts
protocol string
path string
}
func qemuDriveConfig(opts *qemuDriveConfigOpts) []cfgSection {
return qemuHostDrive(&qemuHostDriveOpts{
dev: opts.dev,
// Devices use "qemu_" prefix indicating that this is a internally named device.
name: "qemu_config",
nameSuffix: "-drive",
comment: fmt.Sprintf("Config drive (%s)", opts.protocol),
mountTag: "config",
protocol: opts.protocol,
fsdriver: "local",
readonly: true,
securityModel: "none",
path: opts.path,
})
}
type qemuDriveDirOpts struct {
dev qemuDevOpts
devName string
mountTag string
path string
protocol string
proxyFD int
readonly bool
}
func qemuDriveDir(opts *qemuDriveDirOpts) []cfgSection {
return qemuHostDrive(&qemuHostDriveOpts{
dev: opts.dev,
// Devices use "lxd_" prefix indicating that this is a user named device.
name: fmt.Sprintf("lxd_%s", opts.devName),
comment: fmt.Sprintf("%s drive (%s)", opts.devName, opts.protocol),
mountTag: opts.mountTag,
protocol: opts.protocol,
fsdriver: "proxy",
readonly: opts.readonly,
path: opts.path,
sockFd: fmt.Sprintf("%d", opts.proxyFD),
})
}
type qemuPCIPhysicalOpts struct {
dev qemuDevOpts
devName string
pciSlotName string
}
func qemuPCIPhysical(opts *qemuPCIPhysicalOpts) []cfgSection {
deviceOpts := qemuDevEntriesOpts{
dev: opts.dev,
pciName: "vfio-pci",
ccwName: "vfio-ccw",
}
entries := append(qemuDeviceEntries(&deviceOpts), []cfgEntry{
{key: "host", value: opts.pciSlotName},
}...)
return []cfgSection{{
// Devices use "lxd_" prefix indicating that this is a user named device.
name: fmt.Sprintf(`device "dev-lxd_%s"`, opts.devName),
comment: fmt.Sprintf(`PCI card ("%s" device)`, opts.devName),
entries: entries,
}}
}
type qemuGPUDevPhysicalOpts struct {
dev qemuDevOpts
devName string
pciSlotName string
vgpu string
vga bool
}
func qemuGPUDevPhysical(opts *qemuGPUDevPhysicalOpts) []cfgSection {
deviceOpts := qemuDevEntriesOpts{
dev: opts.dev,
pciName: "vfio-pci",
ccwName: "vfio-ccw",
}
entries := qemuDeviceEntries(&deviceOpts)
if opts.vgpu != "" {
sysfsdev := fmt.Sprintf("/sys/bus/mdev/devices/%s", opts.vgpu)
entries = append(entries, cfgEntry{key: "sysfsdev", value: sysfsdev})
} else {
entries = append(entries, cfgEntry{key: "host", value: opts.pciSlotName})
}
if opts.vga {
entries = append(entries, cfgEntry{key: "x-vga", value: "on"})
}
return []cfgSection{{
// Devices use "lxd_" prefix indicating that this is a user named device.
name: fmt.Sprintf(`device "dev-lxd_%s"`, opts.devName),
comment: fmt.Sprintf(`GPU card ("%s" device)`, opts.devName),
entries: entries,
}}
}
type qemuUSBOpts struct {
devBus string
devAddr string
multifunction bool
ports int
}
func qemuUSB(opts *qemuUSBOpts) []cfgSection {
deviceOpts := qemuDevEntriesOpts{
dev: qemuDevOpts{
busName: "pci",
devAddr: opts.devAddr,
devBus: opts.devBus,
multifunction: opts.multifunction,
},
pciName: "qemu-xhci",
}
sections := []cfgSection{{
name: `device "qemu_usb"`,
comment: "USB controller",
entries: append(qemuDeviceEntries(&deviceOpts), []cfgEntry{
{key: "p2", value: fmt.Sprintf("%d", opts.ports)},
{key: "p3", value: fmt.Sprintf("%d", opts.ports)},
}...),
}}
for i := 1; i <= 3; i++ {
chardev := fmt.Sprintf("qemu_spice-usb-chardev%d", i)
sections = append(sections, []cfgSection{{
name: fmt.Sprintf(`chardev "%s"`, chardev),
entries: []cfgEntry{
{key: "backend", value: "spicevmc"},
{key: "name", value: "usbredir"},
},
}, {
name: fmt.Sprintf(`device "qemu_spice-usb%d"`, i),
entries: []cfgEntry{
{key: "driver", value: "usb-redir"},
{key: "chardev", value: chardev},
},
}}...)
}
return sections
}
type qemuTPMOpts struct {
devName string
path string
}
func qemuTPM(opts *qemuTPMOpts) []cfgSection {
chardev := fmt.Sprintf("qemu_tpm-chardev_%s", opts.devName)
tpmdev := fmt.Sprintf("qemu_tpm-tpmdev_%s", opts.devName)
return []cfgSection{{
name: fmt.Sprintf(`chardev "%s"`, chardev),
entries: []cfgEntry{
{key: "backend", value: "socket"},
{key: "path", value: opts.path},
},
}, {
name: fmt.Sprintf(`tpmdev "%s"`, tpmdev),
entries: []cfgEntry{
{key: "type", value: "emulator"},
{key: "chardev", value: chardev},
},
}, {
name: fmt.Sprintf(`device "dev-lxd_%s"`, opts.devName),
entries: []cfgEntry{
{key: "driver", value: "tpm-crb"},
{key: "tpmdev", value: tpmdev},
},
}}
}
type qemuVmgenIDOpts struct {
guid string
}
func qemuVmgen(opts *qemuVmgenIDOpts) []cfgSection {
return []cfgSection{{
name: `device "vmgenid0"`,
comment: "VM Generation ID",
entries: []cfgEntry{
{key: "driver", value: "vmgenid"},
{key: "guid", value: opts.guid},
},
}}
}