Last active
March 6, 2022 23:08
This file contains 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
set -o pipefail | |
set -o errtrace | |
set -o nounset | |
set -o errexit | |
set -a | |
# Scratch mount is the device which will be mounted on /mnt | |
# and generally used for logs, core dumps etc. | |
if ! $(mount | grep -q /mnt) ; then | |
# Detected NVME drives | |
# They do not always have a consistent drive number, this will scan for the drives slot in the hypervisor | |
# and mount the correct ones, with sda1 always being the base disk and sdb being the extra, larger, disk | |
if lshw | grep nvme &>/dev/null; then | |
for blkdev in $(nvme list | awk '/^\/dev/ { print $1 }'); do | |
mapping=$(nvme id-ctrl --raw-binary "${blkdev}" | cut -c3073-3104 | tr -s ' ' | sed 's/ $//g') | |
if [[ ${mapping} == "sda1" ]]; then | |
echo "$blkdev is $mapping skipping..." | |
elif [[ ${mapping} == "sdb" ]]; then | |
echo "$blkdev is $mapping formatting and mounting..." | |
mkfs -t ext4 ${blkdev} | |
echo "${blkdev} /mnt ext4 defaults,comment=cloudconfig 0 2" >> /etc/fstab | |
mount ${blkdev} | |
else | |
echo "detected unknown drive letter $blkdev: $mapping. Skipping..." | |
fi | |
done | |
else | |
echo "Configuring /dev/xvdb..." | |
mkfs -t ext4 /dev/xvdb | |
echo "/dev/xvdb /mnt ext4 defaults,comment=cloudconfig 0 2" >> /etc/fstab | |
mount /dev/xvdb | |
fi | |
else | |
echo "detected drive already mounted to /mnt, skipping mount..." | |
lsblk | grep mnt | |
fi |
Adapted this recently, but aws now reports the mapping as ephemeral0:sdb
instead of just sdb
, so adjusted to work as:
elif [[ ${mapping} == "ephemeral0:sdb" || ${mapping} == "sdb" ]]; then
(also nvme
wasn't installed by default, so had to add an apt install nvme-cli -y
before the run)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this! FYI, because the drive number isn't necessarily consistent across reboots, you should add to fstab using UUID instead.