Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
agent: Add support for local storage
Browse files Browse the repository at this point in the history
Local storage is just a directory created inside the VM.
This will use whatever the storage driver is for the container
rootfs. This will normally be 9p, but in some cases could be
device mapper. In this case the directory will benefit from the improved
performance of device mapper.

Fixes #524

Signed-off-by: Alex Price <aprice@atlassian.com>
  • Loading branch information
awprice committed Apr 10, 2019
1 parent 7ce9fa5 commit 7e8dd3f
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
driverSCSIType = "scsi"
driverNvdimmType = "nvdimm"
driverEphemeralType = "ephemeral"
driverLocalType = "local"
)

const (
Expand Down
38 changes: 38 additions & 0 deletions mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"syscall"

Expand Down Expand Up @@ -176,6 +177,19 @@ func parseMountFlagsAndOptions(optionList []string) (int, string, error) {
return flags, strings.Join(options, ","), nil
}

func parseOptions(optionList []string) map[string]string {
options := make(map[string]string)
for _, opt := range optionList {
idx := strings.Index(opt, "=")
if idx < 1 {
continue
}
key, val := opt[:idx], opt[idx+1:]
options[key] = val
}
return options
}

func removeMounts(mounts []string) error {
for _, mount := range mounts {
if err := syscall.Unmount(mount, 0); err != nil {
Expand All @@ -198,6 +212,7 @@ var storageHandlerList = map[string]storageHandler{
driverMmioBlkType: virtioMmioBlkStorageHandler,
driverSCSIType: virtioSCSIStorageHandler,
driverEphemeralType: ephemeralStorageHandler,
driverLocalType: localStorageHandler,
}

func ephemeralStorageHandler(storage pb.Storage, s *sandbox) (string, error) {
Expand All @@ -215,6 +230,29 @@ func ephemeralStorageHandler(storage pb.Storage, s *sandbox) (string, error) {
return "", nil
}

func localStorageHandler(storage pb.Storage, s *sandbox) (string, error) {
s.Lock()
defer s.Unlock()
newStorage := s.setSandboxStorage(storage.MountPoint)
if newStorage {

// Extract and parse the mode out of the storage options.
// Default to os.ModePerm.
opts := parseOptions(storage.Options)
mode := os.ModePerm
if val, ok := opts["mode"]; ok {
m, err := strconv.ParseUint(val, 8, 32)
if err != nil {
return "", err
}
mode = os.FileMode(m)
}

return "", os.MkdirAll(storage.MountPoint, mode)
}
return "", nil
}

// virtio9pStorageHandler handles the storage for 9p driver.
func virtio9pStorageHandler(storage pb.Storage, s *sandbox) (string, error) {
return commonStorageHandler(storage)
Expand Down
60 changes: 60 additions & 0 deletions mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,66 @@ func TestEphemeralStorageHandlerSuccessful(t *testing.T) {
assert.Nil(t, err, "ephemeralStorageHandler() failed: %v", err)
}

func TestLocalStorageHandlerSuccessful(t *testing.T) {
skipUnlessRoot(t)

storage, err := createSafeAndFakeStorage()
if err != nil {
t.Fatal(err)
}
defer syscall.Unmount(storage.MountPoint, 0)
defer os.RemoveAll(storage.MountPoint)

sbs := make(map[string]*sandboxStorage)
_, err = localStorageHandler(storage, &sandbox{storages: sbs})
assert.Nil(t, err, "localStorageHandler() failed: %v", err)
}

func TestLocalStorageHandlerPermModeSuccessful(t *testing.T) {
skipUnlessRoot(t)

storage, err := createSafeAndFakeStorage()
if err != nil {
t.Fatal(err)
}
defer syscall.Unmount(storage.MountPoint, 0)
defer os.RemoveAll(storage.MountPoint)

// Set the mode to be 0400 (ready only)
storage.Options = []string{
"mode=0400",
}

sbs := make(map[string]*sandboxStorage)
_, err = localStorageHandler(storage, &sandbox{storages: sbs})
assert.Nil(t, err, "localStorageHandler() failed: %v", err)

// Check the mode of the mountpoint
info, err := os.Stat(storage.MountPoint)
assert.Nil(t, err)
assert.Equal(t, os.FileMode(0400|os.ModeDir), info.Mode())
}

func TestLocalStorageHandlerPermModeFailure(t *testing.T) {
skipUnlessRoot(t)

storage, err := createSafeAndFakeStorage()
if err != nil {
t.Fatal(err)
}
//defer syscall.Unmount(storage.MountPoint, 0)
//defer os.RemoveAll(storage.MountPoint)

// Set the mode to something invalid
storage.Options = []string{
"mode=abcde",
}

sbs := make(map[string]*sandboxStorage)
_, err = localStorageHandler(storage, &sandbox{storages: sbs})
assert.NotNil(t, err, "localStorageHandler() should have failed")
}

func TestVirtio9pStorageHandlerSuccessful(t *testing.T) {
skipUnlessRoot(t)

Expand Down

0 comments on commit 7e8dd3f

Please sign in to comment.