【问题标题】:Detaching disk from stopped instance从停止的实例中分离磁盘
【发布时间】:2017-05-29 02:09:11
【问题描述】:

我正在尝试从当前持有磁盘的已停止实例中分离磁盘。这是我目前的做法。

def detach_disk_from_instance(disk_name, zone=GCLOUD_ZONE):
    disk_info = get_disk_info(disk_name=disk_name, zone=zone)
    if disk_info.get('users'):
        instance_name = disk_info['users'][0].rsplit('/').pop()
        logger.info("detatching disk : {} from instance : {}".format(disk_name, instance_name))
        request = compute.instances().detachDisk(
            project=GCLOUD_PROJECT_NAME, zone=zone, instance=instance_name, deviceName=disk_name)
        return make_http_request(request, {})

def wait_for_operation(operation, zone=GCLOUD_ZONE, worker=1):
    logger.info('Waiting for operation {} to finish...'.format(operation))
    with ThreadPoolExecutor(worker) as executor:
        future = executor.submit(_check_operation_status, operation=operation, zone=zone)
        wait([future])
        return future.result()

req = detach_disk_from_instance(disk_name='test-disk')
wait_for_operation(operation=req['name'])

如果持有磁盘的实例当前正在运行,则上述代码有效。 但如果实例停止,它就不起作用。当我尝试从停止的实例中分离磁盘时出现以下错误。

{'errors': [{'message': "没有找到带有设备名称的附加磁盘 'test-disk'", 'code': 'INVALID_USAGE'}]}

我向您保证,磁盘仍然连接到实例,gcp 仪表板也显示了这一点。

【问题讨论】:

    标签: google-api google-compute-engine google-api-python-client


    【解决方案1】:

    TL;DR - 您传递的是磁盘资源的name,而不是磁盘附加到实例的device name

    在将磁盘附加到 VM 实例的 instances.attachDisk 请求中,您可以在请求的 deviceName 字段中指定设备名称。

    设备名称

    字符串

    指定您选择的唯一设备名称, 反映在 Linux 的 /dev/disk/by-id/google-* 树中 在实例中运行的操作系统。此名称可用于 从内部参考设备以进行安装、调整大小等 实例。

    如果没有指定,服务器会选择一个默认的设备名称来应用 此磁盘,格式为 persistent-disks-x,其中 x 是一个数字 由 Google Compute Engine 分配。该字段仅适用于 永久性磁盘。

    您需要将此名称用作deviceName 查询参数,用于instances.detachDisk 请求。

    必需的查询参数

    设备名称

    字符串

    要分离的磁盘设备名称。

    如文档中所述,如果您在附加磁盘时未指定设备名称,GCE 会以persistent-disks-x 的形式生成设备名称,您必须指定此名称。

    【讨论】:

      猜你喜欢
      • 2021-09-30
      • 1970-01-01
      • 1970-01-01
      • 2013-02-23
      • 1970-01-01
      • 2014-01-17
      • 2016-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多