This repository has been archived by the owner on Feb 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 196
/
dm.go
315 lines (287 loc) · 10.2 KB
/
dm.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// +build linux
package devicemapper
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"strings"
"syscall"
"github.com/golang/glog"
"github.com/hyperhq/hyperd/storage"
"path/filepath"
)
type jsonMetadata struct {
Device_id int `json:"device_id"`
Size int `json:"size"`
Transaction_id int `json:"transaction_id"`
Initialized bool `json:"initialized"`
}
// For device mapper, we do not need to mount the container to sharedDir.
// All of we need to provide the block device name of container.
func MountContainerToSharedDir(containerId, sharedDir, devPrefix string) (string, error) {
devFullName := fmt.Sprintf("/dev/mapper/%s-%s", devPrefix, containerId)
return devFullName, nil
}
func CreateNewDevice(containerId, devPrefix, rootPath string) error {
//TODO use container.Mount or something Other
var metadataPath = fmt.Sprintf("%s/metadata/", rootPath)
// Get device id from the metadata file
idMetadataFile := path.Join(metadataPath, containerId)
if _, err := os.Stat(idMetadataFile); err != nil && os.IsNotExist(err) {
return err
}
jsonData, err := ioutil.ReadFile(idMetadataFile)
if err != nil {
return err
}
var dat jsonMetadata
if err := json.Unmarshal(jsonData, &dat); err != nil {
return err
}
deviceId := dat.Device_id
deviceSize := dat.Size
// Activate the device for that device ID
devName := fmt.Sprintf("%s-%s", devPrefix, containerId)
if _, err := os.Stat(filepath.Join("/dev/mapper", devName)); err == nil {
glog.V(1).Infof("already exist dev %s", devName)
return nil
}
poolName := fmt.Sprintf("/dev/mapper/%s-pool", devPrefix)
createDeviceCmd := fmt.Sprintf("dmsetup create %s --table \"0 %d thin %s %d\"", devName, deviceSize/512, poolName, deviceId)
createDeviceCommand := exec.Command("/bin/sh", "-c", createDeviceCmd)
output, err := createDeviceCommand.CombinedOutput()
if err != nil {
glog.Error(string(output))
return err
}
return nil
}
func InjectFile(src io.Reader, containerId, devPrefix, target, basePath string, perm, uid, gid int) error {
if containerId == "" {
return fmt.Errorf("Please make sure the arguments are not NULL!\n")
}
permDir := perm | 0111
// Define the basic directory, need to get them via the 'info' command
var (
mntPath = fmt.Sprintf("%s/mnt/", basePath)
devName = fmt.Sprintf("%s-%s", devPrefix, containerId)
)
// Get the mount point for the container ID
idMountPath := path.Join(mntPath, containerId)
rootFs := path.Join(idMountPath, "rootfs")
targetFile := path.Join(rootFs, target)
// Whether we have the mounter directory
if _, err := os.Stat(idMountPath); err != nil && os.IsNotExist(err) {
if err := os.MkdirAll(idMountPath, os.FileMode(permDir)); err != nil {
return err
}
}
// Mount the block device to that mount point
var flags uintptr = syscall.MS_MGC_VAL
devFullName := fmt.Sprintf("/dev/mapper/%s", devName)
fstype, err := ProbeFsType(devFullName)
if err != nil {
return err
}
glog.V(3).Infof("The filesytem type is %s", fstype)
options := ""
if fstype == "xfs" {
// XFS needs nouuid or it can't mount filesystems with the same fs
options = joinMountOptions(options, "nouuid")
}
err = syscall.Mount(devFullName, idMountPath, fstype, flags, joinMountOptions("discard", options))
if err != nil && err == syscall.EINVAL {
err = syscall.Mount(devFullName, idMountPath, fstype, flags, options)
}
if err != nil {
return fmt.Errorf("Error mounting '%s' on '%s': %s", devFullName, idMountPath, err)
}
defer syscall.Unmount(idMountPath, syscall.MNT_DETACH)
return storage.WriteFile(src, targetFile, perm, uid, gid)
}
func ProbeFsType(device string) (string, error) {
// The daemon will only be run on Linux platform, so 'file -s' command
// will be used to test the type of filesystem which the device located.
cmd := fmt.Sprintf("file -sL %s", device)
command := exec.Command("/bin/sh", "-c", cmd)
fileCmdOutput, err := command.CombinedOutput()
if err != nil {
return string(fileCmdOutput), err
}
if strings.Contains(strings.ToLower(string(fileCmdOutput)), "ext") {
return "ext4", nil
}
if strings.Contains(strings.ToLower(string(fileCmdOutput)), "xfs") {
return "xfs", nil
}
return "", fmt.Errorf("Unknown filesystem type on %s, %s", device, string(fileCmdOutput))
}
func joinMountOptions(a, b string) string {
if a == "" {
return b
}
if b == "" {
return a
}
return a + "," + b
}
type DeviceMapper struct {
Datafile string
Metadatafile string
DataLoopFile string
MetadataLoopFile string
Size int
PoolName string
}
func CreatePool(dm *DeviceMapper) error {
if _, err := os.Stat("/dev/mapper/" + dm.PoolName); err == nil {
return nil
}
// Create data file and metadata file
parms := fmt.Sprintf("dd if=/dev/zero of=%s bs=1 seek=%d count=0", dm.Datafile, dm.Size)
if res, err := exec.Command("/bin/sh", "-c", parms).CombinedOutput(); err != nil {
glog.Error(string(res))
return fmt.Errorf(string(res))
}
parms = fmt.Sprintf("fallocate -l 128M %s", dm.Metadatafile)
if res, err := exec.Command("/bin/sh", "-c", parms).CombinedOutput(); err != nil {
glog.Error(string(res))
return fmt.Errorf(string(res))
}
if _, err := os.Stat(dm.DataLoopFile); err != nil {
l := len(dm.DataLoopFile)
parms = fmt.Sprintf("mknod -m 0660 %s b 7 %s", dm.DataLoopFile, dm.DataLoopFile[(l-1):l])
if res, err := exec.Command("/bin/sh", "-c", parms).CombinedOutput(); err != nil {
glog.Error(string(res))
return fmt.Errorf(string(res))
}
}
if _, err := os.Stat(dm.MetadataLoopFile); err != nil {
l := len(dm.MetadataLoopFile)
parms = fmt.Sprintf("mknod -m 0660 %s b 7 %s", dm.MetadataLoopFile, dm.MetadataLoopFile[(l-1):l])
if res, err := exec.Command("/bin/sh", "-c", parms).CombinedOutput(); err != nil {
glog.Error(string(res))
return fmt.Errorf(string(res))
}
}
// Setup the loop device for data and metadata files
parms = fmt.Sprintf("losetup %s %s", dm.DataLoopFile, dm.Datafile)
if res, err := exec.Command("/bin/sh", "-c", parms).CombinedOutput(); err != nil {
glog.Error(string(res))
return fmt.Errorf(string(res))
}
parms = fmt.Sprintf("losetup %s %s", dm.MetadataLoopFile, dm.Metadatafile)
if res, err := exec.Command("/bin/sh", "-c", parms).CombinedOutput(); err != nil {
glog.Error(string(res))
return fmt.Errorf(string(res))
}
// Make filesystem for data loop device and metadata loop device
parms = fmt.Sprintf("mkfs.ext4 %s", dm.DataLoopFile)
if res, err := exec.Command("/bin/sh", "-c", parms).CombinedOutput(); err != nil {
glog.Error(string(res))
return fmt.Errorf(string(res))
}
parms = fmt.Sprintf("mkfs.ext4 %s", dm.MetadataLoopFile)
if res, err := exec.Command("/bin/sh", "-c", parms).CombinedOutput(); err != nil {
glog.Error(string(res))
return fmt.Errorf(string(res))
}
parms = fmt.Sprintf("dd if=/dev/zero of=%s bs=4096 count=1", dm.MetadataLoopFile)
if res, err := exec.Command("/bin/sh", "-c", parms).CombinedOutput(); err != nil {
glog.Error(string(res))
return fmt.Errorf(string(res))
}
parms = fmt.Sprintf("dmsetup create %s --table '0 %d thin-pool %s %s 128 0'", dm.PoolName, dm.Size/512, dm.MetadataLoopFile, dm.DataLoopFile)
if res, err := exec.Command("/bin/sh", "-c", parms).CombinedOutput(); err != nil {
glog.Error(string(res))
return fmt.Errorf(string(res))
}
return nil
}
func CreateVolume(poolName, volName, dev_id, mkfs string, size int, restore bool) error {
glog.Infof("/dev/mapper/%s", volName)
if _, err := os.Stat("/dev/mapper/" + volName); err == nil {
return nil
}
if restore == false {
parms := fmt.Sprintf("dmsetup message /dev/mapper/%s 0 \"create_thin %s\"", poolName, dev_id)
if res, err := exec.Command("/bin/sh", "-c", parms).CombinedOutput(); err != nil {
glog.Error(string(res))
return fmt.Errorf(string(res))
}
}
parms := fmt.Sprintf("dmsetup create %s --table \"0 %d thin /dev/mapper/%s %s\"", volName, size/512, poolName, dev_id)
if res, err := exec.Command("/bin/sh", "-c", parms).CombinedOutput(); err != nil {
glog.Error(string(res))
return fmt.Errorf(string(res))
}
if restore == false {
parms = fmt.Sprintf("%s \"/dev/mapper/%s\"", mkfs, volName)
if res, err := exec.Command("/bin/sh", "-c", parms).CombinedOutput(); err != nil {
glog.Error(string(res))
return fmt.Errorf(string(res))
}
}
return nil
}
func UnmapVolume(deviceFullPath string) error {
f, err := os.Stat(deviceFullPath)
if err != nil && os.IsNotExist(err) {
glog.Warningf("device to be umounted (%s) does not exist", deviceFullPath)
return nil
} else if err != nil {
glog.Errorf("file %s could not be unmapped: %v", deviceFullPath, err)
return err
} else if (f.Mode() & os.ModeDevice) == 0 {
err := fmt.Errorf("only device could be unmapped, %s is %v", deviceFullPath, f.Mode())
glog.Error(err)
return err
}
args := fmt.Sprintf("dmsetup remove -f %s", deviceFullPath)
cmd := exec.Command("/bin/sh", "-c", args)
if output, err := cmd.CombinedOutput(); err != nil {
glog.Warningf("Cannot umount device %s: %s, %s", deviceFullPath, err.Error(), output)
// retry
cmd := exec.Command("/bin/sh", "-c", args)
if err := cmd.Run(); err != nil {
return err
}
}
return nil
}
func DeleteVolume(dm *DeviceMapper, dev_id int) error {
var parms string
// Delete the thin pool for test
parms = fmt.Sprintf("dmsetup message /dev/mapper/%s 0 \"delete %d\"", dm.PoolName, dev_id)
if res, err := exec.Command("/bin/sh", "-c", parms).CombinedOutput(); err != nil {
glog.Error(string(res))
return fmt.Errorf(string(res))
}
return nil
}
// Delete the pool which is created in 'Init' function
func DMCleanup(dm *DeviceMapper) error {
var parms string
// Delete the thin pool for test
parms = fmt.Sprintf("dmsetup remove \"/dev/mapper/%s\"", dm.PoolName)
if res, err := exec.Command("/bin/sh", "-c", parms).CombinedOutput(); err != nil {
glog.Error(string(res))
return fmt.Errorf(string(res))
}
// Delete the loop device
parms = fmt.Sprintf("losetup -d %s", dm.MetadataLoopFile)
if res, err := exec.Command("/bin/sh", "-c", parms).CombinedOutput(); err != nil {
glog.Error(string(res))
return fmt.Errorf(string(res))
}
parms = fmt.Sprintf("losetup -d %s", dm.DataLoopFile)
if res, err := exec.Command("/bin/sh", "-c", parms).CombinedOutput(); err != nil {
glog.Error(string(res))
return fmt.Errorf(string(res))
}
return nil
}