Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] tools: refactor all binary packages as library packages #2983

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
2ba8720
tools: roast: refactor as a lib instead of main package
mfrw May 9, 2022
eec64af
tools: boilerplate: remove un-needed dir
mfrw May 9, 2022
1397ef5
tools: mv 'internal/logger' -> 'pkg/logger'
mfrw May 12, 2022
ed3610f
tools: mv 'internal/pkggraph' -> 'pkg/graph/pkggraph'
mfrw May 12, 2022
202773d
tools: mv 'internal/safechroot' -> 'pkg/safechroot'
mfrw May 16, 2022
3e3c98c
tools: srpmpacker: refactor as a lib instead of a main package
mfrw Aug 14, 2022
78abafa
tools: specreader: refactor as a lib instead of a main package
mfrw Aug 14, 2022
326364e
tools: pkgworker: refactor as a lib instead of a main package
mfrw Aug 14, 2022
d6ed34e
tools: graphpkgfetcher: refactor as a lib instead of main package
mfrw Aug 14, 2022
40a13bf
tools: grapher: refactor as a lib instead of main package
mfrw Aug 14, 2022
f97832a
tools: graphpreprocessor: refactor as a lib instead of main package
mfrw Aug 14, 2022
6181abd
tools: mv imagegen -> pkg/imagegen
mfrw Aug 14, 2022
8c6bacd
tools: imageconfigvalidator: refactor as a lib instead of main package
mfrw Aug 14, 2022
0866397
tools: imagepkgfetcher: refactor as a lib instead of main package
mfrw Aug 14, 2022
4b73378
tools: imager: refactor as a lib instead of main package
mfrw Aug 14, 2022
124f312
tools: isomaker: refactor as a lib instead of main package
mfrw May 9, 2022
2bf9744
tools: scheduler: refactor as a lib instead of main package
mfrw Aug 15, 2022
01a0e49
tools: liveinstaller: refactor as a lib instead of main package
mfrw Aug 15, 2022
58d6e44
tools: validatechroot: refactor as a lib instead of main package
mfrw May 11, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion toolkit/docs/formats/imageconfig.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Image configuration

Image configuration consists of two sections - Disks and SystemConfigs - that describe the produced artifact(image). Image configuration code can be found in (configuration.go)[../../tools/imagegen/configuration/configuration.go] and validity of the configuration file can be verified by the [imageconfigvalidator](../../tools/imageconfigvalidator/imageconfigvalidator.go)
Image configuration consists of two sections - Disks and SystemConfigs - that describe the produced artifact(image). Image configuration code can be found in (configuration.go)[../../tools/pkg/imagegen/configuration/configuration.go] and validity of the configuration file can be verified by the [imageconfigvalidator](../../tools/imageconfigvalidator/imageconfigvalidator.go)


## Disks
Expand Down
2 changes: 1 addition & 1 deletion toolkit/docs/security/read-only-roots.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Files in `/etc` such as the `passwd` and `machine-id` files are also part of the
Ideally as much of `/etc` as possible should be left read-only to avoid miss-configuration.

### Forward Error Correction (FEC)
Verity supports error correction which will return the original data even if the underlying blocks have been modified (it does not restore the underlying data stored on disk however). FEC incurs some overhead but the hash tree, which is a sunk cost, makes it much more effective than normal Reed-Solomon codes. For a 2GiB disk, FEC with 2 roots (i.e. 2 bytes of error correction codes per 255 bytes of real data) can correct ~16MiB of errors with ~16MiB of overhead. See [veritydisk.go](../../tools/imagegen/configuration/veritydisk.go) for calculation details.
Verity supports error correction which will return the original data even if the underlying blocks have been modified (it does not restore the underlying data stored on disk however). FEC incurs some overhead but the hash tree, which is a sunk cost, makes it much more effective than normal Reed-Solomon codes. For a 2GiB disk, FEC with 2 roots (i.e. 2 bytes of error correction codes per 255 bytes of real data) can correct ~16MiB of errors with ~16MiB of overhead. See [veritydisk.go](../../tools/pkg/imagegen/configuration/veritydisk.go) for calculation details.

