From 2742bad3c3eb31a93b74751740c83e7cca5f38b3 Mon Sep 17 00:00:00 2001 From: Magicloud <1886157+Magicloud@users.noreply.github.com> Date: Mon, 27 Jan 2020 20:19:16 +0800 Subject: [PATCH] module/init-snippet-attach-ebs-volume: Manually make device name for 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. --- modules/init-snippet-attach-ebs-volume/snippet.tpl | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/modules/init-snippet-attach-ebs-volume/snippet.tpl b/modules/init-snippet-attach-ebs-volume/snippet.tpl index 130a06fd..48515174 100644 --- a/modules/init-snippet-attach-ebs-volume/snippet.tpl +++ b/modules/init-snippet-attach-ebs-volume/snippet.tpl @@ -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}" \ @@ -11,8 +12,15 @@ 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 [ ! -e /dev/disk/by-id/*-Amazon_Elastic_Block_Store_$${vol_id} ]; do + sleep '${wait_interval}' done + +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}" + ${init_suffix}