From 553c4a16d5387c0c582324a92cbe22cd0c0c2519 Mon Sep 17 00:00:00 2001 From: Brent Baude Date: Thu, 30 Oct 2025 07:50:08 -0500 Subject: [PATCH] Fix use of duplicate machine names A condition was changed in the refgactor of init where duplicate names would be allowed but no machine was created. Duplicate names are not permitted and should return an error. Signed-off-by: Brent Baude --- cmd/podman/machine/init.go | 5 ++++- pkg/machine/e2e/init_test.go | 7 +++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/cmd/podman/machine/init.go b/cmd/podman/machine/init.go index eeef6aa838..acae358c1a 100644 --- a/cmd/podman/machine/init.go +++ b/cmd/podman/machine/init.go @@ -208,7 +208,10 @@ func initMachine(cmd *cobra.Command, args []string) error { // Check if machine already exists var errNotExists *define.ErrVMDoesNotExist _, _, err := shim.VMExists(initOpts.Name) - // errors.As checks for nil so safe to use. + // if nil, means we found a vm and need to reject it by name + if err == nil { + return &define.ErrVMAlreadyExists{Name: initOpts.Name} + } if !errors.As(err, &errNotExists) { return err } diff --git a/pkg/machine/e2e/init_test.go b/pkg/machine/e2e/init_test.go index 4c6c1fe884..132fbd9fd5 100644 --- a/pkg/machine/e2e/init_test.go +++ b/pkg/machine/e2e/init_test.go @@ -117,6 +117,11 @@ var _ = Describe("podman machine init", func() { Expect(testMachine.Resources.CPUs).To(Equal(uint64(cpus))) Expect(testMachine.Resources.Memory).To(BeEquivalentTo(uint64(2048))) } + // creating a new VM with the same name must fail + repeatSession, err := mb.setCmd(i.withImage(mb.imagePath)).run() + Expect(err).ToNot(HaveOccurred()) + Expect(repeatSession).To(Exit(125)) + Expect(repeatSession.errorToString()).To(ContainSubstring(fmt.Sprintf("Error: machine %q already exists", mb.names[0]))) }) It("run playbook", func() { @@ -655,7 +660,6 @@ var _ = Describe("podman machine init", func() { session, err := mb.setName(machineName).setCmd(i.withImage(mb.imagePath).withProvider(providerOverride)).run() Expect(err).ToNot(HaveOccurred()) Expect(session.errorToString()).To(ContainSubstring(fmt.Sprintf("unsupported provider %q", providerOverride))) - }) It("machine init --provider", func() { @@ -699,7 +703,6 @@ var _ = Describe("podman machine init", func() { Expect(p).To(Equal(l.VMType)) } }) - }) var p4Config = []byte(`{