#!/bin/bash -x
# 将存储池做成gfs2

# env:
#   CLUNAME
#   DISK
#   MOUNTPOINT

if [ -z "$SSH" ];then
    source /unas/sbin/dualcontroller/config/prepare
fi

if [ -z "$PCS" ];then
    PCS=pcs
fi

gfs_id=0
gfs_name=gfs$gfs_id

while $PCS resource config $gfs_name;do
    ((gfs_id++))
    gfs_name=gfs$gfs_id
done

# 先在全部节点上停用
/unas/sbin/dualcontroller/tool/nodes "partprobe; umount -f '$DISK'; lvchange -an '$DISK'; mkdir -p $MOUNTPOINT ; sed -i '\# $MOUNTPOINT #d' /etc/fstab || :"

# 只在本节点上启用，避免冲突
lvchange -ay "$DISK"
if [ `blkid --output value --match-tag TYPE "$DISK"`n == gfs2n ];then
    tunegfs2 -o locktable=$CLUNAME:$gfs_name "$DISK"
else
    mkfs.gfs2 -p lock_dlm -t $CLUNAME:$gfs_name -j 2 -O "$DISK"
fi

if lvs -a --noheadings -o lv_tags "$DISK";then
    IFS_OLD="$IFS"
    IFS="$IFS,"
    for tag in `lvs -a --noheadings -o lv_tags "$DISK"`;do
        lvchange "$DISK" --deltag $tag
    done
    IFS="$IFS_OLD"
    lvchange "$DISK" --addtag gfs2
fi

# 在各个节点上启用
/unas/sbin/dualcontroller/tool/nodes "lvchange -ay '$DISK'"

$PCS resource create $gfs_name ocf:heartbeat:Filesystem device="$DISK" directory="$MOUNTPOINT" fstype="gfs2" options="acl,quota=on" \
    op monitor interval=10s \
    op start interval=0s timeout=120s on-fail=restart
$PCS resource clone $gfs_name meta clone-max=2 clone-node-max=1 notify=true

$PCS constraint colocation add $gfs_name-clone with dlm-clone
$PCS constraint order dlm-clone then $gfs_name-clone kind=Optional

