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

agent: Add support for local storage #521

Merged
merged 1 commit into from
Apr 12, 2019
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
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we pass dir mode here. Please add the corresponding part in kata-containers/runtime#1485

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the recommendation, I've updated the runtime PR with the mode.

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, 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