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

前端 未结 4 988
清歌不尽
清歌不尽 2021-02-01 10:11

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

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-01 10:38

    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.

提交回复
热议问题