...

Source file src/github.com/Microsoft/hcsshim/pkg/go-runhcs/runhcs_create-scratch.go

Documentation: github.com/Microsoft/hcsshim/pkg/go-runhcs

     1  //go:build windows
     2  
     3  package runhcs
     4  
     5  import (
     6  	"context"
     7  	"errors"
     8  	"path/filepath"
     9  	"strconv"
    10  )
    11  
    12  // CreateScratch creates a scratch vhdx at 'destpath' that is ext4 formatted.
    13  func (r *Runhcs) CreateScratch(context context.Context, destpath string) error {
    14  	return r.CreateScratchWithOpts(context, destpath, nil)
    15  }
    16  
    17  // CreateScratchOpts is the set of options that can be used with the
    18  // `CreateScratchWithOpts` command.
    19  type CreateScratchOpts struct {
    20  	// SizeGB is the size in GB of the scratch file to create.
    21  	SizeGB int
    22  	// CacheFile is the path to an existing `scratch.vhx` to copy. If
    23  	// `CacheFile` does not exit the scratch will be created.
    24  	CacheFile string
    25  }
    26  
    27  func (opt *CreateScratchOpts) args() ([]string, error) {
    28  	var out []string
    29  	if opt.SizeGB < 0 {
    30  		return nil, errors.New("sizeGB must be >= 0")
    31  	} else if opt.SizeGB > 0 {
    32  		out = append(out, "--sizeGB", strconv.Itoa(opt.SizeGB))
    33  	}
    34  	if opt.CacheFile != "" {
    35  		abs, err := filepath.Abs(opt.CacheFile)
    36  		if err != nil {
    37  			return nil, err
    38  		}
    39  		out = append(out, "--cache-path", abs)
    40  	}
    41  	return out, nil
    42  }
    43  
    44  // CreateScratchWithOpts creates a scratch vhdx at 'destpath' that is ext4
    45  // formatted based on `opts`.
    46  func (r *Runhcs) CreateScratchWithOpts(context context.Context, destpath string, opts *CreateScratchOpts) error {
    47  	args := []string{"create-scratch", "--destpath", destpath}
    48  	if opts != nil {
    49  		oargs, err := opts.args()
    50  		if err != nil {
    51  			return err
    52  		}
    53  		args = append(args, oargs...)
    54  	}
    55  	return r.runOrError(r.command(context, args...))
    56  }
    57  

View as plain text