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

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

Introduction

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

Prototype


public BlockDeviceMapping withDeviceName(String deviceName) 

Source Link

Document

The device name (for example, /dev/sdh or xvdh).

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;
    }/*  w  w w . ja  v  a2s.c o m*/

    // ????????????????
    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;
}