Bhyve VM Accouting propostion

Since freebsd accouting is done at process or user level, you can go at user 
level route starting each vm under a separate user but this looks awkard...

So now, looking at the process level, the record command doesn't include anyway 
to filter by arguments.
Accounting should be done in bhyve copying /usr/sbin/bhyve to a unique vm name 
like /usr/local/vmm/bhyvevm01 then the startup script, example vm01.sh
would fire byveload normally but use bhyvevm01 newly copied to launch vm. Also 
vm01.sh should automate copy of bhyve to bhyvevm01  in that same script in case 
world is reinstalled.

Then vmuptime.sh :

#### SCRIPT END ######

#!/bin/sh
if test "$@"; then
if test -f $@; then
LASTDATE=`ls -l -tU -D %s /var/account/acct | awk '{print $6}'`
DIFFD=0
RES=0
PID=`pgrep $@`
if test "$PID"; then
RES=`ps $PID -xo etime | tail -n 1 | awk -F: '{ print ($1 * 60) + ($2) "." $3 
}'`
STARTPS=`cat /proc/$PID/status | awk '{print $8}' | sed s/,/./g`
DIFFD=`echo "scale=0;($LASTDATE - $STARTPS) / 60" | bc -l`
echo "The process was running since $DIFFD minutes before last billing"
echo "The process is running since $RES"
if test $DIFFD -gt 0; then
echo "The process was running before last billing; Removing the charged amount"
RES=`echo "scale=2;$RES - $DIFFD" | bc -l`
fi
else
RES=0
fi
RESAC=`sa -a | grep $@ | awk '{print $2}' | sed 's/re//g'`
if ! test "$RESAC"; then
RESAC="0.00"
fi
echo "Uncharged accounting minutes: $RESAC"
RESULT=`echo "scale=3;$RES + $RESAC" | bc -l`
echo $RESULT
else
echo "Virtual Machine does not exist."
fi
else
echo "Usage: vmuptime.sh <vmname>"
fi

#### SCRIPT END ######

This will compute :

accouting time in /var/account/acct for bhyvevm01
since acct file is updated only when a vm would shutdown or reboot then this 
would cumulate usage for bhyvevm01 process which only runs for our vm01

Then it looks if this vm is running and compute time elapsed.
It then removes the time elapsed since the last accouting log rotation, which 
would mean the last billing.

So the final logic is for a billing workflow would be to execute vmuptime.sh 
<vmname> for all vms in a bhyve storage and generate an invoice for virtual 
machines associated  to client, finally execute /etc/rc.d/accouting rotate_log
Whenever you will rotate the log the vmuptime.sh will report it from this point 
in time, regardless of if the vm is running or not.
Of course the more often this action is done and save to a database the more 
time can be billed before a host would  crash or fails.
And any non billed time in the event of a crash should be gifted to the 
client... But it's freebsd, it doesn't crash, right?

The down side of this solution is the requirement to copy the bhyve binary for 
each specfic virtual machine subjected to accounting purposes.
But it's 264k right now...


Please share if anyone got an opinion on this,

Bruno Lauze
                                          
_______________________________________________
freebsd-virtualization@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-virtualization
To unsubscribe, send any mail to 
"freebsd-virtualization-unsubscr...@freebsd.org"

Reply via email to