AWS EC2 Linux instance storage needs to be initialized first before it can be used. When setting up Jenkins EC2 Linux agent, we can do this with Init Script. The Init Script below formats and mounts instance storage if available and use it as Jenkins workspace and Docker data-root. This Init Script is failsafe. It means that it will not cause any errors if it turns out that the EC2 Linux instance does not have an instance storage.
set -x
export DEBIAN_FRONTEND=noninteractive
# https://engineering.deptagency.com/devops-fix-intermittent-yum-and-apt-errors-on-new-ec2
while [ ! -f /var/lib/cloud/instance/boot-finished ]; do
echo "Waiting for cloud-init to finish ..."
sleep 2
done
apt-get -qq update
apt-get -qq -y install nvme-cli
mkdir -p /storage
EPHEMERAL_DISK=$(nvme list | grep 'Amazon EC2 NVMe Instance Storage' | awk '{ print $1 }')
test -n "$EPHEMERAL_DISK" && (mkfs -t ext4 "$EPHEMERAL_DISK" && mount "$EPHEMERAL_DISK" /storage) || true # Use instance storage (if any) e.g. m5dn.large
lsblk
apt-get -qq -y install default-jre-headless git git-lfs
mkdir -p /storage/jenkins
chmod a+w /storage/jenkins
apt-get -qq -y install make
mkdir -p /etc/docker
mkdir -p /storage/docker
cat << EOF > /etc/docker/daemon.json
{
"data-root": "/storage/docker"
}
EOF
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh
chgrp docker /usr/bin/docker
chmod g+s /usr/bin/docker
apt-get clean