@@ -62,6 +62,14 @@ func (h HyperVStubber) CreateVM(_ define.CreateVMOpts, mc *vmconfigs.MachineConf
6262 // This is to prevent a non-admin user from creating the first machine
6363 // which would require adding vsock entries into the Windows Registry.
6464 if err := h .canCreate (); err != nil {
65+ // If it returns ErrHypervRegistryInitRequiresElevation and we're not already re-executing,
66+ // offer to elevate automatically if user is in admin group
67+ if err == ErrHypervRegistryInitRequiresElevation && ! windows .IsReExecuting () && windows .IsInAdministratorsGroup () {
68+ message := "To initialize the first Hyper-V machine, Podman requires admin rights to set up the Windows Registry.\n \n " +
69+ "A UAC prompt will appear asking for your approval.\n \n " +
70+ "Would you like to continue?"
71+ return launchElevate (message )
72+ }
6573 return err
6674 }
6775
@@ -178,6 +186,14 @@ 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+ "A UAC prompt will appear asking for your approval.\n \n " +
194+ "Would you like to continue?"
195+ return nil , nil , launchElevate (message )
196+ }
181197 return nil , nil , err
182198 }
183199
@@ -236,6 +252,21 @@ func (h HyperVStubber) canCreate() error {
236252 return ErrHypervUserNotInAdminGroup
237253}
238254
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+
262+ err := windows .RelaunchElevatedWait ()
263+ if err != nil {
264+ fmt .Fprintf (os .Stderr , "Elevated process failed with error: %v\n \n " , err )
265+ return fmt .Errorf ("elevated process failed with error: %w" , err )
266+ }
267+ return define .ErrRelaunchAttempt
268+ }
269+
239270func isLegacyMachine (mc * vmconfigs.MachineConfig ) bool {
240271 return mc .HyperVHypervisor != nil && mc .HyperVHypervisor .ReadyVsock .MachineName != ""
241272}
@@ -553,6 +584,12 @@ func (h HyperVStubber) PrepareIgnition(mc *vmconfigs.MachineConfig, _ *ignition.
553584 readySock , err := vsock .LoadHVSockRegistryEntryByPurpose (vsock .Events )
554585 if err != nil {
555586 if ! windows .HasAdminRights () {
587+ if ! windows .IsReExecuting () && windows .IsInAdministratorsGroup () {
588+ message := "Initializing the first Hyper-V machine requires admin rights to set up the Windows Registry.\n \n " +
589+ "A UAC prompt will appear asking for your approval.\n \n " +
590+ "Would you like to continue?"
591+ return nil , launchElevate (message )
592+ }
556593 return nil , ErrHypervRegistryInitRequiresElevation
557594 }
558595 readySock , err = vsock .NewHVSockRegistryEntry (vsock .Events )
0 commit comments