【发布时间】:2014-01-17 15:50:36
【问题描述】:
过去,我通过 AWS Web 控制台创建了一个带有附加 EBS 存储的实例。在“步骤 4。添加存储”步骤中,我会将 EBS 存储添加为device="/dev/sdf",Standard 为Volume type,而不是Snapshot。实例启动后,我将发出以下命令集,将额外的驱动器挂载为单独的目录,并使其可供所有人访问:
sudo mkfs.ext4 /dev/xvdf
sudo mkdir /home/foo/extra_storage_directory
sudo mount -t ext4 /dev/xvdf /home/foo/extra_storage_directory
cd /home/foo
sudo chmod a+w extra_storage_directory
我得到了一段 python 代码,它以编程方式创建实例而无需任何额外的存储。它调用boto.ec2.connection.run_instances。我需要修改此代码才能创建具有额外存储空间的实例。我需要在本质上模拟我通过控制台执行的手动步骤,以确保在我启动新实例后上述sudo 命令有效。
我需要使用哪个boto 函数以及如何添加存储空间?
更新:我做了一些挖掘并编写了一些我认为应该做我想做的代码。但是,行为有点奇怪。这是我所拥有的:
res = state.connection.run_instances(state.ami,key_name=state.key,instance_type=instance_type,security_groups=sg)
inst = res.instances[0]
pmt = inst.placement
time.sleep(60)
try:
vol = state.connection.create_volume(GB, pmt)
tsleep = 60
time.sleep(tsleep)
while True:
vstate = vol.status
if not vstate == 'available':
print "volume state is %s, trying again after %d secs" % (vstate,tsleep)
time.sleep(tsleep)
else:
break
print "Attaching vol %s to inst %s" % (str(vol.id),str(inst.id))
state.connection.attach_volume(vol.id, inst.id, "/dev/sdf")
print "attach_volume OK"
except Exception as e:
print "Exception: %s" % str(e)
对run_instances 的调用来自我需要修改的原始代码。创建卷后,当我在 AWS 控制台中查看其状态时,我看到了 available。但是,我得到了无穷无尽的序列
volume state is creating, trying again after 60 secs
为什么不一样?
【问题讨论】:
-
从来没有做过任何python。但似乎您正在运行一次
volume status checkONLY ,然后使用if循环对其进行永久检查。在运行if循环之前,您应该每次运行volume status check以获取最新更新。否则,if循环将继续与不存在的陈旧值进行比较。 -
是的,您应该在循环中的某个地方调用
vol.update()以获取有关您的音量的最新状态。 -
Garnaat,谢谢,是的,我确实需要更新...
标签: amazon-ec2 storage boto