Skip to content

Instantly share code, notes, and snippets.

@tkuchiki
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkuchiki/dfc4f7e947db3d04c4b9 to your computer and use it in GitHub Desktop.
Save tkuchiki/dfc4f7e947db3d04c4b9 to your computer and use it in GitHub Desktop.
User Data で instance 起動時に ephemeral を mount する(RAID0)
- Amazon Linux AMI release 2014.03
- cloud-init-0.7.2-7.20.amzn1.noarch
(
- Amazon Linux AMI release 2013.09
- cloud-init-0.5.15-77.amzn1.noarch
)
#!/bin/sh
set -e
MOUNT_DIR="/data"
MOUNT_DEVICE="/dev/md0"
if mount | grep -qs "${MOUNT_DIR}"; then
echo "${MOUNT_DIR}: device is busy"
exit 1
fi
EPHEMERAL_DIR="/media/ephemeral0"
if mount | grep -qs "${EPHEMERAL_DIR}"; then
umount $EPHEMERAL_DIR
sed -i "\|$EPHEMERAL_DIR|d" /etc/fstab
fi
FTYPE="ext4"
LABEL="Ephemeral"
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
DEVICE_SYMS=$(aws ec2 describe-instance-attribute --instance-id $INSTANCE_ID --attribute blockDeviceMapping | python -c "exec(\"import json,sys\\nj=json.load(sys.stdin)\\nfor attr in
j.get('BlockDeviceMappings'):\\n print attr.get('DeviceName')\")")
IGNORE_DEVICES=""
for SYMLINK in $DEVICE_SYMS; do
if [ -L "${SYMLINK}" ]; then
IGNORE_DEVICES="${IGNORE_DEVICES}$(basename $(readlink ${SYMLINK}))|"
else
IGNORE_DEVICES="${IGNORE_DEVICES}$(basename ${SYMLINK})|"
fi
done
IGNORE_DEVICES=${IGNORE_DEVICES/%?/}
DEVICES=$(cat /proc/diskstats | egrep -v "${IGNORE_DEVICES}" | awk '{print $3}')
DEVICE_NUM=$(echo $DEVICES | wc -w)
if [ $DEVICE_NUM = 0 ]; then
echo "Ephemeral disk not found"
exit 1
fi
PARTITIONS=$(for DEV in $DEVICES; do printf "/dev/${DEV} "; done)
if [ $DEVICE_NUM -eq 1 ]; then
MOUNT_DEVICE=$PARTITIONS
else
yes | mdadm --create $MOUNT_DEVICE --level=0 --raid-devices=$DEVICE_NUM $PARTITIONS
fi
mkfs.$FTYPE $MOUNT_DEVICE
mkdir -p $MOUNT_DIR
echo "${MOUNT_DEVICE} ${MOUNT_DIR} ${FTYPE} defaults 0 0" >> /etc/fstab
sed -i "\|/mnt|d" /etc/fstab
mount -a
#!/bin/sh
set -e
EPHEMERAL_DIR="/media/ephemeral0"
if [ -d $EPHEMERAL_DIR ]; then
umount $EPHEMERAL_DIR
sed -i "\|$EPHEMERAL_DIR|d" /etc/fstab
fi
FTYPE="ext4"
LABEL="Ephemeral"
MOUNT_DIR="/data"
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
DEVICE_SYMS=$(aws ec2 describe-instance-attribute --instance-id $INSTANCE_ID --attribute blockDeviceMapping | python -c "exec(\"import json,sys\\nj=json.load(sys.stdin)\\nfor attr in j.get('BlockDeviceMappings'):\\n print attr.get('DeviceName')\")")
IGNORE_DEVICES=""
for SYMLINK in $DEVICE_SYMS; do
IGNORE_DEVICES="$(readlink ${SYMLINK})|"
done
IGNORE_DEVICES=${IGNORE_DEVICES/%?/}
DEVICES=$(cat /proc/diskstats | egrep -v "${IGNORE_DEVICES}" | awk '{print $3}')
DEVICE_NUM=$(echo $DEVICES | wc -w)
for DEV in $DEVICES; do
fdisk /dev/$DEV <<\EOF
n
p
1
w
EOF
done
PARTITIONS=$(for DEV in $DEVICES; do printf "/dev/${DEV}1 "; done)
mdadm --create /dev/md0 --level=0 --raid-devices=$DEVICE_NUM $PARTITIONS
mkfs.$FTYPE /dev/md0
e2label /dev/md0 $LABEL
mkdir $MOUNT_DIR
echo "LABEL=${LABEL} ${MOUNT_DIR} ${FTYPE} defaults 0 0" >> /etc/fstab
mount -a
# The top level settings are used as module
# and system configuration.
# A set of users which may be applied and/or used by various modules
# when a 'default' entry is found it will reference the 'default_user'
# from the distro configuration specified below
users:
- default
ssh_pwauth: false
# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: true
# Example datasource config
# datasource:
# Ec2:
# metadata_urls: [ 'blah.com' ]
# timeout: 5 # (defaults to 50 seconds)
# max_wait: 10 # (defaults to 120 seconds)
locale_configfile: /etc/sysconfig/i18n
#mount_default_fields: [~, ~, 'auto', 'defaults,nofail', '0', '2']
resize_rootfs_tmp: /dev
ssh_deletekeys: true
ssh_genkeytypes: [ 'rsa', 'dsa' ]
syslog_fix_perms: ~
# The modules that run in the 'init' stage
cloud_init_modules:
- rsyslog
- migrator
- bootcmd
- write-files
- growpart
- resizefs
- set-hostname
- update-hostname
- update-etc-hosts
- users-groups
# The modules that run in the 'config' stage
cloud_config_modules:
- locale
- ssh
- set-passwords
- mounts
- yum-configure
- yum-add-repo
- package-update-upgrade-install
- timezone
- puppet
- disable-ec2-metadata
- runcmd
# The modules that run in the 'final' stage
cloud_final_modules:
- scripts-per-once
- scripts-per-boot
- scripts-per-instance
- scripts-user
- ssh-authkey-fingerprints
- keys-to-console
- phone-home
- final-message
- power-state-change
# System and/or distro specific settings
# (not accessible to handlers/transforms)
system_info:
# This will affect which distro class gets used
distro: amazon
distro_short: amzn
# Default user name + that default users groups (if added/used)
default_user:
name: ec2-user
lock_passwd: true
gecos: EC2 Default User
groups: [ wheel ]
sudo: [ "ALL=(ALL) NOPASSWD:ALL" ]
shell: /bin/bash
# Other config here will be given to the distro class and/or path classes
paths:
cloud_dir: /var/lib/cloud/
templates_dir: /etc/cloud/templates/
upstart_dir: /etc/init/
package_mirrors:
- arches: [ i386, x86_64 ]
search:
regional:
- repo.%(ec2_region)s.%(services_domain)s
- repo.%(ec2_region)s.amazonaws.com
ssh_svcname: sshd
# vim:syntax=yaml
# If this is set, 'root' will not be able to ssh in and they
# will get a message to login instead as the above $user (ec2-user)
disable_root: true
# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: true
datasource_list: [ Ec2, None ]
repo_upgrade: security
repo_upgrade_exclude:
- kernel
- nvidia*
- cudatoolkit
mounts:
- [ swap, none, swap, sw, "0", "0" ]
# vim:syntax=yaml
runcmd:
- [/usr/local/sbin/create-ephemeral]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment