...

Source file src/github.com/Microsoft/hcsshim/computestorage/setup.go

Documentation: github.com/Microsoft/hcsshim/computestorage

     1  //go:build windows
     2  
     3  package computestorage
     4  
     5  import (
     6  	"context"
     7  	"encoding/json"
     8  
     9  	"github.com/Microsoft/hcsshim/internal/oc"
    10  	"github.com/Microsoft/hcsshim/osversion"
    11  	"github.com/pkg/errors"
    12  	"go.opencensus.io/trace"
    13  	"golang.org/x/sys/windows"
    14  )
    15  
    16  // SetupBaseOSLayer sets up a layer that contains a base OS for a container.
    17  //
    18  // `layerPath` is a path to a directory containing the layer.
    19  //
    20  // `vhdHandle` is an empty file handle of `options.Type == OsLayerTypeContainer`
    21  // or else it is a file handle to the 'SystemTemplateBase.vhdx' if `options.Type
    22  // == OsLayerTypeVm`.
    23  //
    24  // `options` are the options applied while processing the layer.
    25  func SetupBaseOSLayer(ctx context.Context, layerPath string, vhdHandle windows.Handle, options OsLayerOptions) (err error) {
    26  	title := "hcsshim::SetupBaseOSLayer"
    27  	ctx, span := oc.StartSpan(ctx, title) //nolint:ineffassign,staticcheck
    28  	defer span.End()
    29  	defer func() { oc.SetSpanStatus(span, err) }()
    30  	span.AddAttributes(
    31  		trace.StringAttribute("layerPath", layerPath),
    32  	)
    33  
    34  	bytes, err := json.Marshal(options)
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	err = hcsSetupBaseOSLayer(layerPath, vhdHandle, string(bytes))
    40  	if err != nil {
    41  		return errors.Wrap(err, "failed to setup base OS layer")
    42  	}
    43  	return nil
    44  }
    45  
    46  // SetupBaseOSVolume sets up a volume that contains a base OS for a container.
    47  //
    48  // `layerPath` is a path to a directory containing the layer.
    49  //
    50  // `volumePath` is the path to the volume to be used for setup.
    51  //
    52  // `options` are the options applied while processing the layer.
    53  //
    54  // NOTE: This API is only available on builds of Windows greater than 19645. Inside we
    55  // check if the hosts build has the API available by using 'GetVersion' which requires
    56  // the calling application to be manifested. https://docs.microsoft.com/en-us/windows/win32/sbscs/manifests
    57  func SetupBaseOSVolume(ctx context.Context, layerPath, volumePath string, options OsLayerOptions) (err error) {
    58  	if osversion.Build() < 19645 {
    59  		return errors.New("SetupBaseOSVolume is not present on builds older than 19645")
    60  	}
    61  	title := "hcsshim::SetupBaseOSVolume"
    62  	ctx, span := oc.StartSpan(ctx, title) //nolint:ineffassign,staticcheck
    63  	defer span.End()
    64  	defer func() { oc.SetSpanStatus(span, err) }()
    65  	span.AddAttributes(
    66  		trace.StringAttribute("layerPath", layerPath),
    67  		trace.StringAttribute("volumePath", volumePath),
    68  	)
    69  
    70  	bytes, err := json.Marshal(options)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	err = hcsSetupBaseOSVolume(layerPath, volumePath, string(bytes))
    76  	if err != nil {
    77  		return errors.Wrap(err, "failed to setup base OS layer")
    78  	}
    79  	return nil
    80  }
    81  

View as plain text