Skip to content

Commit da10ccd

Browse files
committed
Merge branch 'podman-v4-inspect' into 'main'
podman: Use podman inspect for v4 path See merge request bootc-org/podman-bootc-cli!52
2 parents b2337eb + ba11edd commit da10ccd

File tree

7 files changed

+121
-29
lines changed

7 files changed

+121
-29
lines changed

cmd/run.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,17 @@ func doRun(flags *cobra.Command, args []string) error {
7070
}
7171

7272
if machineInfo == nil {
73+
println(utils.PodmanMachineErrorMessage)
7374
return errors.New("rootful podman machine is required, please run 'podman machine init --rootful'")
7475
}
7576

7677
if !machineInfo.Rootful {
78+
println(utils.PodmanMachineErrorMessage)
7779
return errors.New("rootful podman machine is required, please run 'podman machine set --rootful'")
7880
}
7981

8082
if _, err := os.Stat(machineInfo.PodmanSocket); err != nil {
83+
println(utils.PodmanMachineErrorMessage)
8184
logrus.Errorf("podman machine socket is missing. Is podman machine running?\n%s", err)
8285
return err
8386
}
@@ -88,6 +91,7 @@ func doRun(flags *cobra.Command, args []string) error {
8891
machineInfo.SSHIdentityPath,
8992
true)
9093
if err != nil {
94+
println(utils.PodmanMachineErrorMessage)
9195
logrus.Errorf("failed to connect to the podman socket. Is podman machine running?\n%s", err)
9296
return err
9397
}

pkg/user/user.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,6 @@ func (u *User) SSHDir() string {
4747
return filepath.Join(u.HomeDir(), ".ssh")
4848
}
4949

50-
func (u *User) MachineSocketDir() string {
51-
return filepath.Join(u.HomeDir(), ".local/share/containers/podman/machine/qemu")
52-
}
53-
54-
func (u *User) MachineSocket() string {
55-
return filepath.Join(u.MachineSocketDir(), "podman.sock")
56-
}
57-
58-
func (u *User) MachineSshKeyPriv() string {
59-
return filepath.Join(u.SSHDir(), "podman-machine-default")
60-
}
61-
62-
func (u *User) MachineSshKeyPub() string {
63-
return filepath.Join(u.SSHDir(), "podman-machine-default.pub")
64-
}
65-
6650
func (u *User) ConfigDir() string {
6751
return filepath.Join(u.HomeDir(), config.ConfigDir)
6852
}

pkg/utils/errors.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package utils
2+
3+
const (
4+
PodmanMachineErrorMessage = `
5+
******************************************************************
6+
**** A rootful Podman machine is required to run podman-bootc ****
7+
******************************************************************
8+
`
9+
// PodmanMachineErrorMessage = "\n**** A rootful Podman machine is required to run podman-bootc ****\n"
10+
)

pkg/utils/podman.go

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package utils
22

