mirror of
https://github.com/netbirdio/gvisor.git
synced 2026-05-22 17:12:49 -07:00
Add statefile command to runsc.
PiperOrigin-RevId: 296105337
This commit is contained in:
committed by
Copybara-Service
parent
30794512d3
commit
ec5630527b
@@ -31,6 +31,7 @@ go_library(
|
||||
"spec.go",
|
||||
"start.go",
|
||||
"state.go",
|
||||
"statefile.go",
|
||||
"syscalls.go",
|
||||
"wait.go",
|
||||
],
|
||||
@@ -43,6 +44,8 @@ go_library(
|
||||
"//pkg/sentry/control",
|
||||
"//pkg/sentry/kernel",
|
||||
"//pkg/sentry/kernel/auth",
|
||||
"//pkg/state",
|
||||
"//pkg/state/statefile",
|
||||
"//pkg/sync",
|
||||
"//pkg/unet",
|
||||
"//pkg/urpc",
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
// Copyright 2020 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
|
||||
//
|
||||
// http://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.
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/google/subcommands"
|
||||
"gvisor.dev/gvisor/pkg/state"
|
||||
"gvisor.dev/gvisor/pkg/state/statefile"
|
||||
"gvisor.dev/gvisor/runsc/flag"
|
||||
)
|
||||
|
||||
// Statefile implements subcommands.Command for the "statefile" command.
|
||||
type Statefile struct {
|
||||
list bool
|
||||
get string
|
||||
key string
|
||||
output string
|
||||
html bool
|
||||
}
|
||||
|
||||
// Name implements subcommands.Command.
|
||||
func (*Statefile) Name() string {
|
||||
return "state"
|
||||
}
|
||||
|
||||
// Synopsis implements subcommands.Command.
|
||||
func (*Statefile) Synopsis() string {
|
||||
return "shows information about a statefile"
|
||||
}
|
||||
|
||||
// Usage implements subcommands.Command.
|
||||
func (*Statefile) Usage() string {
|
||||
return `statefile [flags] <statefile>`
|
||||
}
|
||||
|
||||
// SetFlags implements subcommands.Command.
|
||||
func (s *Statefile) SetFlags(f *flag.FlagSet) {
|
||||
f.BoolVar(&s.list, "list", false, "lists the metdata in the statefile.")
|
||||
f.StringVar(&s.get, "get", "", "extracts the given metadata key.")
|
||||
f.StringVar(&s.key, "key", "", "the integrity key for the file.")
|
||||
f.StringVar(&s.output, "output", "", "target to write the result.")
|
||||
f.BoolVar(&s.html, "html", false, "outputs in HTML format.")
|
||||
}
|
||||
|
||||
// Execute implements subcommands.Command.Execute.
|
||||
func (s *Statefile) Execute(_ context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
|
||||
// Check arguments.
|
||||
if s.list && s.get != "" {
|
||||
Fatalf("error: can't specify -list and -get simultaneously.")
|
||||
}
|
||||
|
||||
// Setup output.
|
||||
var output = os.Stdout // Default.
|
||||
if s.output != "" {
|
||||
f, err := os.OpenFile(s.output, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
|
||||
if err != nil {
|
||||
Fatalf("error opening output: %v", err)
|
||||
}
|
||||
defer func() {
|
||||
if err := f.Close(); err != nil {
|
||||
Fatalf("error flushing output: %v", err)
|
||||
}
|
||||
}()
|
||||
output = f
|
||||
}
|
||||
|
||||
// Open the file.
|
||||
if f.NArg() != 1 {
|
||||
f.Usage()
|
||||
return subcommands.ExitUsageError
|
||||
}
|
||||
input, err := os.Open(f.Arg(0))
|
||||
if err != nil {
|
||||
Fatalf("error opening input: %v\n", err)
|
||||
}
|
||||
|
||||
if s.html {
|
||||
fmt.Fprintf(output, "<html><body>\n")
|
||||
defer fmt.Fprintf(output, "</body></html>\n")
|
||||
}
|
||||
|
||||
// Dump the full file?
|
||||
if !s.list && s.get == "" {
|
||||
var key []byte
|
||||
if s.key != "" {
|
||||
key = []byte(s.key)
|
||||
}
|
||||
rc, _, err := statefile.NewReader(input, key)
|
||||
if err != nil {
|
||||
Fatalf("error parsing statefile: %v", err)
|
||||
}
|
||||
if err := state.PrettyPrint(output, rc, s.html); err != nil {
|
||||
Fatalf("error printing state: %v", err)
|
||||
}
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
|
||||
// Load just the metadata.
|
||||
metadata, err := statefile.MetadataUnsafe(input)
|
||||
if err != nil {
|
||||
Fatalf("error reading metadata: %v", err)
|
||||
}
|
||||
|
||||
// Is it a single key?
|
||||
if s.get != "" {
|
||||
val, ok := metadata[s.get]
|
||||
if !ok {
|
||||
Fatalf("metadata key %s: not found", s.get)
|
||||
}
|
||||
fmt.Fprintf(output, "%s\n", val)
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
|
||||
// List all keys.
|
||||
if s.html {
|
||||
fmt.Fprintf(output, " <ul>\n")
|
||||
defer fmt.Fprintf(output, " </ul>\n")
|
||||
}
|
||||
for key := range metadata {
|
||||
if s.html {
|
||||
fmt.Fprintf(output, " <li>%s</li>\n", key)
|
||||
} else {
|
||||
fmt.Fprintf(output, "%s\n", key)
|
||||
}
|
||||
}
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
+2
-1
@@ -116,8 +116,8 @@ func main() {
|
||||
subcommands.Register(new(cmd.Resume), "")
|
||||
subcommands.Register(new(cmd.Run), "")
|
||||
subcommands.Register(new(cmd.Spec), "")
|
||||
subcommands.Register(new(cmd.Start), "")
|
||||
subcommands.Register(new(cmd.State), "")
|
||||
subcommands.Register(new(cmd.Start), "")
|
||||
subcommands.Register(new(cmd.Wait), "")
|
||||
|
||||
// Register internal commands with the internal group name. This causes
|
||||
@@ -127,6 +127,7 @@ func main() {
|
||||
subcommands.Register(new(cmd.Boot), internalGroup)
|
||||
subcommands.Register(new(cmd.Debug), internalGroup)
|
||||
subcommands.Register(new(cmd.Gofer), internalGroup)
|
||||
subcommands.Register(new(cmd.Statefile), internalGroup)
|
||||
|
||||
// All subcommands must be registered before flag parsing.
|
||||
flag.Parse()
|
||||
|
||||
Reference in New Issue
Block a user