This repository has been archived by the owner on Feb 8, 2021. It is now read-only.
/
driver.go
297 lines (262 loc) · 8.08 KB
/
driver.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
package rawblock
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"github.com/docker/docker/daemon/graphdriver"
"github.com/docker/docker/pkg/idtools"
"github.com/docker/docker/pkg/parsers"
"github.com/docker/go-units"
"github.com/golang/glog"
"github.com/opencontainers/runc/libcontainer/label"
//"github.com/docker/docker/pkg/mount"
)
func init() {
graphdriver.Register("rawblock", Init)
}
// Driver holds information about the driver, home directory of the driver.
// Driver implements graphdriver.ProtoDriver. It uses only basic vfs operations.
// In order to support layering, the block is created via reflink with the the parent layer
// Driver must be wrapped in NaiveDiffDriver to be used as a graphdriver.Driver
type Driver struct {
home string
backingFs string // host filesystem of the storage
cow bool
blockFs string // filesystem inside the block
blockSize uint64 // block size in GB
uid int
gid int
sync.Mutex // Protects concurrent modification to active
active map[string]int
}
// test if the filesystem on @home supports reflink, return false also if unsure.
func testReflinkSupport(home string) bool {
file1 := filepath.Join(home, "test-reflink-support-src")
file2 := filepath.Join(home, "test-reflink-support-dest")
if _, err := exec.Command("truncate", fmt.Sprintf("--size=%d", 16*1024), file1).CombinedOutput(); err != nil {
return false
}
defer os.RemoveAll(file1)
defer os.RemoveAll(file2)
if _, err := exec.Command("cp", "-a", "--reflink=always", file1, file2).CombinedOutput(); err != nil {
return false
}
return true
}
// Init returns a new Raw Block driver.
// This sets the home directory for the driver and returns NaiveDiffDriver.
func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (graphdriver.Driver, error) {
backingFs := "<unknown>"
blockFs := "xfs" // TODO: make it configurable
cow := true
supported := "supported"
fsMagic, err := graphdriver.GetFSMagic(home)
if err != nil {
return nil, err
}
if fsName, ok := graphdriver.FsNames[fsMagic]; ok {
backingFs = fsName
}
// check if they are running over btrfs or xfs
switch fsMagic {
case graphdriver.FsMagicBtrfs: // support
case graphdriver.FsMagicXfs: // check support
if testReflinkSupport(home) {
break
}
fallthrough
default:
cow = false
supported = "NOT supported"
}
glog.Infof("RawBlock: copy-on-write is %s", supported)
rootUID, rootGID, err := idtools.GetRootUIDGID(uidMaps, gidMaps)
if err != nil {
return nil, err
}
if err := idtools.MkdirAllAs(home, 0700, rootUID, rootGID); err != nil {
return nil, err
}
var blockSize = uint64(10 * 1024 * 1024 * 1024)
for _, option := range options {
key, val, err := parsers.ParseKeyValueOpt(option)
if err != nil {
return nil, err
}
key = strings.ToLower(key)
switch key {
case "rawblock.basesize":
size, err := units.RAMInBytes(val)
if err != nil {
return nil, err
}
blockSize = uint64(size)
default:
return nil, fmt.Errorf("rawblock: Unknown option %s\n", key)
}
}
d := &Driver{
home: home,
backingFs: backingFs,
cow: cow,
blockFs: blockFs,
blockSize: blockSize,
uid: rootUID,
gid: rootGID,
active: map[string]int{},
}
return graphdriver.NewNaiveDiffDriver(d, uidMaps, gidMaps), nil
}
func (d *Driver) String() string {
return "rawblock"
}
// Status is used for implementing the graphdriver.ProtoDriver interface.
func (d *Driver) Status() [][2]string {
return [][2]string{
{"Backing Filesystem", d.backingFs},
{"Support Copy-On-Write", fmt.Sprintf("%v", d.cow)},
{"Block Filesystem", d.blockFs},
{"Block Size", fmt.Sprintf("%s", units.HumanSize(float64(d.blockSize)))},
}
}
// GetMetadata is used for implementing the graphdriver.ProtoDriver interface.
func (d *Driver) GetMetadata(id string) (map[string]string, error) {
return nil, nil
}
// Cleanup is used to implement graphdriver.ProtoDriver. There is no cleanup required for this driver.
func (d *Driver) Cleanup() error {
return nil
}
// Create prepares the filesystem for the rawblock driver and copies the block from the parent.
func (d *Driver) Create(id, parent, mountLabel string) error {
if err := idtools.MkdirAllAs(filepath.Dir(d.block(id)), 0700, d.uid, d.gid); err != nil {
return err
}
if parent == "" {
return CreateBlock(d.block(id), d.blockFs, mountLabel, d.blockSize)
}
if out, err := exec.Command("cp", "-a", "--reflink=auto", d.block(parent), d.block(id)).CombinedOutput(); err != nil {
return fmt.Errorf("Failed to reflink:%v:%s", err, string(out))
}
return nil
}
func (d *Driver) block(id string) string {
return filepath.Join(d.home, "blocks", id)
}
func (d *Driver) mntBase() string {
return filepath.Join(d.home, "mnt")
}
// Remove deletes the content from the directory for a given id.
func (d *Driver) Remove(id string) error {
if err := os.RemoveAll(d.block(id)); err != nil && !os.IsNotExist(err) {
return err
}
return nil
}
// Get returns the directory for the given id.
func (d *Driver) Get(id, mountLabel string) (string, error) {
d.Lock()
defer d.Unlock()
active := d.active[id]
if active == 0 {
if err := GetImage(filepath.Join(d.home, "blocks"), d.mntBase(), id, d.blockFs, mountLabel, d.uid, d.gid); err != nil {
return "", err
}
}
d.active[id] = active + 1
return filepath.Join(d.mntBase(), id, "rootfs"), nil
}
func (d *Driver) Put(id string) error {
d.Lock()
defer d.Unlock()
active := d.active[id]
if active > 1 {
d.active[id] = active - 1
return nil
}
if err := PutImage(d.mntBase(), id); err != nil {
return err
}
delete(d.active, id)
return nil
}
// Exists checks to see if the directory exists for the given id.
func (d *Driver) Exists(id string) bool {
_, err := os.Stat(d.block(id))
return err == nil
}
func CreateBlock(block, fstype, mountLabel string, size uint64) error {
//opts := []string{"level:s0"}
//if _, mountLabel, err := label.InitLabels(opts); err == nil {
// label.SetFileLabel(dir, mountLabel)
//}
if out, err := exec.Command("truncate", fmt.Sprintf("--size=%d", size), block).CombinedOutput(); err != nil {
return fmt.Errorf("Failed to create block:%v:%s", err, string(out))
}
switch fstype {
case "xfs":
if out, err := exec.Command("mkfs.xfs", "-f", block).CombinedOutput(); err != nil {
os.RemoveAll(block)
return fmt.Errorf("Failed to mkfs the block:%v:%s", err, string(out))
}
case "ext4":
if out, err := exec.Command("mkfs.ext4", "-F", block).CombinedOutput(); err != nil {
os.RemoveAll(block)
return fmt.Errorf("Failed to mkfs the block:%v:%s", err, string(out))
}
default:
os.RemoveAll(block)
return fmt.Errorf("Unsupported filesystem for the block: %s", fstype)
}
return nil
}
func joinMountOptions(a, b string) string {
if a == "" {
return b
}
if b == "" {
return a
}
return a + "," + b
}
func mount(block, mnt, fstype string, mountLabel string) error {
options := "loop"
if fstype == "xfs" {
// XFS needs nouuid or it can't mount filesystems with the same fs
options = joinMountOptions(options, "nouuid")
}
options = joinMountOptions(options, label.FormatMountLabel("", mountLabel))
if out, err := exec.Command("mount", "-t", fstype, "-o", options, block, mnt).CombinedOutput(); err != nil {
return fmt.Errorf("Failed to mount block:%v:%s", err, string(out))
}
return nil
}
func GetImage(blockBase, mntBase, id, fstype, mountLabel string, uid, gid int) error {
block := filepath.Join(blockBase, id)
mnt := filepath.Join(mntBase, id)
rootFs := filepath.Join(mntBase, id, "rootfs")
if err := idtools.MkdirAllAs(mnt, 0755, uid, gid); err != nil {
return err
}
if err := mount(block, mnt, fstype, mountLabel); err != nil {
os.RemoveAll(mnt)
return err
}
if err := idtools.MkdirAllAs(rootFs, 0755, uid, gid); err != nil && !os.IsExist(err) {
exec.Command("umount", "-d", mnt).CombinedOutput()
os.RemoveAll(mnt)
return err
}
return nil
}
func PutImage(mntBase, id string) error {
mnt := filepath.Join(mntBase, id)
if out, err := exec.Command("umount", "-d", mnt).CombinedOutput(); err != nil {
return fmt.Errorf("Failed to umount block:%v:%s", err, string(out))
}
os.RemoveAll(mnt)
return nil
}