forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
165 lines (138 loc) · 3.89 KB
/
map.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
// +build linux
package ceph
import (
"encoding/json"
"fmt"
"os"
"strings"
"time"
"github.com/alibaba/pouch/pkg/exec"
"github.com/alibaba/pouch/volume/driver"
"github.com/alibaba/pouch/volume/types"
)
var (
bin = "/usr/bin/rbd-nbd"
poolName = "rbd"
objectSize = "4M"
imageFeature = "layering,exclusive-lock"
)
func getImageFeature(v *types.Volume) string {
if s, ok := v.Spec.Extra["image-feature"]; ok {
return s
}
return imageFeature
}
func getObjectSize(v *types.Volume) string {
if s, ok := v.Spec.Extra["object-size"]; ok {
return s
}
return objectSize
}
func getPool(v *types.Volume) string {
if p, ok := v.Spec.Extra["pool"]; ok {
return p
}
return poolName
}
func mkpool(poolName, volumeName string) string {
return fmt.Sprintf("%s/%s", poolName, volumeName)
}
func rbdMap(ctx driver.Context, v *types.Volume, address, keyring string) (string, error) {
poolName := getPool(v)
name := v.Name
args := []string{"map", mkpool(poolName, name), "--mon_host", address}
exit, _, stderr, err := exec.RunWithRetry(3, 100*time.Microsecond, defaultTimeout, bin, args...)
if err != nil || exit != 0 {
err = fmt.Errorf("could not map %q: %d (%v) (%v)", name, exit, err, stderr)
ctx.Log.Errorf("Ceph %s", err.Error())
return "", err
}
var device string
rbdmap, err := showMapped(ctx)
if err != nil {
return "", err
}
for _, rbd := range rbdmap.MapDevice {
if rbd.Image == name && rbd.Pool == poolName {
device = rbd.Device
break
}
}
if device == "" {
return "", fmt.Errorf("volume %s in pool %s not found in RBD showmapped output", name, poolName)
}
ctx.Log.Infof("Ceph mapped volume %q as %q", name, device)
return device, nil
}
func rbdUnmap(ctx driver.Context, v *types.Volume) error {
err := exec.Retry(10, 100*time.Millisecond, func() error {
rbdmap, err := showMapped(ctx)
if err != nil {
return err
}
err = doUnmap(ctx, v, rbdmap)
if err != nil {
return err
}
return nil
})
if err != nil {
return fmt.Errorf("could not unmap volume %q after 10 retries", v.Name)
}
return nil
}
func showMapped(ctx driver.Context) (RBDMap, error) {
rbdmap := RBDMap{}
exit, stdout, stderr, err := exec.RunWithRetry(3, 100*time.Millisecond, defaultTimeout,
bin, "--dump-json", "list-mapped")
if err != nil || exit != 0 {
ctx.Log.Warnf("Ceph could not show mapped. Retrying: err:%v exit: %v, stderr: %v, stdout:%v",
err, exit, stderr, stdout)
} else if stdout != "" {
err = json.Unmarshal([]byte(stdout), &rbdmap)
if err != nil {
ctx.Log.Errorf("Ceph could not parse rbd list-mapped output: %s", stdout)
}
}
return rbdmap, err
}
func doUnmap(ctx driver.Context, v *types.Volume, rbdmap RBDMap) error {
poolName := getPool(v)
name := v.Name
for _, rbd := range rbdmap.MapDevice {
if rbd.Image == name && rbd.Pool == poolName {
ctx.Log.Debugf("Ceph unmapping volume %s/%s at device %q", poolName, name, strings.TrimSpace(rbd.Device))
// Check device is exist or not.
if _, err := os.Stat(rbd.Device); err != nil {
ctx.Log.Errorf("Ceph trying to unmap device %q for %s/%s that does not exist, continuing",
poolName, name, rbd.Device)
continue
}
// Unmap device.
exit, _, stderr, err := exec.Run(defaultTimeout, bin, "unmap", rbd.Device)
if err != nil {
ctx.Log.Errorf("Ceph could not unmap volume %q (device %q): %d (%v) (%s)",
name, rbd.Device, exit, err, stderr)
return err
} else if exit != 0 {
err = fmt.Errorf("Ceph could not unmap volume %q (device %q): %d (%s)",
name, rbd.Device, exit, stderr)
ctx.Log.Error(err)
return err
}
// Check have unmapped or not.
rbdmap2, err := showMapped(ctx)
if err != nil {
return err
}
for _, rbd2 := range rbdmap2.MapDevice {
if rbd.Image == rbd2.Image && rbd.Pool == rbd2.Pool {
return fmt.Errorf("could not unmap volume %q, device: %q is exist",
name, rbd.Image)
}
}
break
}
}
return nil
}