Example usage for com.amazonaws.services.ec2.model BlockDeviceMapping withVirtualName

List of usage examples for com.amazonaws.services.ec2.model BlockDeviceMapping withVirtualName

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2.model BlockDeviceMapping withVirtualName.

Prototype


public BlockDeviceMapping withVirtualName(String virtualName) 

Source Link

Document

The virtual device name (ephemeralN).

Usage

From source file:jp.primecloud.auto.process.aws.AwsInstanceProcess.java

License:Open Source License

protected List<BlockDeviceMapping> createInstanceStoreBlockDeviceMappings(AwsProcessClient awsProcessClient,
        ImageAws imageAws, AwsInstance awsInstance, com.amazonaws.services.ec2.model.Image image) {
    int count = AwsInstanceTypeDefinition.getInstanceStoreCount(awsInstance.getInstanceType());
    if (count == 0) {
        return null;
    }//from w ww  .  j a va  2s .com

    // ????????????????
    String maxInstanceStore = Config.getProperty("aws.maxInstanceStore");
    if (StringUtils.isNotEmpty(maxInstanceStore)) {
        int max = Integer.parseInt(maxInstanceStore);
        if (count > max) {
            count = max;
        }
    }

    // ?BlockDeviceMapping????
    Set<String> deviceNames = new HashSet<String>();
    for (BlockDeviceMapping mapping : image.getBlockDeviceMappings()) {
        deviceNames.add(mapping.getDeviceName());
    }

    List<BlockDeviceMapping> mappings = new ArrayList<BlockDeviceMapping>();
    for (int i = 0; i < count; i++) {
        String virtualName = "ephemeral" + i;

        // ?BlockDeviceMapping????????
        boolean exist = false;
        for (BlockDeviceMapping mapping : image.getBlockDeviceMappings()) {
            if (virtualName.equals(mapping.getVirtualName())) {
                exist = true;
                break;
            }
        }
        if (exist) {
            continue;
        }

        // ?????????
        String identifier = null;
        for (int j = 0; j < 25; j++) {
            char id = (char) ('b' + j);
            if (!deviceNames.contains("/dev/sd" + id) && !deviceNames.contains("xvd" + id)) {
                identifier = String.valueOf(id);
                break;
            }
        }

        if (identifier == null) {
            // b ? z ??????????????
            continue;
        }

        String deviceName;
        if (StringUtils.equals(image.getPlatform(), PlatformValues.Windows.toString())) {
            deviceName = "xvd" + identifier;
        } else {
            deviceName = "/dev/sd" + identifier;
        }

        BlockDeviceMapping mapping = new BlockDeviceMapping();
        mapping.withVirtualName(virtualName);
        mapping.withDeviceName(deviceName);
        mappings.add(mapping);

        deviceNames.add(deviceName);
    }

    return mappings;
}