-
Notifications
You must be signed in to change notification settings - Fork 1
Hcsshim Supporting Split Disk #20
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
base: private-peek
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import ( | |
| "errors" | ||
| "fmt" | ||
| "path" | ||
| "strconv" | ||
| "strings" | ||
| "time" | ||
|
|
||
|
|
@@ -63,6 +64,7 @@ type VirtualMachineOptions struct { | |
| SecureBootTemplateId string | ||
| HighMmioBaseInMB int32 | ||
| HighMmioGapInMB int32 | ||
| DataVhdPaths []string | ||
| } | ||
|
|
||
| const plan9Port = 564 | ||
|
|
@@ -83,6 +85,11 @@ func CreateVirtualMachineSpec(opts *VirtualMachineOptions) (*VirtualMachineSpec, | |
| if err := wclayer.GrantVmAccess(opts.Id, opts.IsoPath); err != nil { | ||
| return nil, err | ||
| } | ||
| for _, vhdPath := range opts.DataVhdPaths { | ||
| if err := wclayer.GrantVmAccess(opts.Id, vhdPath); err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| // determine which schema version to use | ||
| schemaVersion := getSchemaVersion(opts) | ||
|
|
@@ -131,6 +138,17 @@ func CreateVirtualMachineSpec(opts *VirtualMachineOptions) (*VirtualMachineSpec, | |
| }, | ||
| } | ||
|
|
||
| numberofdisk := len(spec.VirtualMachine.Devices.Scsi["primary"].Attachments) | ||
| for index, dataVhdPath := range opts.DataVhdPaths { | ||
| datavhdAttachment := hcsschema.Attachment{ | ||
| Path: dataVhdPath, | ||
| Type_: "VirtualDisk", | ||
| } | ||
|
|
||
| indexKey := fmt.Sprint(numberofdisk + index) | ||
| spec.VirtualMachine.Devices.Scsi["primary"].Attachments[indexKey] = datavhdAttachment | ||
| } | ||
|
|
||
| if len(opts.VnicId) > 0 { | ||
| spec.VirtualMachine.Devices.NetworkAdapters["ext"] = hcsschema.NetworkAdapter{ | ||
| EndpointId: opts.VnicId, | ||
|
|
@@ -731,3 +749,71 @@ func getSchemaVersion(opts *VirtualMachineOptions) hcsschema.Version { | |
| Minor: 1, | ||
| } | ||
| } | ||
|
|
||
| func (vm *VirtualMachineSpec) GetDiskInformation(diskPath string) (controlNumber int, err error) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| for key, value := range vm.spec.VirtualMachine.Devices.Scsi["primary"].Attachments { | ||
| if value.Path == diskPath { | ||
| controlNumber, err = strconv.Atoi(key) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| return 0, errors.New("diskpath not found in attached devices") | ||
| } | ||
|
|
||
| func (vm *VirtualMachineSpec) AttachVhd(vhdPath string) (lun int, err error) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| ctx, cancel := context.WithTimeout(context.Background(), 60*time.Minute) | ||
| defer cancel() | ||
| system, err := hcs.OpenComputeSystem(ctx, vm.ID) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| defer system.Close() | ||
|
|
||
| if err := wclayer.GrantVmAccess(vm.ID, vhdPath); err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| for lun := 0; lun < 256; lun++ { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as far as i can tell there is no way to find the next free lun though any hcs/hcsshim calls. unless we want to make a sperate service to track the lun for each vm we create, we will have to get it through trial and error. The spec will not help us in this case since it is not stored anywhere and will not be populated if we call the GetVirtualMachineSpec function to retrieve the VirtualMachineSpec struct. I can start the lun count at 2 since 0 and 1 are required for creation. |
||
| request := hcsschema.ModifySettingRequest{ | ||
| RequestType: requesttype.Add, | ||
| ResourcePath: fmt.Sprintf("VirtualMachine/Devices/Scsi/Primary/Attachments/%d", lun), | ||
| Settings: hcsschema.Attachment{ | ||
| Type_: "VirtualDisk", | ||
| Path: vhdPath, | ||
| }, | ||
| } | ||
| if err = system.Modify(ctx, request); err != nil { | ||
| // error caused by lun collision | ||
| if strings.Contains(err.Error(), "Unspecified error") { | ||
| continue | ||
| } else { | ||
| return 0, err | ||
| } | ||
| } else { | ||
| return lun, nil | ||
| } | ||
| } | ||
|
|
||
| return 0, errors.New("No available LUN for disk to be attached") | ||
| } | ||
|
|
||
| func (vm *VirtualMachineSpec) DetachVhd(lun int) (err error) { | ||
| ctx, cancel := context.WithTimeout(context.Background(), 60*time.Minute) | ||
| defer cancel() | ||
| system, err := hcs.OpenComputeSystem(ctx, vm.ID) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer system.Close() | ||
|
|
||
| request := hcsschema.ModifySettingRequest{ | ||
| RequestType: requesttype.Remove, | ||
| ResourcePath: fmt.Sprintf("VirtualMachine/Devices/Scsi/Primary/Attachments/%d", lun), | ||
| } | ||
| if err = system.Modify(ctx, request); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
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.
move these functions to line#722, keep them in group with other functions for VirtualMachineSpec.