1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
// Copyright (c) 2018-2023, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
// Includes code from https://github.com/containers/podman
// Released under the Apache License Version 2.0
package oci
import (
"context"
"fmt"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strconv"
"syscall"
"github.com/sylabs/singularity/v4/pkg/sylog"
)
// Delete deletes container resources
func Delete(ctx context.Context, containerID string, systemdCgroups bool) error {
runtimeBin, err := Runtime()
if err != nil {
return err
}
rsd, err := runtimeStateDir()
if err != nil {
return err
}
runtimeArgs := []string{
"--root", rsd,
}
if systemdCgroups {
runtimeArgs = append(runtimeArgs, "--systemd-cgroup")
}
runtimeArgs = append(runtimeArgs, "delete", containerID)
cmd := exec.CommandContext(ctx, runtimeBin, runtimeArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdout
sylog.Debugf("Calling %s with args %v", runtimeBin, runtimeArgs)
err = cmd.Run()
if err != nil {
return fmt.Errorf("while calling runc delete: %w", err)
}
sd, err := stateDir(containerID)
if err != nil {
return fmt.Errorf("while computing state directory: %w", err)
}
bLink := filepath.Join(sd, bundleLink)
bundle, err := filepath.EvalSymlinks(bLink)
if err != nil {
return fmt.Errorf("while finding bundle directory: %w", err)
}
sylog.Debugf("Removing bundle symlink")
if err := os.Remove(bLink); err != nil {
return fmt.Errorf("while removing bundle symlink: %w", err)
}
sylog.Debugf("Releasing bundle lock")
return releaseBundle(bundle)
}
// Exec executes a command in a container
func Exec(containerID string, cmdArgs []string, systemdCgroups bool) error {
runtimeBin, err := Runtime()
if err != nil {
return err
}
rsd, err := runtimeStateDir()
if err != nil {
return err
}
runtimeArgs := []string{
"--root", rsd,
}
if systemdCgroups {
runtimeArgs = append(runtimeArgs, "--systemd-cgroup")
}
runtimeArgs = append(runtimeArgs, "exec", containerID)
runtimeArgs = append(runtimeArgs, cmdArgs...)
cmd := exec.Command(runtimeBin, runtimeArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
sylog.Debugf("Calling %s with args %v", runtimeBin, runtimeArgs)
return cmd.Run()
}
// Kill kills container process
func Kill(containerID string, killSignal string) error {
runtimeBin, err := Runtime()
if err != nil {
return err
}
rsd, err := runtimeStateDir()
if err != nil {
return err
}
runtimeArgs := []string{
"--root", rsd,
"kill",
containerID,
killSignal,
}
cmd := exec.Command(runtimeBin, runtimeArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
sylog.Debugf("Calling %s with args %v", runtimeBin, runtimeArgs)
return cmd.Run()
}
// Pause pauses processes in a container
func Pause(containerID string, systemdCgroups bool) error {
runtimeBin, err := Runtime()
if err != nil {
return err
}
rsd, err := runtimeStateDir()
if err != nil {
return err
}
runtimeArgs := []string{
"--root", rsd,
}
if systemdCgroups {
runtimeArgs = append(runtimeArgs, "--systemd-cgroup")
}
runtimeArgs = append(runtimeArgs, "pause", containerID)
cmd := exec.Command(runtimeBin, runtimeArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
sylog.Debugf("Calling %s with args %v", runtimeBin, runtimeArgs)
return cmd.Run()
}
// Resume un-pauses processes in a container
func Resume(containerID string, systemdCgroups bool) error {
runtimeBin, err := Runtime()
if err != nil {
return err
}
rsd, err := runtimeStateDir()
if err != nil {
return err
}
runtimeArgs := []string{
"--root", rsd,
}
if systemdCgroups {
runtimeArgs = append(runtimeArgs, "--systemd-cgroup")
}
runtimeArgs = append(runtimeArgs, "resume", containerID)
cmd := exec.Command(runtimeBin, runtimeArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
sylog.Debugf("Calling %s with args %v", runtimeBin, runtimeArgs)
return cmd.Run()
}
// Run runs a container (equivalent to create/start/delete)
func Run(ctx context.Context, containerID, bundlePath, pidFile string, systemdCgroups bool) error {
runtimeBin, err := Runtime()
if err != nil {
return err
}
absBundle, err := filepath.Abs(bundlePath)
if err != nil {
return fmt.Errorf("failed to determine bundle absolute path: %s", err)
}
if err := os.Chdir(absBundle); err != nil {
return fmt.Errorf("failed to change directory to %s: %s", absBundle, err)
}
rsd, err := runtimeStateDir()
if err != nil {
return err
}
runtimeArgs := []string{
"--root", rsd,
}
if systemdCgroups {
runtimeArgs = append(runtimeArgs, "--systemd-cgroup")
}
runtimeArgs = append(runtimeArgs, "run", "-b", absBundle)
if pidFile != "" {
runtimeArgs = append(runtimeArgs, "--pid-file="+pidFile)
}
signals := make(chan os.Signal, 2)
signal.Notify(signals)
sylog.Debugf("Starting signal proxy for container %s", containerID)
go signalProxy(containerID, signals)
runtimeArgs = append(runtimeArgs, containerID)
cmd := exec.CommandContext(ctx, runtimeBin, runtimeArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
sylog.Debugf("Calling %s with args %v", runtimeBin, runtimeArgs)
return cmd.Run()
}
func signalProxy(containerID string, signals chan os.Signal) {
for {
s := <-signals
switch s {
case syscall.SIGCHLD:
break
case syscall.SIGURG:
// Ignore SIGURG, which is used for non-cooperative goroutine
// preemption starting with Go 1.14. For more information, see
// https://github.com/golang/go/issues/24543.
break
default:
sylog.Debugf("Received signal %s", s.String())
//nolint:forcetypeassert
sysSig := s.(syscall.Signal)
sylog.Debugf("Sending signal %s to container %s", sysSig.String(), containerID)
if err := Kill(containerID, strconv.Itoa(int(sysSig))); err != nil {
sylog.Errorf("Failed to send signal %s to container %s", sysSig.String(), containerID)
}
}
}
}
// Start starts a previously created container
func Start(containerID string, systemdCgroups bool) error {
runtimeBin, err := Runtime()
if err != nil {
return err
}
rsd, err := runtimeStateDir()
if err != nil {
return err
}
runtimeArgs := []string{
"--root", rsd,
}
if systemdCgroups {
runtimeArgs = append(runtimeArgs, "--systemd-cgroup")
}
runtimeArgs = append(runtimeArgs, "start", containerID)
cmd := exec.Command(runtimeBin, runtimeArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
sylog.Debugf("Calling %s with args %v", runtimeBin, runtimeArgs)
return cmd.Run()
}
// State queries container state
func State(containerID string, systemdCgroups bool) error {
runtimeBin, err := Runtime()
if err != nil {
return err
}
rsd, err := runtimeStateDir()
if err != nil {
return err
}
runtimeArgs := []string{
"--root", rsd,
}
if systemdCgroups {
runtimeArgs = append(runtimeArgs, "--systemd-cgroup")
}
runtimeArgs = append(runtimeArgs, "state", containerID)
cmd := exec.Command(runtimeBin, runtimeArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
sylog.Debugf("Calling %s with args %v", runtimeBin, runtimeArgs)
return cmd.Run()
}
// Update updates container cgroups resources
func Update(containerID, cgFile string, systemdCgroups bool) error {
runtimeBin, err := Runtime()
if err != nil {
return err
}
rsd, err := runtimeStateDir()
if err != nil {
return err
}
runtimeArgs := []string{
"--root", rsd,
}
if systemdCgroups {
runtimeArgs = append(runtimeArgs, "--systemd-cgroup")
}
runtimeArgs = append(runtimeArgs, "update", "-r", cgFile, containerID)
cmd := exec.Command(runtimeBin, runtimeArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
sylog.Debugf("Calling %s with args %v", runtimeBin, runtimeArgs)
return cmd.Run()
}
|