@@ -27,6 +27,7 @@ import (
27
27
"github.com/containers/podman/v4/pkg/util"
28
28
"github.com/containers/podman/v4/utils"
29
29
"github.com/containers/storage/pkg/lockfile"
30
+ psutil "github.com/shirou/gopsutil/v3/process"
30
31
"github.com/sirupsen/logrus"
31
32
)
32
33
@@ -578,7 +579,7 @@ func (m *HyperVMachine) Start(name string, opts machine.StartOptions) error {
578
579
if vm .State () != hypervctl .Disabled {
579
580
return hypervctl .ErrMachineStateInvalid
580
581
}
581
- _ , _ , err = m .startHostNetworking ()
582
+ gvproxyPid , _ , _ , err : = m .startHostNetworking ()
582
583
if err != nil {
583
584
return fmt .Errorf ("unable to start host networking: %q" , err )
584
585
}
@@ -601,19 +602,34 @@ func (m *HyperVMachine) Start(name string, opts machine.StartOptions) error {
601
602
// set starting back false now that we are running
602
603
m .Starting = false
603
604
604
- if err := m .startShares (); err != nil {
605
- return err
606
- }
607
-
608
605
if m .HostUser .Modified {
609
606
if machine .UpdatePodmanDockerSockService (m , name , m .UID , m .Rootful ) == nil {
610
607
// Reset modification state if there are no errors, otherwise ignore errors
611
608
// which are already logged
612
609
m .HostUser .Modified = false
613
610
}
614
611
}
612
+
615
613
// Write the config with updated starting status and hostuser modification
616
- return m .writeConfig ()
614
+ if err := m .writeConfig (); err != nil {
615
+ return err
616
+ }
617
+
618
+ // Check if gvproxy is still running.
619
+ // Do this *after* we write config, so we have still recorded that the
620
+ // VM is actually running - to ensure that stopping the machine works as
621
+ // expected.
622
+ _ , err = psutil .NewProcess (gvproxyPid )
623
+ if err != nil {
624
+ return fmt .Errorf ("gvproxy appears to have stopped (PID %d): %w" , gvproxyPid , err )
625
+ }
626
+
627
+ // Finalize starting shares after we are confident gvproxy is still alive.
628
+ if err := m .startShares (); err != nil {
629
+ return err
630
+ }
631
+
632
+ return nil
617
633
}
618
634
619
635
func (m * HyperVMachine ) State (_ bool ) (define.Status , error ) {
@@ -735,24 +751,24 @@ func (m *HyperVMachine) loadHyperVMachineFromJSON(fqConfigPath string) error {
735
751
return json .Unmarshal (b , m )
736
752
}
737
753
738
- func (m * HyperVMachine ) startHostNetworking () (string , machine.APIForwardingState , error ) {
754
+ func (m * HyperVMachine ) startHostNetworking () (int32 , string , machine.APIForwardingState , error ) {
739
755
var (
740
756
forwardSock string
741
757
state machine.APIForwardingState
742
758
)
743
759
cfg , err := config .Default ()
744
760
if err != nil {
745
- return "" , machine .NoForwarding , err
761
+ return - 1 , "" , machine .NoForwarding , err
746
762
}
747
763
748
764
executable , err := os .Executable ()
749
765
if err != nil {
750
- return "" , 0 , fmt .Errorf ("unable to locate executable: %w" , err )
766
+ return - 1 , "" , 0 , fmt .Errorf ("unable to locate executable: %w" , err )
751
767
}
752
768
753
769
gvproxyBinary , err := cfg .FindHelperBinary ("gvproxy.exe" , false )
754
770
if err != nil {
755
- return "" , 0 , err
771
+ return - 1 , "" , 0 , err
756
772
}
757
773
758
774
cmd := gvproxy .NewGvproxyCommand ()
@@ -769,20 +785,20 @@ func (m *HyperVMachine) startHostNetworking() (string, machine.APIForwardingStat
769
785
770
786
if logrus .IsLevelEnabled (logrus .DebugLevel ) {
771
787
if err := logCommandToFile (c , "gvproxy.log" ); err != nil {
772
- return "" , 0 , err
788
+ return - 1 , "" , 0 , err
773
789
}
774
790
}
775
791
776
792
logrus .Debugf ("Starting gvproxy with command: %s %v" , gvproxyBinary , c .Args )
777
793
778
794
if err := c .Start (); err != nil {
779
- return "" , 0 , fmt .Errorf ("unable to execute: %s: %w" , cmd .ToCmdline (), err )
795
+ return - 1 , "" , 0 , fmt .Errorf ("unable to execute: %s: %w" , cmd .ToCmdline (), err )
780
796
}
781
797
782
798
logrus .Debugf ("Got gvproxy PID as %d" , c .Process .Pid )
783
799
784
800
if len (m .MountVsocks ) == 0 {
785
- return forwardSock , state , nil
801
+ return int32 ( c . Process . Pid ), forwardSock , state , nil
786
802
}
787
803
788
804
// Start the 9p server in the background
@@ -807,17 +823,17 @@ func (m *HyperVMachine) startHostNetworking() (string, machine.APIForwardingStat
807
823
808
824
if logrus .IsLevelEnabled (logrus .DebugLevel ) {
809
825
if err := logCommandToFile (fsCmd , "podman-machine-server9.log" ); err != nil {
810
- return "" , 0 , err
826
+ return - 1 , "" , 0 , err
811
827
}
812
828
}
813
829
814
830
if err := fsCmd .Start (); err != nil {
815
- return "" , 0 , fmt .Errorf ("unable to execute: %s %v: %w" , executable , args , err )
831
+ return - 1 , "" , 0 , fmt .Errorf ("unable to execute: %s %v: %w" , executable , args , err )
816
832
}
817
833
818
834
logrus .Infof ("Started podman 9p server as PID %d" , fsCmd .Process .Pid )
819
835
820
- return forwardSock , state , nil
836
+ return int32 ( c . Process . Pid ), forwardSock , state , nil
821
837
}
822
838
823
839
func logCommandToFile (c * exec.Cmd , filename string ) error {
0 commit comments