This repository has been archived by the owner on Jun 16, 2023. It is now read-only.
forked from Mirantis/virtlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diskdriver.go
264 lines (234 loc) · 7.85 KB
/
diskdriver.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
/*
Copyright 2016-2017 Mirantis
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.
*/
package libvirttools
import (
"errors"
"fmt"
"sort"
libvirtxml "github.com/libvirt/libvirt-go-xml"
"github.com/Mirantis/virtlet/pkg/metadata/types"
)
const (
minBlockDevChar = 'a'
// https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/Virtualization/sect-Virtualization-Virtualization_limitations-KVM_limitations.html
// Actually there can be more than 21 block devices (including
// the root volume), but we want to be on the safe side here
maxVirtioBlockDevChar = 'u'
maxScsiBlockDevChar = 'z'
)
type diskDriver interface {
diskPath(domainDef *libvirtxml.Domain) (*diskPath, error)
target() *libvirtxml.DomainDiskTarget
address() *libvirtxml.DomainAddress
}
type diskDriverFactory func(n int) (diskDriver, error)
var diskDriverMap = map[types.DiskDriverName]diskDriverFactory{
types.DiskDriverVirtio: virtioBlkDriverFactory,
types.DiskDriverScsi: scsiDriverFactory,
}
type virtioBlkDriver struct {
n int
diskChar int
}
func virtioBlkDriverFactory(n int) (diskDriver, error) {
diskChar := minBlockDevChar + n
if diskChar > maxVirtioBlockDevChar {
return nil, errors.New("too many virtio block devices")
}
return &virtioBlkDriver{n, diskChar}, nil
}
func (d *virtioBlkDriver) diskPath(domainDef *libvirtxml.Domain) (*diskPath, error) {
disk, err := findDisk(domainDef, d.devName())
if err != nil {
return nil, err
}
devPath, sysfsPath, err := pciPath(domainDef, disk.Address)
if err != nil {
return nil, err
}
return &diskPath{devPath, sysfsPath + "/virtio*/block/"}, nil
}
func (d *virtioBlkDriver) devName() string {
return fmt.Sprintf("vd%c", d.diskChar)
}
func (d *virtioBlkDriver) target() *libvirtxml.DomainDiskTarget {
return &libvirtxml.DomainDiskTarget{
Dev: d.devName(),
Bus: "virtio",
}
}
func (d *virtioBlkDriver) address() *libvirtxml.DomainAddress {
// FIXME: we can let libvirt auto-assign the addresses.
// We'll have to add auto-assignment logic to fake_domain.go though
domain := uint(0)
// use bus1 to have more predictable addressing for virtio devs
bus := uint(1)
slot := uint(d.n + 1)
function := uint(0)
return &libvirtxml.DomainAddress{
PCI: &libvirtxml.DomainAddressPCI{
Domain: &domain,
Bus: &bus,
Slot: &slot,
Function: &function,
},
}
}
type scsiDriver struct {
n int
diskChar int
}
func scsiDriverFactory(n int) (diskDriver, error) {
diskChar := minBlockDevChar + n
if diskChar > maxScsiBlockDevChar {
return nil, errors.New("too many scsi block devices")
}
return &scsiDriver{n, diskChar}, nil
}
func (d *scsiDriver) diskPath(domainDef *libvirtxml.Domain) (*diskPath, error) {
scsiControllers := findControllers(domainDef, "scsi")
switch {
case len(scsiControllers) == 0:
return nil, errors.New("no scsi controllers found")
case len(scsiControllers) > 1:
// linux kernel reports wrong host number in sysfs for some reason
return nil, errors.New("more than one scsi controller is not supported")
}
disk, err := findDisk(domainDef, d.devName())
if err != nil {
return nil, err
}
if disk.Address.Drive == nil || disk.Address.Drive.Controller == nil || disk.Address.Drive.Bus == nil || disk.Address.Drive.Target == nil || disk.Address.Drive.Unit == nil {
return nil, fmt.Errorf("bad disk address for scsi disk %q", d.devName())
}
if *disk.Address.Drive.Controller != 0 {
return nil, fmt.Errorf("bad controller index for scsi disk %q", d.devName())
}
devPath, sysfsPath, err := pciPath(domainDef, scsiControllers[0].Address)
if err != nil {
return nil, err
}
return &diskPath{
fmt.Sprintf("%s-scsi-0:%d:%d:%d", devPath, *disk.Address.Drive.Bus, *disk.Address.Drive.Target, *disk.Address.Drive.Unit),
// host number are wrong in sysfs for some reason
fmt.Sprintf("%s/virtio*/host*/target*:%d:%d/*:%d:%d:%d/block/",
sysfsPath,
*disk.Address.Drive.Bus,
*disk.Address.Drive.Target,
*disk.Address.Drive.Bus,
*disk.Address.Drive.Target,
*disk.Address.Drive.Unit),
}, nil
}
func (d *scsiDriver) devName() string {
return fmt.Sprintf("sd%c", d.diskChar)
}
func (d *scsiDriver) target() *libvirtxml.DomainDiskTarget {
return &libvirtxml.DomainDiskTarget{
Dev: d.devName(),
Bus: "scsi",
}
}
func (d *scsiDriver) address() *libvirtxml.DomainAddress {
// FIXME: we can let libvirt auto-assign the addresses.
// We'll have to add auto-assignment logic to fake_domain.go though
controller := uint(0)
bus := uint(0)
target := uint(0)
unit := uint(d.n)
return &libvirtxml.DomainAddress{
Drive: &libvirtxml.DomainAddressDrive{
Controller: &controller,
Bus: &bus,
Target: &target,
Unit: &unit,
},
}
}
func getDiskDriverFactory(name types.DiskDriverName) (diskDriverFactory, error) {
if f, found := diskDriverMap[name]; found {
return f, nil
}
return nil, fmt.Errorf("bad disk driver name: %q", name)
}
func findDisk(domainDef *libvirtxml.Domain, dev string) (*libvirtxml.DomainDisk, error) {
if domainDef.Devices != nil {
for _, d := range domainDef.Devices.Disks {
if d.Target != nil && d.Target.Dev == dev {
return &d, nil
}
}
}
return nil, fmt.Errorf("disk %q not found in the domain", dev)
}
func findControllers(domainDef *libvirtxml.Domain, controllerType string) []libvirtxml.DomainController {
if domainDef.Devices == nil {
return nil
}
// make an empty slice instead of nil because the effects of
// calling sort.SliceStable() on nil slice are unspecified
r := []libvirtxml.DomainController{}
for _, c := range domainDef.Devices.Controllers {
if c.Type == controllerType {
r = append(r, c)
}
}
sort.SliceStable(r, func(i, j int) bool {
var a, b uint
if r[i].Index != nil {
a = *r[i].Index
}
if r[j].Index != nil {
b = *r[j].Index
}
return a < b
})
return r
}
func pciPath(domainDef *libvirtxml.Domain, address *libvirtxml.DomainAddress) (string, string, error) {
pciControllers := findControllers(domainDef, "pci")
devPath := "/dev/disk/by-path/"
sysfsPath := "/sys/devices"
var recurse func(*libvirtxml.DomainAddress, string, int) error
recurse = func(address *libvirtxml.DomainAddress, pathPrefix string, depth int) error {
if depth > 256 { // 256 is big enough here, we're not expecting that many hops
return fmt.Errorf("can't make path for device address %#v: loop detected", address)
}
if address == nil || address.PCI == nil || address.PCI.Domain == nil || address.PCI.Bus == nil || address.PCI.Slot == nil || address.PCI.Function == nil {
return fmt.Errorf("can't make path for device address %#v", address)
}
if *address.PCI.Bus > uint(len(pciControllers)) {
return fmt.Errorf("bad PCI bus number: %#v", address)
}
ctl := pciControllers[*address.PCI.Bus]
if ctl.Address != nil && ctl.Address.PCI != nil {
if err := recurse(ctl.Address, "pci", depth+1); err != nil {
return err
}
} else {
// pci-root is not mentioned in devPath, but is present in sysfsPath
sysfsPath += fmt.Sprintf("/pci%04x:%02x", *address.PCI.Domain, *address.PCI.Bus)
}
addressStr := fmt.Sprintf("%04x:%02x:%02x.%01x", *address.PCI.Domain, *address.PCI.Bus, *address.PCI.Slot, *address.PCI.Function)
if devPath[len(devPath)-1] != '/' {
devPath += "-"
}
devPath += pathPrefix + "-" + addressStr
sysfsPath += "/" + addressStr
return nil
}
if err := recurse(address, "virtio-pci", 0); err != nil {
return "", "", fmt.Errorf("pciPath for %#v: %v", address, err)
}
return devPath, sysfsPath, nil
}