-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
executable file
·97 lines (83 loc) · 2.63 KB
/
main.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
package main
import (
"os"
"os/exec"
"github.com/alexflint/go-arg"
"github.com/ashald/docker-volume-loopback/context"
"github.com/ashald/docker-volume-loopback/driver"
v "github.com/docker/go-plugins-helpers/volume"
)
type config struct {
Socket string `arg:"--socket,env:SOCKET,help:path to the plugin UNIX socket under /run/docker/plugins/"`
LogLevel int `arg:"--log-level,env:LOG_LEVEL,help:set log level - from 0 to 4 for Error/Warning/Info/Debug/Trace"`
LogFormat string `arg:"--log-format,env:LOG_FORMAT,help:set log format - json/text/nice"`
StateDir string `arg:"--state-dir,env:STATE_DIR,help:dir used to keep track of currently mounted volumes"`
DataDir string `arg:"--data-dir,env:DATA_DIR,help:dir used to store actual volume data"`
MountDir string `arg:"--mount-dir,env:MOUNT_DIR,help:dir used to create mount-points"`
DefaultSize string `arg:"--default-size,env:DEFAULT_SIZE,help:default size for volumes created"`
}
var (
args = &config{
Socket: "/run/docker/plugins/docker-volume-loopback.sock",
StateDir: "/run/docker-volume-loopback",
DataDir: "/var/lib/docker-volume-loopback",
MountDir: "/mnt",
DefaultSize: "1GiB",
LogLevel: 2,
LogFormat: context.FormatNice,
}
)
func main() {
arg.MustParse(args)
context.Init(args.LogLevel, args.LogFormat, os.Stdout)
ctx := context.New()
ctx.
Level(context.Info).
Field("args", args).
Message("initializing plugin")
_, errXfs := exec.LookPath("mkfs.xfs")
if errXfs != nil {
ctx.
Level(context.Warning).
Field("err", errXfs).
Message("mkfs.xfs is not available, please install 'xfsprogs' to be able to use xfs filesystem")
}
_, errExt4 := exec.LookPath("mkfs.ext4")
if errExt4 != nil {
ctx.
Level(context.Warning).
Field("err", errXfs).
Message("mkfs.ext4 is not available, please install 'e2fsprogs' to be able to use ext4 filesystem")
}
if errXfs != nil && errExt4 != nil {
ctx.
Level(context.Error).
Message("Neither of supported filesystems - ext4 or xfs - are available")
os.Exit(1)
}
driverInstance, err := driver.New(
ctx.Derived(),
driver.Config{
StateDir: args.StateDir,
DataDir: args.DataDir,
MountDir: args.MountDir,
DefaultSize: args.DefaultSize,
})
if err != nil {
ctx.
Level(context.Error).
Field("err", err).
Message("failed to initialize 'docker-volume-loopback' driver")
os.Exit(1)
}
handler := v.NewHandler(driverInstance)
err = handler.ServeUnix(args.Socket, 0)
if err != nil {
ctx.
Level(context.Error).
Field("socket", args.Socket).
Message("failed to serve volume plugin api over unix socket")
os.Exit(1)
}
return
}