generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use WMI instead of PowerShell for OS operations
- Loading branch information
Showing
12 changed files
with
1,398 additions
and
384 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
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,52 @@ | ||
package cim | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/microsoft/wmi/pkg/base/query" | ||
"github.com/microsoft/wmi/server2019/root/microsoft/windows/storage" | ||
) | ||
|
||
const ( | ||
PartitionStyleUnknown = 0 | ||
PartitionStyleGPT = 2 | ||
|
||
GPTPartitionTypeBasicData = "{ebd0a0a2-b9e5-4433-87c0-68b6b72699c7}" | ||
GPTPartitionTypeMicrosoftReserved = "{e3c9e316-0b5c-4db8-817d-f92df00215ae}" | ||
) | ||
|
||
func QueryDiskByNumber(diskNumber uint32, selectorList []string) (*storage.MSFT_Disk, error) { | ||
diskQuery := query.NewWmiQueryWithSelectList("MSFT_Disk", selectorList, "Number", strconv.Itoa(int(diskNumber))) | ||
instances, err := QueryInstances(WMINamespaceStorage, diskQuery) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
disk, err := storage.NewMSFT_DiskEx1(instances[0]) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to query disk %d. error: %v", diskNumber, err) | ||
} | ||
|
||
return disk, nil | ||
} | ||
|
||
func ListDisks(selectorList []string) ([]*storage.MSFT_Disk, error) { | ||
diskQuery := query.NewWmiQueryWithSelectList("MSFT_Disk", selectorList) | ||
instances, err := QueryInstances(WMINamespaceStorage, diskQuery) | ||
if IgnoreNotFound(err) != nil { | ||
return nil, err | ||
} | ||
|
||
var disks []*storage.MSFT_Disk | ||
for _, instance := range instances { | ||
disk, err := storage.NewMSFT_DiskEx1(instance) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to query disk %v. error: %v", instance, err) | ||
} | ||
|
||
disks = append(disks, disk) | ||
} | ||
|
||
return disks, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,249 @@ | ||
package cim | ||
|
||
import ( | ||
"fmt" | ||
"github.com/microsoft/wmi/pkg/base/query" | ||
cim "github.com/microsoft/wmi/pkg/wmiinstance" | ||
"github.com/microsoft/wmi/server2019/root/microsoft/windows/storage" | ||
"strconv" | ||
) | ||
|
||
func ListISCSITargetPortals(selectorList []string) ([]*storage.MSFT_iSCSITargetPortal, error) { | ||
q := query.NewWmiQueryWithSelectList("MSFT_IscsiTargetPortal", selectorList) | ||
instances, err := QueryInstances(WMINamespaceStorage, q) | ||
if IgnoreNotFound(err) != nil { | ||
return nil, err | ||
} | ||
|
||
var targetPortals []*storage.MSFT_iSCSITargetPortal | ||
for _, instance := range instances { | ||
portal, err := storage.NewMSFT_iSCSITargetPortalEx1(instance) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to query iSCSI target portal %v. error: %v", instance, err) | ||
} | ||
|
||
targetPortals = append(targetPortals, portal) | ||
} | ||
|
||
return targetPortals, nil | ||
} | ||
|
||
func QueryISCSITargetPortal(address string, port uint32, selectorList []string) (*storage.MSFT_iSCSITargetPortal, error) { | ||
portalQuery := query.NewWmiQueryWithSelectList( | ||
"MSFT_iSCSITargetPortal", selectorList, | ||
"TargetPortalAddress", address, | ||
"TargetPortalPortNumber", strconv.Itoa(int(port))) | ||
instances, err := QueryInstances(WMINamespaceStorage, portalQuery) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
targetPortal, err := storage.NewMSFT_iSCSITargetPortalEx1(instances[0]) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to query iSCSI target portal at (%s:%d). error: %v", address, port, err) | ||
} | ||
|
||
return targetPortal, nil | ||
} | ||
|
||
func NewISCSITargetPortal(targetPortalAddress string, | ||
targetPortalPortNumber uint32, | ||
initiatorInstanceName *string, | ||
initiatorPortalAddress *string, | ||
isHeaderDigest *bool, | ||
isDataDigest *bool) (*storage.MSFT_iSCSITargetPortal, error) { | ||
params := map[string]interface{}{ | ||
"TargetPortalAddress": targetPortalAddress, | ||
"TargetPortalPortNumber": targetPortalPortNumber, | ||
} | ||
if initiatorInstanceName != nil { | ||
params["InitiatorInstanceName"] = *initiatorInstanceName | ||
} | ||
if initiatorPortalAddress != nil { | ||
params["InitiatorPortalAddress"] = *initiatorPortalAddress | ||
} | ||
if isHeaderDigest != nil { | ||
params["IsHeaderDigest"] = *isHeaderDigest | ||
} | ||
if isDataDigest != nil { | ||
params["IsDataDigest"] = *isDataDigest | ||
} | ||
result, _, err := InvokeCimMethod(WMINamespaceStorage, "MSFT_iSCSITargetPortal", "New", params) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create iSCSI target portal with %v. result: %d, error: %v", params, result, err) | ||
} | ||
|
||
return QueryISCSITargetPortal(targetPortalAddress, targetPortalPortNumber, nil) | ||
} | ||
|
||
var ( | ||
mappingISCSIiTargetIndexer = mappingObjectRefIndexer("iSCSITarget", "MSFT_iSCSITarget", "NodeAddress") | ||
mappingISCSITargetPortalIndexer = mappingObjectRefIndexer("iSCSITargetPortal", "MSFT_iSCSITargetPortal", "TargetPortalAddress") | ||
mappingISCSIConnectionIndexer = mappingObjectRefIndexer("iSCSIConnection", "MSFT_iSCSIConnection", "ConnectionIdentifier") | ||
mappingISCSISessionIndexer = mappingObjectRefIndexer("iSCSISession", "MSFT_iSCSISession", "SessionIdentifier") | ||
|
||
iscsiTargetIndexer = stringPropertyIndexer("NodeAddress") | ||
iscsiTargetPortalIndexer = stringPropertyIndexer("TargetPortalAddress") | ||
iscsiConnectionIndexer = stringPropertyIndexer("ConnectionIdentifier") | ||
iscsiSessionIndexer = stringPropertyIndexer("SessionIdentifier") | ||
) | ||
|
||
func ListISCSITargetToISCSITargetPortalMapping() (map[string]string, error) { | ||
return ListWMIInstanceMappings(WMINamespaceStorage, "MSFT_iSCSITargetToiSCSITargetPortal", nil, mappingISCSIiTargetIndexer, mappingISCSITargetPortalIndexer) | ||
} | ||
|
||
func ListISCSIConnectionToISCSITargetMapping() (map[string]string, error) { | ||
return ListWMIInstanceMappings(WMINamespaceStorage, "MSFT_iSCSITargetToiSCSIConnection", nil, mappingISCSIConnectionIndexer, mappingISCSIiTargetIndexer) | ||
} | ||
|
||
func ListISCSISessionToISCSITargetMapping() (map[string]string, error) { | ||
return ListWMIInstanceMappings(WMINamespaceStorage, "MSFT_iSCSITargetToiSCSISession", nil, mappingISCSISessionIndexer, mappingISCSIiTargetIndexer) | ||
} | ||
|
||
func ListDiskToISCSIConnectionMapping() (map[string]string, error) { | ||
return ListWMIInstanceMappings(WMINamespaceStorage, "MSFT_iSCSIConnectionToDisk", nil, mappingObjectRefIndexer("Disk", "MSFT_Disk", "ObjectId"), mappingISCSIConnectionIndexer) | ||
} | ||
|
||
func ListISCSITargetByTargetPortalWithFilters(targetSelectorList []string, portals []*storage.MSFT_iSCSITargetPortal, filters ...*query.WmiQueryFilter) ([]*storage.MSFT_iSCSITarget, error) { | ||
targetQuery := query.NewWmiQueryWithSelectList("MSFT_iSCSITarget", targetSelectorList) | ||
targetQuery.Filters = append(targetQuery.Filters, filters...) | ||
instances, err := QueryInstances(WMINamespaceStorage, targetQuery) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var portalInstances []*cim.WmiInstance | ||
for _, portal := range portals { | ||
portalInstances = append(portalInstances, portal.WmiInstance) | ||
} | ||
|
||
targetToTargetPortalMapping, err := ListISCSITargetToISCSITargetPortalMapping() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
targetInstances, err := FindInstancesByMapping(instances, iscsiTargetIndexer, portalInstances, iscsiTargetPortalIndexer, targetToTargetPortalMapping) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var targets []*storage.MSFT_iSCSITarget | ||
for _, instance := range targetInstances { | ||
target, err := storage.NewMSFT_iSCSITargetEx1(instance) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to query iSCSI target %v. %v", instance, err) | ||
} | ||
|
||
targets = append(targets, target) | ||
} | ||
|
||
return targets, nil | ||
} | ||
|
||
func QueryISCSITarget(address string, port uint32, nodeAddress string, selectorList []string) (*storage.MSFT_iSCSITarget, error) { | ||
portal, err := QueryISCSITargetPortal(address, port, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
targets, err := ListISCSITargetByTargetPortalWithFilters(selectorList, []*storage.MSFT_iSCSITargetPortal{portal}, | ||
query.NewWmiQueryFilter("NodeAddress", nodeAddress, query.Equals)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return targets[0], nil | ||
} | ||
|
||
func QueryISCSISessionByTarget(target *storage.MSFT_iSCSITarget, selectorList []string) (*storage.MSFT_iSCSISession, error) { | ||
sessionQuery := query.NewWmiQueryWithSelectList("MSFT_iSCSISession", selectorList) | ||
sessionInstances, err := QueryInstances(WMINamespaceStorage, sessionQuery) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
targetToTargetSessionMapping, err := ListISCSISessionToISCSITargetMapping() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
filtered, err := FindInstancesByMapping(sessionInstances, iscsiSessionIndexer, []*cim.WmiInstance{target.WmiInstance}, iscsiTargetIndexer, targetToTargetSessionMapping) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
session, err := storage.NewMSFT_iSCSISessionEx1(filtered[0]) | ||
return session, err | ||
} | ||
|
||
func ListDisksByTarget(target *storage.MSFT_iSCSITarget, selectorList []string) ([]*storage.MSFT_Disk, error) { | ||
// list connections to the given iSCSI target | ||
connectionQuery := query.NewWmiQueryWithSelectList("MSFT_iSCSIConnection", selectorList) | ||
connectionInstances, err := QueryInstances(WMINamespaceStorage, connectionQuery) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
connectionToTargetMapping, err := ListISCSIConnectionToISCSITargetMapping() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
connectionsToTarget, err := FindInstancesByMapping(connectionInstances, iscsiConnectionIndexer, []*cim.WmiInstance{target.WmiInstance}, iscsiTargetIndexer, connectionToTargetMapping) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
disks, err := ListDisks(selectorList) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var diskInstances []*cim.WmiInstance | ||
for _, disk := range disks { | ||
diskInstances = append(diskInstances, disk.WmiInstance) | ||
} | ||
|
||
diskToConnectionMapping, err := ListDiskToISCSIConnectionMapping() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
filtered, err := FindInstancesByMapping(diskInstances, objectIDPropertyIndexer, connectionsToTarget, iscsiConnectionIndexer, diskToConnectionMapping) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var filteredDisks []*storage.MSFT_Disk | ||
for _, instance := range filtered { | ||
disk, err := storage.NewMSFT_DiskEx1(instance) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to query disk %v. error: %v", disk, err) | ||
} | ||
|
||
filteredDisks = append(filteredDisks, disk) | ||
} | ||
return filteredDisks, err | ||
} | ||
|
||
func ConnectISCSITarget(portalAddress string, portalPortNumber uint32, nodeAddress string, authType string, chapUsername *string, chapSecret *string) (int, map[string]interface{}, error) { | ||
inParams := map[string]interface{}{ | ||
"NodeAddress": nodeAddress, | ||
"TargetPortalAddress": portalAddress, | ||
"TargetPortalPortNumber": int(portalPortNumber), | ||
"AuthenticationType": authType, | ||
} | ||
// InitiatorPortalAddress | ||
// IsDataDigest | ||
// IsHeaderDigest | ||
// ReportToPnP | ||
if chapUsername != nil { | ||
inParams["ChapUsername"] = *chapUsername | ||
} | ||
if chapSecret != nil { | ||
inParams["ChapSecret"] = *chapSecret | ||
} | ||
|
||
result, outParams, err := InvokeCimMethod(WMINamespaceStorage, "MSFT_iSCSITarget", "Connect", inParams) | ||
return result, outParams, err | ||
} |
Oops, something went wrong.