@@ -3,6 +3,7 @@ package main
3
3
import (
4
4
_ "embed"
5
5
"errors"
6
+ "fmt"
6
7
"os"
7
8
"os/exec"
8
9
"path/filepath"
@@ -19,11 +20,22 @@ func newInstallSystemdCommand() *cobra.Command {
19
20
Short : "install a systemd unit (user)" ,
20
21
RunE : installSystemdAction ,
21
22
}
23
+ installSystemdCommand .Flags ().Int ("tcp-port" , 0 , "use tcp server on specified port" )
24
+ installSystemdCommand .Flags ().Int ("vsock-port" , 0 , "use vsock server on specified port" )
25
+ installSystemdCommand .MarkFlagsMutuallyExclusive ("tcp-port" , "vsock-port" )
22
26
return installSystemdCommand
23
27
}
24
28
25
- func installSystemdAction (_ * cobra.Command , _ []string ) error {
26
- unit , err := generateSystemdUnit ()
29
+ func installSystemdAction (cmd * cobra.Command , _ []string ) error {
30
+ tcp , err := cmd .Flags ().GetInt ("tcp-port" )
31
+ if err != nil {
32
+ return err
33
+ }
34
+ vsock , err := cmd .Flags ().GetInt ("vsock-port" )
35
+ if err != nil {
36
+ return err
37
+ }
38
+ unit , err := generateSystemdUnit (tcp , vsock )
27
39
if err != nil {
28
40
return err
29
41
}
@@ -40,11 +52,11 @@ func installSystemdAction(_ *cobra.Command, _ []string) error {
40
52
return err
41
53
}
42
54
logrus .Infof ("Written file %q" , unitPath )
43
- argss := [][]string {
55
+ args := [][]string {
44
56
{"daemon-reload" },
45
57
{"enable" , "--now" , "lima-guestagent.service" },
46
58
}
47
- for _ , args := range argss {
59
+ for _ , args := range args {
48
60
cmd := exec .Command ("systemctl" , append ([]string {"--system" }, args ... )... )
49
61
cmd .Stdout = os .Stdout
50
62
cmd .Stderr = os .Stderr
@@ -60,13 +72,23 @@ func installSystemdAction(_ *cobra.Command, _ []string) error {
60
72
//go:embed lima-guestagent.TEMPLATE.service
61
73
var systemdUnitTemplate string
62
74
63
- func generateSystemdUnit () ([]byte , error ) {
75
+ func generateSystemdUnit (tcpPort , vsockPort int ) ([]byte , error ) {
64
76
selfExeAbs , err := os .Executable ()
65
77
if err != nil {
66
78
return nil , err
67
79
}
80
+
81
+ var args []string
82
+ if tcpPort != 0 {
83
+ args = append (args , fmt .Sprintf ("--tcp-port %d" , tcpPort ))
84
+ }
85
+ if vsockPort != 0 {
86
+ args = append (args , fmt .Sprintf ("--vsock-port %d" , vsockPort ))
87
+ }
88
+
68
89
m := map [string ]string {
69
90
"Binary" : selfExeAbs ,
91
+ "Args" : strings .Join (args , " " ),
70
92
}
71
93
return textutil .ExecuteTemplate (systemdUnitTemplate , m )
72
94
}
0 commit comments