Skip to content

Commit

Permalink
Merge pull request #23948 from l0rd/build-with-volumes-on-windows
Browse files Browse the repository at this point in the history
Convert windows paths in volume arg of the build command
  • Loading branch information
openshift-merge-bot[bot] authored Sep 16, 2024
2 parents 156efe5 + 1857d7e commit 47b85af
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
23 changes: 22 additions & 1 deletion pkg/bindings/images/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@ func convertAdditionalBuildContexts(additionalBuildContexts map[string]*define.A
}
}

// convertVolumeSrcPath converts windows paths in the HOST-DIR part of a volume
// into the corresponding path in the default Windows machine.
// (e.g. C:\test:/src/docs ==> /mnt/c/test:/src/docs).
// If any error occurs while parsing the volume string, the original volume
// string is returned.
func convertVolumeSrcPath(volume string) string {
splitVol := specgen.SplitVolumeString(volume)
if len(splitVol) < 2 || len(splitVol) > 3 {
return volume
}
convertedSrcPath, err := specgen.ConvertWinMountPath(splitVol[0])
if err != nil {
return volume
}
if len(splitVol) == 2 {
return convertedSrcPath + ":" + splitVol[1]
} else {
return convertedSrcPath + ":" + splitVol[1] + ":" + splitVol[2]
}
}

// Build creates an image using a containerfile reference
func Build(ctx context.Context, containerFiles []string, options types.BuildOptions) (*types.BuildReport, error) {
if options.CommonBuildOpts == nil {
Expand Down Expand Up @@ -352,7 +373,7 @@ func Build(ctx context.Context, containerFiles []string, options types.BuildOpti
}

for _, volume := range options.CommonBuildOpts.Volumes {
params.Add("volume", volume)
params.Add("volume", convertVolumeSrcPath(volume))
}

for _, group := range options.GroupAdd {
Expand Down
8 changes: 8 additions & 0 deletions pkg/machine/e2e/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ var _ = Describe("run basic podman commands", func() {
runAlp, err := mb.setCmd(bm.withPodmanCommand([]string{"run", "-v", tDir + ":/test:Z", "quay.io/libpod/alpine_nginx", "ls", "/test/attr-test-file"})).run()
Expect(err).ToNot(HaveOccurred())
Expect(runAlp).To(Exit(0))

// Test build with --volume option
cf := filepath.Join(tDir, "Containerfile")
err = os.WriteFile(cf, []byte("FROM quay.io/libpod/alpine_nginx\nRUN ls /test/attr-test-file\n"), 0o644)
Expect(err).ToNot(HaveOccurred())
build, err := mb.setCmd(bm.withPodmanCommand([]string{"build", "-t", name, "-v", tDir + ":/test", tDir})).run()
Expect(err).ToNot(HaveOccurred())
Expect(build).To(Exit(0))
})

It("Volume should be virtiofs", func() {
Expand Down

1 comment on commit 47b85af

@packit-as-a-service
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

podman-next COPR build failed. @containers/packit-build please check.

Please sign in to comment.