...

Source file src/github.com/Microsoft/hcsshim/internal/jobcontainers/storage.go

Documentation: github.com/Microsoft/hcsshim/internal/jobcontainers

     1  //go:build windows
     2  
     3  package jobcontainers
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  	"os"
     9  	"path/filepath"
    10  
    11  	"github.com/Microsoft/hcsshim/internal/layers"
    12  	"github.com/Microsoft/hcsshim/internal/log"
    13  	"github.com/Microsoft/hcsshim/internal/resources"
    14  	"github.com/Microsoft/hcsshim/internal/wclayer"
    15  	specs "github.com/opencontainers/runtime-spec/specs-go"
    16  	"github.com/pkg/errors"
    17  )
    18  
    19  // fallbackRootfsFormat is the fallback location for the rootfs if file binding support isn't available.
    20  // %s will be expanded with the container ID. Trailing backslash required for SetVolumeMountPoint and
    21  // DeleteVolumeMountPoint
    22  const fallbackRootfsFormat = `C:\hpc\%s\`
    23  
    24  // defaultSiloRootfsLocation is the default location the rootfs for the container will show up
    25  // inside of a given silo. If bind filter support isn't available the rootfs will be
    26  // C:\hpc\<containerID>
    27  const defaultSiloRootfsLocation = `C:\hpc\`
    28  
    29  func (c *JobContainer) mountLayers(ctx context.Context, containerID string, s *specs.Spec, volumeMountPath string) (_ resources.ResourceCloser, err error) {
    30  	if s == nil || s.Windows == nil || s.Windows.LayerFolders == nil {
    31  		return nil, errors.New("field 'Spec.Windows.Layerfolders' is not populated")
    32  	}
    33  
    34  	// Last layer always contains the sandbox.vhdx, or 'scratch' space for the container.
    35  	scratchFolder := s.Windows.LayerFolders[len(s.Windows.LayerFolders)-1]
    36  	if _, err := os.Stat(scratchFolder); os.IsNotExist(err) {
    37  		if err := os.MkdirAll(scratchFolder, 0777); err != nil {
    38  			return nil, fmt.Errorf("failed to auto-create container scratch folder %s: %w", scratchFolder, err)
    39  		}
    40  	}
    41  
    42  	// Create sandbox.vhdx if it doesn't exist in the scratch folder.
    43  	if _, err := os.Stat(filepath.Join(scratchFolder, "sandbox.vhdx")); os.IsNotExist(err) {
    44  		if err := wclayer.CreateScratchLayer(ctx, scratchFolder, s.Windows.LayerFolders[:len(s.Windows.LayerFolders)-1]); err != nil {
    45  			return nil, fmt.Errorf("failed to CreateSandboxLayer: %w", err)
    46  		}
    47  	}
    48  
    49  	if s.Root == nil {
    50  		s.Root = &specs.Root{}
    51  	}
    52  
    53  	var closer resources.ResourceCloser
    54  	if s.Root.Path == "" {
    55  		log.G(ctx).Debug("mounting job container storage")
    56  		var rootPath string
    57  		rootPath, closer, err = layers.MountWCOWLayers(ctx, containerID, s.Windows.LayerFolders, "", volumeMountPath, nil)
    58  		if err != nil {
    59  			return nil, fmt.Errorf("failed to mount job container storage: %w", err)
    60  		}
    61  		s.Root.Path = rootPath + "\\"
    62  	}
    63  
    64  	return closer, nil
    65  }
    66  
    67  // setupRootfsBinding binds the copy on write volume for the container to a static path
    68  // in the container specified by 'root'.
    69  func (c *JobContainer) setupRootfsBinding(root, target string) error {
    70  	if err := c.job.ApplyFileBinding(root, target, false); err != nil {
    71  		return fmt.Errorf("failed to bind rootfs to %s: %w", root, err)
    72  	}
    73  	return nil
    74  }
    75  

View as plain text