Skip to content

Instantly share code, notes, and snippets.

@ctompkinson
Last active March 6, 2022 23:08
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ctompkinson/30f570882af38879b36fc7bffe3d1a60 to your computer and use it in GitHub Desktop.
Save ctompkinson/30f570882af38879b36fc7bffe3d1a60 to your computer and use it in GitHub Desktop.
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
@luhn
Copy link

luhn commented Oct 23, 2020

Thanks for this! FYI, because the drive number isn't necessarily consistent across reboots, you should add to fstab using UUID instead.

			uuid=$(blkid -s UUID -o value ${blkdev})
			echo "UUID=\"${uuid}\"	${mount_point}	ext4	defaults,nofail" >> /etc/fstab
			mount UUID="${uuid}"

@mattsta
Copy link

mattsta commented Mar 6, 2022

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