@@ -45,6 +45,7 @@ func (h HyperVStubber) CreateVM(_ define.CreateVMOpts, mc *vmconfigs.MachineConf
4545 var err error
4646 callbackFuncs := machine .CleanUp ()
4747 defer callbackFuncs .CleanIfErr (& err )
48+ callbackFuncs .Add (createErrorLogCallback (& err ))
4849 go callbackFuncs .CleanOnSignal ()
4950
5051 hwConfig := hypervctl.HardwareConfig {
@@ -62,6 +63,13 @@ func (h HyperVStubber) CreateVM(_ define.CreateVMOpts, mc *vmconfigs.MachineConf
6263 // This is to prevent a non-admin user from creating the first machine
6364 // which would require adding vsock entries into the Windows Registry.
6465 if err := h .canCreate (); err != nil {
66+ // If it returns ErrHypervRegistryInitRequiresElevation and we're not already re-executing,
67+ // offer to elevate automatically if user is in admin group
68+ if err == ErrHypervRegistryInitRequiresElevation && ! windows .IsReExecuting () && windows .IsInAdministratorsGroup () {
69+ message := "To initialize the first Hyper-V machine, Podman requires admin rights to set up the Windows Registry.\n \n " +
70+ windows .UACConfirmationPrompt
71+ return launchElevate (message )
72+ }
6573 return err
6674 }
6775
@@ -178,6 +186,13 @@ func (h HyperVStubber) Remove(mc *vmconfigs.MachineConfig) ([]string, func() err
178186 // This is to prevent a non-admin user from deleting the last machine
179187 // which would require removal of vsock entries from the Windows Registry.
180188 if err := h .canRemove (mc ); err != nil {
189+ // If we get ErrHypervRegistryRemoveRequiresElevation and we're not already re-executing,
190+ // and the user has admin rights (is in admin group), offer to elevate automatically
191+ if err == ErrHypervRegistryRemoveRequiresElevation && ! windows .IsReExecuting () && windows .IsInAdministratorsGroup () {
192+ message := "Removing this Hyper-V machine requires admin rights to clean up the Windows Registry.\n \n " +
193+ windows .UACConfirmationPrompt
194+ return nil , nil , launchElevate (message )
195+ }
181196 return nil , nil , err
182197 }
183198
@@ -236,6 +251,35 @@ func (h HyperVStubber) canCreate() error {
236251 return ErrHypervUserNotInAdminGroup
237252}
238253
254+ // launchElevate attempts to automatically re-run the command as administrator
255+ // This is similar to how WSL handles elevation.
256+ func launchElevate (message string ) error {
257+ if windows .MessageBox (message , "Podman Machine" , false ) != 1 {
258+ return errors .New ("elevation process cancelled by user. Please rerun the command as administrator" )
259+ }
260+ err := windows .CreateOrTruncateElevatedOutputFile ()
261+ if err != nil {
262+ return err
263+ }
264+
265+ err = windows .RelaunchElevatedWait ()
266+ if err != nil {
267+ windows .DumpOutputFile ()
268+ return fmt .Errorf ("elevated process failed with error: %w" , err )
269+ }
270+ return define .ErrRelaunchAttempt
271+ }
272+
273+ // createErrorLogCallback creates a callback function that logs errors to file when --reexec is detected
274+ func createErrorLogCallback (err * error ) func () error {
275+ return func () error {
276+ if * err != nil && windows .IsReExecuting () {
277+ windows .LogErrorToFile (* err )
278+ }
279+ return nil
280+ }
281+ }
282+
239283func isLegacyMachine (mc * vmconfigs.MachineConfig ) bool {
240284 return mc .HyperVHypervisor != nil && mc .HyperVHypervisor .ReadyVsock .MachineName != ""
241285}
@@ -371,6 +415,7 @@ func (h HyperVStubber) StartVM(mc *vmconfigs.MachineConfig) (func() error, func(
371415
372416 callbackFuncs := machine .CleanUp ()
373417 defer callbackFuncs .CleanIfErr (& err )
418+ callbackFuncs .Add (createErrorLogCallback (& err ))
374419 go callbackFuncs .CleanOnSignal ()
375420
376421 if mc .IsFirstBoot () {
@@ -553,6 +598,11 @@ func (h HyperVStubber) PrepareIgnition(mc *vmconfigs.MachineConfig, _ *ignition.
553598 readySock , err := vsock .LoadHVSockRegistryEntryByPurpose (vsock .Events )
554599 if err != nil {
555600 if ! windows .HasAdminRights () {
601+ if ! windows .IsReExecuting () && windows .IsInAdministratorsGroup () {
602+ message := "Initializing the first Hyper-V machine requires admin rights to set up the Windows Registry.\n \n " +
603+ windows .UACConfirmationPrompt
604+ return nil , launchElevate (message )
605+ }
556606 return nil , ErrHypervRegistryInitRequiresElevation
557607 }
558608 readySock , err = vsock .NewHVSockRegistryEntry (vsock .Events )
0 commit comments