-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcernvm-wrapper.cpp
More file actions
344 lines (302 loc) · 12.8 KB
/
cernvm-wrapper.cpp
File metadata and controls
344 lines (302 loc) · 12.8 KB
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
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2008 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
// wrapper.C
// wrapper program for CernVM - lets you use CernVM on BOINC
//
// Handles:
// - suspend/resume/quit/abort virtual machine
//
// Contributor: Jie Wu <jiewu AT cern DOT ch>
//
// Contributor: Daniel Lombraña González <teleyinex AT gmail DOT com>
#include <stdio.h>
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <time.h>
#include <stdlib.h>
#include "zlib.h"
#ifdef _WIN32
#include <windows.h>
#include <tchar.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
#include <stdio.h>
#include <conio.h>
#include <string.h>
#pragma hdrstop
#include "boinc_win.h"
#include "win_util.h"
#else
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "procinfo.h"
#endif
#include "boinc_api.h"
#include "diagnostics.h"
#include "filesys.h"
#include "parse.h"
#include "str_util.h"
#include "str_replace.h"
#include "util.h"
#include "error_numbers.h"
#include "graphics2.h"
#include "vbox.h"
int main(int argc, char** argv)
{
BOINC_OPTIONS options;
BOINC_STATUS status;
char buffer[2048]; // Enough size for the VBoxManage list vms output
unsigned int i;
//bool graphics = false;
bool headless = false;
bool vrde = false;
bool vm_name = false;
bool retval = false;
// Name for the VM vmdk filename
string cernvm = "cernvm.vmdk";
string resolved_name;
VM vm;
vm.poll_err_number = 0;
// Registering time for progress accounting
time_t init_secs = time (NULL);
for (i = 1; i < (unsigned int)argc; i++) {
if (!strcmp(argv[i], "--debug")) {
std::istringstream ArgStream(argv[i+1]);
if (ArgStream >> vm.debug_level)
if (vm.debug_level >= 4) {
cerr << "INFO: Setting DEBUG level to: " << vm.debug_level << endl;
}
}
if (!strcmp(argv[i], "--vmname")) {
vm.virtual_machine_name = argv[i+1];
if (vm.debug_level >= 3) {
cerr << "NOTICE: The name of the VM is: " << vm.virtual_machine_name << endl;
}
}
// --nthreads to use BOINC mt class
if (!strcmp(argv[i], "--nthreads")) {
vm.n_cpus = atoi(argv[i+1]);
}
}
// If the wrapper has not be called with the command line argument --vmname NAME, give a default name to the VM
if (vm.virtual_machine_name.empty()) {
vm.virtual_machine_name = "BOINC_VM";
}
memset(&options, 0, sizeof(options));
options.main_program = true;
options.check_heartbeat = true;
options.handle_process_control = true;
options.send_status_msgs = true;
boinc_init_options(&options);
#ifdef _WIN32
// Setting up the PATH for Windows machines:
if (!Helper::SettingWindowsPath()) {
cerr << "ERROR: Impossible to set VirtualBox path" << endl;
cerr << "Aborting!" << endl;
boinc_finish(0);
}
#endif
// First print the version of VirtualBox
string arg_list = " --version";
char version[BUFSIZE];
if (vbm_popen(arg_list, version, sizeof(version))) {
cerr << endl;
cerr << endl;
cerr << "====================================" << endl;
cerr << "VirtualBox version: " << version << endl;
cerr << "====================================" << endl;
}
// Get BOINC APP INIT DATA to set several values for the VM
boinc_get_init_data(aid);
// Check if we have to run the VM in headless mode
if (aid.project_preferences) {
if (parse_bool(aid.project_preferences, "vm_headless_mode", headless)) {
cerr << "NOTICE: User has set the VM to run in headless mode!" << endl;
headless = true;
}
else {
cerr << "NOTICE: Running the VM in full mode!" << endl;
headless = false;
}
}
else {
cerr << "WARNING: Impossible to get user preferences for the project" << endl;
}
// BOINC user name and authenticator to authenticate users in Co-Pilot
vm.boinc_username = aid.user_name;
vm.boinc_authenticator = aid.authenticator;
std::ostringstream tmp;
tmp << aid.userid;
vm.boinc_userid = tmp.str();
tmp.str("");
tmp << aid.hostid;
vm.boinc_hostid = tmp.str();
// BOINC user and host total credit
tmp.str("");
tmp << aid.host_total_credit;
vm.boinc_host_total_credit = tmp.str();
// Clean the ostringstream
tmp.str("");
tmp << aid.user_total_credit;
vm.boinc_user_total_credit = tmp.str();
// Multi-core debug information
if (vm.n_cpus > 2) {
vm.n_cpus = 2;
cerr << "=========================================================================================" << endl;
cerr << "WARNING: This Virtual Machine will get any performance improvement with more than 2 cores" << endl;
cerr << "WARNING: Forcing VM to use only 2 cores" << endl;
cerr << "=========================================================================================" << endl;
}
cerr << "This work unit will use " << vm.n_cpus << " cores" << endl;
// We check if the VM has already been created and launched
if (!vm.exists()) {
// First remove old versions
if (vm.debug_level >= 3) {
cerr << "NOTICE: Cleaning old VMs of the project..." << endl;
}
vm.remove();
if (vm.debug_level >= 3) {
cerr << "NOTICE: Cleaning completed" << endl;
}
std::ifstream f(PROGRESS_FN);
if (f.is_open()) {
if (vm.debug_level >= 3) {
cerr << "NOTICE: ProgressFile should not exists. Deleting it" << endl;
}
f.close();
remove(PROGRESS_FN);
}
// Then, Decompress the new VM.gz file
cerr << endl << "Initializing the VM..." << endl;
cerr << "Decompressing the VM" << endl;
retval = boinc_resolve_filename_s("cernvm.vmdk.gz", resolved_name);
if (retval) {
cerr << "ERROR: Impossible to resolve file name: cernvm.vmdk.gz" << endl;
cerr << "ERROR: Aborting WU" << endl;
boinc_finish(1);
}
Helper::unzip(resolved_name.c_str(), cernvm.c_str());
cerr << "Virtual Disk uncompressed. Ready to create the VM" << endl;
// Create VM and register
if (vm.debug_level >= 3) {
cerr << "NOTICE: Virtual machine name: " << vm.virtual_machine_name << endl;
}
cerr << "Registering a new VM from unzipped image..." << endl;
vm.create();
cerr << "VM successfully registered and created!" << endl;
}
else {
cerr << "VM exists, starting it..." << endl;
}
time_t elapsed_secs = 0;
long int t = 0;
double frac_done = 0, dif_secs = 0;
vm.start(vrde, headless);
vm.last_poll_point = time(NULL);
#ifdef APP_GRAPHICS
// create shared mem segment for graphics, and arrange to update it
Share::data = (Share::SharedData*)boinc_graphics_make_shmem("cernvm", sizeof(Share::SharedData));
if (!Share::data) {
cerr << "ERROR: failed to created shared mem segment" << endl;
}
Helper::update_shmem();
boinc_register_timer_callback(Helper::update_shmem);
#endif
cerr << "DEBUG level: " << vm.debug_level << endl;
while (1) {
boinc_get_status(&status);
poll_boinc_messages(vm, status);
// Report progress to BOINC client
if (!status.suspended) {
vm.poll();
if (vm.suspended) {
if (vm.debug_level >= 2) {
cerr << "WARNING: VM should be running as the WU is not suspended" << endl;
}
vm.resume();
}
elapsed_secs = time(NULL);
dif_secs = Helper::update_progress(difftime(elapsed_secs,init_secs));
// Convert it for Windows machines:
t = static_cast<int>(dif_secs);
if (vm.debug_level >= 4) {
cerr << "INFO: Running seconds " << dif_secs << endl;
}
// For 24 hours:
frac_done = floor((t / 86400.0) * 100.0) / 100.0;
if (vm.debug_level >= 4) {
cerr << "INFO: Fraction done " << frac_done << endl;
}
// Checkpoint for reporting correctly the time
boinc_time_to_checkpoint();
boinc_checkpoint_completed();
boinc_fraction_done(frac_done);
if (frac_done >= 1.0) {
if (vm.debug_level >= 3) {
cerr << "NOTICE: Stopping the VM..." << endl;
}
vm.savestate();
if (vm.debug_level >= 3) {
cerr << "NOTICE: VM stopped!" << endl;
}
vm.remove();
// Update the ProgressFile for starting from zero next WU
Helper::write_progress(0);
if (vm.debug_level >= 3) {
cerr << "NOTICE: Work Unit completed" << endl;
cerr << "NOTICE: Creating output file..." << endl;
}
std::ofstream f("output");
if (f.is_open()) {
if (f.good()) {
f << "Work Unit completed!\n";
f.close();
}
}
if (vm.debug_level >= 3) {
cerr << "NOTICE: Done!" << endl;
}
#ifdef APP_GRAPHICS
Helper::update_shmem();
#endif
boinc_finish(0);
}
else {
init_secs = elapsed_secs;
boinc_sleep(POLL_PERIOD);
}
}
else {
init_secs = time(NULL);
boinc_sleep(POLL_PERIOD);
}
}
}
#ifdef _WIN32
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR Args, int WinMode) {
LPSTR command_line;
char* argv[100];
int argc;
command_line = GetCommandLine();
argc = parse_command_line(command_line, argv);
return main(argc, argv);
}
#endif