英文:
AWS EC2 - Unmounting file system before detaching EBS volume from instance via boto3 SDK?
问题
问题:
问题出现在第4步。根据AWS Boto3 SDK文档,在使用EC2.Instance
资源的detach_volume()
方法时,应确保在卸载卷之前卸载操作系统中设备上的任何文件系统。
根据另一份AWS指南,可以通过类似于sudo umount -d <DEVICE_NAME>
(例如sudo umount -d /dev/sdh
)的方式来执行此操作。然而,上述命令是Linux shell命令,并不能通过boto3执行。
问题:
我如何以编程方式运行Linux shell命令来卸载我的Python脚本目标的每个EC2实例上的文件系统,以便停止EC2并安全地卸载附加到它的卷?我是否需要通过某个Python SSH库(例如Paramiko)SSH到每个实例并运行该命令?
英文:
Context:
I'm trying to programmatically encrypt existing unencrypted EBS volumes attached to EC2's in my AWS environment via python
and Boto3
.
My process is as follows (assuming AWS credentials are already setup on the machine from where my Python script is being run from):
- Create unencrypted snapshot(s) from the existing volume(s) attached to an EC2.
- Create encrypted volume(s) from the unencrypted snapshot(s) using an AWS KMS key.
- Stop the EC2 with the unencrypted volume(s) in question.
- Detach the existing unencrypted EBS volume(s) from the EC2 (done via
detach_volume()
). - Attach the encrypted volume(s) we made in step 2 to the EC2.
- Start the EC2 again. Hopefully, the volumes attached to the EC2 work fine in terms of the file system and should now be encrypted.
Problem:
The issue is in step 4. According to the AWS Boto3 SDK Docs, when using the detach_volume()
method of the EC2.Instance
resource, one should make sure to "unmount any file systems on the device within your operating system before detaching the volume."
According to another AWS guide, this can be done via something like sudo umount -d <DEVICE_NAME>
e.g. sudo umount -d /dev/sdh
However, the above is a Linux shell command, and not something that can be executed via boto3.
Question:
How can I programmatically run that Linux shell command to unmount the filesystem on each EC2 instance that my Python script targets so that I can stop the EC2 and safely detach the volumes attached to it?
Would I have to SSH into each instance via some Python SSH library (e.g. Paramiko) and run the command?
答案1
得分: 1
我认为如果你能在你的代码中等待一两分钟,直到实例完全停止,或者查询实例状态并仅在状态等于'已停止'时执行步骤5,那应该就可以了。当你在运行的EC2上卸载卷时,而不是在已停止的EC2上卸载卷。
英文:
I think if you can in your code sleep for a minute or two till the instance become completely stopped, or query the instance state and only execute step 5 when state == 'stopped', that would do it. You have to unmount when you are detaching a volume on a running ec2, not a stopped one.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论