-
Notifications
You must be signed in to change notification settings - Fork 473
/
Copy pathvbox_vboxmanage.cpp
2238 lines (1923 loc) · 73.3 KB
/
vbox_vboxmanage.cpp
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2010-2012 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/>.
#ifdef _WIN32
#include "boinc_win.h"
#include "win_util.h"
#else
#include <algorithm>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <unistd.h>
#endif
using std::string;
#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 "procinfo.h"
#include "network.h"
#include "boinc_api.h"
#include "floppyio.h"
#include "vboxlogging.h"
#include "vboxwrapper.h"
#include "vbox_common.h"
#include "vbox_vboxmanage.h"
VBOX_VM::VBOX_VM() {
}
VBOX_VM::~VBOX_VM() {
}
int VBOX_VM::initialize() {
int rc = 0;
string old_path;
string new_path;
string command;
string output;
bool force_sandbox = false;
get_install_directory(virtualbox_install_directory);
// Prep the environment so we can execute the vboxmanage application
//
#ifdef _WIN32
if (!virtualbox_install_directory.empty()) {
old_path = getenv("PATH");
new_path = virtualbox_install_directory + ";" + old_path;
if (!SetEnvironmentVariable("PATH", const_cast<char*>(new_path.c_str()))) {
vboxlog_msg("Failed to modify the search path.");
}
}
#else
old_path = getenv("PATH");
if(boinc_file_exists("/usr/local/bin/VBoxManage")) {
new_path = "/usr/local/bin/:" + old_path;
}
if(boinc_file_exists("/usr/bin/VBoxManage")) {
new_path = "/usr/bin/:" + old_path;
}
// putenv does not copy its input buffer, so we must use setenv
if (setenv("PATH", const_cast<char*>(new_path.c_str()), 1)) {
vboxlog_msg("Failed to modify the search path.");
}
#endif
// Determine the VirtualBox home directory. Overwrite as needed.
//
if (getenv("VBOX_USER_HOME")) {
virtualbox_home_directory = getenv("VBOX_USER_HOME");
} else {
// If the override environment variable isn't specified then
// it is based of the current users HOME directory.
#ifdef _WIN32
virtualbox_home_directory = getenv("USERPROFILE");
#else
virtualbox_home_directory = getenv("HOME");
#endif
virtualbox_home_directory += "/.VirtualBox";
}
// On *nix style systems, VirtualBox expects
// that there is a home directory specified by environment variable.
// When it doesn't exist it attempts to store logging information
// in root's home directory.
// Bad things happen if the process attempts to use root's home directory.
//
// if the HOME environment variable is missing
// force VirtualBox to use a directory it
// has a reasonable chance of writing log files too.
#ifndef _WIN32
if (NULL == getenv("HOME")) {
force_sandbox = true;
}
#endif
// Set the location in which the VirtualBox Configuration files can be
// stored for this instance.
//
if (aid.using_sandbox || force_sandbox) {
virtualbox_home_directory = aid.project_dir;
virtualbox_home_directory += "/../virtualbox";
if (!boinc_file_exists(virtualbox_home_directory.c_str())) {
boinc_mkdir(virtualbox_home_directory.c_str());
}
#ifdef _WIN32
if (!SetEnvironmentVariable("VBOX_USER_HOME", const_cast<char*>(virtualbox_home_directory.c_str()))) {
vboxlog_msg("Failed to modify the search path.");
}
#else
// putenv does not copy its input buffer, so we must use setenv
if (setenv("VBOX_USER_HOME", const_cast<char*>(virtualbox_home_directory.c_str()), 1)) {
vboxlog_msg("Failed to modify the VBOX_USER_HOME path.");
}
#endif
}
#ifdef _WIN32
// Launch vboxsvc manually so that the DCOM subsystem won't be able too.
// Our version will have permission and direction to write
// its state information to the BOINC data directory.
//
launch_vboxsvc();
#endif
rc = get_version_information(
virtualbox_version_raw, virtualbox_version_display
);
if (rc) return rc;
get_guest_additions(virtualbox_guest_additions);
return 0;
}
int VBOX_VM::create_vm() {
string command;
string output;
string default_interface;
bool disable_acceleration = false;
char buf[256];
int retval;
vboxlog_msg("Create VM. (%s, slot#%d)", vm_master_name.c_str(), aid.slot);
// Reset VM name in case it was changed while deregistering a stale VM
//
vm_name = vm_master_name;
// Fixup chipset and drive controller information for known configurations
//
if (enable_isocontextualization) {
if ("PIIX4" == vm_disk_controller_model) {
vboxlog_msg("Updating drive controller type and model for desired configuration.");
vm_disk_controller_type = "sata";
vm_disk_controller_model = "IntelAHCI";
}
}
// Create and register the VM
//
command = "createvm ";
command += "--name \"" + vm_name + "\" ";
command += "--basefolder \"" + slot_dir_path + "\" ";
command += "--ostype \"" + os_name + "\" ";
command += "--register";
retval = vbm_popen(command, output, "create");
if (retval) return retval;
// Tweak the VM's Description
//
command = "modifyvm \"" + vm_name + "\" ";
command += "--description \"" + vm_master_description + "\" ";
vbm_popen(command, output, "modifydescription", false, false);
// Tweak the VM's Memory Size
//
vboxlog_msg("Setting Memory Size for VM. (%dMB)", (int)memory_size_mb);
snprintf(buf, sizeof(buf), "%d", (int)memory_size_mb);
command = "modifyvm \"" + vm_name + "\" ";
command += "--memory " + string(buf) + " ";
retval = vbm_popen(command, output, "modifymem");
if (retval) return retval;
// Tweak the VM's CPU Count
//
vboxlog_msg("Setting CPU Count for VM. (%s)", vm_cpu_count.c_str());
command = "modifyvm \"" + vm_name + "\" ";
command += "--cpus " + vm_cpu_count + " ";
retval = vbm_popen(command, output, "modifycpu");
if (retval) return retval;
// Tweak the VM's Chipset Options
//
vboxlog_msg("Setting Chipset Options for VM.");
command = "modifyvm \"" + vm_name + "\" ";
command += "--acpi on ";
command += "--ioapic on ";
if (is_hostrtc_set_to_utc()) {
command += "--rtcuseutc on ";
} else {
command += "--rtcuseutc off ";
}
retval = vbm_popen(command, output, "modifychipset");
if (retval) return retval;
// Tweak the VM's Graphics Controller Options
//
vboxlog_msg("Setting Graphics Controller Options for VM.");
snprintf(buf, sizeof(buf), "%d", (int)vram_size_mb);
command = "modifyvm \"" + vm_name + "\" ";
command += "--vram " + string(buf) + " ";
command += "--graphicscontroller " + vm_graphics_controller_type + " ";
retval = vbm_popen(command, output, "modifygraphicscontroller");
if (retval) return retval;
// Tweak the VM's Boot Options
//
vboxlog_msg("Setting Boot Options for VM.");
command = "modifyvm \"" + vm_name + "\" ";
if (boot_iso) {
command += "--boot1 dvd ";
command += "--boot2 disk ";
} else {
command += "--boot1 disk ";
command += "--boot2 dvd ";
}
command += "--boot3 none ";
command += "--boot4 none ";
retval = vbm_popen(command, output, "modifyboot");
if (retval) return retval;
// Tweak the VM's Network Configuration
//
if (network_bridged_mode) {
vboxlog_msg("Setting Network Configuration for Bridged Mode.");
command = "modifyvm \"" + vm_name + "\" ";
command += "--nic1 bridged ";
command += "--cableconnected1 off ";
retval = vbm_popen(command, output, "set bridged mode");
if (retval) return retval;
get_default_network_interface(default_interface);
vboxlog_msg("Setting Bridged Interface. (%s)", default_interface.c_str());
command = "modifyvm \"" + vm_name + "\" ";
command += "--bridgeadapter1 \"";
command += default_interface;
command += "\" ";
retval = vbm_popen(command, output, "set bridged interface");
if (retval) return retval;
} else {
vboxlog_msg("Setting Network Configuration for NAT.");
command = "modifyvm \"" + vm_name + "\" ";
command += "--nic1 nat ";
command += "--natdnsproxy1 on ";
command += "--cableconnected1 off ";
retval = vbm_popen(command, output, "set nat mode");
if (retval) return retval;
}
if (enable_network) {
vboxlog_msg("Enabling VM Network Access.");
command = "modifyvm \"" + vm_name + "\" ";
command += "--cableconnected1 on ";
retval = vbm_popen(command, output, "enable network");
if (retval) return retval;
} else {
vboxlog_msg("Disabling VM Network Access.");
command = "modifyvm \"" + vm_name + "\" ";
command += "--cableconnected1 off ";
retval = vbm_popen(command, output, "disable network");
if (retval) return retval;
}
// Tweak the VM's USB Configuration
//
vboxlog_msg("Disabling USB Support for VM.");
command = "modifyvm \"" + vm_name + "\" ";
command += "--usb off ";
vbm_popen(command, output, "modifyusb", false, false);
// Tweak the VM's COM Port Support
//
vboxlog_msg("Disabling COM Port Support for VM.");
command = "modifyvm \"" + vm_name + "\" ";
command += "--uart1 off ";
command += "--uart2 off ";
vbm_popen(command, output, "modifycom", false, false);
#ifndef __APPLE__
// Tweak the VM's LPT Port Support
//
vboxlog_msg("Disabling LPT Port Support for VM.");
command = "modifyvm \"" + vm_name + "\" ";
command += "--lpt1 off ";
command += "--lpt2 off ";
vbm_popen(command, output, "modifylpt", false, false);
#endif
// Tweak the VM's Audio Support
//
vboxlog_msg("Disabling Audio Support for VM.");
command = "modifyvm \"" + vm_name + "\" ";
if (is_virtualbox_version_newer(7, 0, 4)) {
command += "--audio-enabled off ";
} else {
command += "--audio none ";
}
vbm_popen(command, output, "modifyaudio", false, false);
// Tweak the VM's Clipboard Support
//
vboxlog_msg("Disabling Clipboard Support for VM.");
command = "modifyvm \"" + vm_name + "\" ";
command += "--clipboard disabled ";
vbm_popen(command, output, "modifyclipboard", false, false);
// Tweak the VM's Drag & Drop Support
//
vboxlog_msg("Disabling Drag and Drop Support for VM.");
command = "modifyvm \"" + vm_name + "\" ";
command += "--draganddrop disabled ";
vbm_popen(command, output, "modifydragdrop", false, false);
// Check to see if the processor supports hardware acceleration
// for virtualization.
// If it doesn't, disable the use of it in VirtualBox.
// Multi-core jobs require hardware
// acceleration and actually override this setting.
//
if (!strstr(aid.host_info.p_features, "vmx") && !strstr(aid.host_info.p_features, "svm")) {
vboxlog_msg("Hardware acceleration CPU extensions not detected. Disabling VirtualBox hardware acceleration support.");
disable_acceleration = true;
}
if (strstr(aid.host_info.p_features, "hypervisor")) {
vboxlog_msg("Running under Hypervisor. Disabling VirtualBox hardware acceleration support.");
disable_acceleration = true;
}
if (is_boinc_client_version_newer(7, 2, 16)) {
if (aid.vm_extensions_disabled) {
vboxlog_msg("Hardware acceleration failed with previous execution. Disabling VirtualBox hardware acceleration support.");
disable_acceleration = true;
}
} else {
if (vm_cpu_count == "1") {
// Keep this around for older clients.
// Removing this for older clients might
// lead to a machine that will only return crashed VM reports.
vboxlog_msg("Legacy fallback configuration detected. Disabling VirtualBox hardware acceleration support.");
vboxlog_msg("NOTE: Upgrading to BOINC 7.2.16 or better may re-enable hardware acceleration.");
disable_acceleration = true;
}
}
if (boinc_is_standalone()) {
disable_acceleration = false;
}
// Only allow disabling of hardware acceleration on 32-bit VM types,
// 64-bit VM types require it.
//
if (os_name.find("_64") == string::npos) {
if (disable_acceleration) {
vboxlog_msg("Disabling hardware acceleration support for virtualization.");
command = "modifyvm \"" + vm_name + "\" ";
command += "--hwvirtex off ";
retval = vbm_popen(command, output, "VT-x/AMD-V support");
if (retval) return retval;
}
} else if (os_name.find("_64") != string::npos) {
if (disable_acceleration) {
vboxlog_msg("ERROR: Invalid configuration. VM type requires acceleration but the current configuration cannot support it.");
return ERR_INVALID_PARAM;
}
}
// Add storage controller to VM
// See: http://www.virtualbox.org/manual/ch08.html#vboxmanage-storagectl
// See: http://www.virtualbox.org/manual/ch05.html#iocaching
//
vboxlog_msg("Adding storage controller(s) to VM.");
command = "storagectl \"" + vm_name + "\" ";
command += "--name \"Hard Disk Controller\" ";
command += "--add \"" + vm_disk_controller_type + "\" ";
command += "--controller \"" + vm_disk_controller_model + "\" ";
if (
(vm_disk_controller_type == "sata") || (vm_disk_controller_type == "SATA") ||
(vm_disk_controller_type == "scsi") || (vm_disk_controller_type == "SCSI") ||
(vm_disk_controller_type == "sas") || (vm_disk_controller_type == "SAS")
) {
command += "--hostiocache off ";
}
if ((vm_disk_controller_type == "sata") || (vm_disk_controller_type == "SATA")) {
if (is_virtualbox_version_newer(4, 3, 0)) {
command += "--portcount 3";
} else {
command += "--sataportcount 3";
}
}
retval = vbm_popen(command, output, "add storage controller (fixed disk)");
if (retval) return retval;
// Add storage controller for a floppy device if desired
//
if (enable_floppyio) {
command = "storagectl \"" + vm_name + "\" ";
command += "--name \"Floppy Controller\" ";
command += "--add floppy ";
retval = vbm_popen(command, output, "add storage controller (floppy)");
if (retval) return retval;
}
if (enable_isocontextualization) {
// Add virtual ISO 9660 disk drive to VM
//
vboxlog_msg("Adding virtual ISO 9660 disk drive to VM. (%s)", iso_image_filename.c_str());
command = "storageattach \"" + vm_name + "\" ";
command += "--storagectl \"Hard Disk Controller\" ";
command += "--port 0 ";
command += "--device 0 ";
command += "--type dvddrive ";
command += "--medium \"" + slot_dir_path + "/" + iso_image_filename + "\" ";
retval = vbm_popen(command, output, "storage attach (ISO 9660 image)");
if (retval) return retval;
// Add guest additions to the VM
//
if (virtualbox_guest_additions.size()) {
vboxlog_msg("Adding VirtualBox Guest Additions to VM.");
command = "storageattach \"" + vm_name + "\" ";
command += "--storagectl \"Hard Disk Controller\" ";
command += "--port 2 ";
command += "--device 0 ";
command += "--type dvddrive ";
command += "--medium \"" + virtualbox_guest_additions + "\" ";
retval = vbm_popen(command, output, "storage attach (guest additions image)");
if (retval) return retval;
}
// Add a virtual cache disk drive to VM
//
if (enable_cache_disk){
vboxlog_msg("Adding virtual cache disk drive to VM. (%s)", cache_disk_filename.c_str());
command = "storageattach \"" + vm_name + "\" ";
command += "--storagectl \"Hard Disk Controller\" ";
command += "--port 1 ";
command += "--device 0 ";
command += "--type hdd ";
command += "--setuuid \"\" ";
command += "--medium \"" + slot_dir_path + "/" + cache_disk_filename + "\" ";
retval = vbm_popen(command, output, "storage attach (cached disk)");
if (retval) return retval;
}
} else {
// Adding virtual hard drive to VM
//
string command_fix_part;
command_fix_part = "storageattach \"" + vm_name + "\" ";
command_fix_part += "--storagectl \"Hard Disk Controller\" ";
command_fix_part += "--port 0 ";
command_fix_part += "--device 0 ";
command_fix_part += "--type hdd ";
if (!multiattach_vdi_file.size()) {
// the traditional method:
// copy the vdi file from the projects dir to the slots dir and rename it vm_image.vdi
// each copy must get a new (random) UUID
//
vboxlog_msg("Adding virtual disk drive to VM. (%s)", image_filename.c_str());
command = command_fix_part;
command += "--setuuid \"\" ";
command += "--medium \"" + slot_dir_path + "/" + image_filename + "\" ";
retval = vbm_popen(command, output, "storage attach (fixed disk)");
if (retval) return retval;
} else {
// Use MultiAttach mode and differencing images
// See: https://www.virtualbox.org/manual/ch05.html#hdimagewrites
// https://www.virtualbox.org/manual/ch05.html#diffimages
// the vdi file downloaded to the projects dir becomes the parent (read only)
// each task gets it's own differencing image (writable)
// differencing images are written to the VM's snapshot folder
//
string medium_file = aid.project_dir;
medium_file += "/" + multiattach_vdi_file;
vboxlog_msg("Adding virtual disk drive to VM. (%s)", multiattach_vdi_file.c_str());
int retry_count = 0;
bool log_error = false;
bool vbox_bug_mitigation = false;
do {
string set_new_uuid = "";
string type_line = "";
size_t type_start;
size_t type_end;
command = "showhdinfo \"" + medium_file + "\" ";
retval = vbm_popen(command, output, "check if parent hdd is registered", false);
if (retval) {
// showhdinfo implicitly registers unregistered hdds.
// Hence, this has to be handled first.
//
if ((output.rfind("VBoxManage: error:", 0) != string::npos) &&
(output.find("Cannot register the hard disk") != string::npos) &&
(output.find("because a hard disk") != string::npos) &&
(output.find("with UUID") != string::npos) &&
(output.find("already exists") != string::npos)) {
// May happen if the project admin didn't set a new UUID.
set_new_uuid = "--setuuid \"\" ";
vboxlog_msg("Disk UUID conflicts with an already existing disk.\nWill set a new UUID for '%s'.\nThe project admin should be informed to do this server side running:\nvboxmanage clonemedium <inputfile> <outputfile>\n",
multiattach_vdi_file.c_str()
);
} else {
// other errors
vboxlog_msg("Error in check if parent hdd is registered.\nCommand:\n%s\nOutput:\n%s",
command.c_str(),
output.c_str()
);
return retval;
}
}
// Output from showhdinfo should look a little like this:
// UUID: c119bcaf-636c-41f6-86c9-384739a31339
// Parent UUID: base
// State: created
// Type: multiattach
// Location: C:\Users\romw\VirtualBox VMs\test2\test2.vdi
// Storage format: VDI
// Format variant: dynamic default
// Capacity: 2048 MBytes
// Size on disk: 2 MBytes
// Encryption: disabled
// Property: AllocationBlockSize=1048576
// Child UUIDs: dcb0daa5-3bf9-47cb-bfff-c65e74484615
//
type_line = output;
type_start = type_line.find("\nType: ") + 1;
type_end = type_line.find("\n", type_start) - type_start;
type_line = type_line.substr(type_start, type_end);
if (type_line.find("multiattach") == string::npos) {
// Parent hdd is not (yet) of type multiattach.
// Vdi files can't be registered and set to multiattach mode within 1 step.
// They must first be attached to a VM in normal mode, then detached from the VM
command = command_fix_part;
command += set_new_uuid + "--medium \"" + medium_file + "\" ";
retval = vbm_popen(command, output, "register parent vdi");
if (retval) return retval;
command = command_fix_part;
command += "--medium none ";
retval = vbm_popen(command, output, "detach parent vdi");
if (retval) return retval;
// the vdi file is now registered and ready
// to be attached in multiattach mode
}
do {
command = command_fix_part;
command += "--mtype multiattach ";
command += "--medium \"" + medium_file + "\" ";
retval = vbm_popen(command, output, "storage attach (fixed disk - multiattach mode)", log_error);
if (retval) {
// VirtualBox occasionally writes the 'MultiAttach'
// attribute to the disk entry in VirtualBox.xml
// although this is not allowed there.
// As a result all VMs trying to connect that disk fail.
// The error needs to be cleaned here
// to allow vboxwrapper to succeed even with
// uncorrected VirtualBox versions.
//
// After cleanup attaching the disk should be tried again.
if ((retry_count < 1) &&
(output.find("Cannot attach medium") != string::npos) &&
(output.find("the media type") != string::npos) &&
(output.find("MultiAttach") != string::npos) &&
(output.find("can only be attached to machines that were created with VirtualBox 4.0 or later") != string::npos)) {
// try to deregister the medium from the global media store
command = "closemedium \"" + medium_file + "\" ";
retval = vbm_popen(command, output, "deregister parent vdi");
if (retval) return retval;
retry_count++;
log_error = true;
boinc_sleep(1.0);
break;
}
if (retry_count >= 1) {
// in case of other errors or if retry also failed
vboxlog_msg("Error in storage attach (fixed disk - multiattach mode).\nCommand:\n%s\nOutput:\n%s",
command.c_str(),
output.c_str()
);
return retval;
}
retry_count++;
log_error = true;
boinc_sleep(1.0);
} else {
vbox_bug_mitigation = true;
break;
}
}
while (true);
}
while (!vbox_bug_mitigation);
}
// Add guest additions to the VM
//
if (virtualbox_guest_additions.size()) {
vboxlog_msg("Adding VirtualBox Guest Additions to VM.");
command = "storageattach \"" + vm_name + "\" ";
command += "--storagectl \"Hard Disk Controller\" ";
command += "--port 1 ";
command += "--device 0 ";
command += "--type dvddrive ";
command += "--medium \"" + virtualbox_guest_additions + "\" ";
retval = vbm_popen(command, output, "storage attach (guest additions image)");
if (retval) return retval;
}
}
// Adding virtual floppy disk drive to VM
//
if (enable_floppyio) {
// Put in place the FloppyIO abstraction
//
// NOTE: This creates the floppy.img file at runtime for use by the VM.
//
pFloppy = new FloppyIONS::FloppyIO(floppy_image_filename.c_str());
if (!pFloppy->ready()) {
vboxlog_msg("Creating virtual floppy image failed.");
vboxlog_msg("Error Code '%d' Error Message '%s'", pFloppy->error, pFloppy->errorStr.c_str());
return ERR_FWRITE;
}
vboxlog_msg("Adding virtual floppy disk drive to VM.");
command = "storageattach \"" + vm_name + "\" ";
command += "--storagectl \"Floppy Controller\" ";
command += "--port 0 ";
command += "--device 0 ";
command += "--medium \"" + slot_dir_path + "/" + floppy_image_filename + "\" ";
retval = vbm_popen(command, output, "storage attach (floppy disk)");
if (retval) return retval;
}
// Add network bandwidth throttle group
//
if (is_virtualbox_version_newer(4, 2, 0)) {
vboxlog_msg("Adding network bandwidth throttle group to VM. (Defaulting to 1024GB)");
command = "bandwidthctl \"" + vm_name + "\" ";
command += "add \"" + vm_name + "_net\" ";
command += "--type network ";
command += "--limit 1024G";
command += " ";
retval = vbm_popen(command, output, "network throttle group (add)");
if (retval) return retval;
}
// Enable the network adapter if a network connection is required.
//
if (enable_network) {
// set up port forwarding
//
if (pf_guest_port) {
VBOX_PORT_FORWARD pf;
pf.guest_port = pf_guest_port;
pf.host_port = pf_host_port;
if (!pf_host_port) {
retval = boinc_get_port(false, pf.host_port);
if (retval) return retval;
pf_host_port = pf.host_port;
}
port_forwards.push_back(pf);
}
for (unsigned int i=0; i<port_forwards.size(); i++) {
VBOX_PORT_FORWARD& pf = port_forwards[i];
vboxlog_msg("forwarding host port %d to guest port %d", pf.host_port, pf.guest_port);
// Add new firewall rule
//
snprintf(buf, sizeof(buf), ",tcp,%s,%d,,%d",
pf.is_remote?"":"127.0.0.1",
pf.host_port, pf.guest_port
);
command = "modifyvm \"" + vm_name + "\" ";
command += "--natpf1 \"" + string(buf) + "\" ";
retval = vbm_popen(command, output, "add updated port forwarding rule");
if (retval) return retval;
}
}
// If the VM wants to enable remote desktop for the VM do it here
//
if (enable_remotedesktop) {
vboxlog_msg("Enabling remote desktop for VM.");
if (!is_extpack_installed()) {
vboxlog_msg("Required extension pack not installed, remote desktop not enabled.");
} else {
retval = boinc_get_port(false, rd_host_port);
if (retval) return retval;
snprintf(buf, sizeof(buf), "%d", rd_host_port);
command = "modifyvm \"" + vm_name + "\" ";
command += "--vrde on ";
command += "--vrdeextpack default ";
command += "--vrdeauthlibrary default ";
command += "--vrdeauthtype null ";
command += "--vrdeport " + string(buf) + " ";
retval = vbm_popen(command, output, "remote desktop");
if (retval) return retval;
}
}
// share slot/ or slot/shared
//
if (enable_shared_directory || share_slot_dir) {
vboxlog_msg("Enabling shared directory for VM.");
command = "sharedfolder add \"" + vm_name + "\" ";
command += "--name \"shared\" ";
if (share_slot_dir) {
command += "--hostpath \"" + slot_dir_path + "\"";
} else {
command += "--hostpath \"" + slot_dir_path + "/shared\"";
}
retval = vbm_popen(command, output, "enable shared dir");
if (retval) return retval;
}
// share project/ or project/scratch
//
if (enable_scratch_directory || share_project_dir) {
vboxlog_msg("Enabling shared project directory for VM.");
command = "sharedfolder add \"" + vm_name + "\" ";
if (share_project_dir) {
command += "--name \"project\" ";
command += "--hostpath \"" + project_dir_path + "\"";
} else {
command += "--name \"scratch\" ";
command += "--hostpath \"" + project_dir_path + "/scratch\"";
}
retval = vbm_popen(command, output, "enable shared project dir");
if (retval) return retval;
}
return 0;
}
int VBOX_VM::register_vm() {
string command;
string output;
int retval;
vboxlog_msg("Register VM. (%s, slot#%d)", vm_master_name.c_str(), aid.slot);
// Reset VM name in case it was changed while deregistering a stale VM
//
vm_name = vm_master_name;
// Register the VM
//
command = "registervm ";
command += "\"" + slot_dir_path + "/" + vm_name + "/" + vm_name + ".vbox\" ";
retval = vbm_popen(command, output, "register");
if (retval) return retval;
return 0;
}
int VBOX_VM::deregister_vm(bool delete_media) {
string command;
string output;
vboxlog_msg("Deregistering VM. (%s, slot#%d)", vm_name.c_str(), aid.slot);
// Cleanup any left-over snapshots
//
cleanup_snapshots(true);
// Delete network bandwidth throttle group
//
if (is_virtualbox_version_newer(4, 2, 0)) {
vboxlog_msg("Removing network bandwidth throttle group from VM.");
command = "bandwidthctl \"" + vm_name + "\" ";
command += "remove \"" + vm_name + "_net\" ";
vbm_popen(command, output, "network throttle group (remove)", false, false);
}
if (enable_floppyio) {
command = "storagectl \"" + vm_name + "\" ";
command += "--name \"Floppy Controller\" ";
command += "--remove ";
vbm_popen(command, output, "deregister storage controller (floppy disk)", false, false);
}
// Next, delete VM
//
vboxlog_msg("Removing VM from VirtualBox.");
command = "unregistervm \"" + vm_name + "\" ";
command += "--delete ";
vbm_popen(command, output, "delete VM", false, false);
// Lastly delete medium(s) from Virtual Box Media Registry
//
if (enable_isocontextualization) {
vboxlog_msg("Removing virtual ISO 9660 disk from VirtualBox.");
command = "closemedium dvd \"" + slot_dir_path + "/" + iso_image_filename + "\" ";
if (delete_media) {
command += "--delete ";
}
vbm_popen(command, output, "remove virtual ISO 9660 disk", false, false);
if (enable_cache_disk) {
vboxlog_msg("Removing virtual cache disk from VirtualBox.");
command = "closemedium disk \"" + slot_dir_path + "/" + cache_disk_filename + "\" ";
if (delete_media) {
command += "--delete ";
}
vbm_popen(command, output, "remove virtual cache disk", false, false);
}
}
if (enable_floppyio) {
vboxlog_msg("Removing virtual floppy disk from VirtualBox.");
command = "closemedium floppy \"" + slot_dir_path + "/" + floppy_image_filename + "\" ";
if (delete_media) {
command += "--delete ";
}
vbm_popen(command, output, "remove virtual floppy disk", false, false);
}
return 0;
}
int VBOX_VM::deregister_stale_vm() {
string command;
string output;
size_t uuid_start;
size_t uuid_end;
int retval;
// Output from showhdinfo should look a little like this:
// UUID: c119acaf-636c-41f6-86c9-38e639a31339
// Accessible: yes
// Logical size: 10240 MBytes
// Current size on disk: 0 MBytes
// Type: normal (base)
// Storage format: VDI
// Format variant: dynamic default
// In use by VMs: test2 (UUID: 000ab2be-1254-4c6a-9fdc-1536a478f601)
// Location: C:\Users\romw\VirtualBox VMs\test2\test2.vdi
//
if (enable_isocontextualization) {
command = "showhdinfo \"" + slot_dir_path + "/" + cache_disk_filename + "\" ";
retval = vbm_popen(command, output, "get HDD info");
if (retval) return retval;
uuid_start = output.find("(UUID: ");
if (uuid_start != string::npos) {
// We can parse the virtual machine ID from the output
uuid_start += 7;
uuid_end = output.find(")", uuid_start);
vm_name = output.substr(uuid_start, uuid_end - uuid_start);
// Deregister stale VM by UUID
return deregister_vm(false);
} else {
command = "closemedium dvd \"" + slot_dir_path + "/" + iso_image_filename + "\" ";
// coverity[CHECKED_RETURN]
vbm_popen(command, output, "remove virtual ISO 9660 disk", false);
if (enable_cache_disk) {
command = "closemedium disk \"" + slot_dir_path + "/" + cache_disk_filename + "\" ";
// coverity[CHECKED_RETURN]
vbm_popen(command, output, "remove virtual cache disk", false);
}
}
} else {
command = "showhdinfo \"" + slot_dir_path + "/" + image_filename + "\" ";
retval = vbm_popen(command, output, "get HDD info");
if (retval) return retval;
uuid_start = output.find("(UUID: ");
if (uuid_start != string::npos) {
// We can parse the virtual machine ID from the output
uuid_start += 7;
uuid_end = output.find(")", uuid_start);
vm_name = output.substr(uuid_start, uuid_end - uuid_start);
// Deregister stale VM by UUID
return deregister_vm(false);
} else {
// Did the user delete the VM in VirtualBox and not the medium? If so,
// just remove the medium.
command = "closemedium disk \"" + slot_dir_path + "/" + image_filename + "\" ";
vbm_popen(command, output, "remove virtual disk", false, false);
if (enable_floppyio) {
command = "closemedium floppy \"" + slot_dir_path + "/" + floppy_image_filename + "\" ";
vbm_popen(command, output, "remove virtual floppy disk", false, false);
}
}
}
return 0;