forked from hyperhq/runv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
disk.go
202 lines (169 loc) · 4 KB
/
disk.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
package hypervisor
import (
"sync"
"strconv"
"strings"
"github.com/hyperhq/runv/api"
)
type DiskDescriptor struct {
Name string
Filename string
Format string
Fstype string
DeviceName string
ScsiId int
ScsiAddr string
DockerVolume bool
Options map[string]string
}
func (d *DiskDescriptor) IsDir() bool {
return d.Format == "vfs"
}
func (d *DiskDescriptor) IsNas() bool {
return d.Format == "nas"
}
type DiskContext struct {
*DiskDescriptor
isRootVol bool
sandbox *VmContext
ready bool
observers map[string]*sync.WaitGroup
lock *sync.RWMutex
}
func NewDiskContext(ctx *VmContext, vol *api.VolumeDescription) *DiskContext {
dc := &DiskContext{
DiskDescriptor: &DiskDescriptor{
Name: vol.Name,
Filename: vol.Source,
Format: vol.Format,
Fstype: vol.Fstype,
DockerVolume: vol.DockerVolume,
},
sandbox: ctx,
observers: make(map[string]*sync.WaitGroup),
lock: &sync.RWMutex{},
}
if vol.IsDir() {
dc.ready = true
} else if vol.IsNas() {
dc.DeviceName = vol.Source
dc.ready = true
} else if vol.Format == "rbd" {
dc.Options = map[string]string{
"user": vol.Options.User,
"keyring": vol.Options.Keyring,
"monitors": strings.Join(vol.Options.Monitors, ";"),
"bytespersec": strconv.Itoa(int(vol.Options.BytesPerSec)),
"iops": strconv.Itoa(int(vol.Options.Iops)),
}
}
return dc
}
func (dc *DiskContext) insert(result chan api.Result) {
if result == nil {
result = make(chan api.Result, 4)
}
if dc.isReady() {
result <- api.NewResultBase(dc.Name, true, "already inserted")
return
}
dc.ScsiId = dc.sandbox.nextScsiId()
usage := "volume"
if dc.isRootVol {
usage = "image"
}
go func() {
r := make(chan VmEvent, 4)
dc.sandbox.DCtx.AddDisk(dc.sandbox, usage, dc.DiskDescriptor, r)
ev, ok := <-r
if !ok {
dc.failed()
result <- api.NewResultBase(dc.Name, false, "disk insert session broken")
return
}
de, ok := ev.(*BlockdevInsertedEvent)
if !ok {
dc.failed()
result <- api.NewResultBase(dc.Name, false, "disk insert failed")
return
}
dc.DeviceName = de.DeviceName
dc.ScsiAddr = de.ScsiAddr
result <- api.NewResultBase(dc.Name, true, "")
dc.inserted()
}()
}
func (dc *DiskContext) remove(result chan<- api.Result) {
if result == nil {
result = make(chan api.Result, 4)
}
if dc.IsDir() || dc.IsNas() {
result <- api.NewResultBase(dc.Name, true, "no need to unplug")
return
}
go func() {
r := make(chan VmEvent, 4)
dc.sandbox.DCtx.RemoveDisk(dc.sandbox, dc.DiskDescriptor, &VolumeUnmounted{Name: dc.Name, Success: true}, r)
ev, ok := <-r
if !ok {
dc.failed()
result <- api.NewResultBase(dc.Name, false, "disk remove session broken")
return
}
_, ok = ev.(*VolumeUnmounted)
if !ok {
dc.failed()
result <- api.NewResultBase(dc.Name, false, "disk remove failed")
return
}
result <- api.NewResultBase(dc.Name, true, "")
}()
}
func (dc *DiskContext) wait(id string, wg *sync.WaitGroup) {
dc.lock.Lock()
defer dc.lock.Unlock()
if _, ok := dc.observers[id]; ok {
return
}
dc.observers[id] = wg
if dc.ready {
return
}
wg.Add(1)
}
func (dc *DiskContext) unwait(id string) {
dc.lock.Lock()
defer dc.lock.Unlock()
if _, ok := dc.observers[id]; ok {
dc.sandbox.Log(DEBUG, "container %s unwait disk %s", id, dc.Name)
delete(dc.observers, id)
}
}
func (dc *DiskContext) inserted() {
dc.lock.Lock()
defer dc.lock.Unlock()
dc.ready = true
for c, wg := range dc.observers {
dc.sandbox.Log(INFO, "disk %s for container %s inserted", dc.Name, c)
wg.Done()
}
}
func (dc *DiskContext) failed() {
dc.lock.Lock()
defer dc.lock.Unlock()
dc.ready = false
for c, wg := range dc.observers {
dc.sandbox.Log(INFO, "disk %s for container %s inserted", dc.Name, c)
wg.Done()
}
}
func (dc *DiskContext) isReady() bool {
dc.lock.RLock()
defer dc.lock.RUnlock()
return dc.ready
}
func (dc *DiskContext) containers() int {
dc.lock.RLock()
defer dc.lock.RUnlock()
return len(dc.observers)
}