diff --git a/launch/src/opendev_launch/mirror_volumes.sh b/launch/src/opendev_launch/mirror_volumes.sh new file mode 100644 index 0000000000..c23dfe2275 --- /dev/null +++ b/launch/src/opendev_launch/mirror_volumes.sh @@ -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 " : The Logical Volume name: eg afscache" + echo " : The size (in extents) for the Logical Volume: eg 50%VG" + echo " : 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