Skip to content

Commit

Permalink
add filesystem and delete-on-remove volume create options
Browse files Browse the repository at this point in the history
  • Loading branch information
displague committed Apr 13, 2019
1 parent 088bb3d commit be8f94b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 34 additions & 4 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type linodeVolumeDriver struct {
linodeAPIPtr *linodego.Client
}

const (
fsTagPrefix = "docker-volume-filesystem-"
)

// Constructor
func newLinodeVolumeDriver(linodeLabel string, linodeToken string) linodeVolumeDriver {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
}
Expand Down
8 changes: 2 additions & 6 deletions fs_utils_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit be8f94b

Please sign in to comment.