### Hash Tree and FEC Overhead
The extra data required for verity needs to be stored outside the measured partition. In the case of Mariner it is stored in the initramfs. Assuming the Merkle tree is a full m-ary tree with m=128 (128 branches per node, from `4k/sizeof(sha256)`), the size of the Merkle tree is:
Expand Down
3 changes: 1 addition & 2 deletions toolkit/scripts/tools.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ $(call create_folder,$(BUILD_DIR)/tools)

# List of go utilities in tools/ directory
go_tool_list = \
boilerplate \
depsearch \
grapher \
graphpkgfetcher \
Expand All @@ -37,7 +36,7 @@ go_tool_targets = $(foreach target,$(go_tool_list),$(TOOL_BINS_DIR)/$(target))
# Common files to monitor for all go targets
go_module_files = $(TOOLS_DIR)/go.mod $(TOOLS_DIR)/go.sum
go_internal_files = $(shell find $(TOOLS_DIR)/internal/ -type f -name '*.go')
Copy link
Contributor

@PawelWMS PawelWMS Aug 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious if we still need go_internal_files after all these changes.

go_imagegen_files = $(shell find $(TOOLS_DIR)/imagegen/ -type f -name '*.go')
go_imagegen_files = $(shell find $(TOOLS_DIR)/pkg/imagegen/ -type f -name '*.go')
go_common_files = $(go_module_files) $(go_internal_files) $(go_imagegen_files) $(BUILD_DIR)/tools/internal.test_coverage
# A report on test coverage for all the go tools
test_coverage_report=$(TOOL_BINS_DIR)/test_coverage_report.html
Expand Down
32 changes: 0 additions & 32 deletions toolkit/tools/boilerplate/boilerplate.go

This file was deleted.

9 changes: 0 additions & 9 deletions toolkit/tools/boilerplate/hello/hello.go

This file was deleted.

15 changes: 0 additions & 15 deletions toolkit/tools/boilerplate/hello/hello_test.go

This file was deleted.

12 changes: 6 additions & 6 deletions toolkit/tools/depsearch/depsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
"path/filepath"
"sort"

"github.com/microsoft/CBL-Mariner/toolkit/tools/internal/exe"
"github.com/microsoft/CBL-Mariner/toolkit/tools/internal/file"
"github.com/microsoft/CBL-Mariner/toolkit/tools/internal/logger"
"github.com/microsoft/CBL-Mariner/toolkit/tools/internal/pkggraph"
"github.com/microsoft/CBL-Mariner/toolkit/tools/scheduler/schedulerutils"

"gonum.org/v1/gonum/graph"
"gopkg.in/alecthomas/kingpin.v2"

"github.com/microsoft/CBL-Mariner/toolkit/tools/internal/exe"
"github.com/microsoft/CBL-Mariner/toolkit/tools/internal/file"
"github.com/microsoft/CBL-Mariner/toolkit/tools/pkg/graph/pkggraph"
"github.com/microsoft/CBL-Mariner/toolkit/tools/pkg/logger"
"github.com/microsoft/CBL-Mariner/toolkit/tools/pkg/scheduler/schedulerutils"
)

const (
Expand Down
66 changes: 13 additions & 53 deletions toolkit/tools/graphPreprocessor/graphPreprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"os"

"github.com/microsoft/CBL-Mariner/toolkit/tools/internal/exe"
"github.com/microsoft/CBL-Mariner/toolkit/tools/internal/logger"
"github.com/microsoft/CBL-Mariner/toolkit/tools/internal/pkggraph"
"github.com/microsoft/CBL-Mariner/toolkit/tools/pkg/graph/pkggraph"
"github.com/microsoft/CBL-Mariner/toolkit/tools/pkg/graph/preprocessor"
"github.com/microsoft/CBL-Mariner/toolkit/tools/pkg/logger"

"gopkg.in/alecthomas/kingpin.v2"
)
Expand All @@ -23,69 +24,28 @@ var (
logLevel = exe.LogLevelFlag(app)
)

