-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaws.pas
175 lines (157 loc) · 4.93 KB
/
aws.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
unit aws;
{$mode delphi}
interface
uses
Classes,
studio_tools,
SysUtils,
Variants,
ComObj,
ActiveX;
procedure FindAWSBlockDevices;
function IsAWS : Boolean;
function GetAWSVolumeType(DiskNumber : Integer) : String;
function GetAWSVolumeName(DiskNumber : Integer) : String;
procedure OnlineDisk(DiskNumber : Integer);
implementation
var
VolumeType : TStringList;
VolumeName : TStringList;
function GetAWSVolumeType(DiskNumber : Integer) : String;
var
DiskNo : String;
begin
if not Assigned(VolumeType) then
begin
FindAWSBlockDevices;
end;
DiskNo := IntToStr(DiskNumber);
Result := VolumeType.Values[DiskNo];
end;
function GetAWSVolumeName(DiskNumber : Integer) : String;
var
DiskNo : String;
begin
if not Assigned(VolumeName) then
begin
FindAWSBlockDevices;
end;
DiskNo := IntToStr(DiskNumber);
Result := VolumeName.Values[DiskNo];
end;
// Use WMI to try and work out if this is an AWS/Xen instance
function IsAWS : Boolean;
var
FSWbemLocator : OLEVariant;
FWMIService : OLEVariant;
FWbemObjectSet: OLEVariant;
FWbemObject : OLEVariant;
oEnum : IEnumvariant;
iValue : LongWord;
Version : String;
begin
Result := False;
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer('localhost', 'Root\cimv2', '', '');
FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM win32_bios','WQL');
//get the enumerator
oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
//traverse the data
while oEnum.Next(1, FWbemObject, iValue) = 0 do
begin
if EndsWith(FWbemObject.SMBIOSBIOSVersion, 'amazon', version) then
begin
Result := true;
end
end
end;
procedure FindAWSBlockDevices;
var
FSWbemLocator : OLEVariant;
FWMIService : OLEVariant;
FWbemObjectSet: OLEVariant;
FWbemObject : OLEVariant;
oEnum : IEnumvariant;
iValue : LongWord;
Location : TStringList;
Target : String;
IdStr : String;
Id : Integer;
DiskNumber : String;
begin
VolumeType := TStringList.Create;
VolumeName := TStringList.Create;
if IsAWS then
begin
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer('localhost', 'Root\Microsoft\Windows\Storage', '', '');
FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM MSFT_PhysicalDisk','WQL');
//get the enumerator
oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
//traverse the data
while oEnum.Next(1, FWbemObject, iValue) = 0 do
begin
if (FWbemObject.BusType = 1) {SCSI} and (FWbemObject.Manufacturer = 'AWS') then
begin
Location := TStringList.Create;
Location.StrictDelimiter := True;
Location.Delimiter := ':';
Location.DelimitedText := FWbemObject.PhysicalLocation;
Target := Trim(Location[3]);
if StartsWith(Target, 'Target ', IdStr) then
begin
DiskNumber := FWbemObject.DeviceId;
Id := StrToIntDef(IdStr, -1);
if Id >= 0 then
begin
// We got it at last
if Id = 0 then
begin
VolumeName.Values[DiskNumber] := '/dev/sda1';
VolumeType.Values[DiskNumber] := 'ROOT';
end
else if Id <= 25 then
begin
VolumeName.Values[DiskNumber] := 'xvd' + Chr(Id + Ord('a'));
VolumeType.Values[DiskNumber] := 'EBS';
end
else if (Id >= 78) and (Id <= 89) then
begin
VolumeName.Values[DiskNumber] := 'xvdc' + Chr(id - 78 + Ord('a'));
VolumeType.Values[DiskNumber] := 'INSTANCE';
end;
end;
end;
// DiskNumber := FWbemObject.Number;
Location.Free;
end;
FWbemObject:=Unassigned;
end
end
end;
procedure OnlineDisk(DiskNumber : Integer);
var
FSWbemLocator : OLEVariant;
FWMIService : OLEVariant;
FWbemObjectSet: OLEVariant;
FWbemObject : OLEVariant;
oEnum : IEnumvariant;
iValue : LongWord;
Version : String;
begin
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer('localhost', 'Root\Microsoft\Windows\Storage', '', '');
FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM MSFT_Disk','WQL');
//get the enumerator
oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
//traverse the data
while oEnum.Next(1, FWbemObject, iValue) = 0 do
begin
if StrToInt(FWbemObject.Number) = DiskNumber then
begin
FWbemObject.Online();
FWbemObject.SetAttributes(False); // Set the ReadOnly attribute
end
end
end;
end.