forked from brunsgaard/eks-nvme-ssd-provisioner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eks-nvme-ssd-provisioner.sh
executable file
·83 lines (75 loc) · 2.77 KB
/
eks-nvme-ssd-provisioner.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail
mapfile -t SSD_NVME_DEVICE_LIST < <(nvme list | grep "Amazon EC2 NVMe Instance Storage" | cut -d " " -f 1 || true)
SSD_NVME_DEVICE_COUNT=${#SSD_NVME_DEVICE_LIST[@]}
RAID_DEVICE=${RAID_DEVICE:-/dev/md0}
RAID_CHUNK_SIZE=${RAID_CHUNK_SIZE:-512} # Kilo Bytes
FILESYSTEM_BLOCK_SIZE=${FILESYSTEM_BLOCK_SIZE:-4096} # Bytes
STRIDE=$((RAID_CHUNK_SIZE * 1024 / FILESYSTEM_BLOCK_SIZE))
STRIPE_WIDTH=$((SSD_NVME_DEVICE_COUNT * STRIDE))
TARGET_DIR="/var/lib/pv-disks"
# Checking if provisioning already happend
if [[ "$(ls -A ${TARGET_DIR})" ]]
then
echo "Volumes already present in \"${TARGET_DIR}\""
echo -e "\n$(ls -Al ${TARGET_DIR} | tail -n +2)\n"
echo "I assume that provisioning already happend, trying to assemble and mount!"
case $SSD_NVME_DEVICE_COUNT in
"0")
exit 1
;;
"1")
echo "no need to assable a raid"
DEVICE="${SSD_NVME_DEVICE_LIST[0]}"
;;
*)
# check if raid has already been started and is clean, if not try to assemble
mdadm --detail "$RAID_DEVICE" 2>/dev/null | grep clean >/dev/null || mdadm --assemble "$RAID_DEVICE" "${SSD_NVME_DEVICE_LIST[@]}"
# print details to log
mdadm --detail "$RAID_DEVICE"
DEVICE=$RAID_DEVICE
;;
esac
UUID=$(blkid -s UUID -o value "$DEVICE")
if mount | grep "$DEVICE" > /dev/null; then
echo "device $DEVICE appears to be mounted already"
else
mount -o defaults,noatime,discard,nobarrier --uuid "$UUID" "${TARGET_DIR}/$UUID"
fi
echo "Device $DEVICE has been mounted to ${TARGET_DIR}/$UUID"
while sleep 3600; do :; done
fi
# Perform provisioning based on nvme device count
case $SSD_NVME_DEVICE_COUNT in
"0")
echo 'No devices found of type "Amazon EC2 NVMe Instance Storage"'
echo "Maybe your node selectors are not set correct"
exit 1
;;
"1")
mkfs.ext4 -q -m 0 -b "$FILESYSTEM_BLOCK_SIZE" "${SSD_NVME_DEVICE_LIST[0]}"
DEVICE="${SSD_NVME_DEVICE_LIST[0]}"
;;
*)
mdadm --create --verbose "$RAID_DEVICE" --level=0 -c "${RAID_CHUNK_SIZE}" \
--raid-devices=${#SSD_NVME_DEVICE_LIST[@]} "${SSD_NVME_DEVICE_LIST[@]}"
while [ -n "$(mdadm --detail "$RAID_DEVICE" | grep -ioE 'State :.*resyncing')" ]; do
echo "Raid is resyncing.."
sleep 1
done
echo "Raid0 device $RAID_DEVICE has been created with disks ${SSD_NVME_DEVICE_LIST[*]}"
mkfs.ext4 -q -m 0 -b "$FILESYSTEM_BLOCK_SIZE" -E "stride=$STRIDE,stripe-width=$STRIPE_WIDTH" "$RAID_DEVICE"
DEVICE=$RAID_DEVICE
;;
esac
UUID=$(blkid -s UUID -o value "$DEVICE")
mkdir -p "${TARGET_DIR}/$UUID"
mount -o defaults,noatime,discard,nobarrier --uuid "$UUID" "${TARGET_DIR}/$UUID"
pushd "${TARGET_DIR}"
ln -s "$UUID" "local-nvme"
popd
echo "Device $DEVICE has been mounted to ${TARGET_DIR}/$UUID"
echo "NVMe SSD provisioning is done and I will go to sleep now"
while sleep 3600; do :; done