Skip to content
This repository was archived by the owner on Apr 9, 2025. It is now read-only.

Commit 0566b51

Browse files
committed
Add method to ec2autorun that ensures /mnt is mounted from a device and attempts to mount it if not. Some EC2 instances do not auto-mount devices (e.g., r3 type)
1 parent b1ca25d commit 0566b51

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

files/ec2autorun.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,50 @@ def _handle_freenx(passwd):
362362
else:
363363
log.info("freenx-server is not installed; not configuring it")
364364

365+
366+
def _run(cmd):
367+
if not cmd:
368+
log.error("Trying to run an empty command? '{0}'".format(cmd))
369+
return False
370+
try:
371+
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
372+
stdout, stderr = process.communicate()
373+
if process.returncode == 0:
374+
log.debug("Successfully ran '%s'" % cmd)
375+
if stdout:
376+
return stdout
377+
else:
378+
return True
379+
else:
380+
log.error("Error running '%s'. Process returned code '%s' and following stderr: '%s'"
381+
% (cmd, process.returncode, stderr))
382+
return False
383+
except Exception, e:
384+
log.error("Exception running '%s': '%s'" % (cmd, e))
385+
return False
386+
387+
388+
def _ensure_ephemeral_disk_mounted():
389+
"""
390+
Make sure `/mnt` is a mounted device vs. just being part of `/`.
391+
392+
At least some AWS instance types (e.g., r3) do not auto-mount what's in
393+
`/ets/fstab` so make sure the ephemeral disks are in fact mounted.
394+
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/InstanceStorage.html#InstanceStoreTrimSupport
395+
"""
396+
if not _run('mountpoint -q /mnt'):
397+
device = '/dev/xvdb' # Most of AWS instances have this device
398+
if os.path.exists(device):
399+
log.debug("/mnt is not a mountpoint; will try to mount it from {0}"
400+
.format(device))
401+
_run('mkfs.xfs {0}'.format(device))
402+
_run('mount -o discard {0} /mnt'.format(device))
403+
else:
404+
log.warning("Mountpoint /mnt not available and no device {0}"
405+
.format(device))
406+
else:
407+
log.debug("/mnt is already a mountpoint so not mounting it again.")
408+
365409
# ====================== Actions methods ======================
366410

367411
def _handle_empty():
@@ -492,6 +536,8 @@ def _handle_yaml(user_data):
492536
with open(USER_DATA_FILE, 'w') as ud_yaml:
493537
yaml.dump(ud, ud_yaml, default_flow_style=False)
494538

539+
_ensure_ephemeral_disk_mounted()
540+
495541
# Get & run boot script
496542
if _get_boot_script(ud):
497543
_run_boot_script(DEFAULT_BOOT_SCRIPT_NAME)

0 commit comments

Comments
 (0)