Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
module/init-snippet-attach-ebs-volume: Manually make device name for …
Browse files Browse the repository at this point in the history
…attached EBS volume.

Due to Linux kernel and Distro changes, an attched EBS volume may not get the specified device name (Ref https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/device_naming.html). Hence logic following may break since specified device does not exist.

This change uses Linux trick to manually setup (if it has not) the device name, so other logic can be static, without guessing which device is it.
  • Loading branch information
Magicloud committed Jan 27, 2020
1 parent 5d49e88 commit 8ae64c7
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions modules/init-snippet-attach-ebs-volume/snippet.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ ${init_prefix}
export AWS_DEFAULT_REGION=${region}
VOLUME_ID=${volume_id}
INSTANCE_ID="$(ec2metadata --instance-id)"

echo "${log_prefix} will attach $${VOLUME_ID} via the AWS API in ${region}"
while ! aws ec2 attach-volume \
--volume-id "$${VOLUME_ID}" \
Expand All @@ -11,8 +12,14 @@ while ! aws ec2 attach-volume \
echo "Attaching command failed to run. Retrying."
sleep '${wait_interval}'
done
echo "${log_prefix} $${VOLUME_ID} attached."

while ! ls '${device_path}'; do
sleep '${wait_interval}'
vol_id="$(echo "$${VOLUME_ID}" | tr -d '-')"
while sleep '${wait_interval}'; [ ! -e /dev/disk/by-id/*-Amazon_Elastic_Block_Store_$${vol_id} ]; do
dev_id="$(ls /dev/disk/by-id/*-Amazon_Elastic_Block_Store_$${vol_id} | head -1)"
dev_name="/dev/$(readlink "$${dev_id}" | tr / '\n' | tail -1)"
[ "$${dev_name}" == "${device_path}" ] || ln -s "$${dev_name}" "$${device_path}"
break
done

${init_suffix}

0 comments on commit 8ae64c7

Please sign in to comment.