Using Boto to find to which device and EBS Volume is mounted

假装没事ソ 提交于 2019-12-02 19:39:11
Cbaker510

I believe attach_data.device is what your looking for. part of volume.

Heres an example, not sure if this is the best way, but it outputs volumeid, instanceid, and attachment_data something like:

Attached Volume ID - Instance ID - Device Name
vol-12345678 - i-ab345678 - /dev/sdp
vol-12345678 - i-ab345678 - /dev/sda1
vol-12345678 - i-cd345678 - /dev/sda1


import boto
ec2 = boto.connect_ec2()
res = ec2.get_all_instances()
instances = [i for r in res for i in r.instances]
vol = ec2.get_all_volumes()
def attachedvolumes():
    print 'Attached Volume ID - Instance ID','-','Device Name'
    for volumes in vol:
        if volumes.attachment_state() == 'attached':
            filter = {'block-device-mapping.volume-id':volumes.id}
            volumesinstance = ec2.get_all_instances(filters=filter)
            ids = [z for k in volumesinstance for z in k.instances]
            for s in ids:
                 print volumes.id,'-',s.id,'-',volumes.attach_data.device
# Get a list of unattached volumes           
def unattachedvolumes():
   for unattachedvol in vol:
       state = unattachedvol.attachment_state()
   if state == None:
        print unattachedvol.id, state
attachedvolumes()
unattachedvolumes()

It isn't clear if you're running this from the instance itself or externally. If the latter, you will not need the metadata call. Just supply the instance id.

from boto.ec2.connection import EC2Connection
from boto.utils import get_instance_metadata

conn = EC2Connection()
m = get_instance_metadata()
volumes = [v for v in conn.get_all_volumes() if v.attach_data.instance_id == m['instance-id']]

print volumes[0].attach_data.device

Note that an instance may have multiple volumes, so robust code won't assume there's a single device.

If you also want the block device mappings (in linux, the local device name of the EBS volume), you can also use EC2Connection.get_instance_attribute to retrieve a list of the local device names and their corresponding EBS objects:

def get_block_device_mapping(instance_id):
    return conn.get_instance_attribute(
            instance_id=instance_id,
            attribute='blockDeviceMapping'
            )['blockDeviceMapping']

This will return a dictionary with local device names as keys, and EBS objects as values (from which you can get all sorts of things like the volume-id).

The best way I've found is to get all resources in one region at a time and associate them yourself:

#!/usr/bin/env python2
import boto.ec2

REGION = 'us-east'
CONN = boto.ec2.connect_to_region(REGION)

def main():
    volumes = conn.get_all_volumes()

    for volume in volumes:
        print volume

        # Match to an instance id
        print volume.attach_data.instance_id

        # # Object attributes:
        # print volume.__dict__

        # # Object methods:
        # print(dir(volume))

if __name__ == '__main__':
    main()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!