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

Vincent Theeten picture Vincent Theeten · Mar 9, 2011 · Viewed 19.4k times · Source

How do I find to which device an EBS Volume is mounted with Python Boto v2.0?

boto.ec2.Volume has some interesting properies like attachment_state and volume_state. But are there any functions for device mapping?

boto.manage.volume has get_device(self, params) but requires a CommandLineGetter.

Any pointers on how to proceed or some samples of using boto.manage?

Answer

Cbaker510 picture Cbaker510 · May 11, 2011

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()