Skip to content
This repository has been archived by the owner on Jan 17, 2018. It is now read-only.

Add support for specifying uid, gid, filemode and dirmode #44

Merged
merged 5 commits into from
Jul 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ Please check out the following documentation:
#### Start volume driver daemon

* Make sure you have a Storage Account on Azure (using Azure CLI or Portal).
* The server process must be running on the host machine where Docker engine is installed on
* The server process must be running on the host machine where Docker engine is installed on
at all times for volumes to work properly.
* “cifs-utils” package must be installed on the host system as Azure Files use SMB protocol.
For Debian/Ubuntu, run the following command on your host:
```shell
$ sudo apt-get install -y cifs-utils
```

Please refer to “Installation” section above. If you like to build the binary from source,
Please refer to “Installation” section above. If you like to build the binary from source,
see “Building” section below on how to compile. Once the driver is installed, start it and
check its status.

Expand Down Expand Up @@ -67,6 +67,25 @@ This will create an Azure File Share named `myshare` (if it does not exist)
and start a Docker container in which you can use `/data` directory to directly
read/write from cloud file share location using SMB protocol.

You can specify additional volume options to customize the owner, group, and permissions for files and directories. See the `mount.cifs(8)` man page more details on these options.

Mount Options Available:
* `uid`
* `gid`
* `filemode`
* `dirmode`
* `nolock`

```shell
$ docker volume create -d azurefile \
-o share=sharename \
-o uid=999 \
-o gid=999 \
-o filemode=0600 \
-o dirmode=0755 \
-o nolock=true
```

## Demo

![](http://cl.ly/image/2z1z1y030u3B/Image%202015-10-06%20at%203.18.39%20PM.gif)
Expand Down
33 changes: 30 additions & 3 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (v *volumeDriver) Mount(req volume.Request) (resp volume.Response) {
return
}

if err := mount(v.accountName, v.accountKey, meta.Options.Share, path); err != nil {
if err := mount(v.accountName, v.accountKey, path, meta.Options); err != nil {
resp.Err = err.Error()
logctx.Error(resp.Err)
return
Expand Down Expand Up @@ -281,12 +281,39 @@ func (v *volumeDriver) pathForVolume(name string) string {
return filepath.Join(v.mountpoint, name)
}

func mount(accountName, accountKey, shareName, mountpoint string) error {
func mount(accountName, accountKey, mountpoint string, options VolumeOptions) error {
// Set defaults
if len(options.FileMode) == 0 {
options.FileMode = "0777"
}
if len(options.DirMode) == 0 {
options.DirMode = "0777"
}
if len(options.UID) == 0 {
options.UID = "0"
}
if len(options.GID) == 0 {
options.GID = "0"
}
mount := fmt.Sprintf("//%s.file.core.windows.net/%s", accountName, options.Share)
opts := []string{
"vers=3.0",
fmt.Sprintf("username=%s", accountName),
fmt.Sprintf("password=%s", accountKey),
fmt.Sprintf("file_mode=%s", options.FileMode),
fmt.Sprintf("dir_mode=%s", options.DirMode),
fmt.Sprintf("uid=%s", options.UID),
fmt.Sprintf("gid=%s", options.GID),
}
if options.NoLock {
opts = append(opts, "nolock")
}

// TODO: replace with mount() syscall using docker/docker/pkg/mount
// (currently gives hard-to-debug 'invalid argument' error with the
// following arguments, my guess is, mount program does IP resolution
// and essentially passes a different set of options to system call).
cmd := exec.Command("mount", "-t", "cifs", fmt.Sprintf("//%s.file.core.windows.net/%s", accountName, shareName), mountpoint, "-o", fmt.Sprintf("vers=3.0,username=%s,password=%s,dir_mode=0777,file_mode=0777", accountName, accountKey), "--verbose")
cmd := exec.Command("mount", "-t", "cifs", mount, mountpoint, "-o", strings.Join(opts, ","), "--verbose")
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("mount failed: %v\noutput=%q", err, out)
Expand Down
23 changes: 19 additions & 4 deletions metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

var (
recognizedOptions = []string{"share"}
recognizedOptions = []string{"share", "filemode", "dirmode", "uid", "gid", "nolock"}
)

type volumeMetadata struct {
Expand All @@ -21,7 +21,12 @@ type volumeMetadata struct {

// VolumeOptions stores the opts passed to the driver by the docker engine.
type VolumeOptions struct {
Share string `json:"share"`
Share string `json:"share"`
FileMode string `json:"filemode"`
DirMode string `json:"dirmode"`
UID string `json:"uid"`
GID string `json:"gid"`
NoLock bool `json:"nolock"`
}

type metadataDriver struct {
Expand All @@ -37,6 +42,7 @@ func newMetadataDriver(metaDir string) (*metadataDriver, error) {

func (m *metadataDriver) Validate(meta map[string]string) (volumeMetadata, error) {
var v volumeMetadata
var opts VolumeOptions

// Validate keys
for k := range meta {
Expand All @@ -51,10 +57,19 @@ func (m *metadataDriver) Validate(meta map[string]string) (volumeMetadata, error
return v, fmt.Errorf("not a recognized volume driver option: %q", k)
}
}
opts.Share = meta["share"]
opts.DirMode = meta["dirmode"]
opts.FileMode = meta["filemode"]
opts.GID = meta["gid"]
opts.UID = meta["uid"]

if meta["nolock"] == "true" {
opts.NoLock = true
}

return volumeMetadata{
Options: VolumeOptions{
Share: meta["share"]}}, nil
Options: opts,
}, nil
}

func (m *metadataDriver) Delete(name string) error {
Expand Down