...
1
2
3 package remotevm
4
5 import (
6 "context"
7
8 "github.com/Microsoft/hcsshim/internal/jobobject"
9 "github.com/Microsoft/hcsshim/internal/vm"
10 "github.com/Microsoft/hcsshim/internal/vmservice"
11 ptypes "github.com/gogo/protobuf/types"
12 "github.com/pkg/errors"
13 )
14
15 var _ vm.UVM = &utilityVM{}
16
17 type utilityVM struct {
18 id string
19 waitError error
20 job *jobobject.JobObject
21 config *vmservice.VMConfig
22 client vmservice.VMService
23 capabilities *vmservice.CapabilitiesVMResponse
24 }
25
26 var vmSupportedResourceToVMService = map[vm.Resource]vmservice.CapabilitiesVMResponse_Resource{
27 vm.VPMem: vmservice.CapabilitiesVMResponse_Vpmem,
28 vm.SCSI: vmservice.CapabilitiesVMResponse_Scsi,
29 vm.PCI: vmservice.CapabilitiesVMResponse_Vpci,
30 vm.Plan9: vmservice.CapabilitiesVMResponse_Plan9,
31 vm.Network: vmservice.CapabilitiesVMResponse_VMNic,
32 vm.Memory: vmservice.CapabilitiesVMResponse_Memory,
33 vm.Processor: vmservice.CapabilitiesVMResponse_Processor,
34 }
35
36 func (uvm *utilityVM) ID() string {
37 return uvm.id
38 }
39
40 func (uvm *utilityVM) Start(ctx context.Context) error {
41
42 if _, err := uvm.client.ResumeVM(ctx, &ptypes.Empty{}); err != nil {
43 return errors.Wrap(err, "failed to start remote VM")
44 }
45 return nil
46 }
47
48 func (uvm *utilityVM) Stop(ctx context.Context) error {
49 if _, err := uvm.client.TeardownVM(ctx, &ptypes.Empty{}); err != nil {
50 return errors.Wrap(err, "failed to stop remote VM")
51 }
52 return nil
53 }
54
55 func (uvm *utilityVM) Wait() error {
56 _, err := uvm.client.WaitVM(context.Background(), &ptypes.Empty{})
57 if err != nil {
58 uvm.waitError = err
59 return errors.Wrap(err, "failed to wait on remote VM")
60 }
61 return nil
62 }
63
64 func (uvm *utilityVM) Pause(ctx context.Context) error {
65 if _, err := uvm.client.PauseVM(ctx, &ptypes.Empty{}); err != nil {
66 return errors.Wrap(err, "failed to pause remote VM")
67 }
68 return nil
69 }
70
71 func (uvm *utilityVM) Resume(ctx context.Context) error {
72 if _, err := uvm.client.ResumeVM(ctx, &ptypes.Empty{}); err != nil {
73 return errors.Wrap(err, "failed to resume remote VM")
74 }
75 return nil
76 }
77
78 func (uvm *utilityVM) Save(ctx context.Context) error {
79 return vm.ErrNotSupported
80 }
81
82 func (uvm *utilityVM) Supported(resource vm.Resource, operation vm.ResourceOperation) bool {
83 var foundResource *vmservice.CapabilitiesVMResponse_SupportedResource
84 for _, supportedResource := range uvm.capabilities.SupportedResources {
85 if vmSupportedResourceToVMService[resource] == supportedResource.Resource {
86 foundResource = supportedResource
87 }
88 }
89
90 supported := false
91 switch operation {
92 case vm.Add:
93 supported = foundResource.Add
94 case vm.Remove:
95 supported = foundResource.Remove
96 case vm.Update:
97 supported = foundResource.Update
98 default:
99 return false
100 }
101
102 return supported
103 }
104
105 func (uvm *utilityVM) ExitError() error {
106 return uvm.waitError
107 }
108
View as plain text