Skip to content

Adding storage to the virtual machines

nacx edited this page Jul 16, 2012 · 3 revisions

This example shows how to create a new virtual machine and add some additional storage to it. It shows how to get the Virtual Datacenter where the virtual machine will be created, and how to configure the resources of the virtual machine.

Before you continue, make sure you have read the basics so you can understand the code.

// First of all create the Abiquo context pointing to the Abiquo API
AbiquoContext context = ContextBuilder.newBuilder(new AbiquoApiMetadata())
    .endpoint("http://10.60.21.33/api")
    .credentials("user", "password")
    .modules(ImmutableSet.<Module> of(new SLF4JLoggingModule()))
    .build(AbiquoContext.class);

try
{
    // Get the virtual datacenter where the virtual machine will be created
    VirtualDatacenter vdc =
        context.getCloudService().findVirtualDatacenter(
            VirtualDatacenterPredicates.name("Development"));

    // Get the virtual appliance where the virtual machine will be created
    VirtualAppliance vapp =
        vdc.findVirtualAppliance(VirtualAppliancePredicates.name("Storage"));

    // Get the template to use from the templates available to the virtual datacenter
    VirtualMachineTemplate template = vdc.findAvailableTemplate(
        VirtualMachineTemplatePredicates.name("CentOS 6"));

    // Create the virtual machine
    VirtualMachine vm = VirtualMachine.builder(context.getApiContext(), vapp, template)
        .name("My CentOS 6 VM") // The name of the virtual machine
        .cpu(2)                 // The number of CPUs
        .ram(2048)              // The amount of RAM in MB
        .build();
    vm.save();
 
    // Create a 2GB extra hard disk and attach it to the virtual machine
    HardDisk disk = HardDisk.builder(context.getApiContext(), vdc).sizeInMb(2048L).build();
    disk.save()
    vm.attachHardDisks(disk);
    
    // Create a 20GB persistent volume and attach it to the virtual machine
    Tier tier = vdc.findStorageTier(TierPredicates.name("Standard storage"));
    Volume volume = Volume.builder(context.getApiContext(), vdc, tier)
        .name("Data volume")   // The name of the persistent volume
        .sizeInMb(20480L)      // The size in MB of the persistent volume
        .build();
    volume.save()
    vm.attachVolumes(volume);

    // At this point the virtual machine is configured with an additional 2GB hard
    // disk, and a 20GB additional persistent iscsi volume.
}
finally
{
    if (context != null)
    {
        context.close();
    }
}

Once the virtual machine has been created and configured it can be deployed as explained in the Deploying virtual machines page.

Back to Home