#!/bin/bash

#rbt
# Description in BREST-2936

if [ -z "${ONE_LOCATION}" ]; then
    LIB_LOCATION=/usr/lib/one
else
    LIB_LOCATION=$ONE_LOCATION/lib
fi

. $LIB_LOCATION/sh/scripts_common.sh

DRIVER_PATH=$(dirname $0)
source ${DRIVER_PATH}/../libfs.sh

# -------- Get stat and datastore arguments from OpenNebula core ------------

DRV_ACTION=$1
ID=$2

XPATH="${DRIVER_PATH}/../xpath.rb -b $DRV_ACTION"

unset i XPATH_ELEMENTS

while IFS= read -r -d '' element; do
    XPATH_ELEMENTS[i++]="$element"
done < <($XPATH /DS_DRIVER_ACTION_DATA/IMAGE/PATH)

unset i

SRC="${XPATH_ELEMENTS[i++]}"

# ------------------------------------------------------------------------------
#  Compute the size
# ------------------------------------------------------------------------------

VSIZE=`qemu-img info --output json $SRC | jq '."virtual-size"'`
VSIZEMB=$(( $VSIZE / 1024 / 1024))

if [ "$VSIZEMB" = "0" ]; then
    log_error "Cannot determine size for $SRC"
    exit -1
fi
echo "$VSIZEMB"
