Skip to content

Commit 377a27b

Browse files
committed
Fix machine init failure with read-only disk image
1 parent 12c6562 commit 377a27b

3 files changed

Lines changed: 39 additions & 2 deletions

File tree

pkg/machine/e2e/init_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package e2e_test
33
import (
44
"fmt"
55
"os"
6+
"os/exec"
67
"path/filepath"
78
"runtime"
89
"strconv"
@@ -733,6 +734,21 @@ var _ = Describe("podman machine init", func() {
733734
Expect(p).To(Equal(l.VMType))
734735
}
735736
})
737+
738+
It("init with read-only image should succeed", func() {
739+
// Step 1: create temp image
740+
img := filepath.Join(GinkgoT().TempDir(), "test.qcow2")
741+
742+
// Step 2: copy existing image as read-only (use install)
743+
exec.Command("install", "-m", "444", mb.imagePath, img).Run()
744+
745+
// Step 3: run podman machine init
746+
i := new(initMachine)
747+
session, err := mb.setCmd(i.withImage(img)).run()
748+
749+
Expect(err).To(BeNil())
750+
Expect(session).To(Exit(0))
751+
})
736752
})
737753

738754
var p4Config = []byte(`{

pkg/machine/stdpull/local.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package stdpull
22

33
import (
4+
"fmt"
5+
"os"
6+
47
"github.com/containers/podman/v6/pkg/machine/compression"
58
"github.com/containers/podman/v6/pkg/machine/define"
69
"github.com/sirupsen/logrus"
@@ -26,5 +29,14 @@ func (s *StdDiskPull) Get() error {
2629
return err
2730
}
2831
logrus.Debugf("decompressing (if needed) %s to %s", s.inputPath.GetPath(), s.finalPath.GetPath())
29-
return compression.Decompress(s.inputPath, s.finalPath.GetPath())
32+
if err := compression.Decompress(s.inputPath, s.finalPath.GetPath()); err != nil {
33+
return err
34+
}
35+
36+
// Ensure image is writable
37+
if err := os.Chmod(s.finalPath.GetPath(), 0600); err != nil {
38+
return fmt.Errorf("failed to set permissions on machine image: %w", err)
39+
}
40+
return nil
41+
3042
}

pkg/machine/stdpull/url.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,16 @@ func (d *DiskFromURL) Get() error {
7676
}
7777

7878
logrus.Debugf("decompressing (if needed) %s to %s", d.tempLocation.GetPath(), d.finalPath.GetPath())
79-
return compression.Decompress(d.tempLocation, d.finalPath.GetPath())
79+
if err := compression.Decompress(d.tempLocation, d.finalPath.GetPath()); err != nil {
80+
return err
81+
}
82+
83+
// Ensure image is writable
84+
if err := os.Chmod(d.finalPath.GetPath(), 0600); err != nil {
85+
return fmt.Errorf("failed to set permissions on pulled image %s: %w", d.finalPath.GetPath(), err)
86+
}
87+
88+
return nil
8089
}
8190

8291
func (d *DiskFromURL) pull() error {

0 commit comments

Comments
 (0)