Add a helper script for doing the LVM setup on mirror nodes.

Mirror nodes have an atypical LVN setup.  In that the volume, once
visible to the guest, is split between 2 equal volumes for proxy and
afs caches.

This will do that work for us.  As the volume (at least in ord.rax) is
attached to the guest after initial creation this script isn't integrated
into launch_node (like mount_volume.sh).

Change-Id: I9ebc6daa9a65a654d9e8622ea6004ebbc28348a2
This commit is contained in:
Tony Breeds 2023-11-20 13:51:50 -06:00
parent ec9bf6ea28
commit 5684a75119

View File

@ -0,0 +1,139 @@
#!/usr/bin/env bash
# Copyright 2023 Red Hat, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
# NOTE(tonyb): set and shopts are done after CLI parsing for brevity.
function usage {
echo "$(basename $0): -d [device] -g [vg name] -v [volume spec]"
echo " [device]: The device path to use as the Physical Volume: eg /dev/sdb"
echo " [vg name]: The name for the new Volume Group: eg main"
echo " [volume spec]: A colon ':' separated specification for a volume to create."
echo " This contains 3 parts"
echo " <name>: The Logical Volume name: eg afscache"
echo " <size>: The size (in extents) for the Logical Volume: eg 50%VG"
echo " <mount point>: The mount point for the volume: eg /var/cache/afscache"
echo ""
echo "NOTE: This script doesn't need to know the full size of the volume"
echo ""
echo "Example invocation:"
echo "# $(basename $0) -d /dev/sdb -g main -v afscache:50%VG:/var/cache/openafs -v proxycache:50%VG:/var/cache/apache2"
}
declare -a VOLUMES=()
while [ $# -gt 0 ] ; do
case "$1" in
-d|--device)
PV_DEVICE="${2}"
shift 1
;;
-g|--group)
VOLUME_GROUP="${2}"
shift 1
;;
-v|--volume)
VOLUMES+=("${2}")
shift 1
;;
--help)
usage
exit 0
;;
*)
echo "Unkown arg: '$1'"
echo ""
usage
exit 1
;;
esac
shift 1
done
if [ -z "${PV_DEVICE}" ] ; then
echo "You need to specify a device. Aborting"
usage
exit 1
fi
if [ -z "${VOLUME_GROUP}" ] ; then
echo "You need to specify a volume group name. Aborting"
usage
exit 1
fi
if [ -z "${VOLUMES[*]}" ] ; then
echo "You need to specify at least one volume. Aborting"
usage
exit 1
fi
if [ "$(id -u)" -ne 0 ] ; then
echo "You need to have root permissions for this script to run. Aborting."
echo ""
usage
exit 1
fi
# Sanity check that we don't break anything that already has an fs.
if blkid | grep -q $PV_DEVICE ; then
echo "$PV_DEVICE appears in blkid. Aborting."
blkid
exit 1
fi
# Because images may not have all the things we need.
# TODO(tonyb): 1) Do we really care about yum here? ;
# 2) Audit script for other tools perl/parted etc
# 3) Also other scipts in this dir
if which apt-get ; then
apt-get update && apt-get install -y lvm2
elif which yum ; then
yum -y install lvm2
fi
set -xeuo pipefail
parted --script $PV_DEVICE mklabel msdos mkpart primary 0% 100% set 1 lvm on
partprobe -s $PV_DEVICE
pvcreate ${PV_DEVICE}1
vgcreate ${VOLUME_GROUP} ${PV_DEVICE}1
for vol_spec in "${VOLUMES[@]}"; do
IFS=:
set -- $vol_spec
VOLUME_NAME="$1"
VOLUME_SIZE="$2"
VOLUME_MOUNT="$3"
VOLUME_DEVICE="/dev/${VOLUME_GROUP}/${VOLUME_NAME}"
lvcreate -l "${VOLUME_SIZE}" -n $VOLUME_NAME ${VOLUME_GROUP}
mkfs.ext4 -m 0 -j -L $VOLUME_NAME ${VOLUME_DEVICE}
tune2fs -i 0 -c 0 ${VOLUME_DEVICE}
# Remove existing fstab entries for this device.
# TODO(tonyb): Should this be a pre-check and error rather than silently cleaning it up
# We could also add a flag to do this eg '--fstab-cleanup' or '--permissive'.
# This applies to othre scripts in this dir
perl -nle "m,${VOLUME_DEVICE}, || print" -i /etc/fstab
if [ ! -d "$VOLUME_MOUNT" ] ; then
mkdir -p "$VOLUME_MOUNT"
fi
echo "${VOLUME_DEVICE} ${VOLUME_MOUNT} ext4 errors=remount-ro,barrier=0 0 2" >> /etc/fstab
done
exit 0