Failed to create hard disk!

Discussion about using the VirtualBox API, Tutorials, Samples.
wuzf
Posts: 15
Joined: 30. May 2013, 10:57

Failed to create hard disk!

Post by wuzf »

I complie sdk/bindings/xpcom/samples/tstVBoxAPILinux.cpp and run it on ubuntu12.10, but it could not create a hard disk, the information as follows:
Error: could not find guest OS type! rc=80070057
Failed creating a hard disk object! rc=80BB0004
Error: could not open CD image! rc=80BB0001

why and how to solve, Thanks! I just contact with VirtualBox

source:

Code: Select all

void createVM(IVirtualBox *virtualBox)
{
    nsresult rc;
    /*
     * First create a unnamed new VM. It will be unconfigured and not be saved
     * in the configuration until we explicitely choose to do so.
     */
    nsCOMPtr<IMachine> machine;
    rc = virtualBox->CreateMachine(NULL,        /* settings file */
                                   NS_LITERAL_STRING("A brand new name").get(),
                                   0, nsnull,   /* groups (safearray)*/
                                   nsnull,      /* ostype */
                                   nsnull,      /* create flags */
                                   getter_AddRefs(machine));
    if (NS_FAILED(rc))
    {
        printf("Error: could not create machine! rc=%08X\n", rc);
        return;
    }

    /*
     * Set some properties
     */
    /* alternative to illustrate the use of string classes */
    rc = machine->SetName(NS_ConvertUTF8toUTF16("A new name").get());
    rc = machine->SetMemorySize(128);

    /*
     * Now a more advanced property -- the guest OS type. This is
     * an object by itself which has to be found first. Note that we
     * use the ID of the guest OS type here which is an internal
     * representation (you can find that by configuring the OS type of
     * a machine in the GUI and then looking at the <Guest ostype=""/>
     * setting in the XML file. It is also possible to get the OS type from
     * its description (win2k would be "Windows 2000") by getting the
     * guest OS type collection and enumerating it.
     */
    nsCOMPtr<IGuestOSType> osType;
    rc = virtualBox->GetGuestOSType(NS_LITERAL_STRING("win2k").get(),
                                    getter_AddRefs(osType));
    if (NS_FAILED(rc))
    {
        printf("Error: could not find guest OS type! rc=%08X\n", rc);
    }
    else
    {
        machine->SetOSTypeId (NS_LITERAL_STRING("win2k").get());
    }

    /*
     * Register the VM. Note that this call also saves the VM config
     * to disk. It is also possible to save the VM settings but not
     * register the VM.
     *
     * Also note that due to current VirtualBox limitations, the machine
     * must be registered *before* we can attach hard disks to it.
     */
    rc = virtualBox->RegisterMachine(machine);
    if (NS_FAILED(rc))
    {
        printf("Error: could not register machine! rc=%08X\n", rc);
        printErrorInfo();
        return;
    }

    /*
     * In order to manipulate the registered machine, we must open a session
     * for that machine. Do it now.
     */
    nsCOMPtr<ISession> session;
    {
        nsCOMPtr<nsIComponentManager> manager;
        rc = NS_GetComponentManager (getter_AddRefs (manager));
        if (NS_FAILED(rc))
        {
            printf("Error: could not get component manager! rc=%08X\n", rc);
            return;
        }
        rc = manager->CreateInstanceByContractID (NS_SESSION_CONTRACTID,
                                                  nsnull,
                                                  NS_GET_IID(ISession),
                                                  getter_AddRefs(session));
        if (NS_FAILED(rc))
        {
            printf("Error, could not instantiate session object! rc=0x%x\n", rc);
            return;
        }

        rc = machine->LockMachine(session, LockType_Write);
        if (NS_FAILED(rc))
        {
            printf("Error, could not lock the machine for the session! rc=0x%x\n", rc);
            return;
        }

        /*
         * After the machine is registered, the initial machine object becomes
         * immutable. In order to get a mutable machine object, we must query
         * it from the opened session object.
         */
        rc = session->GetMachine(getter_AddRefs(machine));
        if (NS_FAILED(rc))
        {
            printf("Error, could not get machine session! rc=0x%x\n", rc);
            return;
        }
    }

    /*
     * Create a virtual harddisk
     */
    nsCOMPtr<IMedium> hardDisk = 0;
    rc = virtualBox->CreateHardDisk(NS_LITERAL_STRING("VDI").get(),
                                    NS_LITERAL_STRING("TestHardDisk.vdi").get(),
                                    getter_AddRefs(hardDisk));
    if (NS_FAILED(rc))
    {
        printf("Failed creating a hard disk object! rc=%08X\n", rc);
    }
    else
    {
        /*
         * We have only created an object so far. No on disk representation exists
         * because none of its properties has been set so far. Let's continue creating
         * a dynamically expanding image.
         */
        nsCOMPtr <IProgress> progress;
        rc = hardDisk->CreateBaseStorage(100,                                // size in megabytes
                                         MediumVariant_Standard,
                                         getter_AddRefs(progress));          // optional progress object
        if (NS_FAILED(rc))
        {
            printf("Failed creating hard disk image! rc=%08X\n", rc);
        }
        else
        {
            /*
             * Creating the image is done in the background because it can take quite
             * some time (at least fixed size images). We have to wait for its completion.
             * Here we wait forever (timeout -1)  which is potentially dangerous.
             */
            rc = progress->WaitForCompletion(-1);
            PRInt32 resultCode;
            progress->GetResultCode(&resultCode);
            if (NS_FAILED(rc) || NS_FAILED(resultCode))
            {
                printf("Error: could not create hard disk! rc=%08X\n",
                       NS_FAILED(rc) ? rc : resultCode);
            }
            else
            {
                /*
                 * Now that it's created, we can assign it to the VM.
                 */
                rc = machine->AttachDevice(NS_LITERAL_STRING("IDE Controller").get(), // controller identifier
                                           0,                              // channel number on the controller
                                           0,                              // device number on the controller
                                           DeviceType_HardDisk,
                                           hardDisk);
                if (NS_FAILED(rc))
                {
                    printf("Error: could not attach hard disk! rc=%08X\n", rc);
                }
            }
        }
    }

    /*
     * It's got a hard disk but that one is new and thus not bootable. Make it
     * boot from an ISO file. This requires some processing. First the ISO file
     * has to be registered and then mounted to the VM's DVD drive and selected
     * as the boot device.
     */
    nsCOMPtr<IMedium> dvdImage;
    rc = virtualBox->OpenMedium(NS_LITERAL_STRING("/home/vbox/isos/winnt4ger.iso").get(),
                                DeviceType_DVD,
                                AccessMode_ReadOnly,
                                false /* fForceNewUuid */,
                                getter_AddRefs(dvdImage));
    if (NS_FAILED(rc))
        printf("Error: could not open CD image! rc=%08X\n", rc);
    else
    {
        /*
         * Now assign it to our VM
         */
        rc = machine->MountMedium(NS_LITERAL_STRING("IDE Controller").get(), // controller identifier
                                  2,                              // channel number on the controller
                                  0,                              // device number on the controller
                                  dvdImage,
                                  PR_FALSE);                      // aForce
        if (NS_FAILED(rc))
        {
            printf("Error: could not mount ISO image! rc=%08X\n", rc);
        }
        else
        {
            /*
             * Last step: tell the VM to boot from the CD.
             */
            rc = machine->SetBootOrder (1, DeviceType::DVD);
            if (NS_FAILED(rc))
            {
                printf("Could not set boot device! rc=%08X\n", rc);
            }
        }
    }

    /*
     * Save all changes we've just made.
     */
    rc = machine->SaveSettings();
    if (NS_FAILED(rc))
    {
        printf("Could not save machine settings! rc=%08X\n", rc);
    }

    /*
     * It is always important to close the open session when it becomes not
     * necessary any more.
     */
    session->UnlockMachine();
}
wuzf
Posts: 15
Joined: 30. May 2013, 10:57

