-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce hcsshim.ConvertToBaseLayer
This API allows turning any collection of files into a WCOW base layer. It will create the necessary files in Files/ for hcsshim.ProcessBaseLayer to function, validate the necessary files for hcsshim.ProcessUtilityVMImage if UtilityVM/ exists, and then call those two APIs to complete the process. Calling this on a directory containing an untarred base layer OCI tarball, gives a very similar outcome to passing the tar stream through ociwclayer.ImportLayer. The new API is used in `TestSCSIAddRemoveWCOW` to create nearly-empty base layers for the scratch layers attached and removed from the utility VM. A wclayer command is also introduced: `makebaselayer` for testing and validation purposes. Signed-off-by: Paul "TBBle" Hampson <Paul.Hampson@Pobox.com>
- Loading branch information
Showing
11 changed files
with
213 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"github.com/Microsoft/hcsshim" | ||
"github.com/Microsoft/hcsshim/internal/appargs" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
var makeBaseLayerCommand = cli.Command{ | ||
Name: "makebaselayer", | ||
Usage: "converts a directory containing 'Files/' into a base layer", | ||
ArgsUsage: "<layer path>", | ||
Before: appargs.Validate(appargs.NonEmptyString), | ||
Action: func(context *cli.Context) error { | ||
path, err := filepath.Abs(context.Args().First()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return hcsshim.ConvertToBaseLayer(path) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package wclayer | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"syscall" | ||
|
||
"github.com/Microsoft/hcsshim/internal/hcserror" | ||
"github.com/Microsoft/hcsshim/internal/oc" | ||
"github.com/Microsoft/hcsshim/internal/safefile" | ||
"github.com/Microsoft/hcsshim/internal/winapi" | ||
"github.com/pkg/errors" | ||
"go.opencensus.io/trace" | ||
) | ||
|
||
var hiveNames = []string{"DEFAULT", "SAM", "SECURITY", "SOFTWARE", "SYSTEM"} | ||
|
||
// Ensure the given file exists as an ordinary file, and create a zero-length file if not. | ||
func ensureFile(path string, root *os.File) error { | ||
stat, err := safefile.LstatRelative(path, root) | ||
if err != nil && os.IsNotExist(err) { | ||
// Do I need to restore directory-modifcation times on the rest of the path? | ||
// That doesn't seem necessary since the directory _is_ being modified now. | ||
// See `func (w *baseLayerWriter) Close()` for context | ||
newFile, err := safefile.OpenRelative(path, root, 0, syscall.FILE_SHARE_WRITE, winapi.FILE_CREATE, 0) | ||
if err != nil { | ||
return err | ||
} | ||
return newFile.Close() | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if !stat.Mode().IsRegular() { | ||
fullPath := filepath.Join(root.Name(), path) | ||
return errors.Errorf("%s has unexpected file mode %s", fullPath, stat.Mode().String()) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// safefileMkdirAll works like os.MkdirAll but with safefile | ||
// TODO: Add this to safefile? | ||
func safefileMkdirAllRelative(path string, root *os.File) error { | ||
pathParts := strings.Split(path, (string)(filepath.Separator)) | ||
for index := range pathParts { | ||
|
||
partialPath := filepath.Join(pathParts[0 : index+1]...) | ||
stat, err := safefile.LstatRelative(partialPath, root) | ||
|
||
if err != nil && os.IsNotExist(err) { | ||
if err := safefile.MkdirRelative(partialPath, root); err != nil { | ||
return err | ||
} | ||
continue | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if !stat.IsDir() { | ||
fullPath := filepath.Join(root.Name(), partialPath) | ||
return errors.Errorf("%s has unexpected file mode %s", fullPath, stat.Mode().String()) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func ensureBaseLayer(root *os.File) (hasUtilityVM bool, err error) { | ||
hasUtilityVM = false | ||
|
||
// The base layer registry hives will be copied from here | ||
hiveSourcePath := "Files\\Windows\\System32\\config" | ||
if err = safefileMkdirAllRelative(hiveSourcePath, root); err != nil { | ||
return | ||
} | ||
|
||
for _, hiveName := range hiveNames { | ||
hivePath := filepath.Join(hiveSourcePath, hiveName) | ||
if err = ensureFile(hivePath, root); err != nil { | ||
return | ||
} | ||
} | ||
|
||
stat, err := safefile.LstatRelative(utilityVMFilesPath, root) | ||
|
||
if err != nil && os.IsNotExist(err) { | ||
return false, nil | ||
} | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
if !stat.Mode().IsDir() { | ||
fullPath := filepath.Join(root.Name(), utilityVMFilesPath) | ||
return false, errors.Errorf("%s has unexpected file mode %s", fullPath, stat.Mode().String()) | ||
} | ||
|
||
// Just check that this exists as a regular file. If it exists but is not a valid registry hive, | ||
// ProcessUtilityVMImage will complain: | ||
// "The registry could not read in, or write out, or flush, one of the files that contain the system's image of the registry." | ||
bcdPath := filepath.Join(utilityVMFilesPath, "EFI\\Microsoft\\Boot\\BCD") | ||
|
||
stat, err = safefile.LstatRelative(bcdPath, root) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
if !stat.Mode().IsRegular() { | ||
fullPath := filepath.Join(root.Name(), bcdPath) | ||
return false, errors.Errorf("%s has unexpected file mode %s", fullPath, stat.Mode().String()) | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
func convertToBaseLayer(ctx context.Context, root *os.File) error { | ||
hasUtilityVM, err := ensureBaseLayer(root) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := ProcessBaseLayer(ctx, root.Name()); err != nil { | ||
return err | ||
} | ||
|
||
if !hasUtilityVM { | ||
return nil | ||
} | ||
|
||
err = safefile.EnsureNotReparsePointRelative(utilityVMPath, root) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
utilityVMPath := filepath.Join(root.Name(), utilityVMPath) | ||
return ProcessUtilityVMImage(ctx, utilityVMPath) | ||
} | ||
|
||
// ConvertToBaseLayer processes a candidate base layer, i.e. a directory | ||
// containing the desired file content under Files/, and optionally the | ||
// desired file content for a UtilityVM under UtilityMV/Files/ | ||
func ConvertToBaseLayer(ctx context.Context, path string) (err error) { | ||
title := "hcsshim::ConvertToBaseLayer" | ||
ctx, span := trace.StartSpan(ctx, title) | ||
defer span.End() | ||
defer func() { oc.SetSpanStatus(span, err) }() | ||
span.AddAttributes(trace.StringAttribute("path", path)) | ||
|
||
if root, err := safefile.OpenRoot(path); err != nil { | ||
return hcserror.New(err, title+" - failed", "") | ||
} else if err = convertToBaseLayer(ctx, root); err != nil { | ||
return hcserror.New(err, title+" - failed", "") | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters