Skip to content

Commit 400a9a5

Browse files
Merge pull request #25722 from l0rd/fix-hyperv-volumes-with-space
Fix machines failing to start when a volume's path contains spaces
2 parents 5e8e829 + 8e6ecb9 commit 400a9a5

File tree

5 files changed

+25
-15
lines changed

5 files changed

+25
-15
lines changed

pkg/machine/e2e/init_test.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,6 @@ var _ = Describe("podman machine init", func() {
299299
})
300300

301301
It("machine init with volume", func() {
302-
if testProvider.VMType() == define.HyperVVirt {
303-
Skip("volumes are not supported on hyperv yet")
304-
}
305302
skipIfWSL("WSL volumes are much different. This test will not pass as is")
306303

307304
tmpDir, err := os.MkdirTemp("", "")
@@ -312,9 +309,16 @@ var _ = Describe("podman machine init", func() {
312309
mount := tmpDir + ":/very-long-test-mount-dir-path-more-than-thirty-six-bytes"
313310
defer func() { _ = utils.GuardedRemoveAll(tmpDir) }()
314311

312+
tmpDirWithSpaces, err := os.MkdirTemp("", "with spaces")
313+
Expect(err).ToNot(HaveOccurred())
314+
_, err = os.CreateTemp(tmpDirWithSpaces, "example")
315+
Expect(err).ToNot(HaveOccurred())
316+
mountWithSpaces := tmpDirWithSpaces + ":/host folder"
317+
defer func() { _ = utils.GuardedRemoveAll(tmpDirWithSpaces) }()
318+
315319
name := randomString()
316320
i := new(initMachine)
317-
session, err := mb.setName(name).setCmd(i.withImage(mb.imagePath).withVolume(mount).withNow()).run()
321+
session, err := mb.setName(name).setCmd(i.withImage(mb.imagePath).withVolume(mount).withVolume(mountWithSpaces).withNow()).run()
318322
Expect(err).ToNot(HaveOccurred())
319323
Expect(session).To(Exit(0))
320324

@@ -323,6 +327,11 @@ var _ = Describe("podman machine init", func() {
323327
Expect(err).ToNot(HaveOccurred())
324328
Expect(sshSession).To(Exit(0))
325329
Expect(sshSession.outputToString()).To(ContainSubstring("example"))
330+
331+
sshSession, err = mb.setName(name).setCmd(ssh.withSSHCommand([]string{"ls \"/host folder\""})).run()
332+
Expect(err).ToNot(HaveOccurred())
333+
Expect(sshSession).To(Exit(0))
334+
Expect(sshSession.outputToString()).To(ContainSubstring("example"))
326335
})
327336

328337
It("machine init with ignition path", func() {

pkg/machine/hyperv/stubber.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -420,10 +420,11 @@ func (h HyperVStubber) PostStartNetworking(mc *vmconfigs.MachineConfig, noInfo b
420420
fsCmd := exec.Command(executable, p9ServerArgs...)
421421

422422
if logrus.IsLevelEnabled(logrus.DebugLevel) {
423-
err = logCommandToFile(fsCmd, "podman-machine-server9.log")
423+
log, err := logCommandToFile(fsCmd, "podman-machine-server9.log")
424424
if err != nil {
425425
return err
426426
}
427+
defer log.Close()
427428
}
428429

429430
err = fsCmd.Start()
@@ -501,23 +502,22 @@ func removeIgnitionFromRegistry(vm *hypervctl.VirtualMachine) error {
501502
return nil
502503
}
503504

504-
func logCommandToFile(c *exec.Cmd, filename string) error {
505+
func logCommandToFile(c *exec.Cmd, filename string) (*os.File, error) {
505506
dir, err := env.GetDataDir(define.HyperVVirt)
506507
if err != nil {
507-
return fmt.Errorf("obtain machine dir: %w", err)
508+
return nil, fmt.Errorf("obtain machine dir: %w", err)
508509
}
509510
path := filepath.Join(dir, filename)
510511
logrus.Infof("Going to log to %s", path)
511512
log, err := os.Create(path)
512513
if err != nil {
513-
return fmt.Errorf("create log file: %w", err)
514+
return nil, fmt.Errorf("create log file: %w", err)
514515
}
515-
defer log.Close()
516516

517517
c.Stdout = log
518518
c.Stderr = log
519519

520-
return nil
520+
return log, nil
521521
}
522522

523523
const hyperVVsockNMConnection = `

pkg/machine/hyperv/volumes.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"path"
9+
"strconv"
910
"strings"
1011

1112
"github.com/containers/podman/v5/pkg/machine"
@@ -48,7 +49,7 @@ func startShares(mc *vmconfigs.MachineConfig) error {
4849
if requiresChattr {
4950
args = append(args, "sudo", "chattr", "-i", "/", "; ")
5051
}
51-
args = append(args, "sudo", "mkdir", "-p", cleanTarget, "; ")
52+
args = append(args, "sudo", "mkdir", "-p", strconv.Quote(cleanTarget), "; ")
5253
if requiresChattr {
5354
args = append(args, "sudo", "chattr", "+i", "/", "; ")
5455
}
@@ -61,7 +62,7 @@ func startShares(mc *vmconfigs.MachineConfig) error {
6162
if mount.VSockNumber == nil {
6263
return errors.New("cannot start 9p shares with undefined vsock number")
6364
}
64-
args = append(args, "machine", "client9p", fmt.Sprintf("%d", *mount.VSockNumber), mount.Target)
65+
args = append(args, "machine", "client9p", fmt.Sprintf("%d", *mount.VSockNumber), strconv.Quote(mount.Target))
6566

6667
if err := machine.CommonSSH(mc.SSH.RemoteUsername, mc.SSH.IdentityPath, mc.Name, mc.SSH.Port, args); err != nil {
6768
return err

pkg/machine/qemu/stubber.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ func (q *QEMUStubber) MountVolumesToVM(mc *vmconfigs.MachineConfig, quiet bool)
349349
if !strings.HasPrefix(mount.Target, "/home") && !strings.HasPrefix(mount.Target, "/mnt") {
350350
args = append(args, "sudo", "chattr", "-i", "/", ";")
351351
}
352-
args = append(args, "sudo", "mkdir", "-p", mount.Target)
352+
args = append(args, "sudo", "mkdir", "-p", strconv.Quote(mount.Target))
353353
if !strings.HasPrefix(mount.Target, "/home") && !strings.HasPrefix(mount.Target, "/mnt") {
354354
args = append(args, ";", "sudo", "chattr", "+i", "/", ";")
355355
}
@@ -362,7 +362,7 @@ func (q *QEMUStubber) MountVolumesToVM(mc *vmconfigs.MachineConfig, quiet bool)
362362
// in other words we don't want to make people unnecessarily reprovision their machines
363363
// to upgrade from 9p to virtiofs.
364364
mountOptions := []string{"-t", "virtiofs"}
365-
mountOptions = append(mountOptions, []string{mount.Tag, mount.Target}...)
365+
mountOptions = append(mountOptions, []string{mount.Tag, strconv.Quote(mount.Target)}...)
366366
mountFlags := fmt.Sprintf("context=\"%s\"", machine.NFSSELinuxContext)
367367
if mount.ReadOnly {
368368
mountFlags += ",ro"

pkg/systemd/parser/split.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ func escapeString(escaped *strings.Builder, word string, isPath bool) {
473473
case '\\':
474474
escaped.WriteString("\\\\")
475475
case ' ':
476-
escaped.WriteString(" ")
476+
escaped.WriteString("\\x20")
477477
case '"':
478478
escaped.WriteString("\\\"")
479479
case '\'':

0 commit comments

Comments
 (0)