判断卷是否安装在 ec2 实例上
Tell if a volume is mounted on an ec2 instance
我想知道是否可以通过 boto3 检查是否挂载了附加到 EC2 实例的 EBS 卷。这样做的原因是我相信许多实例已经卸载了卷,但管理员忘记分离卷,所以这些卷被计费,但我们没有使用它。
我在 boto3 文档中看不到任何内容,我唯一能想到的就是尝试分离卷并查看它是否出错,如果卷从安装和使用实例时,可能会导致严重问题。
我唯一能想到的另一件事是使用 salt-key(我们用来管理配置)打印出实例列表,然后 运行 在服务器上 "df -h" ,剥离 LVM,以及 return 已安装的卷列表,我可以将其与实例从 boto3 附加的卷列表进行交叉引用。这似乎是一种更安全的方法,但可能会很痛苦,并且只能 运行 在我们的 salt master 上。
否 Boto 没有任何此类功能。 Boto 允许您与 AWS 基础设施交互,而不是内部 OS 功能。
https://boto3.readthedocs.io/en/latest/reference/services/ec2.html#volume
您可以检查卷是否已附加,但不能检查是否已安装。
您提到的过程是一种实现方式,但需要大量人工干预。另一方面,您始终可以使用 python 到 运行 命令并列出服务器上所有已安装的分区获取输出并将其与您的列表交叉引用您可以使用 Boto 检索附加到实例的 EBS 卷的数量。
我不确定您是否可以通过尝试卸载您的卷来完全解决它。
有一点需要注意的是你的Device of the Volume和挂载的设备可以不同
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.
不好的地方是字母可以改变而且 link 2 并不那么容易(有一些 blog post 关于如何做)
如果你把这部分去掉,我可能会采取安全的方式,而不是使用和解析 df -k
的结果,我会列出可用磁盘及其终点
[root@light ~]# lsblk -o NAME,MOUNTPOINT -r
NAME MOUNTPOINT
xvde /
xvdj
解析会更容易一些,对于每个未安装的卷,您将能够检索它们并从 boto3 或 aws CLI 中找到它们
aws ec2 describe-volumes --query \
'Volumes[*].Attachments[?Device==`<the device>` && InstanceId==`<instance looked up>`].VolumeId' \
--output text
这样会很好,问题又来了
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
我想知道是否可以通过 boto3 检查是否挂载了附加到 EC2 实例的 EBS 卷。这样做的原因是我相信许多实例已经卸载了卷,但管理员忘记分离卷,所以这些卷被计费,但我们没有使用它。
我在 boto3 文档中看不到任何内容,我唯一能想到的就是尝试分离卷并查看它是否出错,如果卷从安装和使用实例时,可能会导致严重问题。
我唯一能想到的另一件事是使用 salt-key(我们用来管理配置)打印出实例列表,然后 运行 在服务器上 "df -h" ,剥离 LVM,以及 return 已安装的卷列表,我可以将其与实例从 boto3 附加的卷列表进行交叉引用。这似乎是一种更安全的方法,但可能会很痛苦,并且只能 运行 在我们的 salt master 上。
否 Boto 没有任何此类功能。 Boto 允许您与 AWS 基础设施交互,而不是内部 OS 功能。 https://boto3.readthedocs.io/en/latest/reference/services/ec2.html#volume
您可以检查卷是否已附加,但不能检查是否已安装。
您提到的过程是一种实现方式,但需要大量人工干预。另一方面,您始终可以使用 python 到 运行 命令并列出服务器上所有已安装的分区获取输出并将其与您的列表交叉引用您可以使用 Boto 检索附加到实例的 EBS 卷的数量。
我不确定您是否可以通过尝试卸载您的卷来完全解决它。
有一点需要注意的是你的Device of the Volume和挂载的设备可以不同
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.
不好的地方是字母可以改变而且 link 2 并不那么容易(有一些 blog post 关于如何做)
如果你把这部分去掉,我可能会采取安全的方式,而不是使用和解析 df -k
的结果,我会列出可用磁盘及其终点
[root@light ~]# lsblk -o NAME,MOUNTPOINT -r
NAME MOUNTPOINT
xvde /
xvdj
解析会更容易一些,对于每个未安装的卷,您将能够检索它们并从 boto3 或 aws CLI 中找到它们
aws ec2 describe-volumes --query \
'Volumes[*].Attachments[?Device==`<the device>` && InstanceId==`<instance looked up>`].VolumeId' \
--output text
这样会很好,问题又来了
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