setup.go 2.2 KB

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