pythonamazon-web-servicesamazon-ec2libcloud

Get volumes attached to a node with apache-libcloud


How can I obtain a list of volumes that are currently attached to a node? I have the Node object, but since I created the volumes with a block device mapping when I created the node with deploy_node, I don't have the volume object that I could use as reference.


Solution

  • I've found that there isn't a direct way to obtain a list of StorageVolumes attached to a node. There is a solution, however.

    With the EC2 driver, you can use a Node's block device mapping to get volume IDs and device names. After you've got the volume ID(s), you can easily select that volume from the list returned by list_volumes(). Below, I demonstrate this by obtaining the ID of an EBS volume with device name /dev/sdb attached to a Node and then selecting the matching StorageVolume from the list returned by list_volumes().

    vol_id = [x['ebs']['volume_id'] for x in node.extra['block_device_mapping']
              if x['device_name'] == '/dev/sdb'][0]
    # vol_id is a string
    volume = [v for v in driver.list_volumes() if v.id == vol_id][0]
    # volume is a StorageVolume
    

    Keep in mind that this exact method can't be used if the driver you're working with doesn't provide a block device mapping in its Node's extra dict. If you're not working with the EC2 driver, first check to see what metadata is provided by a Node's extra dict.