Factor out repeated code

This commit is contained in:
Thomas Stokes committed 2024-04-04 11:12:05 +02:00
1 parent 1597f86557
commit fac6b0a226
3 files changed
+48 -40

No files matched your search

@@ -3,7 +3,6 @@
# Script to deploy on an instance that has an ephemeral volume as /dev/nvme0n1 (adjust env var PARTITION if different)
# Ensures the partition is xfs-formatted, any existing partition contents will be overwritten if formatted otherwise.
# An existing xfs partition will be left alone.
METADATA=$( /bin/ec2-metadata -d | sed -e 's/^user-data: //' )
echo "Metadata: ${METADATA}"
if echo "${METADATA}" | grep -q "^image-upgrade$"; then
@@ -11,23 +10,22 @@ if echo "${METADATA}" | grep -q "^image-upgrade$"; then
imageupgrade.sh
else
echo "No image upgrade; looking for ephemeral volume and trying to format with xfs..."
PARTITION=/dev/nvme0n1
if [ \! -e $PARTITION ]; then
PARTITION=/dev/xvdb
fi
if [ \! -e $PARTITION ]; then
echo "Neither /dev/nvme0n1 nor /dev/xvdb partition found; not formatting/mounting ephemeral volume"
elif cat /proc/mounts | awk '{print $1;}' | grep "${PARTITION}"; then
echo "Partition ${PARTITION} already mounted; not formatting/mounting ephemeral volume"
else
FSTYPE=$(blkid -p $PARTITION -s TYPE -o value)
if [ "$FSTYPE" != "xfs" ]; then
echo FSTYPE was "$FSTYPE" but should have been xfs. Formatting $PARTITION...
mkfs.xfs -f $PARTITION
. imageupgrade_functions.sh
PARTITION_NAME=$(identify_suitable_partition)
if [[ -n "${PARTITION_NAME}" ]]; then
PARTITION=/dev/${PARTITION_NAME}
if cat /proc/mounts | awk '{print $1;}' | grep "${PARTITION}"; then
echo "Partition ${PARTITION} already mounted; not formatting/mounting ephemeral volume"
else
echo FSTYPE was "$FSTYPE" which is just right :-\)
FSTYPE=$(blkid -p $PARTITION -s TYPE -o value)
if [[ "$FSTYPE" != "xfs" ]]; then
echo FSTYPE was "$FSTYPE" but should have been xfs. Formatting $PARTITION...
mkfs.xfs -f $PARTITION
else
echo FSTYPE was "$FSTYPE" which is just right :-\)
fi
# mount the thing to /var/lib/mongo
mount $PARTITION /var/lib/mongo
fi
# mount the thing to /var/lib/mongo
mount $PARTITION /var/lib/mongo
fi
fi
fi
@@ -222,4 +222,21 @@ setup_sshd_resilience() {
ClientAliveCountMax 3
GatewayPorts yes" >> /etc/ssh/sshd_config
systemctl reload sshd.service
}
identify_suitable_partition() {
EPHEMERAL_VOLUME_NAME=$(
# List all block devices and find those named nvme...
for i in $(lsblk | grep -o "nvme[0-9][0-9]\?n[0-9]" | sort -u); do
# If they don't have any partitions, then...
if ! lsblk | grep -o "${i}p[0-9]\+" 2>&1 >/dev/null; then
# ...check whether they are EBS devices
/sbin/ebsnvme-id -u "/dev/$i" >/dev/null
# If not, list their name because then they must be ephemeral instance storage
if [[ $? -ne 0 ]]; then
echo "${i}"
fi
fi
done 2>/dev/null | head -n 1 )
echo $EPHEMERAL_VOLUME_NAME
}
@@ -2,30 +2,23 @@
# Script to deploy on an instance that has an ephemeral volume as /dev/nvme0n1 (adjust env var EPHEMERAL_VOLUME if different)
# Ensures the partition is xfs-formatted, any existing partition contents will be overwritten if formatted otherwise.
# An existing xfs partition will be left alone.
EPHEMERAL_VOLUME_NAME=$(
# List all block devices and find those named nvme...
for i in $(lsblk | grep -o "nvme[0-9][0-9]\?n[0-9]" | sort -u); do
# If they don't have any partitions, then...
if ! lsblk | grep -o "${i}p[0-9]\+" 2>&1 >/dev/null; then
# ...check whether they are EBS devices
/sbin/ebsnvme-id -u "/dev/$i" >/dev/null
# If not, list their name because then they must be ephemeral instance storage
if [[ $? -ne 0 ]]; then
echo "${i}"
fi
fi
done 2>/dev/null | head -n 1 )
if [ -n "${EPHEMERAL_VOLUME_NAME}" ]; then
# An existing xfs partition will be left alone.
. imageupgrade_functions.sh
EPHEMERAL_VOLUME_NAME=$(identify_suitable_partition)
if [[ -n "${EPHEMERAL_VOLUME_NAME}" ]]; then
EPHEMERAL_VOLUME=/dev/${EPHEMERAL_VOLUME_NAME}
FSTYPE=$(blkid -p $EPHEMERAL_VOLUME -s TYPE -o value)
if [ "$FSTYPE" = "" ]; then
echo FSTYPE was empty, creating swap partition
mkswap $EPHEMERAL_VOLUME
swapon --priority 2 -a $EPHEMERAL_VOLUME
if awk '{print $1;}' /proc/mounts | grep "${PARTITION}"; then
echo "Partition ${PARTITION} already mounted; not formatting/mounting volume"
else
echo "FSTYPE was $FSTYPE, not touching"
FSTYPE=$(blkid -p $EPHEMERAL_VOLUME -s TYPE -o value)
if [ "$FSTYPE" = "" ]; then
echo FSTYPE was empty, creating swap partition
mkswap $EPHEMERAL_VOLUME
swapon --priority 2 -a $EPHEMERAL_VOLUME
else
echo "FSTYPE was $FSTYPE, not touching"
fi
fi
else
echo "No ephemeral partition found. Not creating any swap partition."
fi
fi