简体   繁体   English

判断是否在ec2实例上安装了卷

[英]Tell if a volume is mounted on an ec2 instance

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. 我想知道是否可以通过boto3检查是否安装了连接到EC2实例的EBS卷。 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. 我在boto3文档中看不到任何内容,我唯一能想到的就是尝试分离卷并查看它是否有错误,这会让我担心这个方法,以防万一将卷与实例分离安装和使用,可能会导致严重的问题。

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. 我能想到的另一件事是使用salt-key(我们用来管理配置)打印出一个实例列表,然后在服务器上运行“df -h”,剥离LVM,然后返回已挂载的卷列表,我可以与实例从boto3附加的卷列表交叉引用。 This seems like a safer way to do it, but could be a pain, and could only be run on our salt master. 这似乎是一种更安全的方式,但可能是一种痛苦,只能在我们的盐主人身上运行。

No Boto doesn't have any such functionality. 没有 Boto没有任何此类功能。 Boto allows you to interact with the AWS infrastructure not with Internal OS functionalities. Boto允许您与AWS基础架构进行交互,而不是使用内部OS功能。 https://boto3.readthedocs.io/en/latest/reference/services/ec2.html#volume 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. 另一方面,您始终可以使用python运行命令并列出服务器上所有已安装的分区获取输出,并将其与您附加到可以使用Boto检索的实例的EBS卷列表交叉引用。

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; 例如,如果指定设备名称为/ dev / sdh,则内核可能会将设备重命名为/ dev / xvdh或/ dev / hdh; 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). 在某些版本的Red Hat Enterprise Linux(及其变体,如CentOS)中,甚至尾随的字母也可能会发生变化(/ dev / sda可能会变成/ 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. 例如,/ dev / sdb将成为/ dev / xvdf,/ dev / sdc将成为/ 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. Amazon Linux AMI使用您在启动时指定的名称创建符号链接,该名称指向重命名的设备路径,但其他AMI可能表现不同。

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) 不好的一面是,这封信可以改变,并且它不容易链接2(有一些关于你如何做的博客文章

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 如果你把这部分拿出来,我可能会采取安全的方式,而不是使用和解析df -k的结果我会列出可用的磁盘以及它们的终点

[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 解析将更容易一些,对于未安装的每个卷,您将能够检索它们并从boto3或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). 在某些版本的Red Hat Enterprise Linux(及其变体,如CentOS)中,甚至尾随的字母也可能会发生变化(/ dev / sda可能会变成/ dev / xvde)。 In these cases, each device name trailing letter is incremented the same number of times 在这些情况下,每个设备名称尾随字母增加相同的次数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将EBS卷附加到EC2实例 - attach EBS volume to EC2 instance 将现有 EBS 卷附加和挂载到 EC2 实例文件系统问题 - Attaching and mounting existing EBS volume to EC2 instance filesystem issue Amazon EC2 centos 6竞价型实例根卷 - Amazon ec2 centos 6 spot instance root volume 如何自动为Amazon EC2实例的卷创建快照? - How to automatically snapshot a volume of an Amazon EC2 instance? 我应该如何在 AWS EC2 中将已安装的驱动器(卷)与根驱动器合并 - How should I Merge mounted drive(Volume) with root drive in AWS EC2 AWS ec2根卷增加:在aws ubuntu实例上扩展弹性根卷不起作用 - AWS ec2 root volume increase: Expanding elastic root volume on aws ubuntu instance not working 一段时间后冻结在ubuntu机器上的Amazon EC2实例的var / www文件夹冻结 - Amazon EC2 instance's var/www folder mounted on ubuntu machine freezes after some time AWS EC2 实例 - AWS EC2 Instance 将带有AWS市场代码的CentOS根卷附加到其他CentOS EC2实例时,另一个实例将使用附加的根卷启动 - On attaching CentOS root volume with AWS marketplace code to other CentOS EC2 instance, the other instance boots up using the attached root volume EC2-如果没有临时实例存储,临时文件是否会填满根ebs卷? - ec2 - Do temporary files fill up root ebs volume if there is no ephemeral instance store?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM