creating virtual machine at different location apart from default path

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
AdityaM
Posts: 31
Joined: 8. Nov 2016, 06:12

creating virtual machine at different location apart from default path

Post by AdityaM »

Hello,
I am using VirtualBox APIs to create a new virtual machine. I am using following code -

Code: Select all

rc = virtualBox->CreateMachine(NULL, machineClonedName,NULL,NULL,NULL,&machineClone); //Working

if (!FAILED(rc))
{
    rc = machine->CloneTo(machineClone,mode,NULL,&progress);
    if (!FAILED(rc))
    {
        rc = progress->WaitForCompletion (-1);  //This is not good to wait indefinitely
        if(registration)
            rc = virtualBox->RegisterMachine(machineClone);
    }
    else
    {
         //do something
    }
}
else
{
      //do something
}
The problem with using "CreateMachine" the way I am using is (first parameter is NULL) that it creates the virtual machines at default location which is 'Users' folder. I want to create the machine at a different path. How do I do that? From the SDK documentation I came to know about 'settingsfile' path but I am not able to clearly understand it. Am I required to use "composeMachineFilename" function, if yes how?
Please help me understand this thing.
Thanks
Last edited by socratis on 29. Nov 2016, 12:04, edited 1 time in total.
Reason: Enclosed the code in [code] tag for better readability
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: creating virtual machine at different location apart from default path

Post by noteirak »

composeMachineFilename is a helper method which exists to ensure any API call can get a consistent path for new VMs according to user configuration. You don't have to use it.
If you don't specify a settingPath when calling createMachine, this method is called using ISystemProperties::defaultMachineFolder as basePath - you could do it yourself too and all would be as usual.

If you want to create that VM in another directory as a one-off, you can either supply whatever path you want, or you can simply give your basePath to composeMachineFilename so the VM folder and settings filename remain consistent.
If you want to create several VM in the same base path but keep the usual structure with a folder than a settings file which have the VM name, I would recommend simply changing ISystemProperties::defaultMachineFolder to another location (and revert it back afterwards if needed) so you don't have to do things manually.

TL;DR: composeMachineFilename is only a helper method and its use is optional. Either use it to compute the VM settings file path or supply a full path yourself.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
AdityaM
Posts: 31
Joined: 8. Nov 2016, 06:12

Re: creating virtual machine at different location apart from default path

Post by AdityaM »

Hello,
Thanks for your reply. If I try to understand it correctly, when you say "If you want to create that VM in another directory as a one-off, you can either supply whatever path you want" Does that mean I can give the absolute path (like maybe "C:\Program Files") as first argument to the createMachine() function. Because I tried doing so and it was failing.

Also please correct my understanding if I am wrong - that according to SDKref documentation createMachine() function Creates a new virtual machine by creating a machine settings file at the given location; so if I am passing NULL to this function as first argument; then after the function is executed I should be able to see a .VBOX (Settings File) at ..\users\username\VirtualBox VMs\VBox name folder, but it is not so. I am not able to see any new file or folder created after the createMachine() function is executed.

But as per your reply " I would recommend simply changing ISystemProperties::defaultMachineFolder to another location (and revert it back afterwards if needed)" I would like to try out this strategy. So what I would like to do is something like below pseudo code -

Code: Select all

//get the default Machine folder; and store it in some variable
ISystemProperties::get_DefaultMachineFolder (def_path);

//change the the default Machine folder (assuming the path for location of new VM to be created is stored in new_path)
ISystemProperties::put_DefaultMachineFolder (new_path);

//now create the new VM
rc = virtualBox->CreateMachine(NULL, machineClonedName,NULL,NULL,NULL,&machineClone); //Working
if (!FAILED(rc))
{
    rc = machine->CloneTo(machineClone,mode,NULL,&progress);
}

//and now restore the value of Default machine folder to its original
ISystemProperties::put_DefaultMachineFolder (def_path);
Do you think this would work? Can I do this (changing the Default Machine Folder in settings file ) at run-time i.e even when Virtual Box is running?
Please suggest.