func replaceRunNodesWithPrebuiltNodes(pkgGraph *pkggraph.PkgGraph) (err error) {
for _, node := range pkgGraph.AllNodes() {

if node.Type != pkggraph.TypeRun {
continue
}

isPrebuilt, _, missing := pkggraph.IsSRPMPrebuilt(node.SrpmPath, pkgGraph, nil)

if isPrebuilt == false {
logger.Log.Tracef("Can't mark %s as prebuilt, missing: %v", node.SrpmPath, missing)
continue
}

preBuiltNode := pkgGraph.CloneNode(node)
preBuiltNode.State = pkggraph.StateUpToDate
preBuiltNode.Type = pkggraph.TypePreBuilt

parentNodes := pkgGraph.To(node.ID())
for parentNodes.Next() {
parentNode := parentNodes.Node().(*pkggraph.PkgNode)

if parentNode.Type != pkggraph.TypeGoal {
pkgGraph.RemoveEdge(parentNode.ID(), node.ID())

logger.Log.Debugf("Adding a 'PreBuilt' node '%s' with id %d. For '%s'", preBuiltNode.FriendlyName(), preBuiltNode.ID(), parentNode.FriendlyName())
err = pkgGraph.AddEdge(parentNode, preBuiltNode)

if err != nil {
logger.Log.Errorf("Adding edge failed for %v -> %v", parentNode, preBuiltNode)
return
}
}
}
func populatePreprocessorConfig() *preprocessor.Config {
return &preprocessor.Config{
InputGraphFile: *inputGraphFile,
OutputGraphFile: *outputGraphFile,
HydratedBuild: *hydratedBuild,
}

return
}

func main() {
app.Version(exe.ToolkitVersion)
kingpin.MustParse(app.Parse(os.Args[1:]))
logger.InitBestEffort(*logFile, *logLevel)

scrubbedGraph := pkggraph.NewPkgGraph()

err := pkggraph.ReadDOTGraphFile(scrubbedGraph, *inputGraphFile)
cfg := populatePreprocessorConfig()
scrubbedGraph, err := cfg.ReadAndPreprocessGraph()
if err != nil {
logger.Log.Panicf("Failed to read graph to file, %s. Error: %s", *inputGraphFile, err)
logger.Log.Panic(err)
}

if *hydratedBuild {
logger.Log.Debugf("Nodes before replacing prebuilt nodes: %d", len(scrubbedGraph.AllNodes()))
err = replaceRunNodesWithPrebuiltNodes(scrubbedGraph)
logger.Log.Debugf("Nodes after replacing prebuilt nodes: %d", len(scrubbedGraph.AllNodes()))
if err != nil {
logger.Log.Panicf("Failed to replace run nodes with preBuilt nodes. Error: %s", err)
}
}

err = pkggraph.WriteDOTGraphFile(scrubbedGraph, *outputGraphFile)
err = pkggraph.WriteDOTGraphFile(scrubbedGraph, cfg.OutputGraphFile)
if err != nil {
logger.Log.Panicf("Failed to write cache graph to file, %s. Error: %s", *outputGraphFile, err)
logger.Log.Panicf("Failed to write cache graph to file, %s. Error: %s", cfg.OutputGraphFile, err)
}
return
}
4 changes: 2 additions & 2 deletions toolkit/tools/graphanalytics/graphanalytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"strings"

"github.com/microsoft/CBL-Mariner/toolkit/tools/internal/exe"
"github.com/microsoft/CBL-Mariner/toolkit/tools/internal/logger"
"github.com/microsoft/CBL-Mariner/toolkit/tools/internal/pkggraph"
"github.com/microsoft/CBL-Mariner/toolkit/tools/internal/sliceutils"
"github.com/microsoft/CBL-Mariner/toolkit/tools/pkg/graph/pkggraph"
"github.com/microsoft/CBL-Mariner/toolkit/tools/pkg/logger"

"gonum.org/v1/gonum/graph"
graphpath "gonum.org/v1/gonum/graph/path"
Expand Down
Loading