File: inspect.go

package info (click to toggle)
golang-github-checkpoint-restore-checkpointctl 1.3.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 524 kB
  • sloc: ansic: 208; makefile: 172; sh: 40
file content (178 lines) | stat: -rw-r--r-- 4,147 bytes parent folder | download
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
package cmd

import (
	"fmt"
	"path/filepath"

	"github.com/checkpoint-restore/checkpointctl/internal"
	metadata "github.com/checkpoint-restore/checkpointctl/lib"
	"github.com/spf13/cobra"
)

func Inspect() *cobra.Command {
	cmd := &cobra.Command{
		Use:   "inspect",
		Short: "Display low-level information about a container checkpoint",
		RunE:  inspect,
		Args:  cobra.MinimumNArgs(1),
	}
	flags := cmd.Flags()

	flags.BoolVar(
		stats,
		"stats",
		false,
		"Display checkpoint statistics",
	)
	flags.BoolVar(
		mounts,
		"mounts",
		false,
		"Display an overview of mounts used in the container checkpoint",
	)
	flags.Uint32VarP(
		pID,
		"pid",
		"p",
		0,
		"Display the process tree of a specific PID",
	)
	flags.BoolVar(
		psTree,
		"ps-tree",
		false,
		"Display an overview of processes in the container checkpoint",
	)
	flags.BoolVar(
		psTreeCmd,
		"ps-tree-cmd",
		false,
		"Display an overview of processes in the container checkpoint with full command line arguments",
	)
	flags.BoolVar(
		psTreeEnv,
		"ps-tree-env",
		false,
		"Display an overview of processes in the container checkpoint with their environment variables",
	)
	flags.BoolVar(
		files,
		"files",
		false,
		"Display the open file descriptors for processes in the container checkpoint",
	)
	flags.BoolVar(
		sockets,
		"sockets",
		false,
		"Display the open sockets for processes in the container checkpoint",
	)
	flags.BoolVar(
		showAll,
		"all",
		false,
		"Show all information about container checkpoints",
	)
	flags.StringVar(
		format,
		"format",
		"tree",
		"Specify the output format: tree or json",
	)
	flags.BoolVar(
		showMetdata,
		"metadata",
		false,
		"Show metadata about the container",
	)

	return cmd
}

func inspect(cmd *cobra.Command, args []string) error {
	if *showAll {
		*stats = true
		*mounts = true
		*psTreeCmd = true
		*psTreeEnv = true
		*files = true
		*sockets = true
		*showMetdata = true
	}

	requiredFiles := []string{metadata.SpecDumpFile, metadata.ConfigDumpFile}

	if *stats {
		requiredFiles = append(requiredFiles, "stats-dump")
	}

	if *pID != 0 {
		// Enable displaying process tree if the PID filter is passed.
		*psTree = true
	}

	if *files {
		// Enable displaying process tree, even if it is not passed.
		// This is necessary to attach the files to the processes
		// that opened them and display this in the tree.
		*psTree = true
		requiredFiles = append(
			requiredFiles,
			// Unpack files.img, fs-*.img, ids-*.img, fdinfo-*.img
			filepath.Join(metadata.CheckpointDirectory, "files.img"),
			filepath.Join(metadata.CheckpointDirectory, "fs-"),
			filepath.Join(metadata.CheckpointDirectory, "ids-"),
			filepath.Join(metadata.CheckpointDirectory, "fdinfo-"),
		)
	}

	if *sockets {
		// Enable displaying process tree, even if it is not passed.
		// This is necessary to attach the sockets to the processes
		// that opened them and display this in the tree.
		*psTree = true
		requiredFiles = append(
			requiredFiles,
			// Unpack files.img, ids-*.img, fdinfo-*.img
			filepath.Join(metadata.CheckpointDirectory, "files.img"),
			filepath.Join(metadata.CheckpointDirectory, "ids-"),
			filepath.Join(metadata.CheckpointDirectory, "fdinfo-"),
		)
	}

	if *psTreeCmd || *psTreeEnv {
		// Enable displaying process tree when using --ps-tree-cmd or --ps-tree-env.
		*psTree = true
		requiredFiles = append(
			requiredFiles,
			// Unpack pagemap-*.img, pages-*.img, and mm-*.img
			filepath.Join(metadata.CheckpointDirectory, "pagemap-"),
			filepath.Join(metadata.CheckpointDirectory, "pages-"),
			filepath.Join(metadata.CheckpointDirectory, "mm-"),
		)
	}

	if *psTree {
		requiredFiles = append(
			requiredFiles,
			// Unpack pstree.img, core-*.img
			filepath.Join(metadata.CheckpointDirectory, "pstree.img"),
			filepath.Join(metadata.CheckpointDirectory, "core-"),
		)
	}

	tasks, err := internal.CreateTasks(args, requiredFiles)
	if err != nil {
		return err
	}
	defer internal.CleanupTasks(tasks)

	switch *format {
	case "tree":
		return internal.RenderTreeView(tasks)
	case "json":
		return internal.RenderJSONView(tasks)
	default:
		return fmt.Errorf("invalid output format: %s", *format)
	}
}