ifdown: Introduce helper methods
authorDimitris Aragiorgis <dimara@grnet.gr>
Tue, 4 Feb 2014 12:21:34 +0000 (14:21 +0200)
committerDimitris Aragiorgis <dimara@grnet.gr>
Thu, 27 Mar 2014 08:01:13 +0000 (10:01 +0200)
Until now Ganeti upon TAP creation, a NIC file was created
under $RUNDIR/kvm-hypervisor/nic/<instance_name>/<nic_index>

Since NICs have obtained a UUID, we are going to create another
file named after its UUID. This will be needed during ifdown
script in order to retrieve the tap name related to the
corresponding NIC. Index of a NIC may change but UUID will not;
When a NIC is getting hot-plugged may be the third one from
the master perspective but after removing the second, its index
will change.

In the following patches we are going to refer to NIC files
named after NIC's uuid. Here we add helper methods that
add/remove those kind of files. For retrieving NIC's tap name
we search *only* for a NIC file named after its UUID. If not
found (e.g. in case of already existing instances) we fail
silently. External ifdown script must deal with this corner case.

To keep compatibility we still create NIC files based on their
*current* index.

Signed-off-by: Dimitris Aragiorgis <dimara@grnet.gr>

lib/hypervisor/hv_kvm.py

index f5dc5ce..b1e5540 100644 (file)
@@ -945,11 +945,40 @@ class KVMHypervisor(hv_base.BaseHypervisor):
     return utils.PathJoin(cls._NICS_DIR, instance_name)
 
   @classmethod
-  def _InstanceNICFile(cls, instance_name, seq):
+  def _InstanceNICFile(cls, instance_name, seq_or_uuid):
     """Returns the name of the file containing the tap device for a given NIC
 
     """
-    return utils.PathJoin(cls._InstanceNICDir(instance_name), str(seq))
+    return utils.PathJoin(cls._InstanceNICDir(instance_name), str(seq_or_uuid))
+
+  @classmethod
+  def _GetInstanceNICTap(cls, instance_name, nic):
+    """Returns the tap for the corresponding nic
+
+    Search for tap file named after NIC's uuid.
+    For old instances without uuid indexed tap files returns nothing.
+
+    """
+    try:
+      return utils.ReadFile(cls._InstanceNICFile(instance_name, nic.uuid))
+    except EnvironmentError:
+      pass
+
+  @classmethod
+  def _WriteInstanceNICFiles(cls, instance_name, seq, nic, tap):
+    """Write tap name to both instance NIC files
+
+    """
+    for ident in [seq, nic.uuid]:
+      utils.WriteFile(cls._InstanceNICFile(instance_name, ident), data=tap)
+
+  @classmethod
+  def _RemoveInstanceNICFiles(cls, instance_name, seq, nic):
+    """Write tap name to both instance NIC files
+
+    """
+    for ident in [seq, nic.uuid]:
+      utils.RemoveFile(cls._InstanceNICFile(instance_name, ident))
 
   @classmethod
   def _InstanceKeymapFile(cls, instance_name):