-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
usbredir.go
196 lines (165 loc) · 5.11 KB
/
usbredir.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
/*
* 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, 2021 Red Hat, Inc.
*
*/
package usbredir
import (
"fmt"
"io"
"net"
"os"
"os/exec"
"os/signal"
"time"
"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"
"kubevirt.io/client-go/kubecli"
"kubevirt.io/client-go/log"
"kubevirt.io/kubevirt/pkg/virtctl/templates"
)
const usbredirClient = "usbredirect"
func NewCommand(clientConfig clientcmd.ClientConfig) *cobra.Command {
cmd := &cobra.Command{
Use: "usbredir (vendor:product) (VMI)",
Short: "Redirect a usb device to a virtual machine instance.",
Example: usage(),
Args: templates.ExactArgs("usb", 2),
RunE: func(cmd *cobra.Command, args []string) error {
c := usbredirCommand{clientConfig: clientConfig}
return c.Run(cmd, args)
},
}
cmd.SetUsageTemplate(templates.UsageTemplate())
return cmd
}
type usbredirCommand struct {
clientConfig clientcmd.ClientConfig
}
func (usbredirCmd *usbredirCommand) Run(command *cobra.Command, args []string) error {
if _, err := exec.LookPath(usbredirClient); err != nil {
return fmt.Errorf("Error on finding %s in $PATH: %s", usbredirClient, err.Error())
}
namespace, _, err := usbredirCmd.clientConfig.Namespace()
if err != nil {
return err
}
virtCli, err := kubecli.GetKubevirtClientFromClientConfig(usbredirCmd.clientConfig)
if err != nil {
return err
}
vmiArg := args[1]
usbdeviceArg := args[0]
// Get connection to the websocket for usbredir subresource
usbredirVMI, err := virtCli.VirtualMachineInstance(namespace).USBRedir(vmiArg)
if err != nil {
return fmt.Errorf("Can't access VMI %s: %s", vmiArg, err.Error())
}
// We will connect the local USB device using a usbredir TCP client to the
// remote VM using the websocket.
pipeInReader, pipeInWriter := io.Pipe()
pipeOutReader, pipeOutWriter := io.Pipe()
// Configure in/out and start stream with websocket
k8ResChan := make(chan error)
go func() {
defer pipeOutWriter.Close()
k8ResChan <- usbredirVMI.Stream(kubecli.StreamOptions{
In: pipeInReader,
Out: pipeOutWriter,
})
}()
lnAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("localhost:0"))
if err != nil {
return fmt.Errorf("Can't resolve the address: %s", err.Error())
}
// The local tcp server is used to proxy between remote websocket and local USB
ln, err := net.ListenTCP("tcp", lnAddr)
if err != nil {
return fmt.Errorf("Can't listen on unix socket: %s", err.Error())
}
// forward data to/from websocket after usbredir client connects.
usbredirDoneChan := make(chan struct{}, 1)
streamResChan := make(chan error)
streamStop := make(chan error)
go func() {
defer pipeInWriter.Close()
start := time.Now()
usbredirConn, err := ln.Accept()
if err != nil {
log.Log.V(2).Infof("Failed to accept connection: %s", err.Error())
streamResChan <- err
return
}
defer usbredirConn.Close()
log.Log.V(2).Infof("Connected to %s at %v", usbredirClient, time.Now().Sub(start))
// write to local usbredir from pipeOutReader
go func() {
_, err := io.Copy(usbredirConn, pipeOutReader)
streamStop <- err
}()
// read from local usbredir towards pipeInWriter
go func() {
_, err := io.Copy(pipeInWriter, usbredirConn)
streamStop <- err
}()
// Wait for local usbredir to complete
<-usbredirDoneChan
streamResChan <- err
}()
port := ln.Addr().(*net.TCPAddr).Port
// execute local usbredir binary
usbredirExecResChan := make(chan error)
go func() {
defer close(usbredirDoneChan)
bin := usbredirClient
args := []string{}
port_arg := fmt.Sprintf("localhost:%v", port)
args = append(args, "--device", usbdeviceArg, "--to", port_arg)
log.Log.Infof("port_arg: '%s'", port_arg)
log.Log.Infof("args: '%v'", args)
log.Log.Infof("Executing commandline: '%s %v'", bin, args)
command := exec.Command(bin, args...)
output, err := command.CombinedOutput()
if err != nil {
log.Log.Errorf("Failed to execut %v due %v, output: %v", bin, err, string(output))
} else {
log.Log.V(2).Infof("%v output: %v", bin, string(output))
}
usbredirExecResChan <- err
}()
sigStopChan := make(chan struct{}, 1)
go func() {
defer close(sigStopChan)
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
<-interrupt
}()
select {
case <-sigStopChan:
case err = <-streamStop:
case err = <-k8ResChan:
case err = <-usbredirExecResChan:
case err = <-streamResChan:
}
if err != nil {
return fmt.Errorf("Error encountered: %s", err.Error())
}
return nil
}
func usage() string {
return ` # Redirect a local USB device to the remote VMI:\n"
{{ProgramName}} usbredir vendor:product testvmi`
}