#!/bin/bash

#############################################################
# script description:
# filesystem(-f):
# | -- btrfs -- | -- ext4 -- |
# volumeGroup(-v):
# | -- vg0 -- | -- vg1 -- |
# sizeG(-s): unit:g
# | -- 1000g -- | -- 2000g -- |
# auth: jalin
# time: 2019-05-01
# copyright: terra-master tec
#############################################################
source /etc/tos/scripts/scripts

script_get_lock > /dev/null
[ $? -ne 0 ] && exit 1

#########################
# The command line help #
#########################
display_help() {
    echo "Usage:`basename $0` -f[filesystem] -s[sizeG] -v[volumeGroup] -u[uuid]"
    echo "\tuuid: use uuidgen -t -r to generate."
    echo "\tsizeG: unit:GB or percent"
    script_put_lock > /dev/null
    exit 1
}

# get parameter list
TOS_FILESYSTEM="btrfs"
TOS_VOLUMEGROUP=""
TOS_SIZEG="100%"
# create new uuid
TOS_UUID=`uuidgen -t -r`
TOS_LABEL="TOS_VOL_$(date +'%Y%m%d')"
while getopts "f:v:s:u:" opt
do
    case $opt in
        f) TOS_FILESYSTEM=$OPTARG ;; # get filesystem
        v) TOS_VOLUMEGROUP=$OPTARG ;; # get volumeGroup
        s) TOS_SIZEG=$OPTARG ;; # get sizeG (unit:g)
        u) TOS_UUID=$OPTARG ;; # get uuid
    esac
done

if [ -z "$TOS_VOLUMEGROUP" -o -z "$TOS_UUID" ]; then
    display_help
fi

TOS_VOLUME_ROOT=/tmp/volume
[ ! -e $TOS_VOLUME_ROOT ] && mkdir $TOS_VOLUME_ROOT

# read vg_free (unit:g)...
VG_FREE=`vgs -o vg_free --units g $TOS_VOLUMEGROUP $TOS_LVM_OPTS 2>/dev/null|sed s/[[:space:]]//g`
if [ $? -ne 0 ]; then # storage pool isn't exists...
    echo -e $usage
    script_put_lock > /dev/null
    exit 1
fi

TOS_MNT_PATH=/Volume$(iniparse -c -f /etc/volume/volume.conf -sort sort)
if [ "$VG_FREE" = "0g" ]; then
    TOS_LV_NAME=`vgs -o lv_name $TOS_VOLUMEGROUP $TOS_LVM_OPTS 2>/dev/null|sed s/[[:space:]]//g`
    TOS_BLK=/dev/mapper/$TOS_VOLUMEGROUP-$TOS_LV_NAME
    TOS_KEY=${TOS_LV_NAME:2}
else
    TOS_KEY=0
    while [ 1 ]; do
        TOS_LV_NAME=lv${TOS_KEY}
        check_name_exists $TOS_KEY
        [ $? -eq 0 ] && break
        let TOS_KEY=TOS_KEY+1
    done
    TOS_BLK=/dev/mapper/$TOS_VOLUMEGROUP-$TOS_LV_NAME
fi
# get debug
debugFile="$TOS_VOLUME_ROOT/${TOS_LV_NAME}"
process=0
df-json | grep "$TOS_BLK" >/dev/null
if [ $? -eq 0 ]; then
    echo "makeLVM:${TOS_BLK}:${TOS_MNT_PATH}:$TOS_UUID:1:lvm used" > $debugFile
    script_put_lock > /dev/null
    exit 1
fi
echo "makeLVM:${TOS_BLK}:${TOS_MNT_PATH}:$TOS_UUID:0:${process}" > $debugFile
sleep 1
if [ "$TOS_SIZEG" = "100%" ]; then
    echo -e "y\n" | lvcreate -l 100%FREE -n $TOS_LV_NAME $TOS_VOLUMEGROUP >/dev/null
else
    if [ "$TOS_SIZEG" -gt 0 ] 2>/dev/null; then
        TOS_SIZEG="${TOS_SIZEG}g"
    fi
    /etc/tos/scripts/compareSpace ${VG_FREE:0:0-1} ${TOS_SIZEG:0:0-1}
    if [ $? -eq 0 ]; then
        lvcreate -y -L $TOS_SIZEG -n $TOS_LV_NAME $TOS_VOLUMEGROUP >/dev/null
    else
        lvcreate -y -l 100%FREE -n $TOS_LV_NAME $TOS_VOLUMEGROUP >/dev/null
    fi
fi
# create lvm success....sleep 1
sleep 1
process=60
echo "makeLVM:${TOS_BLK}:${TOS_MNT_PATH}:$TOS_UUID:0:${process}" > $debugFile

formatDevice(){
	local fs=$1
	local blk=$2
	if [ "$fs" = "btrfs" ]; then
	    mkfs.btrfs -f -L "$TOS_LABEL" -U "$TOS_UUID" $blk
		mount -t btrfs -o $MNTOPT_BTRFS $blk $TOS_MNT_PATH
        if [ $? -eq 0 ]; then
            iniparse -a -f /etc/volume/volume.conf -s "$TOS_UUID" -system
            /etc/init.d/nas/mkbase
        fi
	elif [ "$fs" = "xfs" ]; then
		mkfs.xfs -f -b size=4096 -L "xfs_$(date +'%Y%m%d')" -m uuid="$TOS_UUID" $blk
		mount -t xfs -o $MNTOPT_XFS $blk $TOS_MNT_PATH
		if [ $? -eq 0 ]; then
			iniparse -a -f /etc/volume/volume.conf -s "$TOS_UUID" -system
			quotacheck -uvgmf $TOS_MNT_PATH
			quotaon -a
			/etc/init.d/nas/mkbase
		fi
	else
		echo -e "y\n" | mke2fs -t ext4 -m .5 -b 4096 -L "$TOS_LABEL" -U "$TOS_UUID" $blk
		wait $!
		mount -t ext4 -o $MNTOPT_EXT4 $blk $TOS_MNT_PATH
		if [ $? -eq 0 ]; then
			iniparse -a -f /etc/volume/volume.conf -s "$TOS_UUID" -system
			quotacheck -uvgmf $TOS_MNT_PATH
			quotaon -a
			/etc/init.d/nas/mkbase
		fi
	fi
}

if [ -b $TOS_BLK ]; then
    [ ! -e $TOS_MNT_PATH ] && mkdir -m 777 -p $TOS_MNT_PATH
    process=80
    echo "makeLVM:${TOS_BLK}:${TOS_MNT_PATH}:$TOS_UUID:0:${process}" > $debugFile
    # format block device...
    formatDevice "$TOS_FILESYSTEM" "$TOS_BLK"

    df-json | grep "$TOS_MNT_PATH$" > /dev/null
    # mount successful to init /usr/local/...
    if [ $? -eq 0 ]; then
        process=100
        echo "makeLVM:${TOS_BLK}:${TOS_MNT_PATH}:$TOS_UUID:0:${process}" > $debugFile
    else
        echo "makeLVM:${TOS_BLK}:${TOS_MNT_PATH}:$TOS_UUID:1:mount failed" > $debugFile
        script_put_lock > /dev/null
        exit 1
    fi
fi
sleep 1
# all process over
script_put_lock > /dev/null
exit 0
