Skip to content

Propose running init/rm command on hyperv machine in elevated mode when required#27932

Merged
l0rd merged 3 commits into
podman-container-tools:mainfrom
lstocchi:i27627
Apr 10, 2026
Merged

Propose running init/rm command on hyperv machine in elevated mode when required#27932
l0rd merged 3 commits into
podman-container-tools:mainfrom
lstocchi:i27627

Conversation

@lstocchi

@lstocchi lstocchi commented Jan 21, 2026

Copy link
Copy Markdown
Contributor

This PR adds automatic UAC elevation prompts for HyperV machine when administrator privileges are required. Similar to what happens on WSL when we need to install WSL features.
Previously, users had to manually run Podman as administrator when creating the first machine or removing the last machine, which requires Windows Registry modifications.

it fixes #27627

Checklist

Ensure you have completed the following checklist for your pull request to be reviewed:

  • Certify you wrote the patch or otherwise have the right to pass it on as an open-source patch by signing all
    commits. (git commit -s). (If needed, use git commit -s --amend). The author email must match
    the sign-off email address. See CONTRIBUTING.md
    for more information.
  • Referenced issues using Fixes: #00000 in commit message (if applicable)
  • Tests have been added/updated (or no tests are needed)
  • Documentation has been updated (or no documentation changes are needed)
  • All commits pass make validatepr (format/lint checks)
  • Release note entered in the section below (or None if no user-facing changes)

Does this PR introduce a user-facing change?

The `podman machine init` and `podman machine rm` commands on Windows using the HyperV provider now automatically prompt for UAC elevation when administrator privileges are required, eliminating the need to manually run Podman as administrator for these operations.

@packit-as-a-service

Copy link
Copy Markdown

[NON-BLOCKING] Packit jobs failed. @containers/packit-build please check. Everyone else, feel free to ignore.

@lstocchi

Copy link
Copy Markdown
Contributor Author

@l0rd related to this there are the start/stop actions on legacy machines. User should execute them as admin. Should we update start/stop and "mess up" the code a bit for a use case which is marginal? Or, as they are legacy stuff, it is ok for the user to run podman as admin if they want to work on old machines?

@l0rd

l0rd commented Jan 21, 2026

Copy link
Copy Markdown
Contributor

@l0rd related to this there are the start/stop actions on legacy machines. User should execute them as admin. Should we update start/stop and "mess up" the code a bit for a use case which is marginal? Or, as they are legacy stuff, it is ok for the user to run podman as admin if they want to work on old machines?

I would not care about legacy machines. There is no request for that.

@l0rd l0rd added the No New Tests Allow PR to proceed without adding regression tests label Jan 21, 2026
@l0rd

l0rd commented Jan 21, 2026

Copy link
Copy Markdown
Contributor

Adding label No New Tests as there is no way to automatically test the e2e scenario right now.

@lstocchi

Copy link
Copy Markdown
Contributor Author

I would not care about legacy machines. There is no request for that.

Ah great, so this can be tested then

@lstocchi lstocchi marked this pull request as ready for review January 23, 2026 09:11

@l0rd l0rd left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @lstocchi for this PR. It looks good except for a couple of things when machine init is re-executed:

  • The image gets pulled and extracted twice (these steps should be skipped when --reexec)
  • The message that informs the user that the command completed successfully, and how to start the machine, gets lost

I have added a few minor comments too.

Comment thread pkg/machine/hyperv/stubber.go Outdated
Comment thread pkg/machine/hyperv/stubber.go Outdated
Comment thread pkg/machine/hyperv/stubber.go Outdated
Comment thread pkg/machine/hyperv/stubber.go Outdated
@lstocchi

Copy link
Copy Markdown
Contributor Author

Thank you @lstocchi for this PR. It looks good except for a couple of things when machine init is re-executed:

  • The image gets pulled and extracted twice (these steps should be skipped when --reexec)
  • The message that informs the user that the command completed successfully, and how to start the machine, gets lost

I have added a few minor comments too.

Updated. Good point for the image pulled twice 👍
My only concern is that if the elevated process fails the user does not see a user-friendly error message but ShellExecuteExW does not allow to return it afaik

@l0rd

l0rd commented Jan 26, 2026

Copy link
Copy Markdown
Contributor

My only concern is that if the elevated process fails the user does not see a user-friendly error message but ShellExecuteExW does not allow to return it afaik

What about using the mechanism used for the WSL install with an output file created and printed in case of error?

@lstocchi lstocchi force-pushed the i27627 branch 2 times, most recently from 058ff14 to 958a6e9 Compare January 27, 2026 14:50
@lstocchi

Copy link
Copy Markdown
Contributor Author

