github.com/apptainer/singularity@v3.1.1+incompatible/internal/app/singularity/oci_update_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  	"io/ioutil"
    13  	"os"
    14  
    15  	specs "github.com/opencontainers/runtime-spec/specs-go"
    16  	"github.com/sylabs/singularity/internal/pkg/cgroups"
    17  	"github.com/sylabs/singularity/pkg/ociruntime"
    18  )
    19  
    20  // OciUpdate updates container cgroups resources
    21  func OciUpdate(containerID string, args *OciArgs) error {
    22  	var reader io.Reader
    23  
    24  	state, err := getState(containerID)
    25  	if err != nil {
    26  		return err
    27  	}
    28  
    29  	if state.State.Status != ociruntime.Running && state.State.Status != ociruntime.Created {
    30  		return fmt.Errorf("container %s is neither running nor created", containerID)
    31  	}
    32  
    33  	if args.FromFile == "" {
    34  		return fmt.Errorf("you must specify --from-file")
    35  	}
    36  
    37  	resources := &specs.LinuxResources{}
    38  	manager := &cgroups.Manager{Pid: state.State.Pid}
    39  
    40  	if args.FromFile == "-" {
    41  		reader = os.Stdin
    42  	} else {
    43  		f, err := os.Open(args.FromFile)
    44  		if err != nil {
    45  			return err
    46  		}
    47  		reader = f
    48  	}
    49  
    50  	data, err := ioutil.ReadAll(reader)
    51  	if err != nil {
    52  		return fmt.Errorf("failed to read cgroups config file: %s", err)
    53  	}
    54  
    55  	if err := json.Unmarshal(data, resources); err != nil {
    56  		return err
    57  	}
    58  
    59  	return manager.UpdateFromSpec(resources)
    60  }