forked from jetkvm/kvm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathblock_device.go
165 lines (143 loc) · 3.52 KB
/
block_device.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
package kvm
import (
"context"
"errors"
"log"
"net"
"os"
"time"
"github.com/pojntfx/go-nbd/pkg/client"
"github.com/pojntfx/go-nbd/pkg/server"
)
type remoteImageBackend struct {
}
func (r remoteImageBackend) ReadAt(p []byte, off int64) (n int, err error) {
virtualMediaStateMutex.RLock()
logger.Debugf("currentVirtualMediaState is %v", currentVirtualMediaState)
logger.Debugf("read size: %d, off: %d", len(p), off)
if currentVirtualMediaState == nil {
return 0, errors.New("image not mounted")
}
source := currentVirtualMediaState.Source
mountedImageSize := currentVirtualMediaState.Size
virtualMediaStateMutex.RUnlock()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
readLen := int64(len(p))
if off+readLen > mountedImageSize {
readLen = mountedImageSize - off
}
var data []byte
if source == WebRTC {
data, err = webRTCDiskReader.Read(ctx, off, readLen)
if err != nil {
return 0, err
}
n = copy(p, data)
return n, nil
} else if source == HTTP {
return httpRangeReader.ReadAt(p, off)
} else {
return 0, errors.New("unknown image source")
}
}
func (r remoteImageBackend) WriteAt(p []byte, off int64) (n int, err error) {
return 0, errors.New("not supported")
}
func (r remoteImageBackend) Size() (int64, error) {
virtualMediaStateMutex.Lock()
defer virtualMediaStateMutex.Unlock()
if currentVirtualMediaState == nil {
return 0, errors.New("no virtual media state")
}
return currentVirtualMediaState.Size, nil
}
func (r remoteImageBackend) Sync() error {
return nil
}
const nbdSocketPath = "/var/run/nbd.socket"
const nbdDevicePath = "/dev/nbd0"
type NBDDevice struct {
listener net.Listener
serverConn net.Conn
clientConn net.Conn
dev *os.File
}
func NewNBDDevice() *NBDDevice {
return &NBDDevice{}
}
func (d *NBDDevice) Start() error {
var err error
if _, err := os.Stat(nbdDevicePath); os.IsNotExist(err) {
return errors.New("NBD device does not exist")
}
d.dev, err = os.Open(nbdDevicePath)
if err != nil {
return err
}
// Remove the socket file if it already exists
if _, err := os.Stat(nbdSocketPath); err == nil {
if err := os.Remove(nbdSocketPath); err != nil {
log.Fatalf("Failed to remove existing socket file %s: %v", nbdSocketPath, err)
}
}
d.listener, err = net.Listen("unix", nbdSocketPath)
if err != nil {
return err
}
d.clientConn, err = net.Dial("unix", nbdSocketPath)
if err != nil {
return err
}
d.serverConn, err = d.listener.Accept()
if err != nil {
return err
}
go d.runServerConn()
go d.runClientConn()
return nil
}
func (d *NBDDevice) runServerConn() {
err := server.Handle(
d.serverConn,
[]*server.Export{
{
Name: "jetkvm",
Description: "",
Backend: &remoteImageBackend{},
},
},
&server.Options{
ReadOnly: true,
MinimumBlockSize: uint32(1024),
PreferredBlockSize: uint32(4 * 1024),
MaximumBlockSize: uint32(16 * 1024),
SupportsMultiConn: false,
})
log.Println("nbd server exited:", err)
}
func (d *NBDDevice) runClientConn() {
err := client.Connect(d.clientConn, d.dev, &client.Options{
ExportName: "jetkvm",
BlockSize: uint32(4 * 1024),
})
log.Println("nbd client exited:", err)
}
func (d *NBDDevice) Close() {
if d.dev != nil {
err := client.Disconnect(d.dev)
if err != nil {
log.Println("error disconnecting nbd client:", err)
}
_ = d.dev.Close()
}
if d.listener != nil {
_ = d.listener.Close()
}
if d.clientConn != nil {
_ = d.clientConn.Close()
}
if d.serverConn != nil {
_ = d.serverConn.Close()
}
}