Skip to content

Commit 500d131

Browse files
Merge pull request #27385 from KonishchevDmitry/bind-mounts-with-spaces
Escape RequiresMountsFor value to properly handle bind mounts with spaces in path
2 parents 5b52ee4 + 601a072 commit 500d131

File tree

13 files changed

+61
-35
lines changed

13 files changed

+61
-35
lines changed

pkg/systemd/parser/unitfile.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,15 @@ func (f *UnitFile) Add(groupName string, key string, value string) {
879879
group.add(key, value)
880880
}
881881

882+
func (f *UnitFile) AddEscaped(groupName string, key string, value string) {
883+
if wordNeedEscape(value) {
884+
var escaped strings.Builder
885+
appendEscapeWord(&escaped, value)
886+
value = escaped.String()
887+
}
888+
f.Add(groupName, key, value)
889+
}
890+
882891
func (f *UnitFile) AddCmdline(groupName string, key string, args []string) {
883892
f.Add(groupName, key, escapeWords(args))
884893
}

pkg/systemd/quadlet/quadlet.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,7 @@ func ConvertVolume(volume *parser.UnitFile, unitsInfoMap map[string]*UnitInfo, i
11511151
if devValid {
11521152
podman.add("--opt", fmt.Sprintf("type=%s", devType))
11531153
if devType == "bind" {
1154-
service.Add(UnitGroup, "RequiresMountsFor", dev)
1154+
service.AddEscaped(UnitGroup, "RequiresMountsFor", dev)
11551155
}
11561156
} else {
11571157
return nil, warnings, fmt.Errorf("key Type can't be used without Device")
@@ -1929,7 +1929,7 @@ func handleStorageSource(quadletUnitFile, serviceUnitFile *parser.UnitFile, sour
19291929
}
19301930
if source[0] == '/' {
19311931
// Absolute path
1932-
serviceUnitFile.Add(UnitGroup, "RequiresMountsFor", source)
1932+
serviceUnitFile.AddEscaped(UnitGroup, "RequiresMountsFor", source)
19331933
} else if strings.HasSuffix(source, ".volume") || (checkImage && strings.HasSuffix(source, ".image")) || strings.HasSuffix(source, ".artifact") {
19341934
sourceUnitInfo, ok := unitsInfoMap[source]
19351935
if !ok {

test/e2e/quadlet/device-bind.volume

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
## assert-key-contains Service ExecStart " --opt type=bind "
2-
## assert-key-contains Service ExecStart " --opt device=/var/lib/data "
3-
## assert-key-contains Service ExecStart " --opt nocopy "
4-
## assert-key-contains Unit RequiresMountsFor "/var/lib/data"
1+
## assert-last-key-contains Service ExecStart " --opt type=bind "
2+
## assert-last-key-contains Service ExecStart " --opt device=/var/lib/data "
3+
## assert-last-key-contains Service ExecStart " --opt nocopy "
4+
## assert-last-key-contains Unit RequiresMountsFor "/var/lib/data"
55

66
[Volume]
77
Device=/var/lib/data

test/e2e/quadlet/device-copy.volume

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
## assert-key-contains Service ExecStart " --opt o=uid=0,gid=11,rw,compress=zstd "
2-
## assert-key-contains Service ExecStart " --opt type=btrfs "
3-
## assert-key-contains Service ExecStart " --opt device=/dev/vda1 "
4-
## assert-key-contains Service ExecStart " --opt copy "
1+
## assert-last-key-contains Service ExecStart " --opt o=uid=0,gid=11,rw,compress=zstd "
2+
## assert-last-key-contains Service ExecStart " --opt type=btrfs "
3+
## assert-last-key-contains Service ExecStart " --opt device=/dev/vda1 "
4+
## assert-last-key-contains Service ExecStart " --opt copy "
55

66
[Volume]
77
# Test usernames too

test/e2e/quadlet/device.volume

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
## assert-key-contains Service ExecStart " --opt o=uid=0,gid=11,rw,compress=zstd "
2-
## assert-key-contains Service ExecStart " --opt type=btrfs "
3-
## assert-key-contains Service ExecStart " --opt device=/dev/vda1 "
4-
## assert-key-contains Service ExecStart " --opt nocopy "
1+
## assert-last-key-contains Service ExecStart " --opt o=uid=0,gid=11,rw,compress=zstd "
2+
## assert-last-key-contains Service ExecStart " --opt type=btrfs "
3+
## assert-last-key-contains Service ExecStart " --opt device=/dev/vda1 "
4+
## assert-last-key-contains Service ExecStart " --opt nocopy "
55

66
[Volume]
77
# Test usernames too

test/e2e/quadlet/ipv6.network

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
## assert-podman-args "--ipv6"
33
## assert-key-is Service Type exec
44
## assert-key-is Service RemainAfterExit no
5-
## assert-key-contains Service SyslogIdentifier "Modify %N"
5+
## assert-last-key-contains Service SyslogIdentifier "Modify %N"
66

77
[Network]
88
IPv6=yes

test/e2e/quadlet/label.volume

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
## assert-key-contains Service ExecStart " --label org.foo.Arg1=arg1 "
2-
## assert-key-contains Service ExecStart " --label org.foo.Arg2=arg2 "
3-
## assert-key-contains Service ExecStart " --label org.foo.Arg3=arg3 "
1+
## assert-last-key-contains Service ExecStart " --label org.foo.Arg1=arg1 "
2+
## assert-last-key-contains Service ExecStart " --label org.foo.Arg2=arg2 "
3+
## assert-last-key-contains Service ExecStart " --label org.foo.Arg3=arg3 "
44

55
[Volume]
66
Label=org.foo.Arg1=arg1

test/e2e/quadlet/mount.container

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
[Container]
22
Image=localhost/imagename
3+
## assert-has-key Unit RequiresMountsFor "/path/on/host"
34
## assert-podman-args-key-val "--mount" "," "type=bind,source=/path/on/host,destination=/path/in/container"
45
Mount=type=bind,source=/path/on/host,destination=/path/in/container
6+
## assert-has-key Unit RequiresMountsFor "\"/path/on/host\\x20with\\x20spaces\""
7+
## assert-podman-args-key-val "--mount" "," "type=bind,source=/path/on/host with spaces,dst=/path"
8+
Mount="type=bind,src=/path/on/host with spaces,dst=/path"
59
## assert-podman-args-key-val "--mount" "," "type=bind,source=/path/on/host,dst=/path/in/container,relabel=shared"
610
Mount=type=bind,src=/path/on/host,dst=/path/in/container,relabel=shared
711
## assert-podman-args-key-val "--mount" "," "type=bind,source=/path/on/host,dst=/path/in/container,relabel=shared,U=true"

test/e2e/quadlet/startwithpod.pod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
## assert-key-contains "Unit" "Wants" "startwithpod_yes.service"
2-
## assert-key-contains "Unit" "Before" "startwithpod_yes.service"
1+
## assert-last-key-contains "Unit" "Wants" "startwithpod_yes.service"
2+
## assert-last-key-contains "Unit" "Before" "startwithpod_yes.service"
33

4-
## assert-key-not-contains "Unit" "Wants" "startwithpod_no.service"
5-
## assert-key-not-contains "Unit" "Before" "startwithpod_no.service"
4+
## assert-last-key-not-contains "Unit" "Wants" "startwithpod_no.service"
5+
## assert-last-key-not-contains "Unit" "Before" "startwithpod_no.service"
66

77
[Pod]

test/e2e/quadlet/startwithpod_no.container

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# assert-key-contains "Unit" "After" "startwithpod-pod.service"
2-
# assert-key-contains "Unit" "BindsTo" "startwithpod-pod.service"
1+
# assert-last-key-contains "Unit" "After" "startwithpod-pod.service"
2+
# assert-last-key-contains "Unit" "BindsTo" "startwithpod-pod.service"
33

44
[Container]
55
Image=localhost/image

0 commit comments

Comments
 (0)