Skip to content

Commit 8a67d52

Browse files
authored
Remove empty mount paths (#2036)
When running mount command with a layout file that contains spaces, those spaces will be converted to empty paths. This commit removes those paths in the Sanitize method for the MountSpec. Signed-off-by: Fredrik Lönnegren <[email protected]>
1 parent d993113 commit 8a67d52

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

pkg/action/mount.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,24 @@ import (
2525
"strings"
2626

2727
"github.com/hashicorp/go-multierror"
28+
2829
"github.com/rancher/elemental-toolkit/v2/pkg/constants"
2930
"github.com/rancher/elemental-toolkit/v2/pkg/types"
3031
"github.com/rancher/elemental-toolkit/v2/pkg/utils"
3132
)
3233

33-
const overlaySuffix = ".overlay"
34-
const labelPref = "LABEL="
35-
const partLabelPref = "PARTLABEL="
36-
const uuidPref = "UUID="
37-
const devPref = "/dev/"
38-
const diskBy = "/dev/disk/by-"
39-
const diskByLabel = diskBy + "label"
40-
const diskByPartLabel = diskBy + "partlabel"
41-
const diskByUUID = diskBy + "uuid"
42-
const runPath = "/run"
34+
const (
35+
overlaySuffix = ".overlay"
36+
labelPref = "LABEL="
37+
partLabelPref = "PARTLABEL="
38+
uuidPref = "UUID="
39+
devPref = "/dev/"
40+
diskBy = "/dev/disk/by-"
41+
diskByLabel = diskBy + "label"
42+
diskByPartLabel = diskBy + "partlabel"
43+
diskByUUID = diskBy + "uuid"
44+
runPath = "/run"
45+
)
4346

4447
func RunMount(cfg *types.RunConfig, spec *types.MountSpec) error {
4548
var fstabData string

pkg/types/config.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"os"
2222
"path/filepath"
2323
"runtime"
24+
"slices"
2425
"sort"
2526
"strings"
2627

@@ -300,6 +301,11 @@ func (spec *MountSpec) Sanitize() error {
300301
const separator = string(os.PathSeparator)
301302

302303
if spec.Persistent.Paths != nil {
304+
// Remove empty paths
305+
spec.Persistent.Paths = slices.DeleteFunc(spec.Persistent.Paths, func(s string) bool {
306+
return s == ""
307+
})
308+
303309
sort.Slice(spec.Persistent.Paths, func(i, j int) bool {
304310
return strings.Count(spec.Persistent.Paths[i], separator) < strings.Count(spec.Persistent.Paths[j], separator)
305311
})
@@ -313,6 +319,11 @@ func (spec *MountSpec) Sanitize() error {
313319
}
314320

315321
if spec.Ephemeral.Paths != nil {
322+
// Remove empty paths
323+
spec.Ephemeral.Paths = slices.DeleteFunc(spec.Ephemeral.Paths, func(s string) bool {
324+
return s == ""
325+
})
326+
316327
sort.Slice(spec.Ephemeral.Paths, func(i, j int) bool {
317328
return strings.Count(spec.Ephemeral.Paths[i], separator) < strings.Count(spec.Ephemeral.Paths[j], separator)
318329
})

pkg/types/config_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,4 +532,23 @@ var _ = Describe("Types", Label("types", "config"), func() {
532532
Expect(spec.Sanitize()).Should(HaveOccurred())
533533
})
534534
})
535+
Describe("MountSpec", func() {
536+
It("sanitizes empty paths", func() {
537+
spec := types.MountSpec{
538+
Ephemeral: types.EphemeralMounts{
539+
Type: constants.Tmpfs,
540+
Paths: []string{"/var", "", "/etc"},
541+
},
542+
Persistent: types.PersistentMounts{
543+
Mode: constants.OverlayMode,
544+
Paths: []string{"/etc/rancher", "", "/root"},
545+
},
546+
}
547+
548+
Expect(spec.Sanitize()).To(Succeed())
549+
550+
Expect(spec.Ephemeral.Paths).To(Equal([]string{"/var", "/etc"}))
551+
Expect(spec.Persistent.Paths).To(Equal([]string{"/root", "/etc/rancher"}))
552+
})
553+
})
535554
})

0 commit comments

Comments
 (0)