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
|
// Copyright 2021 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"
"math/rand"
"os"
"github.com/google/subcommands"
specs "github.com/opencontainers/runtime-spec/specs-go"
"golang.org/x/sys/unix"
"gvisor.dev/gvisor/runsc/config"
"gvisor.dev/gvisor/runsc/flag"
"gvisor.dev/gvisor/runsc/specutils"
)
// VerityPrepare implements subcommands.Commands for the "verity-prepare"
// command. It sets up a sandbox with a writable verity mount mapped to "--dir",
// and executes the verity measure tool specified by "--tool" in the sandbox. It
// is intended to prepare --dir to be mounted as a verity filesystem.
type VerityPrepare struct {
root string
tool string
dir string
}
// Name implements subcommands.Command.Name.
func (*VerityPrepare) Name() string {
return "verity-prepare"
}
// Synopsis implements subcommands.Command.Synopsis.
func (*VerityPrepare) Synopsis() string {
return "Generates the data structures necessary to enable verityfs on a filesystem."
}
// Usage implements subcommands.Command.Usage.
func (*VerityPrepare) Usage() string {
return "verity-prepare --tool=<measure_tool> --dir=<path>"
}
// SetFlags implements subcommands.Command.SetFlags.
func (c *VerityPrepare) SetFlags(f *flag.FlagSet) {
f.StringVar(&c.root, "root", "/", `path to the root directory, defaults to "/"`)
f.StringVar(&c.tool, "tool", "", "path to the verity measure_tool")
f.StringVar(&c.dir, "dir", "", "path to the directory to be hashed")
}
// Execute implements subcommands.Command.Execute.
func (c *VerityPrepare) Execute(_ context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
conf := args[0].(*config.Config)
waitStatus := args[1].(*unix.WaitStatus)
hostname, err := os.Hostname()
if err != nil {
return Errorf("Error to retrieve hostname: %v", err)
}
// Map the entire host file system.
absRoot, err := resolvePath(c.root)
if err != nil {
return Errorf("Error resolving root: %v", err)
}
spec := &specs.Spec{
Root: &specs.Root{
Path: absRoot,
},
Process: &specs.Process{
Cwd: absRoot,
Args: []string{c.tool, "--path", "/verityroot"},
Env: os.Environ(),
Capabilities: specutils.AllCapabilities(),
},
Hostname: hostname,
Mounts: []specs.Mount{
specs.Mount{
Source: c.dir,
Destination: "/verityroot",
Type: "bind",
Options: []string{"verity.roothash="},
},
},
}
cid := fmt.Sprintf("runsc-%06d", rand.Int31n(1000000))
// Force no networking, it is not necessary to run the verity measure tool.
conf.Network = config.NetworkNone
return startContainerAndWait(spec, conf, cid, waitStatus)
}
|