From 24deec835ca2367bee8fca2bb7898aedfbe1db0e Mon Sep 17 00:00:00 2001 From: Jake Correnti Date: Thu, 12 Sep 2024 15:03:03 -0400 Subject: [PATCH] Update connection on removal Modify `RemoveConnections` to verify the new default system connection's rootful state matches the rootful-ness of the podman machine it is associated with. Signed-off-by: Jake Correnti --- cmd/podman/system/reset_machine.go | 7 ++++++- pkg/machine/connection/connection.go | 30 ++++++++++++++++++---------- pkg/machine/shim/host.go | 21 ++++++++++++++++--- pkg/machine/vmconfigs/machine.go | 4 ++-- 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/cmd/podman/system/reset_machine.go b/cmd/podman/system/reset_machine.go index 35430dfb7d..2c49e1758a 100644 --- a/cmd/podman/system/reset_machine.go +++ b/cmd/podman/system/reset_machine.go @@ -30,6 +30,11 @@ func resetMachine() error { logrus.Errorf("unable to load machines: %q", err) } + machines, err := p.GetAllMachinesAndRootfulness() + if err != nil { + return err + } + for _, mc := range mcs { state, err := provider.State(mc, false) if err != nil { @@ -42,7 +47,7 @@ func resetMachine() error { } } - if err := connection.RemoveConnections(mc.Name, mc.Name+"-root"); err != nil { + if err := connection.RemoveConnections(machines, mc.Name, mc.Name+"-root"); err != nil { logrus.Error(err) } diff --git a/pkg/machine/connection/connection.go b/pkg/machine/connection/connection.go index 8ed9fdafc9..9c77ddeeff 100644 --- a/pkg/machine/connection/connection.go +++ b/pkg/machine/connection/connection.go @@ -75,26 +75,34 @@ func UpdateConnectionPairPort(name string, port, uid int, remoteUsername string, // Returns true if it modified the default func UpdateConnectionIfDefault(rootful bool, name, rootfulName string) error { return config.EditConnectionConfig(func(cfg *config.ConnectionsFile) error { - if name == cfg.Connection.Default && rootful { - cfg.Connection.Default = rootfulName - } else if rootfulName == cfg.Connection.Default && !rootful { - cfg.Connection.Default = name - } + updateConnection(cfg, rootful, name, rootfulName) return nil }) } -func RemoveConnections(names ...string) error { +func updateConnection(cfg *config.ConnectionsFile, rootful bool, name, rootfulName string) { + if name == cfg.Connection.Default && rootful { + cfg.Connection.Default = rootfulName + } else if rootfulName == cfg.Connection.Default && !rootful { + cfg.Connection.Default = name + } +} + +func RemoveConnections(machines map[string]bool, names ...string) error { var dest config.Destination var service string if err := config.EditConnectionConfig(func(cfg *config.ConnectionsFile) error { - err := setNewDefaultConnection(cfg, &dest, &service, names...) - if err != nil { - return err - } + err := setNewDefaultConnection(cfg, &dest, &service, names...) + if err != nil { + return err + } - return nil + rootful, ok := machines[service] + if dest.IsMachine && ok { + updateConnection(cfg, rootful, service, service+"-root") + } + return nil }); err != nil { return err } diff --git a/pkg/machine/shim/host.go b/pkg/machine/shim/host.go index e1c1834f60..5664d6baf4 100644 --- a/pkg/machine/shim/host.go +++ b/pkg/machine/shim/host.go @@ -16,6 +16,7 @@ import ( "github.com/containers/podman/v5/pkg/machine/env" "github.com/containers/podman/v5/pkg/machine/ignition" "github.com/containers/podman/v5/pkg/machine/lock" + "github.com/containers/podman/v5/pkg/machine/provider" "github.com/containers/podman/v5/pkg/machine/proxyenv" "github.com/containers/podman/v5/pkg/machine/vmconfigs" "github.com/containers/podman/v5/utils" @@ -230,7 +231,11 @@ func Init(opts machineDefine.InitOptions, mp vmconfigs.VMProvider) error { } cleanup := func() error { - return connection.RemoveConnections(mc.Name, mc.Name+"-root") + machines, err := provider.GetAllMachinesAndRootfulness() + if err != nil { + return err + } + return connection.RemoveConnections(machines, mc.Name, mc.Name+"-root") } callbackFuncs.Add(cleanup) @@ -580,7 +585,12 @@ func Remove(mc *vmconfigs.MachineConfig, mp vmconfigs.VMProvider, dirs *machineD } } - rmFiles, genericRm, err := mc.Remove(opts.SaveIgnition, opts.SaveImage) + machines, err := provider.GetAllMachinesAndRootfulness() + if err != nil { + return err + } + + rmFiles, genericRm, err := mc.Remove(machines, opts.SaveIgnition, opts.SaveImage) if err != nil { return err } @@ -655,12 +665,17 @@ func Reset(mps []vmconfigs.VMProvider, opts machine.ResetOptions) error { } removeDirs = append(removeDirs, d) + machines, err := provider.GetAllMachinesAndRootfulness() + if err != nil { + return err + } + for _, mc := range mcs { err := Stop(mc, p, d, true) if err != nil { resetErrors = multierror.Append(resetErrors, err) } - _, genericRm, err := mc.Remove(false, false) + _, genericRm, err := mc.Remove(machines, false, false) if err != nil { resetErrors = multierror.Append(resetErrors, err) } diff --git a/pkg/machine/vmconfigs/machine.go b/pkg/machine/vmconfigs/machine.go index bbd0f6cc9b..f47fce845f 100644 --- a/pkg/machine/vmconfigs/machine.go +++ b/pkg/machine/vmconfigs/machine.go @@ -153,7 +153,7 @@ func (mc *MachineConfig) updateLastBoot() error { //nolint:unused return mc.Write() } -func (mc *MachineConfig) Remove(saveIgnition, saveImage bool) ([]string, func() error, error) { +func (mc *MachineConfig) Remove(machines map[string]bool, saveIgnition, saveImage bool) ([]string, func() error, error) { ignitionFile, err := mc.IgnitionFile() if err != nil { return nil, nil, err @@ -195,7 +195,7 @@ func (mc *MachineConfig) Remove(saveIgnition, saveImage bool) ([]string, func() mcRemove := func() error { var errs []error - if err := connection.RemoveConnections(mc.Name, mc.Name+"-root"); err != nil { + if err := connection.RemoveConnections(machines, mc.Name, mc.Name+"-root"); err != nil { errs = append(errs, err) }