diff --git a/pkg/mountutil/mountutil_linux.go b/pkg/mountutil/mountutil_linux.go index 02da626213b..eb8ec64b866 100644 --- a/pkg/mountutil/mountutil_linux.go +++ b/pkg/mountutil/mountutil_linux.go @@ -247,7 +247,7 @@ func parseVolumeOptionsWithMountInfo(vType, src, optsRaw, ociRuntime string, get if err != nil { return nil, nil, err } - if err := ensureMountOptionalValue(mi, "shared:"); err != nil { + if err := ensureMountOptionalValue(mi, got, "shared:"); err != nil { return nil, nil, err } @@ -276,7 +276,7 @@ func parseVolumeOptionsWithMountInfo(vType, src, optsRaw, ociRuntime string, get if err != nil { return nil, nil, err } - if err := ensureMountOptionalValue(mi, "shared:", "master:"); err != nil { + if err := ensureMountOptionalValue(mi, got, "shared:", "master:"); err != nil { return nil, nil, err } @@ -320,7 +320,7 @@ func parseVolumeOptionsWithMountInfo(vType, src, optsRaw, ociRuntime string, get // // For more details about "optional" field: // - https://github.com/moby/sys/blob/mountinfo/v0.4.1/mountinfo/mountinfo.go#L52-L56 -func ensureMountOptionalValue(mi mount.Info, vals ...string) error { +func ensureMountOptionalValue(mi mount.Info, propagation string, vals ...string) error { var hasValue bool for _, opt := range strings.Split(mi.Optional, " ") { for _, mark := range vals { @@ -330,7 +330,7 @@ func ensureMountOptionalValue(mi mount.Info, vals ...string) error { } } if !hasValue { - return fmt.Errorf("mountpoint %q doesn't have optional field neither of %+v", mi.Mountpoint, vals) + return fmt.Errorf("mountpoint %q is not a shared or slave mount, so it cannot be bind-mounted with propagation %q; try running `mount --make-rshared %q` on the host", mi.Mountpoint, propagation, mi.Mountpoint) } return nil } diff --git a/pkg/mountutil/mountutil_linux_test.go b/pkg/mountutil/mountutil_linux_test.go index 5ee383d7065..4fb80140cd4 100644 --- a/pkg/mountutil/mountutil_linux_test.go +++ b/pkg/mountutil/mountutil_linux_test.go @@ -59,6 +59,7 @@ func TestParseVolumeOptions(t *testing.T) { wants []string wantRootfsPropagation string wantFail bool + wantErrContains string }{ { name: "unknown option is ignored (with warning)", @@ -176,12 +177,13 @@ func TestParseVolumeOptions(t *testing.T) { wants: []string{"ro", "rshared"}, }, { - name: "shared propagation is not allowed if the src is not shared", - vType: "bind", - src: "dummy", - optsRaw: "ro,shared", - srcOptional: nil, - wantFail: true, + name: "shared propagation is not allowed if the src is not shared", + vType: "bind", + src: "dummy", + optsRaw: "ro,shared", + srcOptional: nil, + wantFail: true, + wantErrContains: `mount --make-rshared "dummy"`, }, { name: "make bind slave", @@ -203,12 +205,13 @@ func TestParseVolumeOptions(t *testing.T) { wants: []string{"ro", "slave"}, }, { - name: "slave propagation is not allowed if the src is not slave", - vType: "bind", - src: "dummy", - optsRaw: "ro,slave", - srcOptional: nil, - wantFail: true, + name: "slave propagation is not allowed if the src is not slave", + vType: "bind", + src: "dummy", + optsRaw: "ro,slave", + srcOptional: nil, + wantFail: true, + wantErrContains: `mount --make-rshared "dummy"`, }, } for _, tt := range tests { @@ -222,6 +225,9 @@ func TestParseVolumeOptions(t *testing.T) { }) if err != nil { if tt.wantFail { + if tt.wantErrContains != "" { + assert.ErrorContains(t, err, tt.wantErrContains) + } return } t.Errorf("failed to parse option %q: %v", tt.optsRaw, err)