From 79bafc5fd8a1dcda6f44ecd830dfe50f4ef1b34b Mon Sep 17 00:00:00 2001
From: holyfei <yangfeiyu20092010@163.com>
Date: Tue, 18 Aug 2020 20:41:30 +0800
Subject: [PATCH 10/16] storage: mount nfs and gpath in agent

reason: add nfsStorageHandler and gpathStorageHandler in
storageHandlerList to mount nfs and gpath

Signed-off-by: yangfeiyu <yangfeiyu2@huawei.com>
---
 device.go |  2 ++
 mount.go  | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+)

diff --git a/device.go b/device.go
index 8e6950c..29f72e9 100644
--- a/device.go
+++ b/device.go
@@ -35,6 +35,8 @@ const (
 	driverEphemeralType = "ephemeral"
 	driverLocalType     = "local"
 	vmRootfs            = "/"
+	driverNfsType       = "nfs"
+	driverGpathType     = "gpath"
 )
 
 const (
diff --git a/mount.go b/mount.go
index a05d4a6..de2bfaf 100644
--- a/mount.go
+++ b/mount.go
@@ -17,6 +17,7 @@ import (
 	"strings"
 	"syscall"
 
+	rmtStorage "github.com/kata-containers/agent/pkg/storage"
 	pb "github.com/kata-containers/agent/protocols/grpc"
 	"github.com/pkg/errors"
 	"github.com/sirupsen/logrus"
@@ -218,6 +219,8 @@ var storageHandlerList = map[string]storageHandler{
 	driverEphemeralType: ephemeralStorageHandler,
 	driverLocalType:     localStorageHandler,
 	driverNvdimmType:    nvdimmStorageHandler,
+	driverNfsType:       nfsStorageHandler,
+	driverGpathType:     gpathStorageHandler,
 }
 
 func ephemeralStorageHandler(_ context.Context, storage pb.Storage, s *sandbox) (string, error) {
@@ -339,6 +342,40 @@ func nvdimmStorageHandler(_ context.Context, storage pb.Storage, s *sandbox) (st
 	return "", fmt.Errorf("invalid nvdimm source path: %v", storage.Source)
 }
 
+func nfsStorageHandler(_ context.Context, storage pb.Storage, s *sandbox) (string, error) {
+	s.Lock()
+	defer s.Unlock()
+
+	// mount nfs
+	err := rmtStorage.MountRemoteNfs(storage.Source, storage.Options, s.id)
+	if err != nil {
+		return "", fmt.Errorf("mount %s to %s failed, get err:%s", storage.Source, storage.MountPoint, err)
+	}
+	if err := os.MkdirAll(storage.MountPoint, 0750); err != nil {
+		logrus.Infof("mkdir %s failed after mount remote nfs server", storage.MountPoint)
+		return "", err
+	}
+
+	defer func() {
+		if err != nil {
+			os.RemoveAll(storage.MountPoint)
+		}
+	}()
+
+	return storage.MountPoint, nil
+}
+
+func gpathStorageHandler(_ context.Context, storage pb.Storage, s *sandbox) (string, error) {
+	s.Lock()
+	defer s.Unlock()
+	// validate guespath
+	err := rmtStorage.ValidateGpath(storage.Source, storage.Options)
+	if err != nil {
+		return "", err
+	}
+	return "", nil
+}
+
 // virtioSCSIStorageHandler handles the storage for scsi driver.
 func virtioSCSIStorageHandler(ctx context.Context, storage pb.Storage, s *sandbox) (string, error) {
 	// Retrieve the device path from SCSI address.
-- 
2.14.3 (Apple Git-98)

