4
votes

I want to find out if it is possible to check via boto3 if a EBS volume which is attached to an EC2 instance is mounted. The reason for this is that I believe a number of instances have had volumes unmounted, but the admin has forgotten to detach the volume, so the volumes is billed, but we're not using it.

I can't see anything in the boto3 documentation, the only thing I can think of is to try and detach the volume and see if it errors, which would worry me as a method in case the volume is detached from the instance when it is mounted and in use, it could cause serious issues.

The only other thing I can think of is to use salt-key (which we use to manage config) to print out a list of instances, then run a "df -h" on the server, stripping out the LVMs, and return the list of volumes that are mounted, which I could cross reference with the list of volumes that the instance has attached from boto3. This seems like a safer way to do it, but could be a pain, and could only be run on our salt master.

2
You are right, mounting provisioned EBS storage is the OS job. Boto3 does not deal with OS layer.mootmoot
lsblk might be more straightforward/easier to use than df.Michael - sqlbot

2 Answers

3
votes

No Boto doesn't have any such functionality. Boto allows you to interact with the AWS infrastructure not with Internal OS functionalities. https://boto3.readthedocs.io/en/latest/reference/services/ec2.html#volume

You can check if the volume is attached, but not if it is mounted.

The process you have mentioned is one way of doing it but with a lot of manual intervention. On the other hand you can always use python to run the command and list all the mounted partitions on the server get the output and cross refer it with your list of EBS volumes attached to instance which you can retrieve using Boto.

3
votes

I am not sure you can fully resolve it with trying to unmount your volume.

There's an important thing to note is that your Device of the Volume and the mounted device can be different

Depending on the block device driver of the kernel, the device might be attached with a different name than what you specify. For example, if you specify a device name of /dev/sdh, your device might be renamed /dev/xvdh or /dev/hdh by the kernel; in most cases, the trailing letter remains the same. In some versions of Red Hat Enterprise Linux (and its variants, such as CentOS), even the trailing letter might also change (where /dev/sda could become /dev/xvde). In these cases, each device name trailing letter is incremented the same number of times. For example, /dev/sdb would become /dev/xvdf and /dev/sdc would become /dev/xvdg. Amazon Linux AMIs create a symbolic link with the name you specify at launch that points to the renamed device path, but other AMIs might behave differently.

The bad part is that letter can change and its not as easy to link the 2 (there's some blog post about how you can do)

If you take this part out, I would probably do the safe way but rather than using and parsing the result of df -k I would list the available disk as well as their end point

[root@light ~]# lsblk -o NAME,MOUNTPOINT -r
NAME MOUNTPOINT
xvde /
xvdj

The parsing will be a bit easier and for each volume that are not mounted you will be able to retrieve them and find them from boto3 or aws CLI

aws ec2 describe-volumes --query \
'Volumes[*].Attachments[?Device==`<the device>` && InstanceId==`<instance looked up>`].VolumeId' \
--output text

That would work pretty well, the issue is again

In some versions of Red Hat Enterprise Linux (and its variants, such as CentOS), even the trailing letter might also change (where /dev/sda could become /dev/xvde). In these cases, each device name trailing letter is incremented the same number of times