Skip to content

Commit

Permalink
Unix OS Driver
Browse files Browse the repository at this point in the history
This patch renames the Linux and Darwin OS drivers to the Unix OS
driver. This driver is loaded for both the Linux and Darwin OSs. The
Darwin implementation lacks implementations of the Mount & Format
functions.
  • Loading branch information
akutz committed May 23, 2016
1 parent 3a876c1 commit a91441c
Show file tree
Hide file tree
Showing 15 changed files with 410 additions and 192 deletions.
4 changes: 2 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/emccode/libstorage/api/utils"
apicnfg "github.com/emccode/libstorage/api/utils/config"

// load the local imports
_ "github.com/emccode/libstorage/imports/local"
// load the client imports
_ "github.com/emccode/libstorage/imports/client"
)

type client struct {
Expand Down
91 changes: 0 additions & 91 deletions drivers/os/darwin/darwin.go

This file was deleted.

1 change: 0 additions & 1 deletion drivers/os/darwin/darwin_os.go

This file was deleted.

10 changes: 0 additions & 10 deletions drivers/os/linux/linux_docs.go

This file was deleted.

29 changes: 0 additions & 29 deletions drivers/os/linux/linux_label.go

This file was deleted.

1 change: 0 additions & 1 deletion drivers/os/linux/linux_os.go

This file was deleted.

115 changes: 67 additions & 48 deletions drivers/os/linux/linux.go → drivers/os/unix/unix.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
// +build linux
// +build linux darwin

package linux
/*
Package unix is the OS driver for linux and darwin. In order to reduce external
dependencies, this package borrows the following packages:
- github.com/docker/docker/pkg/mount
- github.com/opencontainers/runc/libcontainer/label
*/
package unix

import (
"bytes"
Expand All @@ -9,19 +16,19 @@ import (
"os/exec"
"runtime"
"strings"
"syscall"
"time"

log "github.com/Sirupsen/logrus"
"github.com/akutz/gofig"
"github.com/akutz/goof"

"github.com/emccode/libstorage/api/registry"
"github.com/emccode/libstorage/api/types"
)

const driverName = "linux"
var driverName = runtime.GOOS

var (
errUnknownOS = goof.New("unknown OS")
errUnknownFileSystem = goof.New("unknown file system")
errUnsupportedFileSystem = goof.New("unsupported file system")
)
Expand All @@ -40,9 +47,6 @@ func newDriver() types.OSDriver {
}

func (d *driver) Init(ctx types.Context, config gofig.Config) error {
if runtime.GOOS != "linux" {
return errUnknownOS
}
d.config = config
return nil
}
Expand All @@ -56,7 +60,7 @@ func (d *driver) Mounts(
deviceName, mountPoint string,
opts types.Store) ([]*types.MountInfo, error) {

mounts, err := getMounts()
mounts, err := mounts(ctx, deviceName, mountPoint, opts)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -122,66 +126,59 @@ func (d *driver) Unmount(
mountPoint string,
opts types.Store) error {

return unmount(mountPoint)
var (
err error
isMounted bool
)

isMounted, err = d.IsMounted(ctx, mountPoint, opts)
if err != nil || !isMounted {
return err
}

for i := 0; i < 10; i++ {
if err = syscall.Unmount(mountPoint, 0); err == nil {
return nil
}
time.Sleep(100 * time.Millisecond)
}

return nil
}

func (d *driver) IsMounted(
ctx types.Context,
mountPoint string,
opts types.Store) (bool, error) {

return mounted(mountPoint)
entries, err := mounts(ctx, "", mountPoint, opts)
if err != nil {
return false, err
}

// Search the table for the mountpoint
for _, e := range entries {
if e.MountPoint == mountPoint {
return true, nil
}
}
return false, nil
}

func (d *driver) Format(
ctx types.Context,
deviceName string,
opts *types.DeviceFormatOpts) error {

fsType, err := probeFsType(deviceName)
if err != nil && err != errUnknownFileSystem {
return err
}
fsDetected := fsType != ""

ctx.WithFields(log.Fields{
"fsDetected": fsDetected,
"fsType": fsType,
"deviceName": deviceName,
"overwriteFs": opts.OverwriteFS,
"driverName": driverName}).Info("probe information")

if opts.OverwriteFS || !fsDetected {
switch opts.NewFSType {
case "ext4":
if err := exec.Command(
"mkfs.ext4", "-F", deviceName).Run(); err != nil {
return goof.WithFieldE(
"deviceName", deviceName,
"error creating filesystem",
err)
}
case "xfs":
if err := exec.Command(
"mkfs.xfs", "-f", deviceName).Run(); err != nil {
return goof.WithFieldE(
"deviceName", deviceName,
"error creating filesystem",
err)
}
default:
return errUnsupportedFileSystem
}
}

return nil
return format(ctx, deviceName, opts)
}

func (d *driver) isNfsDevice(device string) bool {
return strings.Contains(device, ":")
}

func (d *driver) nfsMount(device, target string) error {

command := exec.Command("mount", device, target)
output, err := command.CombinedOutput()
if err != nil {
Expand Down Expand Up @@ -246,6 +243,28 @@ func probeFsType(device string) (string, error) {
return "", errUnknownFileSystem
}

/*
formatMountLabel returns a string to be used by the mount command.
The format of this string will be used to alter the labeling of the mountpoint.
The string returned is suitable to be used as the options field of the mount
command.
If you need to have additional mount point options, you can pass them in as
the first parameter. Second parameter is the label that you wish to apply
to all content in the mount point.
*/
func formatMountLabel(src, mountLabel string) string {
if mountLabel != "" {
switch src {
case "":
src = fmt.Sprintf("context=%q", mountLabel)
default:
src = fmt.Sprintf("%s,context=%q", src, mountLabel)
}
}
return src
}

func (d *driver) volumeMountPath(target string) string {
return fmt.Sprintf("%s%s", target, d.volumeRootPath())
}
Expand Down
22 changes: 22 additions & 0 deletions drivers/os/unix/unix_darwin.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// +build darwin

#include "unix_darwin.h"
#include <errno.h>

statfs_result _statfs(char* path) {
statfs_result r;
r.val = (struct statfs*)malloc(sizeof(struct statfs));
r.err = statfs((const char*) path, r.val) == 0 ? 0 : errno;
return r;
}

getmntinfo_result _getmntinfo(int flags) {
getmntinfo_result r;
r.err = 0;
r.len = getmntinfo(&r.val, flags);
if (r.len < 1) {
r.err = errno;
r.len = 0;
}
return r;
}
Loading

0 comments on commit a91441c

Please sign in to comment.