What about using the mechanism used for the WSL install with an output file created and printed in case of error?

Updated👍

@github-actions

Copy link
Copy Markdown

A friendly reminder that this PR had no activity for 30 days.

@l0rd l0rd left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lstocchi I have tested again, and the PR now works fine. Thank you. I have added a few notes/questions.

Comment thread cmd/podman/machine/init.go
Comment thread pkg/machine/shim/host.go Outdated
Comment on lines +79 to +81
// Do not clean up on relaunch: the elevated child process
// completed init successfully and is using the resources
// (e.g. the disk image) that cleanup would remove.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

go callbackFuncs.CleanOnSignal() has the same problem: when a user hits CTRL+C, then the callbackfuncs.Clean() will be invoked for the original, non-privileged, init. What about ensuring that the cleanup is done by the same process that performed the action (created the lock, downloaded the image, etc.)?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad. The error cleanup is skipped only if the error is ErrRelaunchSucceed because that's not really an error.

lpFile: uintptr(unsafe.Pointer(exe)),
lpParameters: uintptr(unsafe.Pointer(arg)),
lpDirectory: uintptr(unsafe.Pointer(cwd)),
nShow: syscall.SW_SHOWNORMAL,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about SW_HIDE? Opening another window may annoy the user. This code already existed, but it may still be better to hide it. What do you think?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After testing, we decided to keep SW_SHOWNORMAL. Otherwise, commands that required an interaction (such as podman machine rm) would hang forever.

@lstocchi lstocchi force-pushed the i27627 branch 2 times, most recently from cd1110f to 9b00d75 Compare April 3, 2026 14:05
@lstocchi

lstocchi commented Apr 3, 2026

Copy link
Copy Markdown
Contributor Author

@l0rd it should be ready. To summarize what are the new changes we agreed in the call.

  1. the callbackFuncs to delete the image has been moved inside the if !opts.reExec clause so the process that download the image also delete it
  2. ErrRelaunchAttempt has been renamed ErrLaunchSucceeded and all comments where it is used updated to avoid confusion

lstocchi added 2 commits April 3, 2026 18:37
This commit adds automatic UAC elevation prompts for HyperV machine
init/rm actions when administrator privileges are required.
Previously, users had to manually run Podman as administrator
when creating the first machine or removing the last machine, which
requires Windows Registry modifications.

When the HyperV command gets relaunched as elevated, the error of the
elevated process is saved on a file to be displayed by the caller. The
implementation is the same as that used by WSL.

Signed-off-by: lstocchi <lstocchi@redhat.com>
…ror handling

The old ErrRelaunchAttempt name was ambiguous — it reads as though the
relaunch attempt failed, when it actually signals success. Rename to
ErrRelaunchSucceeded and update comments at every call site to clarify
that this is not a real error but a sentinel indicating the elevated
child process completed the operation successfully.

Also fix a bug in WSL's launchElevate where a failed elevated process
was incorrectly wrapped with the sentinel, causing callers to treat the
failure as success and print "Machine init complete."

Signed-off-by: lstocchi <lstocchi@redhat.com>

@l0rd l0rd left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@l0rd

l0rd commented Apr 3, 2026

Copy link
Copy Markdown
Contributor

@containers/podman-maintainers PTAL

@Honny1 Honny1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM, defer to Windows experts.

@l0rd l0rd merged commit e3b5d0f into podman-container-tools:main Apr 10, 2026
89 checks passed
@lstocchi lstocchi deleted the i27627 branch April 15, 2026 16:29
@l0rd l0rd mentioned this pull request Apr 22, 2026
6 tasks
mheon pushed a commit that referenced this pull request Apr 22, 2026
Luca remarkable knowledge of the Windows virtualization
platform would be beneficial for reviewing PRs and triaging
issues.

lstocchi contributions:
- #28535
- #27932
- #27931
- #27885
- #27650
- #26277
- #26201
- #20478

Signed-off-by: Mario Loriedo <mario.loriedo@gmail.com>
simonbrauner pushed a commit to simonbrauner/podman that referenced this pull request Jun 15, 2026
Luca remarkable knowledge of the Windows virtualization
platform would be beneficial for reviewing PRs and triaging
issues.

lstocchi contributions:
- podman-container-tools#28535
- podman-container-tools#27932
- podman-container-tools#27931
- podman-container-tools#27885
- podman-container-tools#27650
- podman-container-tools#26277
- podman-container-tools#26201
- podman-container-tools#20478

Signed-off-by: Mario Loriedo <mario.loriedo@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

machine No New Tests Allow PR to proceed without adding regression tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Propose running in elevated mode when a user tries to machine init/rm with Hyper-V

3 participants