...

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

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

     1  //go:build windows
     2  
     3  package runhcs
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  	"path/filepath"
     9  	"strings"
    10  
    11  	irunhcs "github.com/Microsoft/hcsshim/internal/runhcs"
    12  	runc "github.com/containerd/go-runc"
    13  )
    14  
    15  // CreateOpts is set of options that can be used with the Create command.
    16  type CreateOpts struct {
    17  	runc.IO
    18  	// PidFile is the path to the file to write the process id to.
    19  	PidFile string
    20  	// ShimLog is the path to the log file or named pipe (e.g. \\.\pipe\ProtectedPrefix\Administrators\runhcs-<container-id>-shim-log) for the launched shim process.
    21  	ShimLog string
    22  	// VMLog is the path to the log file or named pipe (e.g. \\.\pipe\ProtectedPrefix\Administrators\runhcs-<container-id>-vm-log) for the launched VM shim process.
    23  	VMLog string
    24  	// VMConsole is the path to the pipe for the VM's console (e.g. \\.\pipe\debugpipe)
    25  	VMConsole string
    26  }
    27  
    28  func (opt *CreateOpts) args() ([]string, error) {
    29  	var out []string
    30  	if opt.PidFile != "" {
    31  		abs, err := filepath.Abs(opt.PidFile)
    32  		if err != nil {
    33  			return nil, err
    34  		}
    35  		out = append(out, "--pid-file", abs)
    36  	}
    37  	if opt.ShimLog != "" {
    38  		if strings.HasPrefix(opt.ShimLog, irunhcs.SafePipePrefix) {
    39  			out = append(out, "--shim-log", opt.ShimLog)
    40  		} else {
    41  			abs, err := filepath.Abs(opt.ShimLog)
    42  			if err != nil {
    43  				return nil, err
    44  			}
    45  			out = append(out, "--shim-log", abs)
    46  		}
    47  	}
    48  	if opt.VMLog != "" {
    49  		if strings.HasPrefix(opt.VMLog, irunhcs.SafePipePrefix) {
    50  			out = append(out, "--vm-log", opt.VMLog)
    51  		} else {
    52  			abs, err := filepath.Abs(opt.VMLog)
    53  			if err != nil {
    54  				return nil, err
    55  			}
    56  			out = append(out, "--vm-log", abs)
    57  		}
    58  	}
    59  	if opt.VMConsole != "" {
    60  		out = append(out, "--vm-console", opt.VMConsole)
    61  	}
    62  	return out, nil
    63  }
    64  
    65  // Create creates a new container and returns its pid if it was created
    66  // successfully.
    67  func (r *Runhcs) Create(context context.Context, id, bundle string, opts *CreateOpts) error {
    68  	args := []string{"create", "--bundle", bundle}
    69  	if opts != nil {
    70  		oargs, err := opts.args()
    71  		if err != nil {
    72  			return err
    73  		}
    74  		args = append(args, oargs...)
    75  	}
    76  	cmd := r.command(context, append(args, id)...)
    77  	if opts != nil && opts.IO != nil {
    78  		opts.Set(cmd)
    79  	}
    80  	if cmd.Stdout == nil && cmd.Stderr == nil {
    81  		data, err := cmdOutput(cmd, true)
    82  		if err != nil {
    83  			return fmt.Errorf("%s: %s", err, data)
    84  		}
    85  		return nil
    86  	}
    87  	ec, err := runc.Monitor.Start(cmd)
    88  	if err != nil {
    89  		return err
    90  	}
    91  	if opts != nil && opts.IO != nil {
    92  		if c, ok := opts.IO.(runc.StartCloser); ok {
    93  			if err := c.CloseAfterStart(); err != nil {
    94  				return err
    95  			}
    96  		}
    97  	}
    98  	status, err := runc.Monitor.Wait(cmd, ec)
    99  	if err == nil && status != 0 {
   100  		err = fmt.Errorf("%s did not terminate successfully", cmd.Args[0])
   101  	}
   102  	return err
   103  }
   104  

View as plain text