Re: Failed to create hard disk!

Post by wuzf »

Failed creating a hard disk object! rc=80BB0004
I use absolute path when CreateHardDisk,as follows,rc=0, it successed
rc = virtualBox->CreateHardDisk(NS_LITERAL_STRING("VDI").get(),
NS_LITERAL_STRING("/home/VirtualBox_VM/TestHardDisk.vdi").get(),
getter_AddRefs(hardDisk));

but new error:
Error: could not create hard disk! rc=80BB0004

how to solve? Thanks!
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: Failed to create hard disk!

Post by noteirak »

You only get the return code. The acutal text of the error would help here as I have no idea what that error code refers to.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
wuzf
Posts: 15
Joined: 30. May 2013, 10:57

Re: Failed to create hard disk!

Post by wuzf »

I found VirtualBox COM result codes:
const HRESULT VBOX_E_OBJECT_NOT_FOUND = 0x80BB0001 Object corresponding to the supplied arguments does not exist.
const HRESULT VBOX_E_INVALID_VM_STATE = 0x80BB0002 Current virtual machine state prevents the operation.
const HRESULT VBOX_E_VM_ERROR = 0x80BB0003 Virtual machine error occurred attempting the operation.
const HRESULT VBOX_E_FILE_ERROR = 0x80BB0004 File not accessible or erroneous file contents.
const HRESULT VBOX_E_IPRT_ERROR = 0x80BB0005 Runtime subsystem error.
const HRESULT VBOX_E_PDM_ERROR = 0x80BB0006 Pluggable Device Manager error.
const HRESULT VBOX_E_INVALID_OBJECT_STATE = 0x80BB0007 Current object state prohibits operation.
const HRESULT VBOX_E_HOST_ERROR = 0x80BB0008 Host operating system related error.
const HRESULT VBOX_E_NOT_SUPPORTED = 0x80BB0009 Requested operation is not supported.
const HRESULT VBOX_E_XML_ERROR = 0x80BB000A Invalid XML found.
const HRESULT VBOX_E_INVALID_SESSION_STATE = 0x80BB000B Current session state prohibits operation.
wuzf
Posts: 15
Joined: 30. May 2013, 10:57

