setup.go 2.5 KB

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