-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (49 loc) · 1.83 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
package main
import (
"os/user"
"sort"
"strconv"
"strings"
"sync"
"github.com/alecthomas/kong"
"github.com/docker/go-plugins-helpers/volume"
"gitlab.com/tozd/go/cli"
"gitlab.com/tozd/go/errors"
"gitlab.com/tozd/go/zerolog"
)
var fileSystems = map[string][]string{ //nolint:gochecknoglobals
"ext4": {"mkfs.ext4", "-F"},
"xfs": {"mkfs.xfs", "-f"},
}
//nolint:lll
type Driver struct {
zerolog.LoggingConfig
Partitions []string `arg:"" help:"Partition(s) to use." name:"partition" required:"" type:"path"`
Dir string ` default:"/mnt" help:"Directory under which to mount partitions. Default: ${default}." placeholder:"DIR" short:"d" type:"path"`
Default string ` default:"ext4" enum:"${fileSystems}" help:"Default file system to format partitions as. Possible: ${fileSystems}. Default: ${default}." placeholder:"FS"`
// Map between volume names and activeVolume.
volumes map[string]activeVolume
// Map between volume names and active mount IDs.
mounts map[string][]string
// Lock.
mu sync.Mutex
}
func main() {
names := []string{}
for name := range fileSystems {
names = append(names, name)
}
sort.Strings(names)
var driver Driver
cli.Run(&driver, kong.Vars{
"fileSystems": strings.Join(names, ","),
}, func(ctx *kong.Context) errors.E {
driver.volumes = make(map[string]activeVolume)
driver.mounts = make(map[string][]string)
handler := volume.NewHandler(&driver)
u, _ := user.Lookup("root")
gid, _ := strconv.Atoi(u.Gid)
driver.Logger.Debug().Str("path", "/run/docker/plugins/mkfs.sock").Msg("running")
return errors.WithStack(handler.ServeUnix("mkfs", gid))
})
}