|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Write to system console and to our log file |
| 5 | +# See https://alestic.com/2010/12/ec2-user-data-output/ |
| 6 | +exec > >(tee -a /var/log/elastic-stack.log|logger -t user-data -s 2>/dev/console) 2>&1 |
| 7 | + |
| 8 | +# Mount instance storage if we can |
| 9 | +# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/InstanceStorage.html |
| 10 | + |
| 11 | +# Move docker root to the ephemeral device |
| 12 | +if [[ "${BUILDKITE_ENABLE_INSTANCE_STORAGE:-false}" != "true" ]] ; then |
| 13 | + echo "Skipping mounting instance storage" |
| 14 | + exit 0 |
| 15 | +fi |
| 16 | + |
| 17 | +#shellcheck disable=SC2207 |
| 18 | +devices=($(nvme list | grep "Amazon EC2 NVMe Instance Storage"| cut -f1 -d' ')) |
| 19 | + |
| 20 | +if [ -z "${devices[*]}" ] |
| 21 | +then |
| 22 | + echo "No Instance Storage NVMe drives to mount" >&2 |
| 23 | + exit 0 |
| 24 | +fi |
| 25 | + |
| 26 | +if [[ "${#devices[@]}" -eq 1 ]] ; then |
| 27 | + echo "Mounting instance storage device directly" >&2 |
| 28 | + logicalname="${devices[0]}" |
| 29 | +elif [[ "${#devices[@]}" -gt 1 ]] ; then |
| 30 | + echo "Mounting instance storage devices using software RAID" >&2 |
| 31 | + logicalname=/dev/md0 |
| 32 | + |
| 33 | + mdadm \ |
| 34 | + --create "$logicalname" \ |
| 35 | + --level=0 \ |
| 36 | + -c256 \ |
| 37 | + --raid-devices="${#devices[@]}" "${devices[@]}" |
| 38 | + |
| 39 | + echo "DEVICE ${devices[*]}" > /etc/mdadm.conf |
| 40 | + |
| 41 | + mdadm --detail --scan >> /etc/mdadm.conf |
| 42 | + blockdev --setra 65536 "$logicalname" |
| 43 | +fi |
| 44 | + |
| 45 | +# Make an ext4 file system, [-F]orce creation, don’t TRIM at fs creation time |
| 46 | +# (-E nodiscard) |
| 47 | +echo "Formatting $logicalname as ext4" >&2 |
| 48 | +mkfs.ext4 -F -E nodiscard "$logicalname" > /dev/null |
| 49 | + |
| 50 | +devicemount=/mnt/ephemeral |
| 51 | + |
| 52 | +echo "Mounting $logicalname to $devicemount" >&2 |
| 53 | + |
| 54 | +fs_type="ext4" |
| 55 | +mount_options="defaults,noatime" |
| 56 | + |
| 57 | +mkdir -p "$devicemount" |
| 58 | +mount -t "${fs_type}" -o "${mount_options}" "$logicalname" "$devicemount" |
| 59 | + |
| 60 | +if [ ! -f /etc/fstab.backup ]; then |
| 61 | + cp -rP /etc/fstab /etc/fstab.backup |
| 62 | + echo "$logicalname $devicemount ${fs_type} ${mount_options} 0 0" >> /etc/fstab |
| 63 | +fi |
0 commit comments