/
qemu.go
1129 lines (1031 loc) · 37.1 KB
/
qemu.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
package qemu
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"os/exec"
"os/user"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"time"
"github.com/lima-vm/lima/pkg/networks/usernet"
"github.com/lima-vm/lima/pkg/osutil"
"github.com/coreos/go-semver/semver"
"github.com/digitalocean/go-qemu/qmp"
"github.com/digitalocean/go-qemu/qmp/raw"
"github.com/docker/go-units"
"github.com/lima-vm/lima/pkg/fileutils"
"github.com/lima-vm/lima/pkg/iso9660util"
"github.com/lima-vm/lima/pkg/limayaml"
"github.com/lima-vm/lima/pkg/localpathutil"
"github.com/lima-vm/lima/pkg/networks"
"github.com/lima-vm/lima/pkg/qemu/imgutil"
"github.com/lima-vm/lima/pkg/store"
"github.com/lima-vm/lima/pkg/store/filenames"
"github.com/mattn/go-shellwords"
"github.com/sirupsen/logrus"
)
type Config struct {
Name string
InstanceDir string
LimaYAML *limayaml.LimaYAML
SSHLocalPort int
}
// MinimumQemuVersion is the minimum supported QEMU version
const (
MinimumQemuVersion = "4.0.0"
)
// EnsureDisk also ensures the kernel and the initrd
func EnsureDisk(ctx context.Context, cfg Config) error {
diffDisk := filepath.Join(cfg.InstanceDir, filenames.DiffDisk)
if _, err := os.Stat(diffDisk); err == nil || !errors.Is(err, os.ErrNotExist) {
// disk is already ensured
return err
}
baseDisk := filepath.Join(cfg.InstanceDir, filenames.BaseDisk)
kernel := filepath.Join(cfg.InstanceDir, filenames.Kernel)
kernelCmdline := filepath.Join(cfg.InstanceDir, filenames.KernelCmdline)
initrd := filepath.Join(cfg.InstanceDir, filenames.Initrd)
if _, err := os.Stat(baseDisk); errors.Is(err, os.ErrNotExist) {
var ensuredBaseDisk bool
errs := make([]error, len(cfg.LimaYAML.Images))
for i, f := range cfg.LimaYAML.Images {
if _, err := fileutils.DownloadFile(ctx, baseDisk, f.File, true, "the image", *cfg.LimaYAML.Arch); err != nil {
errs[i] = err
continue
}
if f.Kernel != nil {
if _, err := fileutils.DownloadFile(ctx, kernel, f.Kernel.File, false, "the kernel", *cfg.LimaYAML.Arch); err != nil {
errs[i] = err
continue
}
if f.Kernel.Cmdline != "" {
if err := os.WriteFile(kernelCmdline, []byte(f.Kernel.Cmdline), 0o644); err != nil {
errs[i] = err
continue
}
}
}
if f.Initrd != nil {
if _, err := fileutils.DownloadFile(ctx, initrd, *f.Initrd, false, "the initrd", *cfg.LimaYAML.Arch); err != nil {
errs[i] = err
continue
}
}
ensuredBaseDisk = true
break
}
if !ensuredBaseDisk {
return fileutils.Errors(errs)
}
}
diskSize, _ := units.RAMInBytes(*cfg.LimaYAML.Disk)
if diskSize == 0 {
return nil
}
isBaseDiskISO, err := iso9660util.IsISO9660(baseDisk)
if err != nil {
return err
}
baseDiskInfo, err := imgutil.GetInfo(baseDisk)
if err != nil {
return fmt.Errorf("failed to get the information of base disk %q: %w", baseDisk, err)
}
if err = imgutil.AcceptableAsBasedisk(baseDiskInfo); err != nil {
return fmt.Errorf("file %q is not acceptable as the base disk: %w", baseDisk, err)
}
if baseDiskInfo.Format == "" {
return fmt.Errorf("failed to inspect the format of %q", baseDisk)
}
args := []string{"create", "-f", "qcow2"}
if !isBaseDiskISO {
args = append(args, "-F", baseDiskInfo.Format, "-b", baseDisk)
}
args = append(args, diffDisk, strconv.Itoa(int(diskSize)))
cmd := exec.Command("qemu-img", args...)
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("failed to run %v: %q: %w", cmd.Args, string(out), err)
}
return nil
}
func CreateDataDisk(dir, format string, size int) error {
dataDisk := filepath.Join(dir, filenames.DataDisk)
if _, err := os.Stat(dataDisk); err == nil || !errors.Is(err, fs.ErrNotExist) {
// datadisk already exists
return err
}
args := []string{"create", "-f", format, dataDisk, strconv.Itoa(size)}
cmd := exec.Command("qemu-img", args...)
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("failed to run %v: %q: %w", cmd.Args, string(out), err)
}
return nil
}
func ResizeDataDisk(dir, format string, size int) error {
dataDisk := filepath.Join(dir, filenames.DataDisk)
args := []string{"resize", "-f", format, dataDisk, strconv.Itoa(size)}
cmd := exec.Command("qemu-img", args...)
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("failed to run %v: %q: %w", cmd.Args, string(out), err)
}
return nil
}
func newQmpClient(cfg Config) (*qmp.SocketMonitor, error) {
qmpSock := filepath.Join(cfg.InstanceDir, filenames.QMPSock)
qmpClient, err := qmp.NewSocketMonitor("unix", qmpSock, 5*time.Second)
if err != nil {
return nil, err
}
return qmpClient, nil
}
func sendHmpCommand(cfg Config, cmd, tag string) (string, error) {
qmpClient, err := newQmpClient(cfg)
if err != nil {
return "", err
}
if err := qmpClient.Connect(); err != nil {
return "", err
}
defer func() { _ = qmpClient.Disconnect() }()
rawClient := raw.NewMonitor(qmpClient)
logrus.Infof("Sending HMP %s command", cmd)
hmc := fmt.Sprintf("%s %s", cmd, tag)
return rawClient.HumanMonitorCommand(hmc, nil)
}
func execImgCommand(cfg Config, args ...string) (string, error) {
diffDisk := filepath.Join(cfg.InstanceDir, filenames.DiffDisk)
args = append(args, diffDisk)
logrus.Debugf("Running qemu-img %v command", args)
cmd := exec.Command("qemu-img", args...)
b, err := cmd.Output()
if err != nil {
return "", err
}
return string(b), err
}
func Del(cfg Config, run bool, tag string) error {
if run {
out, err := sendHmpCommand(cfg, "delvm", tag)
// there can still be output, even if no error!
if out != "" {
logrus.Warnf("output: %s", strings.TrimSpace(out))
}
return err
}
// -d deletes a snapshot
_, err := execImgCommand(cfg, "snapshot", "-d", tag)
return err
}
func Save(cfg Config, run bool, tag string) error {
if run {
out, err := sendHmpCommand(cfg, "savevm", tag)
// there can still be output, even if no error!
if out != "" {
logrus.Warnf("output: %s", strings.TrimSpace(out))
}
return err
}
// -c creates a snapshot
_, err := execImgCommand(cfg, "snapshot", "-c", tag)
return err
}
func Load(cfg Config, run bool, tag string) error {
if run {
out, err := sendHmpCommand(cfg, "loadvm", tag)
// there can still be output, even if no error!
if out != "" {
logrus.Warnf("output: %s", strings.TrimSpace(out))
}
return err
}
// -a applies a snapshot
_, err := execImgCommand(cfg, "snapshot", "-a", tag)
return err
}
// List returns a space-separated list of all snapshots, with header and newlines
func List(cfg Config, run bool) (string, error) {
if run {
out, err := sendHmpCommand(cfg, "info", "snapshots")
if err == nil {
out = strings.ReplaceAll(out, "\r", "")
out = strings.Replace(out, "List of snapshots present on all disks:\n", "", 1)
out = strings.Replace(out, "There is no snapshot available.\n", "", 1)
}
return out, err
}
// -l lists all snapshots
args := []string{"snapshot", "-l"}
out, err := execImgCommand(cfg, args...)
if err == nil {
// remove the redundant heading, result is not machine-parseable
out = strings.Replace(out, "Snapshot list:\n", "", 1)
}
return out, err
}
func argValue(args []string, key string) (string, bool) {
if !strings.HasPrefix(key, "-") {
panic(fmt.Errorf("got unexpected key %q", key))
}
for i, s := range args {
if s == key {
if i == len(args)-1 {
return "", true
}
value := args[i+1]
if strings.HasPrefix(value, "-") {
return "", true
}
return value, true
}
}
return "", false
}
// appendArgsIfNoConflict can be used for: -cpu, -machine, -m, -boot ...
// appendArgsIfNoConflict cannot be used for: -drive, -cdrom, ...
func appendArgsIfNoConflict(args []string, k, v string) []string {
if !strings.HasPrefix(k, "-") {
panic(fmt.Errorf("got unexpected key %q", k))
}
switch k {
case "-drive", "-cdrom", "-chardev", "-blockdev", "-netdev", "-device":
panic(fmt.Errorf("appendArgsIfNoConflict() must not be called with k=%q", k))
}
if v == "" {
if _, ok := argValue(args, k); ok {
return args
}
return append(args, k)
}
if origV, ok := argValue(args, k); ok {
logrus.Warnf("Not adding QEMU argument %q %q, as it conflicts with %q %q", k, v, k, origV)
return args
}
return append(args, k, v)
}
type features struct {
// AccelHelp is the output of `qemu-system-x86_64 -accel help`
// e.g. "Accelerators supported in QEMU binary:\ntcg\nhax\nhvf\n"
// Not machine-readable, but checking strings.Contains() should be fine.
AccelHelp []byte
// NetdevHelp is the output of `qemu-system-x86_64 -netdev help`
// e.g. "Available netdev backend types:\nsocket\nhubport\ntap\nuser\nvde\nbridge\vhost-user\n"
// Not machine-readable, but checking strings.Contains() should be fine.
NetdevHelp []byte
// MachineHelp is the output of `qemu-system-x86_64 -machine help`
// e.g. "Supported machines are:\nakita...\n...virt-6.2...\n...virt-7.0...\n...\n"
// Not machine-readable, but checking strings.Contains() should be fine.
MachineHelp []byte
// CPUHelp is the output of `qemu-system-x86_64 -cpu help`
// e.g. "Available CPUs:\n...\nx86 base...\nx86 host...\n...\n"
// Not machine-readable, but checking strings.Contains() should be fine.
CPUHelp []byte
// VersionGEQ7 is true when the QEMU version seems v7.0.0 or later
VersionGEQ7 bool
}
func inspectFeatures(exe, machine string) (*features, error) {
var (
f features
stdout bytes.Buffer
stderr bytes.Buffer
)
cmd := exec.Command(exe, "-M", "none", "-accel", "help")
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("failed to run %v: stdout=%q, stderr=%q", cmd.Args, stdout.String(), stderr.String())
}
f.AccelHelp = stdout.Bytes()
// on older versions qemu will write "help" output to stderr
if len(f.AccelHelp) == 0 {
f.AccelHelp = stderr.Bytes()
}
cmd = exec.Command(exe, "-M", "none", "-netdev", "help")
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
logrus.Warnf("failed to run %v: stdout=%q, stderr=%q", cmd.Args, stdout.String(), stderr.String())
} else {
f.NetdevHelp = stdout.Bytes()
if len(f.NetdevHelp) == 0 {
f.NetdevHelp = stderr.Bytes()
}
}
cmd = exec.Command(exe, "-machine", "help")
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
logrus.Warnf("failed to run %v: stdout=%q, stderr=%q", cmd.Args, stdout.String(), stderr.String())
} else {
f.MachineHelp = stdout.Bytes()
if len(f.MachineHelp) == 0 {
f.MachineHelp = stderr.Bytes()
}
}
f.VersionGEQ7 = strings.Contains(string(f.MachineHelp), "-7.0")
// Avoid error: "No machine specified, and there is no default"
cmd = exec.Command(exe, "-cpu", "help", "-machine", machine)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
logrus.Warnf("failed to run %v: stdout=%q, stderr=%q", cmd.Args, stdout.String(), stderr.String())
} else {
f.CPUHelp = stdout.Bytes()
if len(f.CPUHelp) == 0 {
f.CPUHelp = stderr.Bytes()
}
}
return &f, nil
}
// showDarwinARM64HVFQEMU620Warning shows a warning on M1 macOS when QEMU is older than 6.2.0_1.
//
// See:
// - https://gitlab.com/qemu-project/qemu/-/issues/899
// - https://github.com/Homebrew/homebrew-core/pull/96743
// - https://github.com/lima-vm/lima/issues/712
func showDarwinARM64HVFQEMU620Warning(exe, accel string, features *features) {
if runtime.GOOS != "darwin" {
return
}
if runtime.GOARCH != "arm64" {
return
}
if accel != "hvf" {
return
}
if features.VersionGEQ7 {
return
}
if exeFull, err := exec.LookPath(exe); err == nil {
if exeResolved, err2 := filepath.EvalSymlinks(exeFull); err2 == nil {
if strings.Contains(exeResolved, "Cellar/qemu/6.2.0_") {
// Homebrew's QEMU 6.2.0_1 or later
return
}
}
}
w := "This version of QEMU might not be able to boot recent Linux guests on M1 macOS hosts."
if _, err := exec.LookPath("brew"); err == nil {
w += "Run `brew upgrade` and make sure your QEMU version is 6.2.0_1 or later."
} else {
w += `Reinstall QEMU with the following commits (included in QEMU 7.0.0):
- https://github.com/qemu/qemu/commit/ad99f64f "hvf: arm: Use macros for sysreg shift/masking"
- https://github.com/qemu/qemu/commit/7f6c295c "hvf: arm: Handle unknown ID registers as RES0"
`
w += "See https://github.com/Homebrew/homebrew-core/pull/96743 for the further information."
}
logrus.Warn(w)
}
// adjustMemBytesDarwinARM64HVF adjusts the memory to be <= 3 GiB, only when the following conditions are met:
//
// - Host OS < macOS 12.4
// - Host Arch == arm64
// - Accel == hvf
// - QEMU >= 7.0
//
// This adjustment is required for avoiding host kernel panic. The issue was fixed in macOS 12.4 Beta 1.
// See https://github.com/lima-vm/lima/issues/795 https://gitlab.com/qemu-project/qemu/-/issues/903#note_911000975
func adjustMemBytesDarwinARM64HVF(memBytes int64, accel string, features *features) int64 {
const safeSize = 3 * 1024 * 1024 * 1024 // 3 GiB
if memBytes <= safeSize {
return memBytes
}
if runtime.GOOS != "darwin" {
return memBytes
}
if runtime.GOARCH != "arm64" {
return memBytes
}
if accel != "hvf" {
return memBytes
}
if !features.VersionGEQ7 {
return memBytes
}
macOSProductVersion, err := osutil.ProductVersion()
if err != nil {
logrus.Warn(err)
return memBytes
}
if !macOSProductVersion.LessThan(*semver.New("12.4.0")) {
return memBytes
}
logrus.Warnf("Reducing the guest memory from %s to %s, to avoid host kernel panic on macOS <= 12.3 with QEMU >= 7.0; "+
"Please update macOS to 12.4 or later, or downgrade QEMU to 6.2; "+
"See https://github.com/lima-vm/lima/issues/795 for the further background.",
units.BytesSize(float64(memBytes)), units.BytesSize(float64(safeSize)))
memBytes = safeSize
return memBytes
}
// qemuMachine returns string to use for -machine
func qemuMachine(arch limayaml.Arch) string {
if arch == limayaml.X8664 {
return "q35"
}
return "virt"
}
func Cmdline(ctx context.Context, cfg Config) (exe string, args []string, err error) {
y := cfg.LimaYAML
exe, args, err = Exe(*y.Arch)
if err != nil {
return "", nil, err
}
features, err := inspectFeatures(exe, qemuMachine(*y.Arch))
if err != nil {
return "", nil, err
}
version, err := getQemuVersion(exe)
if err != nil {
logrus.WithError(err).Warning("Failed to detect QEMU version")
} else {
logrus.Debugf("QEMU version %s detected", version.String())
if version.LessThan(*semver.New(MinimumQemuVersion)) {
logrus.Fatalf("QEMU %v is too old, %v or later required", version, MinimumQemuVersion)
}
if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" && version.Equal(*semver.New("8.2.0")) {
logrus.Fatal("QEMU 8.2.0 is no longer supported on ARM Mac due to <https://gitlab.com/qemu-project/qemu/-/issues/1990>. " +
"Please upgrade QEMU to v8.2.1 (or downgrade to v8.1.x).")
}
}
// Architecture
accel := Accel(*y.Arch)
if !strings.Contains(string(features.AccelHelp), accel) {
return "", nil, fmt.Errorf("accelerator %q is not supported by %s", accel, exe)
}
showDarwinARM64HVFQEMU620Warning(exe, accel, features)
// Memory
memBytes, err := units.RAMInBytes(*y.Memory)
if err != nil {
return "", nil, err
}
memBytes = adjustMemBytesDarwinARM64HVF(memBytes, accel, features)
args = appendArgsIfNoConflict(args, "-m", strconv.Itoa(int(memBytes>>20)))
if *y.MountType == limayaml.VIRTIOFS {
args = appendArgsIfNoConflict(args, "-object",
fmt.Sprintf("memory-backend-file,id=virtiofs-shm,size=%s,mem-path=/dev/shm,share=on", strconv.Itoa(int(memBytes))))
args = appendArgsIfNoConflict(args, "-numa", "node,memdev=virtiofs-shm")
}
// CPU
cpu := y.CPUType[*y.Arch]
if runtime.GOOS == "darwin" && runtime.GOARCH == "amd64" {
switch {
case strings.HasPrefix(cpu, "host"), strings.HasPrefix(cpu, "max"):
if !strings.Contains(cpu, ",-pdpe1gb") {
logrus.Warnf("On Intel Mac, CPU type %q typically needs \",-pdpe1gb\" option (https://stackoverflow.com/a/72863744/5167443)", cpu)
}
}
}
if !strings.Contains(string(features.CPUHelp), strings.Split(cpu, ",")[0]) {
return "", nil, fmt.Errorf("cpu %q is not supported by %s", cpu, exe)
}
args = appendArgsIfNoConflict(args, "-cpu", cpu)
// Machine
switch *y.Arch {
case limayaml.X8664:
if strings.HasPrefix(cpu, "qemu64") && runtime.GOOS != "windows" {
// use q35 machine with vmware io port disabled.
args = appendArgsIfNoConflict(args, "-machine", "q35,vmport=off")
// use tcg accelerator with multi threading with 512MB translation block size
// https://qemu-project.gitlab.io/qemu/devel/multi-thread-tcg.html?highlight=tcg
// https://qemu-project.gitlab.io/qemu/system/invocation.html?highlight=tcg%20opts
// this will make sure each vCPU will be backed by 1 host user thread.
args = appendArgsIfNoConflict(args, "-accel", "tcg,thread=multi,tb-size=512")
// This will disable CPU S3/S4 state.
args = append(args, "-global", "ICH9-LPC.disable_s3=1")
args = append(args, "-global", "ICH9-LPC.disable_s4=1")
} else if runtime.GOOS == "windows" && accel == "whpx" {
// whpx: injection failed, MSI (0, 0) delivery: 0, dest_mode: 0, trigger mode: 0, vector: 0
args = appendArgsIfNoConflict(args, "-machine", "q35,accel="+accel+",kernel-irqchip=off")
} else {
args = appendArgsIfNoConflict(args, "-machine", "q35,accel="+accel)
}
case limayaml.AARCH64:
machine := "virt,accel=" + accel
// QEMU >= 7.0 requires highmem=off NOT to be set, otherwise fails with "Addressing limited to 32 bits, but memory exceeds it by 1073741824 bytes"
// QEMU < 7.0 requires highmem=off to be set, otherwise fails with "VCPU supports less PA bits (36) than requested by the memory map (40)"
// https://github.com/lima-vm/lima/issues/680
// https://github.com/lima-vm/lima/pull/24
// But when the memory size is <= 3 GiB, we can always set highmem=off.
if !features.VersionGEQ7 || memBytes <= 3*1024*1024*1024 {
machine += ",highmem=off"
}
args = appendArgsIfNoConflict(args, "-machine", machine)
case limayaml.RISCV64:
machine := "virt,accel=" + accel
args = appendArgsIfNoConflict(args, "-machine", machine)
case limayaml.ARMV7L:
machine := "virt,accel=" + accel
args = appendArgsIfNoConflict(args, "-machine", machine)
}
// SMP
args = appendArgsIfNoConflict(args, "-smp",
fmt.Sprintf("%d,sockets=1,cores=%d,threads=1", *y.CPUs, *y.CPUs))
// Firmware
legacyBIOS := *y.Firmware.LegacyBIOS
if legacyBIOS && *y.Arch != limayaml.X8664 && *y.Arch != limayaml.ARMV7L {
logrus.Warnf("field `firmware.legacyBIOS` is not supported for architecture %q, ignoring", *y.Arch)
legacyBIOS = false
}
if !legacyBIOS {
var firmware string
downloadedFirmware := filepath.Join(cfg.InstanceDir, filenames.QemuEfiCodeFD)
if _, stErr := os.Stat(downloadedFirmware); errors.Is(stErr, os.ErrNotExist) {
loop:
for _, f := range y.Firmware.Images {
switch f.VMType {
case "", limayaml.QEMU:
if f.Arch == *y.Arch {
if _, err = fileutils.DownloadFile(ctx, downloadedFirmware, f.File, true, "UEFI code "+f.Location, *y.Arch); err != nil {
logrus.WithError(err).Warnf("failed to download %q", f.Location)
continue loop
}
firmware = downloadedFirmware
logrus.Infof("Using firmware %q (downloaded from %q)", firmware, f.Location)
break loop
}
}
}
} else {
firmware = downloadedFirmware
logrus.Infof("Using existing firmware (%q)", firmware)
}
if firmware == "" && *y.Arch != limayaml.RISCV64 {
firmware, err = getFirmware(exe, *y.Arch)
if err != nil {
return "", nil, err
}
logrus.Infof("Using system firmware (%q)", firmware)
}
if firmware != "" {
args = append(args, "-drive", fmt.Sprintf("if=pflash,format=raw,readonly=on,file=%s", firmware))
}
}
// Disk
baseDisk := filepath.Join(cfg.InstanceDir, filenames.BaseDisk)
diffDisk := filepath.Join(cfg.InstanceDir, filenames.DiffDisk)
extraDisks := []string{}
if len(y.AdditionalDisks) > 0 {
for _, d := range y.AdditionalDisks {
diskName := d.Name
disk, err := store.InspectDisk(diskName)
if err != nil {
logrus.Errorf("could not load disk %q: %q", diskName, err)
return "", nil, err
}
if disk.Instance != "" {
if disk.InstanceDir != cfg.InstanceDir {
logrus.Errorf("could not attach disk %q, in use by instance %q", diskName, disk.Instance)
return "", nil, err
}
err = disk.Unlock()
if err != nil {
logrus.Errorf("could not unlock disk %q to reuse in the same instance %q", diskName, cfg.Name)
return "", nil, err
}
}
logrus.Infof("Mounting disk %q on %q", diskName, disk.MountPoint)
err = disk.Lock(cfg.InstanceDir)
if err != nil {
logrus.Errorf("could not lock disk %q: %q", diskName, err)
return "", nil, err
}
dataDisk := filepath.Join(disk.Dir, filenames.DataDisk)
extraDisks = append(extraDisks, dataDisk)
}
}
isBaseDiskCDROM, err := iso9660util.IsISO9660(baseDisk)
if err != nil {
return "", nil, err
}
if isBaseDiskCDROM {
args = appendArgsIfNoConflict(args, "-boot", "order=d,splash-time=0,menu=on")
args = append(args, "-drive", fmt.Sprintf("file=%s,format=raw,media=cdrom,readonly=on", baseDisk))
} else {
args = appendArgsIfNoConflict(args, "-boot", "order=c,splash-time=0,menu=on")
}
if diskSize, _ := units.RAMInBytes(*cfg.LimaYAML.Disk); diskSize > 0 {
args = append(args, "-drive", fmt.Sprintf("file=%s,if=virtio,discard=on", diffDisk))
} else if !isBaseDiskCDROM {
baseDiskInfo, err := imgutil.GetInfo(baseDisk)
if err != nil {
return "", nil, fmt.Errorf("failed to get the information of %q: %w", baseDisk, err)
}
if err = imgutil.AcceptableAsBasedisk(baseDiskInfo); err != nil {
return "", nil, fmt.Errorf("file %q is not acceptable as the base disk: %w", baseDisk, err)
}
if baseDiskInfo.Format == "" {
return "", nil, fmt.Errorf("failed to inspect the format of %q", baseDisk)
}
args = append(args, "-drive", fmt.Sprintf("file=%s,format=%s,if=virtio,discard=on", baseDisk, baseDiskInfo.Format))
}
for _, extraDisk := range extraDisks {
args = append(args, "-drive", fmt.Sprintf("file=%s,if=virtio,discard=on", extraDisk))
}
// cloud-init
args = append(args,
"-drive", "id=cdrom0,if=none,format=raw,readonly=on,file="+filepath.Join(cfg.InstanceDir, filenames.CIDataISO),
"-device", "virtio-scsi-pci,id=scsi0",
"-device", "scsi-cd,bus=scsi0.0,drive=cdrom0")
// Kernel
kernel := filepath.Join(cfg.InstanceDir, filenames.Kernel)
kernelCmdline := filepath.Join(cfg.InstanceDir, filenames.KernelCmdline)
initrd := filepath.Join(cfg.InstanceDir, filenames.Initrd)
if _, err := os.Stat(kernel); err == nil {
args = appendArgsIfNoConflict(args, "-kernel", kernel)
}
if b, err := os.ReadFile(kernelCmdline); err == nil {
args = appendArgsIfNoConflict(args, "-append", string(b))
}
if _, err := os.Stat(initrd); err == nil {
args = appendArgsIfNoConflict(args, "-initrd", initrd)
}
// Network
// Configure default usernetwork with limayaml.MACAddress(driver.Instance.Dir) for eth0 interface
firstUsernetIndex := limayaml.FirstUsernetIndex(y)
if firstUsernetIndex == -1 {
args = append(args, "-netdev", fmt.Sprintf("user,id=net0,net=%s,dhcpstart=%s,hostfwd=tcp:127.0.0.1:%d-:22",
networks.SlirpNetwork, networks.SlirpIPAddress, cfg.SSHLocalPort))
} else {
qemuSock, err := usernet.Sock(y.Networks[firstUsernetIndex].Lima, usernet.QEMUSock)
if err != nil {
return "", nil, err
}
args = append(args, "-netdev", fmt.Sprintf("socket,id=net0,fd={{ fd_connect %q }}", qemuSock))
}
args = append(args, "-device", "virtio-net-pci,netdev=net0,mac="+limayaml.MACAddress(cfg.InstanceDir))
for i, nw := range y.Networks {
if nw.Lima != "" {
nwCfg, err := networks.Config()
if err != nil {
return "", nil, err
}
// Handle usernet connections
isUsernet, err := nwCfg.Usernet(nw.Lima)
if err != nil {
return "", nil, err
}
if isUsernet {
if i == firstUsernetIndex {
continue
}
qemuSock, err := usernet.Sock(nw.Lima, usernet.QEMUSock)
if err != nil {
return "", nil, err
}
args = append(args, "-netdev", fmt.Sprintf("socket,id=net%d,fd={{ fd_connect %q }}", i+1, qemuSock))
args = append(args, "-device", fmt.Sprintf("virtio-net-pci,netdev=net%d,mac=%s", i+1, nw.MACAddress))
} else {
if runtime.GOOS != "darwin" {
return "", nil, fmt.Errorf("networks.yaml '%s' configuration is only supported on macOS right now", nw.Lima)
}
logrus.Debugf("Using socketVMNet (%q)", nwCfg.Paths.SocketVMNet)
sock, err := networks.Sock(nw.Lima)
if err != nil {
return "", nil, err
}
args = append(args, "-netdev", fmt.Sprintf("socket,id=net%d,fd={{ fd_connect %q }}", i+1, sock))
// TODO: should we also validate that the socket exists, or do we rely on the
// networks reconciler to throw an error when the network cannot start?
}
} else if nw.Socket != "" {
args = append(args, "-netdev", fmt.Sprintf("socket,id=net%d,fd={{ fd_connect %q }}", i+1, nw.Socket))
} else {
return "", nil, fmt.Errorf("invalid network spec %+v", nw)
}
args = append(args, "-device", fmt.Sprintf("virtio-net-pci,netdev=net%d,mac=%s", i+1, nw.MACAddress))
}
// virtio-rng-pci accelerates starting up the OS, according to https://wiki.gentoo.org/wiki/QEMU/Options
args = append(args, "-device", "virtio-rng-pci")
// Input
input := "mouse"
// Sound
if *y.Audio.Device != "" {
id := "default"
// audio device
audiodev := *y.Audio.Device
audiodev += fmt.Sprintf(",id=%s", id)
args = append(args, "-audiodev", audiodev)
// audio controller
args = append(args, "-device", "ich9-intel-hda")
// audio codec
args = append(args, "-device", fmt.Sprintf("hda-output,audiodev=%s", id))
}
// Graphics
if *y.Video.Display != "" {
display := *y.Video.Display
if display == "vnc" {
display += "=" + *y.Video.VNC.Display
display += ",password=on"
// use tablet to avoid double cursors
input = "tablet"
}
args = appendArgsIfNoConflict(args, "-display", display)
}
switch *y.Arch {
case limayaml.X8664, limayaml.RISCV64:
args = append(args, "-device", "virtio-vga")
args = append(args, "-device", "virtio-keyboard-pci")
args = append(args, "-device", "virtio-"+input+"-pci")
args = append(args, "-device", "qemu-xhci,id=usb-bus")
case limayaml.AARCH64, limayaml.ARMV7L:
if features.VersionGEQ7 {
args = append(args, "-device", "virtio-gpu")
args = append(args, "-device", "virtio-keyboard-pci")
args = append(args, "-device", "virtio-"+input+"-pci")
} else { // kernel panic with virtio and old versions of QEMU
args = append(args, "-vga", "none", "-device", "ramfb")
args = append(args, "-device", "usb-kbd,bus=usb-bus")
args = append(args, "-device", "usb-"+input+",bus=usb-bus")
}
args = append(args, "-device", "qemu-xhci,id=usb-bus")
}
// Parallel
args = append(args, "-parallel", "none")
// Serial (default)
// This is ttyS0 for Intel and RISC-V, ttyAMA0 for ARM.
serialSock := filepath.Join(cfg.InstanceDir, filenames.SerialSock)
if err := os.RemoveAll(serialSock); err != nil {
return "", nil, err
}
serialLog := filepath.Join(cfg.InstanceDir, filenames.SerialLog)
if err := os.RemoveAll(serialLog); err != nil {
return "", nil, err
}
const serialChardev = "char-serial"
args = append(args, "-chardev", fmt.Sprintf("socket,id=%s,path=%s,server=on,wait=off,logfile=%s", serialChardev, serialSock, serialLog))
args = append(args, "-serial", "chardev:"+serialChardev)
// Serial (PCI, ARM only)
// On ARM, the default serial is ttyAMA0, this PCI serial is ttyS0.
// https://gitlab.com/qemu-project/qemu/-/issues/1801#note_1494720586
switch *y.Arch {
case limayaml.AARCH64, limayaml.ARMV7L:
serialpSock := filepath.Join(cfg.InstanceDir, filenames.SerialPCISock)
if err := os.RemoveAll(serialpSock); err != nil {
return "", nil, err
}
serialpLog := filepath.Join(cfg.InstanceDir, filenames.SerialPCILog)
if err := os.RemoveAll(serialpLog); err != nil {
return "", nil, err
}
const serialpChardev = "char-serial-pci"
args = append(args, "-chardev", fmt.Sprintf("socket,id=%s,path=%s,server=on,wait=off,logfile=%s", serialpChardev, serialpSock, serialpLog))
args = append(args, "-device", "pci-serial,chardev="+serialpChardev)
}
// Serial (virtio)
serialvSock := filepath.Join(cfg.InstanceDir, filenames.SerialVirtioSock)
if err := os.RemoveAll(serialvSock); err != nil {
return "", nil, err
}
serialvLog := filepath.Join(cfg.InstanceDir, filenames.SerialVirtioLog)
if err := os.RemoveAll(serialvLog); err != nil {
return "", nil, err
}
const serialvChardev = "char-serial-virtio"
args = append(args, "-chardev", fmt.Sprintf("socket,id=%s,path=%s,server=on,wait=off,logfile=%s", serialvChardev, serialvSock, serialvLog))
// max_ports=1 is required for https://github.com/lima-vm/lima/issues/1689 https://github.com/lima-vm/lima/issues/1691
args = append(args, "-device", "virtio-serial-pci,id=virtio-serial0,max_ports=1")
args = append(args, "-device", fmt.Sprintf("virtconsole,chardev=%s,id=console0", serialvChardev))
// We also want to enable vsock here, but QEMU does not support vsock for macOS hosts
if *y.MountType == limayaml.NINEP || *y.MountType == limayaml.VIRTIOFS {
for i, f := range y.Mounts {
tag := fmt.Sprintf("mount%d", i)
location, err := localpathutil.Expand(f.Location)
if err != nil {
return "", nil, err
}
if err := os.MkdirAll(location, 0o755); err != nil {
return "", nil, err
}
switch *y.MountType {
case limayaml.NINEP:
options := "local"
options += fmt.Sprintf(",mount_tag=%s", tag)
options += fmt.Sprintf(",path=%s", location)
options += fmt.Sprintf(",security_model=%s", *f.NineP.SecurityModel)
if !*f.Writable {
options += ",readonly"
}
args = append(args, "-virtfs", options)
case limayaml.VIRTIOFS:
// Note that read-only mode is not supported on the QEMU/virtiofsd side yet:
// https://gitlab.com/virtio-fs/virtiofsd/-/issues/97
chardev := fmt.Sprintf("char-virtiofs-%d", i)
vhostSock := filepath.Join(cfg.InstanceDir, fmt.Sprintf(filenames.VhostSock, i))
args = append(args, "-chardev", fmt.Sprintf("socket,id=%s,path=%s", chardev, vhostSock))
options := "vhost-user-fs-pci"
options += fmt.Sprintf(",queue-size=%d", *f.Virtiofs.QueueSize)
options += fmt.Sprintf(",chardev=%s", chardev)
options += fmt.Sprintf(",tag=%s", tag)
args = append(args, "-device", options)
}
}
}
// QMP
qmpSock := filepath.Join(cfg.InstanceDir, filenames.QMPSock)
if err := os.RemoveAll(qmpSock); err != nil {
return "", nil, err
}
const qmpChardev = "char-qmp"
args = append(args, "-chardev", fmt.Sprintf("socket,id=%s,path=%s,server=on,wait=off", qmpChardev, qmpSock))
args = append(args, "-qmp", "chardev:"+qmpChardev)
// Guest agent via serialport
guestSock := filepath.Join(cfg.InstanceDir, filenames.GuestAgentSock)
args = append(args, "-chardev", fmt.Sprintf("socket,path=%s,server=on,wait=off,id=qga0", guestSock))
args = append(args, "-device", "virtio-serial")
args = append(args, "-device", "virtserialport,chardev=qga0,name="+filenames.VirtioPort)
// QEMU process
args = append(args, "-name", "lima-"+cfg.Name)
args = append(args, "-pidfile", filepath.Join(cfg.InstanceDir, filenames.PIDFile(*y.VMType)))
return exe, args, nil
}
func FindVirtiofsd(qemuExe string) (string, error) {
type vhostUserBackend struct {
BackendType string `json:"type"`
Binary string `json:"binary"`
}
currentUser, err := user.Current()
if err != nil {
return "", err
}
const relativePath = "share/qemu/vhost-user"
binDir := filepath.Dir(qemuExe) // "/usr/local/bin"
usrDir := filepath.Dir(binDir) // "/usr/local"
userLocalDir := filepath.Join(currentUser.HomeDir, ".local") // "$HOME/.local"
candidates := []string{
filepath.Join(userLocalDir, relativePath),
filepath.Join(usrDir, relativePath),
}
if usrDir != "/usr" {
candidates = append(candidates, filepath.Join("/usr", relativePath))
}
for _, vhostConfigsDir := range candidates {
logrus.Debugf("Checking vhost directory %s", vhostConfigsDir)
configEntries, err := os.ReadDir(vhostConfigsDir)
if err != nil {
logrus.Debugf("Failed to list vhost directory: %v", err)
continue
}
for _, configEntry := range configEntries {
logrus.Debugf("Checking vhost config %s", configEntry.Name())
if !strings.HasSuffix(configEntry.Name(), ".json") {
continue
}
var config vhostUserBackend
contents, err := os.ReadFile(filepath.Join(vhostConfigsDir, configEntry.Name()))
if err == nil {
err = json.Unmarshal(contents, &config)
}
if err != nil {
logrus.Warnf("Failed to load vhost-user config %s: %v", configEntry.Name(), err)
continue
}
logrus.Debugf("%v", config)
if config.BackendType != "fs" {
continue
}
// Only rust virtiofsd supports --version, so use that to make sure this isn't
// QEMU's virtiofsd, which requires running as root.
cmd := exec.Command(config.Binary, "--version")
output, err := cmd.CombinedOutput()
if err != nil {
logrus.Warnf("Failed to run %s --version (is this QEMU virtiofsd?): %s: %s",
config.Binary, err, output)
continue
}
return config.Binary, nil
}
}
return "", errors.New("Failed to locate virtiofsd")
}
func VirtiofsdCmdline(cfg Config, mountIndex int) ([]string, error) {
mount := cfg.LimaYAML.Mounts[mountIndex]
location, err := localpathutil.Expand(mount.Location)
if err != nil {
return nil, err
}
vhostSock := filepath.Join(cfg.InstanceDir, fmt.Sprintf(filenames.VhostSock, mountIndex))
// qemu_driver has to wait for the socket to appear, so make sure any old ones are removed here.
if err := os.Remove(vhostSock); err != nil && !errors.Is(err, fs.ErrNotExist) {
logrus.Warnf("Failed to remove old vhost socket: %v", err)
}