Re: Failed to create hard disk!

Post by wuzf »

I have two questions:
1,when get OS type, virtualBox->GetGuestOSType(NS_LITERAL_STRING("win2k").get(), getter_AddRefs(osType));return error code,but when virtualBox->GetGuestOSType(NS_LITERAL_STRING("ubuntu").get(), getter_AddRefs(osType)); it successed.
2,Before create hard disk, Do I need to add storageController to machine?
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: Failed to create hard disk!

Post by noteirak »

/home/VirtualBox_VM/TestHardDisk.vdi
This is the path you use, which seems incomplete, unless you're actually running this as the VirtualBox_VM user? You say that if you use the full path, it works. But then it doesn't work only if you use a local path?

#1 : win2k is not an valid OS type, Windows2000 is one tho.
#2 : No you don't need to
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
wuzf
Posts: 15
Joined: 30. May 2013, 10:57

Re: Failed to create hard disk!

Post by wuzf »

My default virtual machine location is : /home/VirtualBox_VM, code of create hard disk as follows:

Code: Select all

void createVM(IVirtualBox *virtualBox)
{
    nsresult rc;
    nsCOMPtr<IMachine> machine;
    rc = virtualBox->CreateMachine(NULL,  NS_LITERAL_STRING("WinXP").get(),0, nsnull,  nsnull,  nsnull, getter_AddRefs(machine));
    if (NS_FAILED(rc))
    {
        printf("Error: could not create machine! rc=%08X\n", rc);
        return;
    }
    rc = machine->SetName(NS_ConvertUTF8toUTF16("WinXP").get());
    rc = machine->SetMemorySize(1024);

    nsCOMPtr<IGuestOSType> osType;
    rc = virtualBox->GetGuestOSType(NS_LITERAL_STRING("WindowsXP").get(),
                                    getter_AddRefs(osType));
    if (NS_FAILED(rc))
    {
        printf("Error: could not find guest OS type! rc=%08X\n", rc);
    }
    else
    {
        machine->SetOSTypeId (NS_LITERAL_STRING("WindowsXP").get());
    }
    rc = virtualBox->RegisterMachine(machine);
    if (NS_FAILED(rc))
    {
        printf("Error: could not register machine! rc=%08X\n", rc);
        printErrorInfo();
        return;
    }
	
    nsCOMPtr<ISession> session;
    {
        ......
    }

    /*
     * Create a virtual harddisk
     */
    nsCOMPtr<IMedium> hardDisk = 0;
    rc = virtualBox->CreateHardDisk(NS_LITERAL_STRING("VDI").get(),NS_LITERAL_STRING("/home/VirtualBox_VM/WinXP/WindowsXP.vdi").get(),  getter_AddRefs(hardDisk));
    if (NS_FAILED(rc))
    {
        printf("Failed creating a hard disk object! rc=%08X\n", rc);
    }
    else
    {
        nsCOMPtr <IProgress> progress;
        rc = hardDisk->CreateBaseStorage(10240,  MediumVariant_Standard, getter_AddRefs(progress));
        if (NS_FAILED(rc))
        {
            printf("Failed creating hard disk image! rc=%08X\n", rc);
        }
        else
        {
            rc = progress->WaitForCompletion(-1);
            if(NS_FAILED(rc))
            {
                printf("Error: WaitForCompletion error! rc=%08X\n",rc);
            }
            PRInt32 resultCode;
            progress->GetResultCode(&resultCode);
            if (NS_FAILED(rc) || NS_FAILED(resultCode))
            {
                printf("Error: could not create hard disk! rc=%08X\n",NS_FAILED(rc) ? rc : resultCode);
            }
            else
            {
                /*
                 * Now that it's created, we can assign it to the VM. Controller
                 */
                rc = machine->AttachDevice(NS_LITERAL_STRING("IDE").get(), 0, 0,  DeviceType_HardDisk, hardDisk);
                if (NS_FAILED(rc))
                {
                    printf("Error: could not attach hard disk! rc=%08X\n", rc);
                }
            }
        }
    }

    /*
     * Save all changes we've just made.
     */
    rc = machine->SaveSettings();
    if (NS_FAILED(rc))
    {
        printf("Could not save machine settings! rc=%08X\n", rc);
    }
    session->UnlockMachine();
}
failed to create hard disk,
Error: could not create hard disk! rc=80BB0004
const HRESULT VBOX_E_FILE_ERROR = 0x80BB0004 File not accessible or erroneous file contents.
Like there is something wrong with the path, but i don't know where error.Do you konw?
thaks!
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: Failed to create hard disk!

