summaryrefslogtreecommitdiff
path: root/source4/selftest/win/VMHost.pm
blob: 1f02f21c284a1f02cf7d0316e34a713c6da0e922 (plain)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/perl -w

# A perl object to provide a simple, unified method of handling some
# VMware Server VM management functions using the perl and VIX API's.
# Copyright Brad Henry <brad@samba.org> 2006
# Released under the GNU GPL version 3 or later.

# VMware Perl API
use VMware::VmPerl;
use VMware::VmPerl::VM;
use VMware::VmPerl::ConnectParams;

# VMware C bindings
use VMware::Vix::Simple;
use VMware::Vix::API::Constants;

# Create a class to abstract from the Vix and VMPerl APIs.
{ package VMHost;
	my $perl_vm = VMware::VmPerl::VM::new();
	my $perl_vm_credentials;
	my $vix_vm;
	my $vix_vm_host;

	my $err_code = 0;
	my $err_str = "";

	my $hostname;
	my $port;
	my $username;
	my $password;
	my $vm_cfg_path;
	my $guest_admin_username;
	my $guest_admin_password;

	sub error {
		my $old_err_code = $err_code;
		my $old_err_str = $err_str;
		$err_code = 0;
		$err_str = "";
		return ($old_err_code, $old_err_str);
	}

	# Power on the guest if it isn't already running.
	# Returns 0 when the guest is already running, and
	# if not, it waits until it is started.
	sub start_guest {
		my $vm_power_state = $perl_vm->get_execution_state();
		if (!defined($vm_power_state)) {
			($err_code, $err_str) = $perl_vm->get_last_error();
			return ($err_code);
		}
		if ($vm_power_state == VMware::VmPerl::VM_EXECUTION_STATE_OFF
			|| $vm_power_state ==
				VMware::VmPerl::VM_EXECUTION_STATE_SUSPENDED)
		{
			if (!$perl_vm->start()) {
				($err_code, $err_str) =
					$perl_vm->get_last_error();
				return ($err_code);
			}
			while ($perl_vm->get_tools_last_active() == 0) {
				sleep(60);
			}
		}
		return ($err_code);
	}

	sub host_connect {
		# When called as a method, the first parameter passed is the
		# name of the method. Called locally, this function will lose
		# the first parameter.
		shift @_;
		($hostname, $port, $username, $password, $vm_cfg_path,
			$guest_admin_username, $guest_admin_password) = @_;

		# Connect to host using vmperl api.
		$perl_vm_credentials =
			VMware::VmPerl::ConnectParams::new($hostname, $port,
				$username, $password);
		if (!$perl_vm->connect($perl_vm_credentials, $vm_cfg_path)) {
			($err_code, $err_str) = $perl_vm->get_last_error();
			undef $perl_vm;
			return ($err_code);
		}

		# Connect to host using vix api.
		($err_code, $vix_vm_host) =
			VMware::Vix::Simple::HostConnect(
				VMware::Vix::Simple::VIX_API_VERSION,
				VMware::Vix::Simple::VIX_SERVICEPROVIDER_VMWARE_SERVER,
				$hostname, $port, $username, $password,
				0, VMware::Vix::Simple::VIX_INVALID_HANDLE);
		if ($err_code != VMware::Vix::Simple::VIX_OK) {
			$err_str =
				VMware::Vix::Simple::GetErrorText($err_code);
			undef $perl_vm;
			undef $vix_vm;
			undef $vix_vm_host;
			return ($err_code);
		}

		# Power on our guest os if it isn't already running.
		$err_code = start_guest();
		if ($err_code != 0) {
			my $old_err_str = $err_str;
			$err_str = "Starting guest power after connect " .
					"failed: " . $old_err_str;
			undef $perl_vm;
			undef $vix_vm;
			undef $vix_vm_host;
			return ($err_code);
		}

		# Open VM.
		($err_code, $vix_vm) =
			VMware::Vix::Simple::VMOpen($vix_vm_host, $vm_cfg_path);
		if ($err_code != VMware::Vix::Simple::VIX_OK) {
			$err_str =
				VMware::Vix::Simple::GetErrorText($err_code);
			undef $perl_vm;
			undef $vix_vm;
			undef $vix_vm_host;
			return ($err_code);
		}

		# Login to $vix_vm guest OS.
		$err_code = VMware::Vix::Simple::VMLoginInGuest($vix_vm,
				$guest_admin_username, $guest_admin_password,
				0);
		if ($err_code != VMware::Vix::Simple::VIX_OK) {
			$err_str =
				VMware::Vix::Simple::GetErrorText($err_code);
			undef $perl_vm;
			undef $vix_vm;
			undef $vix_vm_host;
			return ($err_code);
		}
		return ($err_code);
	}

	sub host_disconnect {
		undef $perl_vm;

		$perl_vm = VMware::VmPerl::VM::new();
		if (!$perl_vm) {
			$err_code = 1;
			$err_str = "Error creating new VmPerl object";
		}

		undef $vix_vm;
		VMware::Vix::Simple::HostDisconnect($vix_vm_host);
		VMware::Vix::Simple::ReleaseHandle($vix_vm_host);
		return ($err_code);
	}

	sub host_reconnect {
		$err_code = host_disconnect();
		if ($err_code != 0) {
			my $old_err_str = $err_str;
			$err_str = "Disconnecting from host failed: " .
					$old_err_str;
			return ($err_code);
		}

		$err_code = host_connect(NULL, $hostname, $port, $username,
				$password, $vm_cfg_path, $guest_admin_username,
				$guest_admin_password);
		if ($err_code != 0) {
			my $old_err_str = $err_str;
			$err_str = "Re-connecting to host failed: " .
					$old_err_str;
			return ($err_code);
		}
		return ($err_code);
	}

	sub create_snapshot {
		my $snapshot;

		($err_code, $snapshot) =
			VMware::Vix::Simple::VMCreateSnapshot($vix_vm,
			"Snapshot", "Created by vm_setup.pl", 0,
			VMware::Vix::Simple::VIX_INVALID_HANDLE);

		VMware::Vix::Simple::ReleaseHandle($snapshot);

		if ($err_code != VMware::Vix::Simple::VIX_OK) {
			$err_str =
				VMware::Vix::Simple::GetErrorText($err_code);
			return $err_code;
		}

		$err_code = host_reconnect();
		if ($err_code != 0) {
			my $old_err_str = $err_str;
			$err_str = "Reconnecting to host after creating " .
					"snapshot: " . $old_err_str;
			return ($err_code);
		}
		return ($err_code);
	}

	sub revert_snapshot {
		# Because of problems with VMRevertToSnapshot(), we have to
		# rely on the guest having set 'Revert to Snapshot' following
		# a power-off event.
		$err_code = VMware::Vix::Simple::VMPowerOff($vix_vm, 0);
		if ($err_code != VMware::Vix::Simple::VIX_OK) {
			$err_str =
				VMware::Vix::Simple::GetErrorText($err_code);
			return $err_code;
		}

		# host_reconnect() will power-on a guest in a non-running state.
		$err_code = host_reconnect();
		if ($err_code != 0) {
			my $old_err_str = $err_str;
			$err_str = "Reconnecting to host after reverting " .
					"snapshot: " . $old_err_str;
			return ($err_code);
		}
		return ($err_code);
	}

	# $dest_path must exist. It doesn't get created.
	sub copy_files_to_guest {
		shift @_;
		my (%files) = @_;

		my $src_file;
		my $dest_file;

		foreach $src_file (keys(%files)) {
			$dest_file = $files{$src_file};
			$err_code =
				VMware::Vix::Simple::VMCopyFileFromHostToGuest(
					$vix_vm, $src_file, $dest_file, 0,
					VMware::Vix::Simple::VIX_INVALID_HANDLE);
			if ($err_code != VMware::Vix::Simple::VIX_OK) {
				$err_str = "Copying $src_file: " .
					VMware::Vix::Simple::GetErrorText(
						$err_code);
				return $err_code;
			}
		}
		return $err_code;
	}

	sub copy_to_guest {
		# Read parameters $src_path, $dest_path.
		shift @_;
		my ($src_path, $dest_dir) = @_;

		my $len = length($dest_dir);
		my $idx = rindex($dest_dir, '\\');
		if ($idx != ($len - 1)) {
			$err_code = -1;
			$err_str = "Destination $dest_dir must be a " .
					"directory path";
			return ($err_code);
		}

		# Create the directory $dest_path on the guest VM filesystem.
		my $cmd = "cmd.exe ";
		my $cmd_args = "/C MKDIR " . $dest_dir;
		$err_code = run_on_guest(NULL, $cmd, $cmd_args);
		if ( $err_code != 0) {
			my $old_err_str = $err_str;
			$err_str = "Creating directory $dest_dir on host: " .
					$old_err_str;
			return ($err_code);
		}

		# If $src_filepath specifies a file, create it in $dest_path
		# and keep the same name.
		# If $src_path is a directory, create the files it contains in
		# $dest_path, keeping the same names.
		$len = length($src_path);
		my %files;
		$idx = rindex($src_path, '/');
		if ($idx == ($len - 1)) {
			# $src_path is a directory.
			if (!opendir (DIR_HANDLE, $src_path)) {
				$err_code = -1;
				$err_str = "Error opening directory $src_path";
				return $err_code;
			}

			foreach $file (readdir DIR_HANDLE) {
				my $src_file = $src_path . $file;

				if (!opendir(DIR_HANDLE2, $src_file)) {
					# We aren't interested in subdirs.
					my $dest_path = $dest_dir . $file;
					$files{$src_file} = $dest_path;
				} else {
					closedir(DIR_HANDLE2);
				}
			}
		} else {
			# Strip if preceeding path from $src_path.
			my $src_file = substr($src_path, ($idx + 1), $len);
			my $dest_path = $dest_dir . $src_file;

			# Add $src_path => $dest_path to %files.
			$files{$src_path} = $dest_path;
		}

		$err_code = copy_files_to_guest(NULL, %files);
		if ($err_code != 0) {
			my $old_err_str = $err_str;
			$err_str = "Copying files to host after " .
				"populating %files: " . $old_err_str;
			return ($err_code);
		}
		return ($err_code);
	}

	sub run_on_guest {
		# Read parameters $cmd, $cmd_args.
		shift @_;
		my ($cmd, $cmd_args) = @_;

		$err_code = VMware::Vix::Simple::VMRunProgramInGuest($vix_vm,
				$cmd, $cmd_args, 0,
				VMware::Vix::Simple::VIX_INVALID_HANDLE);
		if ($err_code != VMware::Vix::Simple::VIX_OK) {
			$err_str = VMware::Vix::Simple::GetErrorText(
					$err_code);
			return ($err_code);
		}

		return ($err_code);
	}

	sub get_guest_ip {
		my $guest_ip = $perl_vm->get_guest_info('ip');

		if (!defined($guest_ip)) {
			($err_code, $err_str) = $perl_vm->get_last_error();
			return NULL;
		}

		if (!($guest_ip)) {
			$err_code = 1;
			$err_str = "Guest did not set the 'ip' variable";
			return NULL;
		}
		return $guest_ip;
	}

	sub DESTROY {
		host_disconnect();
		undef $perl_vm;
		undef $vix_vm_host;
	}
}

return TRUE;