33
import (
4+
"encoding/json"
45
"errors"
56
"fmt"
7+
"os/exec"
68
"podman-bootc/pkg/user"
9+
"strings"
710

811
"github.com/containers/podman/v5/pkg/machine"
912
"github.com/containers/podman/v5/pkg/machine/define"
@@ -68,11 +71,74 @@ func getMachineInfo() (*MachineInfo, error) {
6871

6972
// Just to support podman v4.9, it will be removed in the future
7073
func getPv4MachineInfo(user user.User) (*MachineInfo, error) {
71-
// Let's cheat and use hard-coded values for podman v4.
72-
// We do that because libpod doesn't work if we import both v4 and v5.
74+
//check if a default podman machine exists
75+
listCmd := exec.Command("podman", "machine", "list", "--format", "json")
76+
var listCmdOutput strings.Builder
77+
listCmd.Stdout = &listCmdOutput
78+
err := listCmd.Run()
79+
if err != nil {
80+
return nil, fmt.Errorf("failed to list podman machines: %w", err)
81+
}
82+
var machineList []MachineList
83+
err = json.Unmarshal([]byte(listCmdOutput.String()), &machineList)
84+
if err != nil {
85+
return nil, fmt.Errorf("failed to unmarshal podman machine inspect output: %w", err)
86+
}
87+
88+
var defaultMachineName string
89+
if len(machineList) == 0 {
90+
return nil, errors.New("no podman machine found")
91+
} else if len(machineList) == 1 {
92+
// if there is only one machine, use it as the default
93+
// afaict, podman will use a single machine as the default, even if Default is false
94+
// in the output of `podman machine list`
95+
if !machineList[0].Running {
96+
println(PodmanMachineErrorMessage)
97+
return nil, errors.New("the default podman machine is not running")
98+
}
99+
defaultMachineName = machineList[0].Name
100+
} else {
101+
foundDefaultMachine := false
102+
for _, machine := range machineList {
103+
if machine.Default {
104+
if !machine.Running {
105+
println(PodmanMachineErrorMessage)
106+
return nil, errors.New("the default podman machine is not running")
107+
}
108+
109+
foundDefaultMachine = true
110+
defaultMachineName = machine.Name
111+
}
112+
}
113+
114+
if !foundDefaultMachine {
115+
println(PodmanMachineErrorMessage)
116+
return nil, errors.New("a default podman machine is not running")
117+
}
118+
}
119+
120+
// check if the default podman machine is rootful
121+
inspectCmd := exec.Command("podman", "machine", "inspect", defaultMachineName)
122+
var inspectCmdOutput strings.Builder
123+
inspectCmd.Stdout = &inspectCmdOutput
124+
err = inspectCmd.Run()
125+
if err != nil {
126+
return nil, fmt.Errorf("failed to inspect podman machine: %w", err)
127+
}
128+
129+
var machineInspect []MachineInspect
130+
err = json.Unmarshal([]byte(inspectCmdOutput.String()), &machineInspect)
131+
if err != nil {
132+
return nil, fmt.Errorf("failed to unmarshal podman machine inspect output: %w", err)
133+
}
134+
135+
if len(machineInspect) == 0 {
136+
return nil, errors.New("no podman machine found")
137+
}
138+
73139
return &MachineInfo{
74-
PodmanSocket: user.MachineSocket(),
75-
SSHIdentityPath: user.MachineSshKeyPriv(),
76-
Rootful: true,
140+
PodmanSocket: machineInspect[0].ConnectionInfo.PodmanSocket.Path,
141+
SSHIdentityPath: machineInspect[0].SSHConfig.IdentityPath,
142+
Rootful: machineInspect[0].Rootful,
77143
}, nil
78144
}

pkg/utils/types.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package utils
2+
3+
type MachineInspect struct {
4+
ConnectionInfo ConnectionInfo `json:"ConnectionInfo"`
5+
SSHConfig SSHConfig `json:"SSHConfig"`
6+
Rootful bool `json:"Rootful"`
7+
}
8+
9+
type PodmanSocket struct {
10+
Path string `json:"Path"`
11+
}
12+
13+
type ConnectionInfo struct {
14+
PodmanSocket PodmanSocket `json:"PodmanSocket"`
15+
}
16+
17+
type SSHConfig struct {
18+
IdentityPath string `json:"IdentityPath"`
19+
}
20+
21+
type MachineList struct {
22+
Name string `json:"Name"`
23+
Running bool `json:"Running"`
24+
Default bool `json:"Default"`
25+
}

pkg/vm/vm_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ const (
4949
testLibvirtUri = "test:///default"
5050
)
5151

52+
var testUserSSHKey = filepath.Join(testUser.SSHDir(), "podman-machine-default")
53+
5254
var _ = BeforeSuite(func() {
5355
// populate the test user home directory.
5456
// This is most likely temporary. It enables the VM tests
@@ -58,13 +60,13 @@ var _ = BeforeSuite(func() {
5860
Expect(err).To(Not(HaveOccurred()))
5961
err = os.MkdirAll(testUser.SSHDir(), 0700)
6062
Expect(err).To(Not(HaveOccurred()))
61-
err = os.WriteFile(testUser.MachineSshKeyPriv(), []byte(""), 0700)
63+
err = os.WriteFile(testUserSSHKey, []byte(""), 0700)
6264
Expect(err).To(Not(HaveOccurred()))
63-
err = os.WriteFile(testUser.MachineSshKeyPub(), []byte(""), 0700)
65+
err = os.WriteFile(testUserSSHKey + ".pub", []byte(""), 0700)
6466
Expect(err).To(Not(HaveOccurred()))
6567
err = os.MkdirAll(filepath.Join(testUser.HomeDir(), ".local/share/containers/podman/machine/qemu"), 0700)
6668
Expect(err).To(Not(HaveOccurred()))
67-
err = os.WriteFile(testUser.MachineSocket(), []byte(""), 0700)
69+
err = os.WriteFile(filepath.Join(testUser.HomeDir(), ".local/share/containers/podman/machine/qemu/podman.sock"), []byte(""), 0700)
6870
Expect(err).To(Not(HaveOccurred()))
6971
})
7072

@@ -97,7 +99,7 @@ func runTestVM(bootcVM vm.BootcVM) {
9799
Cmd: []string{},
98100
RemoveVm: false,
99101
Background: false,
100-
SSHIdentity: testUser.MachineSshKeyPriv(),
102+
SSHIdentity: testUserSSHKey,
101103
})
102104
Expect(err).To(Not(HaveOccurred()))
103105

@@ -206,7 +208,7 @@ var _ = Describe("VM", func() {
206208
Expect(vmList[0]).To(Equal(vm.BootcVMConfig{
207209
Id: testImageID[:12],
208210
SshPort: 22,
209-
SshIdentity: testUser.MachineSshKeyPriv(),
211+
SshIdentity: testUserSSHKey,
210212
RepoTag: testRepoTag,
211213
Created: "About a minute ago",
212214
DiskSize: "10.7GB",
@@ -235,7 +237,7 @@ var _ = Describe("VM", func() {
235237
Expect(vmList).To(ContainElement(vm.BootcVMConfig{
236238
Id: testImageID[:12],
237239
SshPort: 22,
238-
SshIdentity: testUser.MachineSshKeyPriv(),
240+
SshIdentity: testUserSSHKey,
239241
RepoTag: testRepoTag,
240242
Created: "About a minute ago",
241243
DiskSize: "10.7GB",
@@ -245,7 +247,7 @@ var _ = Describe("VM", func() {
245247
Expect(vmList).To(ContainElement(vm.BootcVMConfig{
246248
Id: id2[:12],
247249
SshPort: 22,
248-
SshIdentity: testUser.MachineSshKeyPriv(),
250+
SshIdentity: testUserSSHKey,
249251
RepoTag: testRepoTag,
250252
Created: "About a minute ago",
251253
DiskSize: "10.7GB",
@@ -255,7 +257,7 @@ var _ = Describe("VM", func() {
255257
Expect(vmList).To(ContainElement(vm.BootcVMConfig{
256258
Id: id3[:12],
257259
SshPort: 22,
258-
SshIdentity: testUser.MachineSshKeyPriv(),
260+
SshIdentity: testUserSSHKey,
259261
RepoTag: testRepoTag,
260262
Created: "About a minute ago",
261263
DiskSize: "10.7GB",

podman-bootc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func cleanup() {
5252
machineInfo.SSHIdentityPath,
5353
true)
5454
if err != nil {
55+
println(utils.PodmanMachineErrorMessage)
5556
logrus.Errorf("failed to connect to the podman socket. Is podman machine running?\n%s", err)
5657
os.Exit(1)
5758
}

0 commit comments

Comments
 (0)