From 2adf2ab30b3bb06e3819c42283b8eccfd5d4a1fd Mon Sep 17 00:00:00 2001 From: Brett Shouse Date: Tue, 3 Sep 2019 13:20:11 -0600 Subject: [PATCH] Use sparse volumes --- cmd/hostpathplugin/main.go | 2 +- pkg/hostpath/hostpath.go | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/cmd/hostpathplugin/main.go b/cmd/hostpathplugin/main.go index 44c253b2c..56cb64f0e 100644 --- a/cmd/hostpathplugin/main.go +++ b/cmd/hostpathplugin/main.go @@ -30,7 +30,7 @@ func init() { } var ( - endpoint = flag.String("endpoint", "unix://tmp/csi.sock", "CSI endpoint") + endpoint = flag.String("endpoint", "unix://tmp/csi-hostpath.sock", "CSI endpoint") driverName = flag.String("drivername", "hostpath.csi.k8s.io", "name of the driver") nodeID = flag.String("nodeid", "", "node id") ephemeral = flag.Bool("ephemeral", false, "publish volumes in ephemeral mode even if kubelet did not ask for it (only needed for Kubernetes 1.15)") diff --git a/pkg/hostpath/hostpath.go b/pkg/hostpath/hostpath.go index 57a363687..576efed23 100644 --- a/pkg/hostpath/hostpath.go +++ b/pkg/hostpath/hostpath.go @@ -163,7 +163,7 @@ func getVolumePath(volID string) string { return filepath.Join(dataRoot, volID) } -// createVolume create the directory for the hostpath volume. +// createVolume create the sparse ext2 filesystem for the hostpath volume. // It returns the volume path or err if one occurs. func createHostpathVolume(volID, name string, cap int64, volAccessType accessType, ephemeral bool) (*hostPathVolume, error) { path := getVolumePath(volID) @@ -177,10 +177,17 @@ func createHostpathVolume(volID, name string, cap int64, volAccessType accessTyp case blockAccess: executor := utilexec.New() size := fmt.Sprintf("%dM", cap/mib) + // Create a block file. - out, err := executor.Command("fallocate", "-l", size, path).CombinedOutput() + out, err := executor.Command("dd", "if=/dev/zero", "bs=1", "count=0", String.Join("seek=", size), String.Join("of=", path)).CombinedOutput() + if err != nil { + return nil, fmt.Errorf("failed to create block device allocation: %v, %v", err, string(out)) + } + + //Format a block file + out, err := executor.Command("mkfs.ext2", "-q", path).CombinedOutput() if err != nil { - return nil, fmt.Errorf("failed to create block device: %v, %v", err, string(out)) + return nil, fmt.Errorf("failed to create ext2 filesystem on block device allocation: %v, %v", err, string(out)) } // Associate block file with the loop device.