github.com/apptainer/singularity@v3.1.1+incompatible/internal/app/singularity/oci_start_linux.go (about)

     1  // Copyright (c) 2018-2019, Sylabs Inc. All rights reserved.
     2  // This software is licensed under a 3-clause BSD license. Please consult the
     3  // LICENSE.md file distributed with the sources of this project regarding your
     4  // rights to use or distribute this software.
     5  
     6  package singularity
     7  
     8  import (
     9  	"encoding/json"
    10  	"fmt"
    11  	"io"
    12  
    13  	"github.com/sylabs/singularity/pkg/ociruntime"
    14  	"github.com/sylabs/singularity/pkg/util/unix"
    15  )
    16  
    17  // OciStart starts a previously create container
    18  func OciStart(containerID string) error {
    19  	state, err := getState(containerID)
    20  	if err != nil {
    21  		return err
    22  	}
    23  
    24  	if state.Status != ociruntime.Created {
    25  		return fmt.Errorf("cannot start '%s', the state of the container must be %s", containerID, ociruntime.Created)
    26  	}
    27  
    28  	if state.ControlSocket == "" {
    29  		return fmt.Errorf("can't find control socket")
    30  	}
    31  
    32  	ctrl := &ociruntime.Control{}
    33  	ctrl.StartContainer = true
    34  
    35  	c, err := unix.Dial(state.ControlSocket)
    36  	if err != nil {
    37  		return fmt.Errorf("failed to connect to control socket")
    38  	}
    39  	defer c.Close()
    40  
    41  	enc := json.NewEncoder(c)
    42  	if err != nil {
    43  		return err
    44  	}
    45  
    46  	if err := enc.Encode(ctrl); err != nil {
    47  		return err
    48  	}
    49  
    50  	// wait runtime close socket connection for ACK
    51  	d := make([]byte, 1)
    52  	if _, err := c.Read(d); err != io.EOF {
    53  		return err
    54  	}
    55  
    56  	return nil
    57  }