From be8f94b4eb7bd4615e550defa0cf76d98bd23ed6 Mon Sep 17 00:00:00 2001 From: Marques Johansson Date: Sat, 13 Apr 2019 18:13:06 -0400 Subject: [PATCH] add filesystem and delete-on-remove volume create options --- README.md | 8 +++++++- driver.go | 38 ++++++++++++++++++++++++++++++++++---- fs_utils_linux.go | 8 ++------ 3 files changed, 43 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index c8c579d..43c4ec3 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,13 @@ my-test-volume #### Create Options -This driver offers `size` as [driver specific option](https://docs.docker.com/engine/reference/commandline/volume_create/#driver-specific-options). The `size` option specifies the size (in GB) of the volume to be created. Volumes must be at least 10GB in size, so the default is 10GB. +The driver offers [driver specific volume create options](https://docs.docker.com/engine/reference/commandline/volume_create/#driver-specific-options): + +| Option | Type | Default | Description | +| --- | --- | --- | --- | +| `size` | int | `10` | the size (in GB) of the volume to be created. Volumes must be at least 10GB in size, so the default is 10GB. +| `filesystem` | string | `ext4` | the filesystem argument for `mkfs` when formating the new (raw) volume +| `delete-on-remove` | bool | `false`| if the Linode volume should be deleted when removed ```sh $ docker volume create -o size=50 -d linode my-test-volume-50 diff --git a/driver.go b/driver.go index 8ed5127..79e4ddd 100644 --- a/driver.go +++ b/driver.go @@ -30,6 +30,9 @@ type linodeVolumeDriver struct { linodeAPIPtr *linodego.Client } +const ( + fsTagPrefix = "docker-volume-filesystem-" +) // Constructor func newLinodeVolumeDriver(linodeLabel string, linodeToken string) linodeVolumeDriver { @@ -191,6 +194,20 @@ func (driver *linodeVolumeDriver) Create(req *volume.CreateRequest) error { Size: size, } + if fsOpt, ok := req.Options["filesystem"]; ok { + createOpts.Tags = append(createOpts.Tags, fsTagPrefix+fsOpt) + } + + if deleteOpt, ok := req.Options["delete-on-remove"]; ok { + b, err := strconv.ParseBool(deleteOpt) + if err != nil { + return fmt.Errorf("Invalid delete-on-remove argument") + } + if b { + createOpts.Tags = append(createOpts.Tags, "docker-volume-delete-on-remove") + } + } + if _, err := api.CreateVolume(context.Background(), createOpts); err != nil { return fmt.Errorf("Create(%s) Failed: %s", req.Name, err) } @@ -221,10 +238,16 @@ func (driver *linodeVolumeDriver) Remove(req *volume.RemoveRequest) error { return err } - // Send Delete request - if err := api.DeleteVolume(context.Background(), linVol.ID); err != nil { - return err + // Optionally send Delete request + for _, t := range linVol.Tags { + if t == "docker-volume-delete-on-remove" { + if err := api.DeleteVolume(context.Background(), linVol.ID); err != nil { + return err + } + break + } } + return nil } @@ -287,7 +310,14 @@ func (driver *linodeVolumeDriver) Mount(req *volume.MountRequest) (*volume.Mount // Format block device if no FS found if GetFSType(linVol.FilesystemPath) == "" { log.Infof("Formatting device:%s;", linVol.FilesystemPath) - if err := Format(linVol.FilesystemPath); err != nil { + filesystem := "ext4" + for _, tag := range linVol.Tags { + if strings.HasPrefix(tag, fsTagPrefix) { + filesystem = tag[len(fsTagPrefix):] + break + } + } + if err := Format(linVol.FilesystemPath, filesystem); err != nil { return nil, err } } diff --git a/fs_utils_linux.go b/fs_utils_linux.go index 75decdc..9dfd556 100644 --- a/fs_utils_linux.go +++ b/fs_utils_linux.go @@ -8,13 +8,9 @@ import ( log "github.com/sirupsen/logrus" ) -const ( - formatFSType = "ext4" -) - // Format calls mke2fs on path -func Format(path string) error { - cmd := exec.Command("mke2fs", "-t", formatFSType, path) +func Format(path string, formatFSType string) error { + cmd := exec.Command("mkfs", "-t", formatFSType, path) stdOutAndErr, err := cmd.CombinedOutput() log.Debugf("Mke2fs Output:\n%s", stdOutAndErr) return err