Created
April 7, 2023 05:27
-
-
Save zph/5935caeeebc001e2af38f087da19d5af to your computer and use it in GitHub Desktop.
EBS disk warming script using fio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# EBS disk warming script to run on boot to automatically warm restored snapshot | |
# disks once per volume id | |
# Place entry in crontab for root using `crontab -e` | |
# ### Note some paths are restrictive and require extra workarounds | |
# PATH=/sbin:/usr/local/bin:/bin | |
# @reboot bash -c "sleep 60 && ebs_warm_disk >> /var/log/fio.log 2>&1" | |
# Assumptions | |
# volume to warm is /data and xfs type | |
set -eou pipefail | |
set -x | |
readonly FIO_COMPLETED="${FIO_COMPLETED:-/data/fio.completed}" | |
readonly FIO_LOG="${FIO_LOG:-/var/log/fio.log}" | |
readonly FIO_LOCK_FILE="${FIO_LOCK_FILE:-/data/fio.lock}" | |
declare fio_bin | |
declare ebsnvme_bin | |
fio_bin="$(which fio)" | |
ebsnvme_bin="$(which ebsnvme-id)" | |
# Use 200mb as the rate since that can be tolerated by most/all drives in our network | |
# with these settings it works out to be 1600 iops | |
function run_fio() { | |
local device="$1" | |
sudo "$fio_bin" --status-interval=60 --eta-newline=60 --filename="$device" --rw=read --bs=128k --iodepth=64 --ioengine=libaio --direct=1 --name=volume-initialize --rate=200m 2>&1 | sudo tee -a "$FIO_LOG" | |
echo "$volumeId" > "$FIO_COMPLETED" | |
} | |
function main() { | |
local device | |
local volumeId | |
device="$(grep '/data xfs' < /proc/mounts | awk '{print $1}')" | |
volumeId="$(sudo "${ebsnvme_bin}" "$device" | awk -F': ' '{print $2}')" | |
# when fio completes create /data/.fio.complete that contains the volume-id | |
if [[ ! -f "$FIO_COMPLETED" ]]; then | |
# missing fio completion file, needs warming | |
run_fio "$device" | |
elif ! grep "$volumeId" < "$FIO_COMPLETED" ; then | |
# file present but lacks the correct volumeid | |
run_fio "$device" | |
else | |
# no warming needed | |
echo "ebs volume ${device} already warmed" | |
fi | |
} | |
{ | |
# Exit if cannot acquire exclusive lock | |
flock --nonblock -x 42 || exit 1 | |
main | |
} 42>"$FIO_LOCK_FILE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment