EC2 の EBS Volume を自動マウントするコマンドをちょっと改造。


IMG_0026



※写真と本文は関係ありません。


Amazon EBSのボリュームを自動でattachしてマウントしたい – cyberarchitect

を参考にさせて頂き試してみましたがエラーが…。><
『Client.InvalidVolume.NotFound: The volume ‘vol-XXXXXXXX’ does not exist.』

リージョンの設定が足りないようだったので。

Scripts to automatically attach and mount an EBS Volume at Boot Time
のスクリプトを改造して、ec2-attach-volume コマンドに –region のオプションを追加した。

# diff -Nur mount_ebs_volume.py_org mount_ebs_volume.py > mount_ebs_volume_add_region.patch
# cat mount_ebs_volume_add_region.patch


--- mount_ebs_volume.py_org 2011-09-27 16:04:22.028387625 +0900
+++ mount_ebs_volume.py 2011-09-28 13:14:12.100673654 +0900
@@ -31,7 +31,7 @@
 
 def mount():
    logging.info('Attempting to attach volume [%s] to instance [%s] as [%s]' % (volume_name, instance_id, device_name))
-   cmd = 'ec2-attach-volume -C %s -K %s %s -i %s -d %s' % (ec2conf['CERT'], ec2conf['PRIVKEY'], volume_name, instance_id, device_name)
+   cmd = 'ec2-attach-volume -C %s -K %s %s -i %s -d %s --region %s' % (ec2conf['CERT'], ec2conf['PRIVKEY'], volume_name, instance_id, device_name, ec2conf['REGION'])
 
    for i in range(0, 20):
        p = Popen(cmd, shell=True,stdout=PIPE);
@@ -49,7 +49,7 @@
            logging.info('Volume [%s] attaching to device [%s] in attempt #%d' % (_volume_id, _device_id, i))
 
            # wait for fully attached
-           cmd = 'ec2-describe-volumes -C %s -K %s' % (ec2conf['CERT'], ec2conf['PRIVKEY'])
+           cmd = 'ec2-describe-volumes -C %s -K %s --region %s' % (ec2conf['CERT'], ec2conf['PRIVKEY'], ec2conf['REGION'])
            for n in range(0, 20):
                logging.debug('Running ec2-describe-volumes for the %dth time' % n)
                p = Popen(cmd, shell=True,stdout=PIPE);
@@ -79,13 +79,21 @@
 def unmount():
    logging.info('Attempting to detach volume [%s] from instance [%s] as [%s]' % (volume_name, instance_id, device_name))
 
-   cmd = 'ec2-detach-volume -C %s -K %s %s -i %s -d %s' % (ec2conf['CERT'], ec2conf['PRIVKEY'], volume_name, instance_id, device_name)
+   cmd = 'umount %s ' % (device_name)
+   p = Popen(cmd, shell=True,stdout=PIPE);
+   exitcode = p.wait() & 0xff;    # exit code is in the high byte
+   logging.debug('umount returned status code %d' % exitcode)
+   if exitcode == 0:
+       logging.info('Successfully unmount volume [%s] as [%s]' % (volume_name, device_name))
+
+   cmd = 'ec2-detach-volume -C %s -K %s %s -i %s -d %s --region %s' % (ec2conf['CERT'], ec2conf['PRIVKEY'], volume_name, instance_id, device_name, ec2conf['REGION'])
    p = Popen(cmd, shell=True,stdout=PIPE);
    exitcode = p.wait() & 0xff;    # exit code is in the high byte
    logging.debug('ec2-detach-volume returned status code %d' % exitcode)
    if exitcode == 0:
        logging.info('Successfully detached volume [%s] from instance [%s] as [%s]' % (volume_name, instance_id, device_name))
 
+
    
 # arguments
 if len(sys.argv) < 5:
@@ -119,9 +127,9 @@
 logger.setLevel(logging.INFO)
 
 # setup environment
-os.putenv('PATH', '/root/ec2-api-tools/bin/:' + os.getenv('PATH'))
-os.putenv('EC2_HOME', '/root/ec2-api-tools/')
-os.putenv('JAVA_HOME', '/usr/lib/jvm/java-6-sun')
+os.putenv('PATH', '/opt/aws/bin/:' + os.getenv('PATH'))
+os.putenv('EC2_HOME', '/opt/aws/apitools/ec2/')
+os.putenv('JAVA_HOME', '/usr/lib/jvm/jre/')
 
 # EC2 conf
 ec2conf = shellVars2Dict('/root/.ec2cred')


※ ec2-detach-volume するときに unmount しないとうまくいかなかったのでそれも追加した。

.ec2cred に
REGION=ap-northeast-1
を追加した。

# diff -Nur .ec2cred_org .ec2cred


--- .ec2cred_org    2011-09-27 08:40:53.000000000 +0900
+++ .ec2cred    2011-09-27 16:17:53.789461128 +0900
@@ -1,4 +1,5 @@
-CERT=/root/cert-XXX.pem
-PRIVKEY=/root/pk-XXX.pem
-AWSACCOUNTID=XXXX-XXXX-XXXX
+CERT=/root/cert-XXXAAA.pem
+PRIVKEY=/root/pk-XXXAAA.pem
+AWSACCOUNTID=XXXX-XXXX-XXXX
+REGION=ap-northeast-1


これでOKになりました。