...

Source file src/github.com/Microsoft/hcsshim/internal/vm/hcs/builder.go

Documentation: github.com/Microsoft/hcsshim/internal/vm/hcs

     1  //go:build windows
     2  
     3  package hcs
     4  
     5  import (
     6  	"context"
     7  
     8  	"github.com/Microsoft/hcsshim/internal/hcs"
     9  	hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2"
    10  	"github.com/Microsoft/hcsshim/internal/schemaversion"
    11  	"github.com/Microsoft/hcsshim/internal/vm"
    12  	"github.com/pkg/errors"
    13  )
    14  
    15  var _ vm.UVMBuilder = &utilityVMBuilder{}
    16  
    17  type utilityVMBuilder struct {
    18  	id      string
    19  	guestOS vm.GuestOS
    20  	doc     *hcsschema.ComputeSystem
    21  }
    22  
    23  func NewUVMBuilder(id string, owner string, guestOS vm.GuestOS) (vm.UVMBuilder, error) {
    24  	doc := &hcsschema.ComputeSystem{
    25  		Owner:                             owner,
    26  		SchemaVersion:                     schemaversion.SchemaV21(),
    27  		ShouldTerminateOnLastHandleClosed: true,
    28  		VirtualMachine: &hcsschema.VirtualMachine{
    29  			StopOnReset: true,
    30  			Chipset:     &hcsschema.Chipset{},
    31  			ComputeTopology: &hcsschema.Topology{
    32  				Memory: &hcsschema.Memory2{
    33  					AllowOvercommit: true,
    34  				},
    35  				Processor: &hcsschema.Processor2{},
    36  			},
    37  			Devices: &hcsschema.Devices{
    38  				HvSocket: &hcsschema.HvSocket2{
    39  					HvSocketConfig: &hcsschema.HvSocketSystemConfig{
    40  						// Allow administrators and SYSTEM to bind to vsock sockets
    41  						// so that we can create a GCS log socket.
    42  						DefaultBindSecurityDescriptor: "D:P(A;;FA;;;SY)(A;;FA;;;BA)",
    43  					},
    44  				},
    45  			},
    46  		},
    47  	}
    48  
    49  	switch guestOS {
    50  	case vm.Windows:
    51  		doc.VirtualMachine.Devices.VirtualSmb = &hcsschema.VirtualSmb{}
    52  	case vm.Linux:
    53  		doc.VirtualMachine.Devices.Plan9 = &hcsschema.Plan9{}
    54  	default:
    55  		return nil, vm.ErrUnknownGuestOS
    56  	}
    57  
    58  	return &utilityVMBuilder{
    59  		id:      id,
    60  		guestOS: guestOS,
    61  		doc:     doc,
    62  	}, nil
    63  }
    64  
    65  func (uvmb *utilityVMBuilder) Create(ctx context.Context) (_ vm.UVM, err error) {
    66  	cs, err := hcs.CreateComputeSystem(ctx, uvmb.id, uvmb.doc)
    67  	if err != nil {
    68  		return nil, errors.Wrap(err, "failed to create hcs compute system")
    69  	}
    70  
    71  	defer func() {
    72  		if err != nil {
    73  			_ = cs.Terminate(ctx)
    74  			_ = cs.Wait()
    75  		}
    76  	}()
    77  
    78  	backingType := vm.MemoryBackingTypeVirtual
    79  	if !uvmb.doc.VirtualMachine.ComputeTopology.Memory.AllowOvercommit {
    80  		backingType = vm.MemoryBackingTypePhysical
    81  	}
    82  
    83  	uvm := &utilityVM{
    84  		id:          uvmb.id,
    85  		guestOS:     uvmb.guestOS,
    86  		cs:          cs,
    87  		backingType: backingType,
    88  	}
    89  
    90  	properties, err := cs.Properties(ctx)
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  	uvm.vmID = properties.RuntimeID
    95  	return uvm, nil
    96  }
    97  

View as plain text