Post by noteirak »

Looks to me like a permission issue. Try creating the file somewhere else, where you are sure that you have write rights.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
wuzf
Posts: 15
Joined: 30. May 2013, 10:57

Re: Failed to create hard disk!

Post by wuzf »

1,I use fopen("("/home/VirtualBox_VM/WinXP/WindowsXP.vdi", w) to create the file, it successed;
2,when I change vdi file to /tmp/VirtualBox_VM/WinXP/WindowsXP.vdi, it is still failure. I am sure that have the right to write.
wuzf
Posts: 15
Joined: 30. May 2013, 10:57

Re: Failed to create hard disk!

Post by wuzf »

Seems to be in the use of CreateBaseStorage() function, first parameter error, when i change size to 2147483648, the vdi file is created,
rc = hardDisk->CreateBaseStorage(2147483648, MediumVariant_Standaed,getter_AddRefs(progress));
New error appeared:
Error:could not create hard disk! rc = 80070057, i not found this error code mean.
What is this error?
Thanks!
wuzf
Posts: 15
Joined: 30. May 2013, 10:57

Re: Failed to create hard disk!

Post by wuzf »

I have successed to create hard disk.Use a new virtual machine name,and successed to created hard disk, but failed to attachDevice, when I create a storagecontroller before create a hard disk, it successed.
Thanke!
rousseauhk
Posts: 45
Joined: 8. Apr 2013, 09:16
Primary OS: MS Windows 7
VBox Version: OSE other
Guest OSses: Ubuntu Server
Contact:

Re: Failed to create hard disk!

Post by rousseauhk »

I had similar confusion when first trying to get this working. Maybe we need some better examples or a wiki where we can add/maintain this kind of info?

thanks
Steve
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: Failed to create hard disk!

Post by noteirak »

I'm still not grasping where is the confusion here? What is not clear?
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
rousseauhk
Posts: 45
Joined: 8. Apr 2013, 09:16
Primary OS: MS Windows 7
VBox Version: OSE other
Guest OSses: Ubuntu Server
Contact:

Re: Failed to create hard disk!

Post by rousseauhk »

I'm referring to the absence of a good example for how to create a new VM from start to finish using the API. e.g. Section 3 in the SDK docs has some very short code snippets, but doesn't have a complete example, and doesn't cover basic things like CreateMachine().
rousseauhk
Posts: 45
Joined: 8. Apr 2013, 09:16
Primary OS: MS Windows 7
VBox Version: OSE other
Guest OSses: Ubuntu Server
Contact:

Re: Failed to create hard disk!

Post by rousseauhk »

btw, don't get me wrong - I'm not b*tching about anything (VBox is a great product) - just making a suggestion that if this stuff was in a wiki somewhere it would be a lot easier for the community to edit, than it is in a standalone doc.
Post Reply