Skip to content

Commit 43fe73f

Browse files
authored
Merge pull request #557 from buildkite/add-instance-storage
Optionally mount Instance Storage on boot
2 parents 2ba5604 + 5c7391a commit 43fe73f

File tree

8 files changed

+113
-5
lines changed

8 files changed

+113
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ CloudWatch Logs Agent | ✅ | ✅
5151
Per-Instance Bootstrap Script | ✅ | ✅
5252
🧑‍🔬 git-mirrors experiment | ✅ | ✅
5353
SSM Access | ✅ | ✅
54+
Instance Storage (NVMe) | ✅ |
5455
SSH Access | ✅ |
5556
Periodic authorized_keys Refresh | ✅ |
5657
Periodic Instance Health Check | ✅ |

packer/linux/buildkite-ami.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@
6565
"type": "shell",
6666
"script": "scripts/install-session-manager-plugin.sh"
6767
},
68+
{
69+
"type": "shell",
70+
"script": "scripts/install-nvme-cli.sh"
71+
},
6872
{
6973
"type": "shell",
7074
"inline": [

packer/linux/conf/bin/bk-check-disk-space.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -euo pipefail
44
DISK_MIN_AVAILABLE=${DISK_MIN_AVAILABLE:-5242880} # 5GB
55
DISK_MIN_INODES=${DISK_MIN_INODES:-250000} # docker needs lots
66

7-
DOCKER_DIR="/var/lib/docker/"
7+
DOCKER_DIR="$(jq -r '."data-root" // "/var/lib/docker"' /etc/docker/daemon.json)"
88

99
disk_avail=$(df -k --output=avail "$DOCKER_DIR" | tail -n1)
1010

packer/linux/conf/bin/bk-configure-docker.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ fi
1717
if [[ "${DOCKER_EXPERIMENTAL:-false}" == "true" ]] ; then
1818
cat <<< "$(jq '.experimental=true' /etc/docker/daemon.json)" > /etc/docker/daemon.json
1919
fi
20+
21+
# Move docker root to the ephemeral device
22+
if [[ "${BUILDKITE_ENABLE_INSTANCE_STORAGE:-false}" == "true" ]] ; then
23+
cat <<< "$(jq '."data-root"="/mnt/ephemeral/docker"' /etc/docker/daemon.json)" > /etc/docker/daemon.json
24+
fi

packer/linux/conf/bin/bk-install-elastic-stack.sh

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,32 @@ if [[ -n "${BUILDKITE_AGENT_TAGS:-}" ]] ; then
126126
fi
127127

128128
# Enable git-mirrors
129+
BUILDKITE_AGENT_GIT_MIRRORS_PATH=""
129130
if [[ "${BUILDKITE_AGENT_ENABLE_GIT_MIRRORS_EXPERIMENT}" == "true" ]] ; then
130131
if [[ -z "$BUILDKITE_AGENT_EXPERIMENTS" ]] ; then
131132
BUILDKITE_AGENT_EXPERIMENTS="git-mirrors"
132133
else
133134
BUILDKITE_AGENT_EXPERIMENTS+=",git-mirrors"
134135
fi
135-
BUILDKITE_AGENT_GIT_MIRRORS_PATH="/var/lib/buildkite-agent/git-mirrors"
136-
else
137-
BUILDKITE_AGENT_GIT_MIRRORS_PATH=""
136+
137+
if [ "${BUILDKITE_ENABLE_INSTANCE_STORAGE:-false}" == "true" ]
138+
then
139+
BUILDKITE_AGENT_GIT_MIRRORS_PATH="/mnt/ephemeral/git-mirrors"
140+
141+
mkdir -p "${BUILDKITE_AGENT_GIT_MIRRORS_PATH}"
142+
chown buildkite-agent: "${BUILDKITE_AGENT_GIT_MIRRORS_PATH}"
143+
else
144+
BUILDKITE_AGENT_GIT_MIRRORS_PATH="/var/lib/buildkite-agent/git-mirrors"
145+
fi
146+
fi
147+
148+
BUILDKITE_AGENT_BUILD_PATH="/var/lib/buildkite-agent/builds"
149+
if [ "${BUILDKITE_ENABLE_INSTANCE_STORAGE:-false}" == "true" ]
150+
then
151+
BUILDKITE_AGENT_BUILD_PATH="/mnt/ephemeral/builds"
152+
153+
mkdir -p "${BUILDKITE_AGENT_BUILD_PATH}"
154+
chown buildkite-agent: "${BUILDKITE_AGENT_BUILD_PATH}"
138155
fi
139156

140157
BUILDKITE_AGENT_TOKEN="$(aws ssm get-parameter --name "${BUILDKITE_AGENT_TOKEN_PATH}" --with-decryption --query Parameter.Value --output text)"
@@ -146,7 +163,7 @@ tags=$(IFS=, ; echo "${agent_metadata[*]}")
146163
tags-from-ec2-meta-data=true
147164
timestamp-lines=${BUILDKITE_AGENT_TIMESTAMP_LINES}
148165
hooks-path=/etc/buildkite-agent/hooks
149-
build-path=/var/lib/buildkite-agent/builds
166+
build-path=${BUILDKITE_AGENT_BUILD_PATH}
150167
plugins-path=/var/lib/buildkite-agent/plugins
151168
git-mirrors-path="${BUILDKITE_AGENT_GIT_MIRRORS_PATH}"
152169
experiment="${BUILDKITE_AGENT_EXPERIMENTS}"
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
sudo yum install -y nvme-cli

templates/aws-stack.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Metadata:
4141
- ImageId
4242
- ImageIdParameter
4343
- InstanceType
44+
- EnableInstanceStorage
4445
- AgentsPerInstance
4546
- KeyName
4647
- SpotPrice
@@ -383,6 +384,14 @@ Parameters:
383384
- "false"
384385
Default: "false"
385386

387+
EnableInstanceStorage:
388+
Type: String
389+
Description: Mount available NVMe Instance Storage at /mnt/ephemeral
390+
AllowedValues:
391+
- "true"
392+
- "false"
393+
Default: "false"
394+
386395
EnableCostAllocationTags:
387396
Type: String
388397
Description: Enables AWS Cost Allocation tags for all resources in the stack. See https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/cost-alloc-tags.html
@@ -952,8 +961,13 @@ Resources:
952961
MIME-Version: 1.0
953962
--==BOUNDARY==
954963
Content-Type: text/cloud-boothook; charset="us-ascii"
964+
BUILDKITE_ENABLE_INSTANCE_STORAGE="${EnableInstanceStorage}" \
965+
/usr/local/bin/bk-mount-instance-storage.sh
966+
--==BOUNDARY==
967+
Content-Type: text/cloud-boothook; charset="us-ascii"
955968
DOCKER_USERNS_REMAP=${EnableDockerUserNamespaceRemap} \
956969
DOCKER_EXPERIMENTAL=${EnableDockerExperimental} \
970+
BUILDKITE_ENABLE_INSTANCE_STORAGE="${EnableInstanceStorage}" \
957971
/usr/local/bin/bk-configure-docker.sh
958972
--==BOUNDARY==
959973
Content-Type: text/x-shellscript; charset="us-ascii"
@@ -971,6 +985,7 @@ Resources:
971985
BUILDKITE_QUEUE="${BuildkiteQueue}" \
972986
BUILDKITE_AGENT_ENABLE_GIT_MIRRORS_EXPERIMENT=${EnableAgentGitMirrorsExperiment} \
973987
BUILDKITE_ELASTIC_BOOTSTRAP_SCRIPT="${BootstrapScriptUrl}" \
988+
BUILDKITE_ENABLE_INSTANCE_STORAGE="${EnableInstanceStorage}" \
974989
BUILDKITE_AUTHORIZED_USERS_URL="${AuthorizedUsersUrl}" \
975990
BUILDKITE_ECR_POLICY=${ECRAccessPolicy} \
976991
BUILDKITE_TERMINATE_INSTANCE_AFTER_JOB=${BuildkiteTerminateInstanceAfterJob} \

0 commit comments

Comments
 (0)