VBOX: Update set_network_access(), get_guest_additions(), and get_version_information()

This commit is contained in:
Rom Walton 2014-10-16 22:05:28 -04:00 committed by Rom Walton
parent 91b236a6a9
commit c399c433f6
1 changed files with 83 additions and 91 deletions

View File

@ -1891,7 +1891,12 @@ int VBOX_VM::is_registered() {
retval = BOINC_SUCCESS; retval = BOINC_SUCCESS;
} }
SysFreeString(vm_name); if (vm_name) SysFreeString(vm_name);
if (pMachine) {
pMachine->Release();
pMachine = NULL;
}
return retval; return retval;
} }
@ -2039,63 +2044,40 @@ int VBOX_VM::get_install_directory(string& install_directory ) {
} }
int VBOX_VM::get_version_information(string& version) { int VBOX_VM::get_version_information(string& version) {
string command; int retval = ERR_EXEC;
string output; HRESULT rc;
int retval; BSTR tmp;
// Record the VirtualBox version information for later use. rc = g_pVirtualBox->get_VersionNormalized(&tmp);
command = "--version "; if (SUCCEEDED(rc)) {
retval = vbm_popen(command, output, "version check"); version = W2A(tmp);
retval = BOINC_SUCCESS;
if (!retval) {
// Remove \r or \n from the output spew
string::iterator iter = output.begin();
while (iter != output.end()) {
if (*iter == '\r' || *iter == '\n') {
iter = output.erase(iter);
} else {
++iter;
}
}
version = output;
} }
if (tmp) SysFreeString(tmp);
return retval; return retval;
} }
int VBOX_VM::get_guest_additions(string& guest_additions) { int VBOX_VM::get_guest_additions(string& guest_additions) {
string command; int retval = ERR_EXEC;
string output; HRESULT rc;
size_t ga_start; ISystemProperties* properties = NULL;
size_t ga_end; BSTR tmp;
int retval;
// Get the location of where the guest additions are rc = g_pVirtualBox->get_SystemProperties(&properties);
command = "list systemproperties"; if (SUCCEEDED(rc)) {
retval = vbm_popen(command, output, "guest additions"); rc = properties->get_DefaultAdditionsISO(&tmp);
if (SUCCEEDED(rc)) {
// Output should look like this: guest_additions = W2A(tmp);
// API version: 4_3 retval = BOINC_SUCCESS;
// Minimum guest RAM size: 4 Megabytes
// Maximum guest RAM size: 2097152 Megabytes
// Minimum video RAM size: 1 Megabytes
// Maximum video RAM size: 256 Megabytes
// ...
// Default Guest Additions ISO: C:\Program Files\Oracle\VirtualBox/VBoxGuestAdditions.iso
//
ga_start = output.find("Default Guest Additions ISO:");
if (ga_start == string::npos) {
return ERR_NOT_FOUND;
} }
ga_start += strlen("Default Guest Additions ISO:");
ga_end = output.find("\n", ga_start);
guest_additions = output.substr(ga_start, ga_end - ga_start);
strip_whitespace(guest_additions);
if (guest_additions.size() <= 0) {
return ERR_NOT_FOUND;
} }
if (tmp) SysFreeString(tmp);
if (properties) {
properties->Release();
properties = NULL;
}
return retval; return retval;
} }
@ -2233,10 +2215,10 @@ int VBOX_VM::get_vm_exit_code(unsigned long& exit_code) {
// machine is already behind the company firewall. // machine is already behind the company firewall.
// //
int VBOX_VM::set_network_access(bool enabled) { int VBOX_VM::set_network_access(bool enabled) {
string command;
string output;
char buf[256];
int retval; int retval;
char buf[256];
INetworkAdapter* pNetworkAdapter = NULL;
HRESULT rc = ERR_EXEC;
network_suspended = !enabled; network_suspended = !enabled;
@ -2246,24 +2228,49 @@ int VBOX_VM::set_network_access(bool enabled) {
"%s Enabling network access for VM.\n", "%s Enabling network access for VM.\n",
vboxwrapper_msg_prefix(buf, sizeof(buf)) vboxwrapper_msg_prefix(buf, sizeof(buf))
); );
command = "modifyvm \"" + vm_name + "\" ";
command += "--cableconnected1 on ";
retval = vbm_popen(command, output, "enable network"); rc = g_pMachine->GetNetworkAdapter(0, &pNetworkAdapter);
if (retval) return retval; if (FAILED(rc)) {
fprintf(
stderr,
"%s Error retrieving virtualized network adapter for VM! rc = 0x%x\n",
vboxwrapper_msg_prefix(buf, sizeof(buf)),
rc
);
}
rc = pNetworkAdapter->put_Enabled(TRUE);
if (SUCCEEDED(rc)) {
retval = BOINC_SUCCESS;
}
} else { } else {
fprintf( fprintf(
stderr, stderr,
"%s Disabling network access for VM.\n", "%s Disabling network access for VM.\n",
vboxwrapper_msg_prefix(buf, sizeof(buf)) vboxwrapper_msg_prefix(buf, sizeof(buf))
); );
command = "modifyvm \"" + vm_name + "\" ";
command += "--cableconnected1 off ";
retval = vbm_popen(command, output, "disable network"); rc = g_pMachine->GetNetworkAdapter(0, &pNetworkAdapter);
if (retval) return retval; if (FAILED(rc)) {
fprintf(
stderr,
"%s Error retrieving virtualized network adapter for VM! rc = 0x%x\n",
vboxwrapper_msg_prefix(buf, sizeof(buf)),
rc
);
} }
return 0;
rc = pNetworkAdapter->put_Enabled(FALSE);
if (SUCCEEDED(rc)) {
retval = BOINC_SUCCESS;
}
}
if (pNetworkAdapter) {
pNetworkAdapter->Release();
pNetworkAdapter = NULL;
}
return retval;
} }
int VBOX_VM::set_cpu_usage(int percentage) { int VBOX_VM::set_cpu_usage(int percentage) {
@ -2314,8 +2321,6 @@ int VBOX_VM::set_network_usage(int kilobytes) {
); );
} }
if (is_virtualbox_version_newer(4, 2, 0)) {
// Update bandwidth group limits // Update bandwidth group limits
// //
if (kilobytes == 0) { if (kilobytes == 0) {
@ -2337,19 +2342,6 @@ int VBOX_VM::set_network_usage(int kilobytes) {
if (retval) return retval; if (retval) return retval;
} }
} else {
sprintf(buf, "%d", kilobytes);
command = "modifyvm \"" + vm_name + "\" ";
command += "--nicspeed1 ";
command += buf;
command += " ";
retval = vbm_popen(command, output, "network throttle");
if (retval) return retval;
}
return 0; return 0;
} }