使用 Boto 查找安装到哪个设备和 EBS 卷

2024-01-29

如何使用 Python Boto v2.0 查找 EBS 卷安装到哪个设备?

boto.ec2.卷 https://github.com/boto/boto/blob/master/boto/ec2/volume.py有一些有趣的属性,例如attachment_state and volume_state。但是有没有设备映射的函数呢?

boto.管理音量 https://github.com/boto/boto/blob/master/boto/manage/volume.py has get_device(self, params)但需要 CommandLineGetter。

有关如何进行的任何指示或一些使用示例boto.manage?


我相信 Attach_data.device 就是您所寻找的。体积的一部分。

这是一个示例,不确定这是否是最好的方法,但它输出volumeid、instanceid和attachment_data,如下所示:

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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Boto 查找安装到哪个设备和 EBS 卷 的相关文章

随机推荐