Java JClouds:如何将新卷附加到AWS EC2实例

Java JClouds:如何将新卷附加到AWS EC2实例,java,amazon-web-services,jclouds,Java,Amazon Web Services,Jclouds,我正在尝试使用JClouds将一个新卷附加到我的实例。但我找不到办法 final String POLL_PERIOD_TWENTY_SECONDS = String.valueOf(SECONDS.toMillis(20)); Properties overrides = new Properties(); overrides.setProperty(ComputeServiceProperties.POLL_INITIAL_PERIOD, POLL_PERIOD_T

我正在尝试使用JClouds将一个新卷附加到我的实例。但我找不到办法

    final String POLL_PERIOD_TWENTY_SECONDS = String.valueOf(SECONDS.toMillis(20));

    Properties overrides = new Properties();
    overrides.setProperty(ComputeServiceProperties.POLL_INITIAL_PERIOD, POLL_PERIOD_TWENTY_SECONDS);
    overrides.setProperty(ComputeServiceProperties.POLL_MAX_PERIOD, POLL_PERIOD_TWENTY_SECONDS);

    Iterable<Module> modules = ImmutableSet.<Module> of(new SshjSshClientModule(), new SLF4JLoggingModule());
    //Iterable<Module> modules = ImmutableSet.<Module> of(new SshjSshClientModule());

    ComputeServiceContext context = ContextBuilder.newBuilder("aws-ec2")
            .credentials("valid user", "valid password")
            .modules(modules)
            .overrides(overrides)
            .buildView(ComputeServiceContext.class);
    ComputeService computeService = context.getComputeService();

    // Ubuntu AMI
    Template template = computeService.templateBuilder()
            .locationId("us-east-1")
            .imageId("us-east-1/ami-7c807d14")
            .hardwareId("t1.micro")
            .build();

    // This line creates the volume but does not attach it
    template.getOptions().as(EC2TemplateOptions.class).mapNewVolumeToDeviceName("/dev/sdm", 100, true);

    Set<? extends NodeMetadata> nodes = computeService.createNodesInGroup("m456", 1, template);

    for (NodeMetadata nodeMetadata : nodes) {
        String publicAddress = nodeMetadata.getPublicAddresses().iterator().next();
        //String privateAddress = nodeMetadata.getPrivateAddresses().iterator().next();
        String username = nodeMetadata.getCredentials().getUser();
        String password = nodeMetadata.getCredentials().getPassword();
        // [...]
        System.out.println(String.format("ssh %s@%s  %s", username, publicAddress, password));
        System.out.println(nodeMetadata.getCredentials().getPrivateKey());
    }
final String POLL_PERIOD_twenth_SECONDS=String.valueOf(SECONDS.toMillis(20));
属性覆盖=新属性();
overrides.setProperty(ComputeServiceProperties.POLL\u初始\u周期,POLL\u周期\u二十秒);
overrides.setProperty(ComputeServiceProperties.POLL\u MAX\u PERIOD,POLL\u PERIOD\u二十秒);
Iterable模块=不可变表集。of(新的SSHJSHClientModule(),新的SLF4JLoggingModule());
//Iterable模块=不可变表集。of(新的SSHJSHClientModule());
ComputeServiceContext上下文=ContextBuilder.newBuilder(“aws-ec2”)
.凭据(“有效用户”、“有效密码”)
.模块(模块)
.覆盖(覆盖)
.buildView(ComputeServiceContext.class);
ComputeService ComputeService=context.getComputeService();
//Ubuntu AMI
Template Template=computeService.templateBuilder()
.locationId(“us-east-1”)
.imageId(“us-east-1/ami-7c807d14”)
.hardwareId(“t1.micro”)
.build();
//此行创建卷,但不附加卷
template.getOptions().as(EC2TemplateOptions.class).mapNewVolumeToDeviceName(“/dev/sdm”,100,true);
设置您可以在jclouds中使用。您已经创建了实例,因此请按如下方式创建并附加卷:

//获取节点
NodeMetadata node=Iterables.getOnlyElement(节点);
//获取AWSEC2 API
EC2Api EC2Api=computeServiceContext.unwappi(EC2Api.class);
//创建100 GiB卷
Volume-Volume=ec2Api.getElasticBlockStoreApi().get()
.createVolumeInAvailabilityZone(zoneId,100);
//附加到实例
附件附件=ec2Api.getElasticBlockStoreApi().get()
.attachVolumeInRegion(region,volume.getId(),node.getId(),“/dev/sdx”);
现在,您已经连接了一个EBS卷,并且VM已经为其创建了一个块设备,您只需要运行正确的命令将其装载到
/var
目录,这将取决于您的特定操作系统。您可以运行如下脚本:

//在实例上运行脚本
computeService.RunScriptionNode(node.getId(),
exec(“mount/dev/sdx/var”);
重新启动后,Statements.exec(“mount/dev/sdx/var”)是否会保留。我的意思是,重新启动后,文件夹/var是否会指向/dev/sdx文件系统?