From 597ccff0bca2f4bd53655f6874656be7756a328e Mon Sep 17 00:00:00 2001 From: Jake Correnti Date: Tue, 1 Aug 2023 19:49:26 -0400 Subject: [PATCH] Move some logic of `setRootful` to a common file Moves most of the logic of `setRootful` to the common file `pkg/machine/machine_common.go`. Signed-off-by: Jake Correnti --- pkg/machine/hyperv/machine.go | 14 +------------- pkg/machine/machine_common.go | 21 +++++++++++++++++++++ pkg/machine/qemu/machine.go | 14 +------------- pkg/machine/wsl/machine.go | 14 +------------- 4 files changed, 24 insertions(+), 39 deletions(-) diff --git a/pkg/machine/hyperv/machine.go b/pkg/machine/hyperv/machine.go index 185efd6561..677cb16b8b 100644 --- a/pkg/machine/hyperv/machine.go +++ b/pkg/machine/hyperv/machine.go @@ -692,22 +692,10 @@ func (m *HyperVMachine) writeConfig() error { } func (m *HyperVMachine) setRootful(rootful bool) error { - changeCon, err := machine.AnyConnectionDefault(m.Name, m.Name+"-root") - if err != nil { + if err := machine.SetRootful(rootful, m.Name, m.Name+"-root"); err != nil { return err } - if changeCon { - newDefault := m.Name - if rootful { - newDefault += "-root" - } - err := machine.ChangeDefault(newDefault) - if err != nil { - return err - } - } - m.HostUser.Modified = true return nil } diff --git a/pkg/machine/machine_common.go b/pkg/machine/machine_common.go index 10f76fe58d..a447de8f12 100644 --- a/pkg/machine/machine_common.go +++ b/pkg/machine/machine_common.go @@ -122,3 +122,24 @@ func WaitAPIAndPrintInfo(forwardState APIForwardingState, name, helper, forwardS } } } + +// SetRootful modifies the machine's default connection to be either rootful or +// rootless +func SetRootful(rootful bool, name, rootfulName string) error { + changeCon, err := AnyConnectionDefault(name, rootfulName) + if err != nil { + return err + } + + if changeCon { + newDefault := name + if rootful { + newDefault += "-root" + } + err := ChangeDefault(newDefault) + if err != nil { + return err + } + } + return nil +} diff --git a/pkg/machine/qemu/machine.go b/pkg/machine/qemu/machine.go index 9a3f2cf68f..67aac835ea 100644 --- a/pkg/machine/qemu/machine.go +++ b/pkg/machine/qemu/machine.go @@ -1607,22 +1607,10 @@ func (v *MachineVM) resizeDisk(diskSize uint64, oldSize uint64) error { } func (v *MachineVM) setRootful(rootful bool) error { - changeCon, err := machine.AnyConnectionDefault(v.Name, v.Name+"-root") - if err != nil { + if err := machine.SetRootful(rootful, v.Name, v.Name+"-root"); err != nil { return err } - if changeCon { - newDefault := v.Name - if rootful { - newDefault += "-root" - } - err := machine.ChangeDefault(newDefault) - if err != nil { - return err - } - } - v.HostUser.Modified = true return nil } diff --git a/pkg/machine/wsl/machine.go b/pkg/machine/wsl/machine.go index 1de1d2df95..2ff886ed2e 100644 --- a/pkg/machine/wsl/machine.go +++ b/pkg/machine/wsl/machine.go @@ -1525,22 +1525,10 @@ func getMem(vm *MachineVM) (uint64, error) { } func (v *MachineVM) setRootful(rootful bool) error { - changeCon, err := machine.AnyConnectionDefault(v.Name, v.Name+"-root") - if err != nil { + if err := machine.SetRootful(rootful, v.Name, v.Name+"-root"); err != nil { return err } - if changeCon { - newDefault := v.Name - if rootful { - newDefault += "-root" - } - err := machine.ChangeDefault(newDefault) - if err != nil { - return err - } - } - dist := toDist(v.Name) return v.setupPodmanDockerSock(dist, rootful) }