@@ -46,6 +46,7 @@ func (h HyperVStubber) CreateVM(_ define.CreateVMOpts, mc *vmconfigs.MachineConf
4646 var err error
4747 callbackFuncs := machine .CleanUp ()
4848 defer callbackFuncs .CleanIfErr (& err )
49+ callbackFuncs .Add (createErrorLogCallback (& err ))
4950 go callbackFuncs .CleanOnSignal ()
5051
5152 hwConfig := hypervctl.HardwareConfig {
@@ -63,6 +64,13 @@ func (h HyperVStubber) CreateVM(_ define.CreateVMOpts, mc *vmconfigs.MachineConf
6364 // This is to prevent a non-admin user from creating the first machine
6465 // which would require adding vsock entries into the Windows Registry.
6566 if err := h .canCreate (); err != nil {
67+ // If it returns ErrHypervRegistryInitRequiresElevation and we're not already re-executing,
68+ // offer to elevate automatically if user is in admin group
69+ if err == ErrHypervRegistryInitRequiresElevation && ! windows .IsReExecuting () && windows .IsInAdministratorsGroup () {
70+ message := "To initialize the first Hyper-V machine, Podman requires admin rights to set up the Windows Registry.\n \n " +
71+ windows .UACConfirmationPrompt
72+ return launchElevate (message )
73+ }
6674 return err
6775 }
6876
@@ -179,6 +187,13 @@ func (h HyperVStubber) Remove(mc *vmconfigs.MachineConfig) ([]string, func() err
179187 // This is to prevent a non-admin user from deleting the last machine
180188 // which would require removal of vsock entries from the Windows Registry.
181189 if err := h .canRemove (mc ); err != nil {
190+ // If we get ErrHypervRegistryRemoveRequiresElevation and we're not already re-executing,
191+ // and the user has admin rights (is in admin group), offer to elevate automatically
192+ if err == ErrHypervRegistryRemoveRequiresElevation && ! windows .IsReExecuting () && windows .IsInAdministratorsGroup () {
193+ message := "Removing this Hyper-V machine requires admin rights to clean up the Windows Registry.\n \n " +
194+ windows .UACConfirmationPrompt
195+ return nil , nil , launchElevate (message )
196+ }
182197 return nil , nil , err
183198 }
184199
@@ -237,6 +252,35 @@ func (h HyperVStubber) canCreate() error {
237252 return ErrHypervUserNotInAdminGroup
238253}
239254
255+ // launchElevate attempts to automatically re-run the command as administrator
256+ // This is similar to how WSL handles elevation.
257+ func launchElevate (message string ) error {
258+ if windows .MessageBox (message , "Podman Machine" , false ) != 1 {
259+ return errors .New ("elevation process cancelled by user. Please rerun the command as administrator" )
260+ }
261+ err := windows .CreateOrTruncateElevatedOutputFile ()
262+ if err != nil {
263+ return err
264+ }
265+
266+ err = windows .RelaunchElevatedWait ()
267+ if err != nil {
268+ windows .DumpOutputFile ()
269+ return fmt .Errorf ("elevated process failed with error: %w" , err )
270+ }
271+ return define .ErrRelaunchAttempt
272+ }
273+
274+ // createErrorLogCallback creates a callback function that logs errors to file when --reexec is detected
275+ func createErrorLogCallback (err * error ) func () error {
276+ return func () error {
277+ if * err != nil && windows .IsReExecuting () {
278+ windows .LogErrorToFile (* err )
279+ }
280+ return nil
281+ }
282+ }
283+
240284func isLegacyMachine (mc * vmconfigs.MachineConfig ) bool {
241285 return mc .HyperVHypervisor != nil && mc .HyperVHypervisor .ReadyVsock .MachineName != ""
242286}
@@ -372,6 +416,7 @@ func (h HyperVStubber) StartVM(mc *vmconfigs.MachineConfig) (func() error, func(
372416
373417 callbackFuncs := machine .CleanUp ()
374418 defer callbackFuncs .CleanIfErr (& err )
419+ callbackFuncs .Add (createErrorLogCallback (& err ))
375420 go callbackFuncs .CleanOnSignal ()
376421
377422 if mc .IsFirstBoot () {
@@ -547,6 +592,11 @@ func (h HyperVStubber) PrepareIgnition(mc *vmconfigs.MachineConfig, _ *ignition.
547592 readySock , err := vsock .LoadHVSockRegistryEntryByPurpose (vsock .Events )
548593 if err != nil {
549594 if ! windows .HasAdminRights () {
595+ if ! windows .IsReExecuting () && windows .IsInAdministratorsGroup () {
596+ message := "Initializing the first Hyper-V machine requires admin rights to set up the Windows Registry.\n \n " +
597+ windows .UACConfirmationPrompt
598+ return nil , launchElevate (message )
599+ }
550600 return nil , ErrHypervRegistryInitRequiresElevation
551601 }
552602 readySock , err = vsock .NewHVSockRegistryEntry (vsock .Events )
0 commit comments