scripts/partition-and-mount.sh
#!/bin/bash
set -eufx -o pipefail

MOUNT_POINT="${1:-}"
DEVICES=("${@:2}")
if [ -z "${DEVICES}" ] || [ -z "${MOUNT_POINT}" ]
then
    echo "Usage: $0 <mount_point> <device> [device]..."
    exit 1
fi
BACKUP_DIR="${MOUNT_POINT}_backup"
BACKED_UP=false

# If there's more than one device, we'll create a volume group and
# then turn it into a logical volume.
# Otherwise, we'll just create a partition on the first device.
if [ ${#DEVICES[@]} -gt 1 ]
then
    VOLUME_NAME="$(basename "${MOUNT_POINT}")"
    echo "Creating a new volume group on [${DEVICES[@]}]"
    sudo vgcreate ${VOLUME_NAME} ${DEVICES[@]}
    sudo lvcreate -l 100%FREE -n ${VOLUME_NAME} ${VOLUME_NAME}

    FS_TYPE="xfs"
    FSTAB_OPTIONS="defaults,pquota 0 0"
    MOUNT_OPTIONS="-o pquota"
    PARTITION=/dev/${VOLUME_NAME}/${VOLUME_NAME}
else
    echo "Creating a new partition on ${DEVICES[0]}"
    sudo parted -s ${DEVICES[0]} mklabel gpt
    sudo parted -s ${DEVICES[0]} mkpart primary ext4 0% 100%

    FS_TYPE="ext4"
    FSTAB_OPTIONS="defaults 0 2"
    MOUNT_OPTIONS=""
    PARTITION="${DEVICES[0]}p1"
fi

echo "Creating a new ${FS_TYPE} filesystem on ${PARTITION}"
sudo mkfs.${FS_TYPE} ${PARTITION}

if [ -d "${MOUNT_POINT}" ]
then
    MOUNT_POINT_TMP="$(mktemp -d)"
    echo "Mounting the new partition temporarily at ${MOUNT_POINT_TMP}"
    sudo mount ${MOUNT_OPTIONS} ${PARTITION} ${MOUNT_POINT_TMP}

    echo "Copying contents of ${MOUNT_POINT} to the new partition"
    sudo rsync -aHAXv ${MOUNT_POINT}/ ${MOUNT_POINT_TMP}/
    
    echo "Unmounting the new partition from ${MOUNT_POINT_TMP}"
    sudo umount ${MOUNT_POINT_TMP}

    echo "Backing up the existing ${MOUNT_POINT} directory to ${BACKUP_DIR}"
    sudo mv ${MOUNT_POINT} ${BACKUP_DIR}

    BACKED_UP=true
fi

echo "Creating a new ${MOUNT_POINT} directory"
sudo mkdir ${MOUNT_POINT}

if [ "${BACKED_UP}" = true ]
then
    echo "Correcting permissions on ${MOUNT_POINT}"
    PERMISSIONS=$(stat -c "%a" ${BACKUP_DIR})
    OWNER=$(stat -c "%u" ${BACKUP_DIR})
    GROUP=$(stat -c "%g" ${BACKUP_DIR})
    sudo chown ${OWNER}:${GROUP} ${MOUNT_POINT}
    sudo chmod ${PERMISSIONS} ${MOUNT_POINT}
fi

echo "Updating /etc/fstab"
UUID=$(sudo blkid -s UUID -o value ${PARTITION})
echo "UUID=${UUID} ${MOUNT_POINT} ${FS_TYPE} ${FSTAB_OPTIONS}" | sudo tee -a /etc/fstab

echo "Mounting the new partition at ${MOUNT_POINT}"
sudo mount ${MOUNT_OPTIONS} ${PARTITION} ${MOUNT_POINT}

echo "Verifying the mount"
sudo ls ${MOUNT_POINT}

echo "Removing the backup directory"
sudo rm -rf ${BACKUP_DIR}

echo "Done! The new partition is now mounted at ${MOUNT_POINT}."
