Skip to content

Commit 97c2960

Browse files
committed
Remove fs name from volume data file name
1 parent 23ad5ce commit 97c2960

File tree

5 files changed

+102
-54
lines changed

5 files changed

+102
-54
lines changed

driver/driver.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ func (d Driver) List() (response *v.ListResponse, err error) {
355355
response.Volumes = make([]*v.Volume, len(volumes))
356356
for idx, vol := range volumes {
357357
response.Volumes[idx] = &v.Volume{
358-
Name: vol.Name,
358+
Name: vol,
359359
}
360360
}
361361

@@ -412,6 +412,7 @@ func (d Driver) Get(request *v.GetRequest) (response *v.GetResponse, err error)
412412
if err != nil {
413413
return
414414
}
415+
fs, err := vol.Fs(ctx.Derived())
415416

416417
ctx.
417418
Level(context.Trace).
@@ -424,7 +425,7 @@ func (d Driver) Get(request *v.GetRequest) (response *v.GetResponse, err error)
424425
CreatedAt: fmt.Sprintf(vol.CreatedAt.Format(time.RFC3339)),
425426
Mountpoint: vol.MountPointPath,
426427
Status: map[string]interface{}{
427-
"fs": vol.Fs,
428+
"fs": fs,
428429
"size-max": strconv.FormatUint(vol.MaxSizeInBytes, 10),
429430
"size-allocated": strconv.FormatUint(vol.AllocatedSizeInBytes, 10),
430431
},

manager/manager.go

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func New(ctx *context.Context, cfg Config) (manager Manager, err error) {
119119
return
120120
}
121121

122-
func (m Manager) List(ctx *context.Context) (volumes []Volume, err error) {
122+
func (m Manager) List(ctx *context.Context) (volumes []string, err error) {
123123
// tracing
124124
ctx = ctx.
125125
Field(":func", "manager/List")
@@ -149,11 +149,9 @@ func (m Manager) List(ctx *context.Context) (volumes []Volume, err error) {
149149
// read data dir
150150
var files []os.FileInfo
151151
{
152-
ctx := ctx.
153-
Field("data-dir", m.dataDir)
154-
155152
ctx.
156153
Level(context.Trace).
154+
Field("data-dir", m.dataDir).
157155
Message("checking if data-dir exists")
158156

159157
_, err = os.Stat(m.dataDir)
@@ -187,15 +185,8 @@ func (m Manager) List(ctx *context.Context) (volumes []Volume, err error) {
187185
Message("processing entry")
188186

189187
if file.Mode().IsRegular() {
190-
var vol Volume
191-
192-
name := strings.TrimSuffix(file.Name(), filepath.Ext(file.Name()))
193-
vol, err = m.getVolume(ctx.Derived(), name)
194-
if err != nil {
195-
return
196-
}
188+
volumes = append(volumes, file.Name())
197189

198-
volumes = append(volumes, vol)
199190
ctx.
200191
Level(context.Trace).
201192
Message("including as a volume")
@@ -344,7 +335,7 @@ func (m Manager) Create(ctx *context.Context, name string, sizeInBytes int64, sp
344335
}
345336

346337
// create data file
347-
var dataFilePath = filepath.Join(m.dataDir, name+"."+fs)
338+
var dataFilePath = filepath.Join(m.dataDir, name)
348339
{
349340
ctx := ctx.
350341
Field("data-file", dataFilePath).
@@ -562,7 +553,7 @@ func (m Manager) Mount(ctx *context.Context, name string, lease string) (result
562553
ctx.
563554
Level(context.Trace).
564555
Message("checking if volume is mounted anywhere else")
565-
isAlreadyMounted, err = volume.IsMounted() // checking mount status early before we record a lease
556+
isAlreadyMounted, err = volume.IsMounted(ctx.Derived()) // checking mount status early before we record a lease
566557
if err != nil {
567558
err = errors.Wrap(err, "cannot check volume mount status")
568559
return
@@ -667,8 +658,18 @@ func (m Manager) Mount(ctx *context.Context, name string, lease string) (result
667658
return
668659
}
669660

670-
// we should've validated FS by now if it's not found then we will get empty list of options
671-
mountFlags := MountOptions[volume.Fs]
661+
ctx.
662+
Level(context.Trace).
663+
Message("resolving volume fs to determine mount options")
664+
var fs string
665+
fs, err = volume.Fs(ctx.Derived())
666+
if err != nil {
667+
err = errors.Wrapf(err, "cannot resolve volume fs to determine mount options")
668+
return
669+
}
670+
671+
mountFlags := MountOptions[fs]
672+
672673
ctx.
673674
Level(context.Trace).
674675
Field("mount-flags", mountFlags).
@@ -678,6 +679,7 @@ func (m Manager) Mount(ctx *context.Context, name string, lease string) (result
678679
"mount",
679680
append(mountFlags, volume.DataFilePath, volume.MountPointPath)...,
680681
)
682+
681683
if err != nil {
682684
ctx.
683685
Level(context.Trace).
@@ -768,7 +770,7 @@ func (m Manager) UnMount(ctx *context.Context, name string, lease string) (err e
768770
ctx.
769771
Level(context.Trace).
770772
Message("checking if volume is mounted anywhere else")
771-
isMountedAnywhereElse, err = volume.IsMounted()
773+
isMountedAnywhereElse, err = volume.IsMounted(ctx.Derived())
772774
if err != nil {
773775
err = errors.Wrapf(err, "cannot figure out if volume is used anywhere else", lease)
774776
return
@@ -873,7 +875,7 @@ func (m Manager) Delete(ctx *context.Context, name string) (err error) {
873875
Message("checking if volume is still mounted")
874876

875877
var isMounted bool
876-
isMounted, err = volume.IsMounted()
878+
isMounted, err = volume.IsMounted(ctx.Derived())
877879

878880
if err != nil {
879881
err = errors.Wrap(err, "cannot get volume mount status")
@@ -929,36 +931,19 @@ func (m Manager) getVolume(ctx *context.Context, name string) (volume Volume, er
929931
}()
930932
}
931933

932-
prefix := filepath.Join(m.dataDir, name) + ".*"
933-
matches, err := filepath.Glob(prefix)
934-
if err != nil {
935-
err = errors.Wrapf(err,
936-
"an issue occurred while retrieving details about volume '%s' - cannot glob data dir", name)
937-
return
938-
}
939-
if len(matches) > 1 {
940-
err = errors.Errorf("more than 1 data file found for volume '%s'", name)
941-
return
942-
} else if len(matches) == 0 {
943-
err = errors.Errorf("volume '%s' does not exist", name)
944-
return
945-
}
946-
947-
volumeDataFilePath := matches[0]
948-
fs := strings.TrimLeft(filepath.Ext(volumeDataFilePath), ".")
949-
934+
volumeDataFilePath := filepath.Join(m.dataDir, name)
950935
volumeDataFileInfo, err := os.Stat(volumeDataFilePath)
951936

952937
if err != nil {
953-
if os.IsNotExist(err) { // this should not happen but...
954-
err = errors.Errorf("volume '%s' disappeared just a moment ago", name)
938+
if os.IsNotExist(err) {
939+
err = errors.Errorf("volume '%s' does not exist", name)
955940
}
956941
return
957942
}
958943

959944
if !volumeDataFileInfo.Mode().IsRegular() {
960945
err = errors.Errorf(
961-
"volume data path expected to point to a file but it appears to be something else: '%s'",
946+
"volume data path expected to point toa file but it appears to be something else: '%s'",
962947
volumeDataFilePath)
963948
return
964949
}
@@ -974,7 +959,6 @@ func (m Manager) getVolume(ctx *context.Context, name string) (volume Volume, er
974959

975960
volume = Volume{
976961
Name: name,
977-
Fs: fs,
978962
AllocatedSizeInBytes: uint64(details.Blocks * 512),
979963
MaxSizeInBytes: uint64(details.Size),
980964
StateDir: filepath.Join(m.stateDir, name),

manager/volume.go

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,50 @@
11
package manager
22

33
import (
4+
"github.com/ashald/docker-volume-loopback/context"
45
"github.com/pkg/errors"
56
"io/ioutil"
67
"os"
8+
"strings"
79
"time"
810
)
911

1012
type Volume struct {
1113
Name string
1214
AllocatedSizeInBytes uint64
13-
Fs string
1415
MaxSizeInBytes uint64
1516
StateDir string
1617
DataFilePath string
1718
MountPointPath string
1819
CreatedAt time.Time
20+
fs string
1921
}
2022

21-
func (v Volume) IsMounted() (mounted bool, err error) {
23+
func (v Volume) IsMounted(ctx *context.Context) (mounted bool, err error) {
24+
{
25+
ctx = ctx.
26+
Field(":func", "Volume/IsMounted")
27+
28+
ctx.
29+
Level(context.Debug).
30+
Message("invoked")
31+
32+
defer func() {
33+
if err != nil {
34+
ctx.
35+
Level(context.Error).
36+
Field(":return/err", err).
37+
Message("failed with an error")
38+
return
39+
} else {
40+
ctx.
41+
Level(context.Debug).
42+
Field(":return/mounted", mounted).
43+
Message("finished")
44+
}
45+
}()
46+
}
47+
2248
files, err := ioutil.ReadDir(v.StateDir)
2349
if err != nil {
2450
if os.IsNotExist(err) {
@@ -34,3 +60,40 @@ func (v Volume) IsMounted() (mounted bool, err error) {
3460
mounted = len(files) > 0
3561
return
3662
}
63+
64+
func (v Volume) Fs(ctx *context.Context) (fs string, err error) {
65+
{
66+
ctx = ctx.
67+
Field(":func", "Volume/Fs")
68+
69+
ctx.
70+
Level(context.Debug).
71+
Message("invoked")
72+
73+
defer func() {
74+
if err != nil {
75+
ctx.
76+
Level(context.Error).
77+
Field(":return/err", err).
78+
Message("failed with an error")
79+
return
80+
} else {
81+
ctx.
82+
Level(context.Debug).
83+
Field(":return/fs", fs).
84+
Message("finished")
85+
}
86+
}()
87+
}
88+
89+
var output string
90+
91+
if len(v.fs) == 0 {
92+
output, err = runCommand(ctx.Derived(), "file", v.DataFilePath)
93+
tokens := strings.Split(strings.TrimSpace(strings.Split(output, "filesystem")[0]), " ")
94+
v.fs = strings.ToLower(tokens[len(tokens)-1])
95+
}
96+
97+
fs = v.fs
98+
return
99+
}

tests/test_fs_ext4.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ testRegularVolumeDoesNotReserveDiskSpace() {
2323
# setup
2424
volume=$(docker volume create -d "${DRIVER}" -o fs=${FS} -o sparse=false)
2525

26-
allocated_size="$(($(run stat -c '%b %B' ${DATA_DIR}/${volume}.${FS} | tr ' ' '*')))" # allocated space in bytes
27-
apparent_size="$(run stat -c '%s' ${DATA_DIR}/${volume}.${FS})" # apparent space in bytes
26+
allocated_size="$(($(run stat -c '%b %B' ${DATA_DIR}/${volume} | tr ' ' '*')))" # allocated space in bytes
27+
apparent_size="$(run stat -c '%s' ${DATA_DIR}/${volume})" # apparent space in bytes
2828

2929
# checks
3030
assertTrue "Regular ${FS} volume of ${apparent_size} MiB should take less space: ${allocated_size} MiB" "[ ${allocated_size} -lt ${apparent_size} ]"
@@ -41,7 +41,7 @@ testSparseVolumeDoesNotCheckAvailableDiskSpace() {
4141
volume=$(docker volume create -d "${DRIVER}" -o fs=${FS} -o sparse=true -o size=10GiB)
4242
result=$?
4343

44-
apparent_size="$(run stat -c '%s' ${DATA_DIR}/${volume}.${FS})" # apparent space in bytes
44+
apparent_size="$(run stat -c '%s' ${DATA_DIR}/${volume})" # apparent space in bytes
4545

4646
# checks
4747
assertEquals "0" "${result}"
@@ -56,8 +56,8 @@ testSparseVolumeDoesNotReserveDiskSpace() {
5656
# setup
5757
volume=$(docker volume create -d "${DRIVER}" -o fs=${FS} -o sparse=true)
5858

59-
allocated_size="$(($(run stat -c '%b %B' ${DATA_DIR}/${volume}.${FS} | tr ' ' '*')))" # allocated space in bytes
60-
apparent_size="$(run stat -c '%s' ${DATA_DIR}/${volume}.${FS})" # apparent space in bytes
59+
allocated_size="$(($(run stat -c '%b %B' ${DATA_DIR}/${volume} | tr ' ' '*')))" # allocated space in bytes
60+
apparent_size="$(run stat -c '%s' ${DATA_DIR}/${volume})" # apparent space in bytes
6161

6262
# checks
6363
assertTrue "Sparse ${FS} volume of ${apparent_size} MiB should take less space: ${allocated_size} MiB" "[ ${allocated_size} -lt ${apparent_size} ]"

tests/test_fs_xfs.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ testRegularVolumeReservesDiskSpace() {
2323
# setup
2424
local volume=$(docker volume create -d "${DRIVER}" -o fs=${FS} -o sparse=false)
2525

26-
allocated_size="$(($(run stat -c '%b %B' ${DATA_DIR}/${volume}.${FS} | tr ' ' '*')))" # allocated space in bytes
27-
apparent_size="$(run stat -c '%s' ${DATA_DIR}/${volume}.${FS})" # apparent space in bytes
26+
allocated_size="$(($(run stat -c '%b %B' ${DATA_DIR}/${volume} | tr ' ' '*')))" # allocated space in bytes
27+
apparent_size="$(run stat -c '%s' ${DATA_DIR}/${volume})" # apparent space in bytes
2828

2929
# checks
3030
assertTrue "Regular ${FS} volume of ${apparent_size} MiB should take at least same space: ${allocated_size} MiB" "[ ${allocated_size} -ge ${apparent_size} ]"
@@ -41,7 +41,7 @@ testSparseVolumeDoesNotCheckAvailableDiskSpace() {
4141
volume=$(docker volume create -d "${DRIVER}" -o fs=${FS} -o sparse=true -o size=10GiB)
4242
result=$?
4343

44-
apparent_size="$(run stat -c '%s' ${DATA_DIR}/${volume}.${FS})" # apparent space in bytes
44+
apparent_size="$(run stat -c '%s' ${DATA_DIR}/${volume})" # apparent space in bytes
4545

4646
# checks
4747
assertEquals "0" "${result}"
@@ -56,8 +56,8 @@ testSparseVolumeDoesNotTakeDiskSpace() {
5656
# setup
5757
local volume=$(docker volume create -d "${DRIVER}" -o fs=${FS} -o sparse=true)
5858

59-
allocated_size="$(($(run stat -c '%b %B' ${DATA_DIR}/${volume}.${FS} | tr ' ' '*')))" # allocated space in bytes
60-
apparent_size="$(run stat -c '%s' ${DATA_DIR}/${volume}.${FS})" # apparent space in bytes
59+
allocated_size="$(($(run stat -c '%b %B' ${DATA_DIR}/${volume} | tr ' ' '*')))" # allocated space in bytes
60+
apparent_size="$(run stat -c '%s' ${DATA_DIR}/${volume})" # apparent space in bytes
6161

6262
# checks
6363
assertTrue "Sparse ${FS} volume of ${apparent_size} MiB should take less space: ${allocated_size} MiB" "[ ${allocated_size} -lt ${apparent_size} ]"

0 commit comments

Comments
 (0)