#!/bin/bash
#│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
#
# Copyright (c) 2024 Oracle and/or its affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at
# https://oss.oracle.com/licenses/upl.
#
# Since: August, 2024
# Author: ruggero.citton@oracle.com
# Description: 02_setup_storage_container.sh
#
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
#
#│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│

echo "-----------------------------------------------------------------"
echo -e "${INFO}`date +%F' '%T`: Setting-up '/var/lib/containers'"
echo "-----------------------------------------------------------------"
# Single GPT partition for the whole disk
BOX_DISK_NUM=$1
PROVIDER=$2

if [ "${PROVIDER}" == "libvirt" ]; then
  DEVICE="vd"
elif [ "${PROVIDER}" == "virtualbox" ]; then
  DEVICE="sd"
else
  echo "Not supported provider: ${PROVIDER}"
  exit 1
fi

LETTER=`tr 0123456789 abcdefghij <<< $BOX_DISK_NUM`
parted -s -a optimal /dev/${DEVICE}${LETTER} mklabel gpt -- mkpart primary 2048s 100%

# LVM setup
pvcreate /dev/${DEVICE}${LETTER}1
vgcreate VolGroupSC /dev/${DEVICE}${LETTER}1
lvcreate -l 100%FREE -n LogVolSC VolGroupSC

# Make XFS
mkfs.xfs -f /dev/VolGroupSC/LogVolSC

# Set fstab
UUID=`blkid -s UUID -o value /dev/VolGroupSC/LogVolSC`
mkdir -p /var/lib/containers
cat >> /etc/fstab <<EOF
UUID=${UUID}  /var/lib/containers    xfs    defaults 1 2
EOF

# Mount
mount /var/lib/containers
#----------------------------------------------------------
# EndOfFile
#----------------------------------------------------------
