Index: trunk/packages/invirt-remote/debian/changelog
===================================================================
--- trunk/packages/invirt-remote/debian/changelog	(revision 2114)
+++ trunk/packages/invirt-remote/debian/changelog	(revision 2132)
@@ -2,8 +2,10 @@
 
   * modified host/usr/sbin/invirt-availability and invirt-vmcontrol to stat
-    /etc/invirt/nocreate; if it exists, they advertise zero free memory and 
+    /etc/invirt/nocreate; if it exists, they advertise zero free memory and
     refuse to create VMs
-
- -- Peter A. Iannucci <iannucci@mit.edu>  Sat, 14 Feb 2009 18:10:54 -0500
+  * added memory quota validation to invirt-remote-create
+  * added owner table to database with ram_quota_total and ram_quota_single
+
+ -- Peter A. Iannucci <iannucci@mit.edu>  Mon, 16 Feb 2009 23:49:14 -0500
 
 invirt-remote (0.3.3) unstable; urgency=low
Index: trunk/packages/invirt-remote/server/usr/sbin/invirt-remote-create
===================================================================
--- trunk/packages/invirt-remote/server/usr/sbin/invirt-remote-create	(revision 2114)
+++ trunk/packages/invirt-remote/server/usr/sbin/invirt-remote-create	(revision 2132)
@@ -13,4 +13,16 @@
 import sys
 import yaml
+import invirt.database
+
+def maxMemory(owner, xmlist):
+    """
+    Return the memory available for a new machine.
+    """
+    machines = invirt.database.Machine.query().filter_by(owner=owner)
+    (quota_total, quota_single) = invirt.database.Owner.getQuotas(owner)
+
+    active_machines = [m for m in machines if m.name in xmlist]
+    mem_usage = sum([x.memory for x in active_machines])
+    return min(quota_single, quota_total-mem_usage)
 
 def choose_host():
@@ -50,4 +62,16 @@
         return 1
 
+    if operation == "create":
+        invirt.database.connect()
+        machine = invirt.database.Machine.query().filter_by(name=machine_name).first()
+
+        owner = machine.owner
+        vm_memory = machine.memory
+
+        max_memory = maxMemory(owner, vms.keys())
+        if vm_memory > max_memory:
+            print >>sys.stderr, "owner %s requested %d MB of memory for vm %s; %d MB allowed" % (owner, vm_memory, machine_name, max_memory)
+            return 1
+
     host = choose_host()
     print 'Creating on host %s...' % host
