-
Notifications
You must be signed in to change notification settings - Fork 285
Add base layer/uvm helpers to computestorage package #902
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,197 @@ | ||
| package computestorage | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "path/filepath" | ||
| "syscall" | ||
|
|
||
| "github.com/Microsoft/go-winio/pkg/security" | ||
| "github.com/Microsoft/go-winio/vhd" | ||
| "github.com/pkg/errors" | ||
| "golang.org/x/sys/windows" | ||
| ) | ||
|
|
||
| const defaultVHDXBlockSizeInMB = 1 | ||
|
|
||
| // SetupContainerBaseLayer is a helper to setup a containers scratch. It | ||
| // will create and format the vhdx's inside and the size is configurable with the sizeInGB | ||
| // parameter. | ||
| // | ||
| // `layerPath` is the path to the base container layer on disk. | ||
| // | ||
| // `baseVhdPath` is the path to where the base vhdx for the base layer should be created. | ||
| // | ||
| // `diffVhdPath` is the path where the differencing disk for the base layer should be created. | ||
| // | ||
| // `sizeInGB` is the size in gigabytes to make the base vhdx. | ||
| func SetupContainerBaseLayer(ctx context.Context, layerPath, baseVhdPath, diffVhdPath string, sizeInGB uint64) (err error) { | ||
| var ( | ||
| hivesPath = filepath.Join(layerPath, "Hives") | ||
| layoutPath = filepath.Join(layerPath, "Layout") | ||
| ) | ||
|
|
||
| // We need to remove the hives directory and layout file as `SetupBaseOSLayer` fails if these files | ||
| // already exist. `SetupBaseOSLayer` will create these files internally. We also remove the base and | ||
| // differencing disks if they exist in case we're asking for a different size. | ||
| if _, err := os.Stat(hivesPath); err == nil { | ||
| if err := os.RemoveAll(hivesPath); err != nil { | ||
| return errors.Wrap(err, "failed to remove prexisting hives directory") | ||
| } | ||
| } | ||
| if _, err := os.Stat(layoutPath); err == nil { | ||
| if err := os.RemoveAll(layoutPath); err != nil { | ||
| return errors.Wrap(err, "failed to remove prexisting layout file") | ||
| } | ||
| } | ||
|
|
||
| if _, err := os.Stat(baseVhdPath); err == nil { | ||
| if err := os.RemoveAll(baseVhdPath); err != nil { | ||
| return errors.Wrap(err, "failed to remove base vhdx path") | ||
| } | ||
| } | ||
| if _, err := os.Stat(diffVhdPath); err == nil { | ||
| if err := os.RemoveAll(diffVhdPath); err != nil { | ||
| return errors.Wrap(err, "failed to remove differencing vhdx") | ||
| } | ||
| } | ||
|
|
||
| createParams := &vhd.CreateVirtualDiskParameters{ | ||
| Version: 2, | ||
| Version2: vhd.CreateVersion2{ | ||
| MaximumSize: sizeInGB * 1024 * 1024 * 1024, | ||
| BlockSizeInBytes: defaultVHDXBlockSizeInMB * 1024 * 1024, | ||
| }, | ||
| } | ||
| handle, err := vhd.CreateVirtualDisk(baseVhdPath, vhd.VirtualDiskAccessNone, vhd.CreateVirtualDiskFlagNone, createParams) | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to create vhdx") | ||
| } | ||
|
|
||
| defer func() { | ||
| if err != nil { | ||
| syscall.CloseHandle(handle) | ||
| os.RemoveAll(baseVhdPath) | ||
| if os.Stat(diffVhdPath); err == nil { | ||
| os.RemoveAll(diffVhdPath) | ||
| } | ||
| } | ||
| }() | ||
|
|
||
| if err = FormatWritableLayerVhd(ctx, windows.Handle(handle)); err != nil { | ||
| return err | ||
| } | ||
| // Base vhd handle must be closed before calling SetupBaseLayer in case of Container layer | ||
| if err = syscall.CloseHandle(handle); err != nil { | ||
| return errors.Wrap(err, "failed to close vhdx handle") | ||
| } | ||
|
|
||
| options := OsLayerOptions{ | ||
| Type: OsLayerTypeContainer, | ||
| } | ||
|
|
||
| // SetupBaseOSLayer expects an empty vhd handle for a container layer and will | ||
| // error out otherwise. | ||
| if err = SetupBaseOSLayer(ctx, layerPath, 0, options); err != nil { | ||
| return err | ||
| } | ||
| // Create the differencing disk that will be what's copied for the final rw layer | ||
| // for a container. | ||
| if err = vhd.CreateDiffVhd(diffVhdPath, baseVhdPath, defaultVHDXBlockSizeInMB); err != nil { | ||
| return errors.Wrap(err, "failed to create differencing disk") | ||
| } | ||
|
|
||
| if err = security.GrantVmGroupAccess(baseVhdPath); err != nil { | ||
| return errors.Wrapf(err, "failed to grant vm group access to %s", baseVhdPath) | ||
| } | ||
| if err = security.GrantVmGroupAccess(diffVhdPath); err != nil { | ||
| return errors.Wrapf(err, "failed to grant vm group access to %s", diffVhdPath) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // SetupUtilityVMBaseLayer is a helper to setup a UVMs scratch space. It will create and format | ||
| // the vhdx inside and the size is configurable by the sizeInGB parameter. | ||
| // | ||
| // `uvmPath` is the path to the UtilityVM filesystem. | ||
| // | ||
| // `baseVhdPath` is the path to where the base vhdx for the UVM should be created. | ||
| // | ||
| // `diffVhdPath` is the path where the differencing disk for the UVM should be created. | ||
| // | ||
| // `sizeInGB` specifies the size in gigabytes to make the base vhdx. | ||
| func SetupUtilityVMBaseLayer(ctx context.Context, uvmPath, baseVhdPath, diffVhdPath string, sizeInGB uint64) (err error) { | ||
| // Remove the base and differencing disks if they exist in case we're asking for a different size. | ||
| if _, err := os.Stat(baseVhdPath); err == nil { | ||
| if err := os.RemoveAll(baseVhdPath); err != nil { | ||
| return errors.Wrap(err, "failed to remove base vhdx") | ||
| } | ||
| } | ||
| if _, err := os.Stat(diffVhdPath); err == nil { | ||
| if err := os.RemoveAll(diffVhdPath); err != nil { | ||
| return errors.Wrap(err, "failed to remove differencing vhdx") | ||
| } | ||
| } | ||
|
|
||
| // Just create the vhdx for utilityVM layer, no need to format it. | ||
| createParams := &vhd.CreateVirtualDiskParameters{ | ||
| Version: 2, | ||
| Version2: vhd.CreateVersion2{ | ||
| MaximumSize: sizeInGB * 1024 * 1024 * 1024, | ||
| BlockSizeInBytes: defaultVHDXBlockSizeInMB * 1024 * 1024, | ||
| }, | ||
| } | ||
| handle, err := vhd.CreateVirtualDisk(baseVhdPath, vhd.VirtualDiskAccessNone, vhd.CreateVirtualDiskFlagNone, createParams) | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to create vhdx") | ||
| } | ||
|
|
||
| defer func() { | ||
| if err != nil { | ||
| syscall.CloseHandle(handle) | ||
| os.RemoveAll(baseVhdPath) | ||
| if os.Stat(diffVhdPath); err == nil { | ||
| os.RemoveAll(diffVhdPath) | ||
| } | ||
| } | ||
| }() | ||
|
|
||
| // If it is a UtilityVM layer then the base vhdx must be attached when calling | ||
| // `SetupBaseOSLayer` | ||
| attachParams := &vhd.AttachVirtualDiskParameters{ | ||
| Version: 2, | ||
| } | ||
| if err := vhd.AttachVirtualDisk(handle, vhd.AttachVirtualDiskFlagNone, attachParams); err != nil { | ||
| return errors.Wrapf(err, "failed to attach virtual disk") | ||
| } | ||
|
|
||
| options := OsLayerOptions{ | ||
| Type: OsLayerTypeVM, | ||
| } | ||
| if err := SetupBaseOSLayer(ctx, uvmPath, windows.Handle(handle), options); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Detach and close the handle after setting up the layer as we don't need the handle | ||
| // for anything else and we no longer need to be attached either. | ||
| if err = vhd.DetachVirtualDisk(handle); err != nil { | ||
| return errors.Wrap(err, "failed to detach vhdx") | ||
| } | ||
| if err = syscall.CloseHandle(handle); err != nil { | ||
| return errors.Wrap(err, "failed to close vhdx handle") | ||
| } | ||
|
|
||
| // Create the differencing disk that will be what's copied for the final rw layer | ||
| // for a container. | ||
| if err = vhd.CreateDiffVhd(diffVhdPath, baseVhdPath, defaultVHDXBlockSizeInMB); err != nil { | ||
| return errors.Wrap(err, "failed to create differencing disk") | ||
| } | ||
|
|
||
| if err := security.GrantVmGroupAccess(baseVhdPath); err != nil { | ||
| return errors.Wrapf(err, "failed to grant vm group access to %s", baseVhdPath) | ||
| } | ||
| if err := security.GrantVmGroupAccess(diffVhdPath); err != nil { | ||
| return errors.Wrapf(err, "failed to grant vm group access to %s", diffVhdPath) | ||
| } | ||
| return nil | ||
| } | ||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the shared logic should be applied to both container and pod? if yes, is there a clean way to incapsulate it into one place, so we don't need to worry about having to remember to check both places? nothing needs to change, more of a patterns question here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto here, not fully following the question :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mostly referring to similarities in what
SetupContainerBaseLayerandSetupSandboxBaseLayerdo in the beginning of the method (i.e.os.RemoveAllcalls) on the same paths as well as in the endCreateDiffVhd/Grant*calls in the end, which is some shared code at the start and at the end with some different functionality in between.so my question basically comes down to: can we dedup this somehow?
again, I'm not too worried about this. just seen some code that does stuff like:
this is obviously not a place to do it, but for mostly educational purposes I was asking about other options (patterns) do we usually have?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't worry about this too much and we can discuss this offline