Another thing that I would like to mention is that I was not able to use "ISystemProperties::get_DefaultMachineFolder " API as it was giving some error. So any suggestions on that as well would be appreciated.

Pardon me for asking these questions to so much details, but I am new to using Oracle Virtual Box APIs and am trying to learn them.

Thanks
Last edited by noteirak on 30. Nov 2016, 11:56, edited 1 time in total.
Reason: Added code tag
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: creating virtual machine at different location apart from default path

Post by noteirak »

Just so we are clear on terminology:
  • Full path: The path including the filename and extension, e.g. C:\Path\To\My\Vm\Vm.vbox
  • VM path: The path excluding the filename and extension, but including the base directory of the VM, e.g. C:\Path\To\My\Vm
  • Base path: The path excluding the filename and extension and the VM directory, e.g. C:\Path\To\My
AdityaM wrote:If I try to understand it correctly, when you say "If you want to create that VM in another directory as a one-off, you can either supply whatever path you want" Does that mean I can give the absolute path (like maybe "C:\Program Files") as first argument to the createMachine() function. Because I tried doing so and it was failing.
The first argument must be the full path, not the VM or Base path. If it still fails, please give the exact error code and full error message associated with it.
AdityaM wrote:Also please correct my understanding if I am wrong - that according to SDKref documentation createMachine() function Creates a new virtual machine by creating a machine settings file at the given location; so if I am passing NULL to this function as first argument; then after the function is executed I should be able to see a .VBOX (Settings File) at ..\users\username\VirtualBox VMs\VBox name folder, but it is not so. I am not able to see any new file or folder created after the createMachine() function is executed.
The first sentence is missleading. If you read further, the steps of how you would create a VM are clearly explained and the step #3 states that the settings file will not be created until you call IMachine::saveSettings.
So up to that point, the IMachine object is only in memory.
AdityaM wrote: But as per your reply " I would recommend simply changing ISystemProperties::defaultMachineFolder to another location (and revert it back afterwards if needed)" I would like to try out this strategy. So what I would like to do is something like below pseudo code -

Code: Select all

//get the default Machine folder; and store it in some variable
ISystemProperties::get_DefaultMachineFolder (def_path);

//change the the default Machine folder (assuming the path for location of new VM to be created is stored in new_path)
ISystemProperties::put_DefaultMachineFolder (new_path);

//now create the new VM
rc = virtualBox->CreateMachine(NULL, machineClonedName,NULL,NULL,NULL,&machineClone); //Working
if (!FAILED(rc))
{
    rc = machine->CloneTo(machineClone,mode,NULL,&progress);
}

//and now restore the value of Default machine folder to its original
ISystemProperties::put_DefaultMachineFolder (def_path);
Do you think this would work? Can I do this (changing the Default Machine Folder in settings file ) at run-time i.e even when Virtual Box is running?
Please suggest.
Yes this would work and yes you can change the default machine folder while VirtualBox and/or VMs are running.

AdityaM wrote:Another thing that I would like to mention is that I was not able to use "ISystemProperties::get_DefaultMachineFolder " API as it was giving some error. So any suggestions on that as well would be appreciated.
Without the exact error code and message, we cannot help I'm afraid.
AdityaM wrote:Pardon me for asking these questions to so much details, but I am new to using Oracle Virtual Box APIs and am trying to learn them.
There is nothing to pardon, that's what this forum is for! Ask away all the questions you want. I would just ask you to keep questions in related topics. If you have questions not related to create a VM at a different location than the default part but to another specific task you want to perform, just make a topic for it.
Or if you want to ask general questions spanning several topics, just create a topic called "questions about API" or something like that :) It will help new comers like you to find specific answers to specific questions.
If you would like sample code to help you on how to achieve something, you can have a look here if one already exists or you can just ask me directly and I'll write it (in Java)
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
AdityaM
Posts: 31
Joined: 8. Nov 2016, 06:12

Re: creating virtual machine at different location apart from default path

Post by AdityaM »

Thanks for your reply.
When you gave the clarification on terminologies regarding full path, I was able to get the required results. So thanks a lot for your support and encouragement
Regards
Post Reply