Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 13 Mar 2013 19:08:24 -0400
From:      =?iso-8859-1?B?QnJ1bm8gTGF1euk=?= <brunolauze@msn.com>
To:        "freebsd-virtualization@freebsd.org" <freebsd-virtualization@freebsd.org>
Subject:   bhyve accouting propostion
Message-ID:  <BLU002-W2318442AE6720A9DF5B2468C6E30@phx.gbl>

next in thread | raw e-mail | index | archive | help
Bhyve VM Accouting propostion

Since freebsd accouting is done at process or user level=2C you can go at u=
ser level route starting each vm under a separate user but this looks awkar=
d...

So now=2C looking at the process level=2C the record command doesn't includ=
e anyway to filter by arguments.
Accounting should be done in bhyve copying /usr/sbin/bhyve to a unique vm n=
ame like /usr/local/vmm/bhyvevm01 then the startup script=2C example vm01.s=
h
would fire byveload normally but use bhyvevm01 newly copied to launch vm. A=
lso 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 "$@"=3B then
if test -f $@=3B then
LASTDATE=3D`ls -l -tU -D %s /var/account/acct | awk '{print $6}'`
DIFFD=3D0
RES=3D0
PID=3D`pgrep $@`
if test "$PID"=3B then
RES=3D`ps $PID -xo etime | tail -n 1 | awk -F: '{ print ($1 * 60) + ($2) ".=
" $3 }'`
STARTPS=3D`cat /proc/$PID/status | awk '{print $8}' | sed s/=2C/./g`
DIFFD=3D`echo "scale=3D0=3B($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=3B then
echo "The process was running before last billing=3B Removing the charged a=
mount"
RES=3D`echo "scale=3D2=3B$RES - $DIFFD" | bc -l`
fi
else
RES=3D0
fi
RESAC=3D`sa -a | grep $@ | awk '{print $2}' | sed 's/re//g'`
if ! test "$RESAC"=3B then
RESAC=3D"0.00"
fi
echo "Uncharged accounting minutes: $RESAC"
RESULT=3D`echo "scale=3D3=3B$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 thi=
s 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=2C w=
hich would mean the last billing.

So the final logic is for a billing workflow would be to execute vmuptime.s=
h <vmname> for all vms in a bhyve storage and generate an invoice for virtu=
al machines associated  to client=2C finally execute /etc/rc.d/accouting ro=
tate_log
Whenever you will rotate the log the vmuptime.sh will report it from this p=
oint in time=2C regardless of if the vm is running or not.
Of course the more often this action is done and save to a database the mor=
e 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 cli=
ent... But it's freebsd=2C it doesn't crash=2C 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=2C

Bruno Lauze
 		 	   		  =



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?BLU002-W2318442AE6720A9DF5B2468C6E30>