Add MAC addrees into mic-appliance.
authorZhuoX Li <zhuox.li@intel.com>
Fri, 17 Oct 2014 01:49:31 +0000 (09:49 +0800)
committerLin Yang <lin.a.yang@intel.com>
Tue, 21 Oct 2014 02:07:10 +0000 (10:07 +0800)
When create imager, Mic-appliance is started without specified MAC address.
As the result, all mic-appliance instances that run in same network,
get also same default MAC address.
This potentially leads to collisions and conflicts in addressing
mic-appliance VM sessions.
So need to add unique MAC addresses, when run mic-appliance.

Fixes: #2132

Change-Id: I870798cabb8f6043a4a0182619b8e49c04fdba59

job_imager.py

index 65265d0..738f89d 100755 (executable)
@@ -36,17 +36,40 @@ def convert_image_dir(image_dir, repo_name, image_name):
             return
         shutil.move(image_name, repo_name)
 
+def octet_type(num):
+    """
+    change int data to the same otctet type
+    """
+    return '%02x' % int(num)
+
+def create_mac():
+    """
+    create unique MAC address from static part=52,
+    node IP, slot number, process number
+    """
+    ssh_connection = os.getenv('SSH_CONNECTION')
+    exe_number = os.getenv('EXECUTOR_NUMBER', '1')
+    pid = os.getpid()
+    nodenum = octet_type(ssh_connection.split(' ')[2].split('.')[3])
+    slotnum = octet_type(int(exe_number)%256)
+    p_1 = pid/65536
+    p_2 = pid/256
+    p_3 = pid - p_2*256
+    return '52:%s:%s:%s:%s:%s' % \
+           (nodenum, slotnum, octet_type(p_1), octet_type(p_2), octet_type(p_3))
+
 def run_inside_vm(vm_image, vm_memory, vm_cpus, basedir):
     """
     Run mic inside VM
     basedir(usually $WORKSPACE/mic) is shared by VM as Plan9 mount
     for cache and results
     """
-    cmd = 'qemu-system-x86_64 -machine accel=kvm:xen:tcg -name opensuse '\
-          '-M pc -m %d -smp %d -vga none -drive file=%s,snapshot=on '\
-          '-nographic -virtfs local,id=test_dev,path=%s, '\
-          'security_model=mapped,mount_tag=share' % \
-          (vm_memory, vm_cpus, vm_image, basedir)
+    cmd = 'qemu-system-x86_64 -machine accel=kvm:xen:tcg -name '\
+          'opensuse -M pc -m %d -smp %d -vga none -drive file=%s,'\
+          'snapshot=on -nographic -virtfs local,id=test_dev,'\
+          'path=%s,security_model=mapped,mount_tag=share '\
+          '-net nic,macaddr=%s -net user' % \
+          (vm_memory, vm_cpus, vm_image, basedir, create_mac())
 
     subprocess.call(cmd, stdout=sys.stdout,
                     stderr=sys.stderr, shell=True)