2020-05-05 18:06:46 -07:00
|
|
|
// Copyright 2018 The gVisor Authors.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
2019-01-29 18:51:18 -08:00
|
|
|
|
2021-01-12 17:50:33 -08:00
|
|
|
// Package utils container miscellaneous utility function used by the shim.
|
2019-01-29 18:51:18 -08:00
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2024-10-10 20:36:24 +09:00
|
|
|
"os"
|
2019-01-29 18:51:18 -08:00
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
|
|
|
|
)
|
|
|
|
|
|
2021-05-17 13:52:51 -07:00
|
|
|
const configFilename = "config.json"
|
|
|
|
|
|
2019-01-29 18:51:18 -08:00
|
|
|
// ReadSpec reads OCI spec from the bundle directory.
|
|
|
|
|
func ReadSpec(bundle string) (*specs.Spec, error) {
|
2024-10-10 20:36:24 +09:00
|
|
|
b, err := os.ReadFile(filepath.Join(bundle, configFilename))
|
2019-01-29 18:51:18 -08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
var spec specs.Spec
|
|
|
|
|
if err := json.Unmarshal(b, &spec); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &spec, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-17 13:52:51 -07:00
|
|
|
// WriteSpec writes OCI spec to the bundle directory.
|
|
|
|
|
func WriteSpec(bundle string, spec *specs.Spec) error {
|
|
|
|
|
b, err := json.Marshal(spec)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2024-10-10 20:36:24 +09:00
|
|
|
return os.WriteFile(filepath.Join(bundle, configFilename), b, 0666)
|
2021-05-17 13:52:51 -07:00
|
|
|
}
|
|
|
|
|
|
2019-01-29 18:51:18 -08:00
|
|
|
// IsSandbox checks whether a container is a sandbox container.
|
|
|
|
|
func IsSandbox(spec *specs.Spec) bool {
|
2021-05-17 13:52:51 -07:00
|
|
|
t, ok := spec.Annotations[ContainerTypeAnnotation]
|
2020-07-13 16:10:58 -07:00
|
|
|
return !ok || t == containerTypeSandbox
|
2019-01-29 18:51:18 -08:00
|
|
|
}
|
2019-03-08 17:53:36 -08:00
|
|
|
|
|
|
|
|
// UserLogPath gets user log path from OCI annotation.
|
|
|
|
|
func UserLogPath(spec *specs.Spec) string {
|
2020-07-13 16:10:58 -07:00
|
|
|
sandboxLogDir := spec.Annotations[sandboxLogDirAnnotation]
|
2019-03-08 17:53:36 -08:00
|
|
|
if sandboxLogDir == "" {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return filepath.Join(sandboxLogDir, "gvisor.log")
|
|
|
|
|
}
|
2022-08-10 14:28:57 -07:00
|
|
|
|
|
|
|
|
// PanicLogPath gets the panic log path from OCI annotation.
|
|
|
|
|
func PanicLogPath(spec *specs.Spec) string {
|
2023-02-28 13:45:29 -08:00
|
|
|
if spec == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2022-08-10 14:28:57 -07:00
|
|
|
sandboxLogDir := spec.Annotations[sandboxLogDirAnnotation]
|
|
|
|
|
if sandboxLogDir == "" {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return filepath.Join(sandboxLogDir, "gvisor_panic.log")
|
|
|
|
|
}
|