How to Remove VM and Related Files using API

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
MVS
Posts: 11
Joined: 27. Aug 2013, 15:08

How to Remove VM and Related Files using API

Post by MVS »

Hi sir,

I wanna automate entire process of Virtual machine in Virtual box from creating vm and boot the machine by attaching vhd or vmdk file and taking snapshot and after that i wanna remove the created vm and related backup files as well.

API I am using :: VirtualBoxSDK-4.2.16-86992
Platform :: Windows7 and Windows 2008 R2
Language :: C++

I have created machine using CreateMachine() function, I can able to create machine, start machine after that following issues troubling me alot

These are list of things i wanna know:
================================

1) In my VirtualBox.h Unregister() and delete() function definitions are as follows.

virtual HRESULT STDMETHODCALLTYPE Unregister( /* [in] */ CleanupMode aCleanupMode,
/* [retval][out] */ SAFEARRAY * *aAMedia) = 0;

virtual HRESULT STDMETHODCALLTYPE Delete( /* [in] */ SAFEARRAY * aAMedia,
/* [retval][out] */ IProgress **aAProgress) = 0;


In my c++ file iam using functions like this


SAFEARRAY * *aAMedia = NULL;

machine->Unregister(CleanupMode_UnregisterOnly , aAMedia);
machine->Delete(*aAMedia, &progress);



Here "Unregister()" not returning proper value for "aAMedia" . When iam trying to use the same "aAMedia" in my delete() function as input argument value function fails


2)And after starting machine by attaching either vhd or vmdk, is there any ways to check whether machine is booted successfully or not to take snapshot of machine? as of now iam setting sleep time and taking snapshot but through that i cnt say machine is succesfully booted or not


Please any one help me here to solve these issues

Regard's
MVS
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: How to Remove VM and Related Files using API

Post by noteirak »

1) A code sample with the exact error code AND message would help here.
2) by "booted", do you mean that the guest OS has finished loading? if so, it is not directly in the scope of Virtualbox, which is a hardware emulator. You could still start a guest process and see what is the state of the OS, but that require teh additions. Or do you mean something else by "booted"?
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
MVS
Posts: 11
Joined: 27. Aug 2013, 15:08

Re: How to Remove VM and Related Files using API

Post by MVS »

Thanks for your response,

As part of my coding related issue.

Here i am sharing my piece of code with you

Code: Select all

 else
    {
        ISession *session = NULL;
        IConsole *console = NULL;
        IProgress *progress = NULL;
        BSTR sessiontype = SysAllocString(L"gui");
        BSTR guid;

        do
        {
            rc = machine->get_Id(&guid); /* Get the GUID of the machine. */
            if (!SUCCEEDED(rc))
            {
                printf("Error retrieving machine ID! rc = 0x%x\n", rc);
                break;
            }

            /* Create the session object. */
            rc = CoCreateInstance(CLSID_Session,        /* the VirtualBox base object */
                                  NULL,                 /* no aggregation */
                                  CLSCTX_INPROC_SERVER, /* the object lives in a server process on this machine */
                                  IID_ISession,         /* IID of the interface */
                                  (void**)&session);
            if (!SUCCEEDED(rc))
            {
                printf("Error creating Session instance! rc = 0x%x\n", rc);
                break;
            }

            /* Start a VM session using the delivered VBox GUI. */
            rc = machine->LaunchVMProcess(session, sessiontype,
                                          NULL, &progress);
            if (!SUCCEEDED(rc))
            {
                printf("Could not open remote session! rc = 0x%x\n", rc);
                break;
            }

            /* Wait until VM is running. */
            printf ("Starting VM, please wait ...\n");
            rc = progress->WaitForCompletion (-1);

            /* Get console object. */
            session->get_Console(&console);

            /* Bring console window to front. */
            machine->ShowConsoleWindow(0);

			//Sleep(30000);
			
			//Take Screen shot and save it to file.
			//TakeScreenShot(console);

            printf ("Press enter to power off VM and close the session...\n");
            getchar();

            /* Power down the machine. */
            rc = console->PowerDown(&progress);

            /* Wait until VM is powered down. */
            printf ("Powering off VM, please wait...\n");
            rc = progress->WaitForCompletion (-1);

			SAFEARRAY * *aAMedia = NULL;

			machine->Unregister(CleanupMode_Full , aAMedia);
			machine->Delete(*aAMedia, &progress);

			machine->SaveSettings();

            /* Close the session. */
            rc = session->UnlockMachine();

        } while (0);

        SAFE_RELEASE(console);
        SAFE_RELEASE(progress);
        SAFE_RELEASE(session);
        SysFreeString(guid);
        SysFreeString(sessiontype);
        SAFE_RELEASE(machine);
    }
Here at Delete() function call code breaking and throughing this alert

Unhandled exception at 0x000000013f361ef0 in VirtualBoxAPI.exe: 0xC0000005: Access violation reading location 0x0000000000000000.

because here "aAMedia" is null, now i wanna remove the vm from "Oracle VM Virtualbox manager" and the machine related log files and all. Help me to do that


2)Yes, how to get to know the guest OS has finished loading or not using the virtual box API version VirtualBoxSDK-4.2.16-86992, if i get the status like asking username and password to login to windows then i can tel that os loaded successfully, like that can you suggest me any way ?

Regard's
MVS
Last edited by mpack on 3. Sep 2013, 14:25, edited 1 time in total.
Reason: Added "code" tags.
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: How to Remove VM and Related Files using API

Post by noteirak »

1) Not an expert in C++ but I was wondering why you are using the variable aAMedia for this code instead of passing a reference like you did for every other variable before?

Code: Select all

machine->Unregister(CleanupMode_Full , aAMedia);
machine->Delete(*aAMedia, &progress);
Also, you call the following code after, which is boud to fail since the VM doesn't exist anymore. Wouldn't that be an issue aswell?

Code: Select all

machine->SaveSettings();

/* Close the session. */
rc = session->UnlockMachine();
2) Directly via the Vbox API, no there is no way to do such thing. The only way to do it is either via the guest additions and the guest processes, but that involves having the guest additions installed, or the same way you would do for a physical computer (check a port, try to run a command remotely, that sort of thing).
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
MVS
Posts: 11
Joined: 27. Aug 2013, 15:08

Re: How to Remove VM and Related Files using API

Post by MVS »

Thanks for your response,

I have finished 1st thing that remove and deleting vm and related files

Now I am looking for option to check machine os loaded successfully or not, with out installing guest additions, can you suggest me any way to do this using api or atleast using c++ language

Regard's
MVS
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: How to Remove VM and Related Files using API

Post by noteirak »

There is no way to properly check for that via Virtualbox alone.
You would need to do it the exact same way you would do it if you had a physical machine next to yours, and you have to find out if it started.

To do that, as I said, you would most likely try to connect to a specific port, and try to do some kind of network communication that prooves that the guest OS is started.
Another way, which seems difficult to put in place, is to watch the guest metrics and see when it stops using the disk a lot (which is typical behaviour for a loading OS) an you have an idle period. But that would depends on what's running in the machine. If you have a heavy program started at boot time, you won't see an idle time...

All in all, difficult task, which you won't be able to perform in a reliable fashion without at least some networking in place.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
Post Reply