Implemented running mic under VM
authorEd Bartosh <eduard.bartosh@intel.com>
Wed, 4 Sep 2013 17:39:50 +0000 (20:39 +0300)
committerHasan Wan <hasan.wan@intel.com>
Wed, 11 Sep 2013 02:11:02 +0000 (19:11 -0700)
If configuration variable USE_VM is set to 1 in jobs/configuration
job_imager will try to run qemu with -hda <mic seed image> and
accelerator choices: kvm:xen:tcg. It means that qemu will first try to
run image with kvm and then with xen if kvm is not found.

There are number of optional VM related parameters that can be set in
jobs/configuration: VM_IMAGE($JENKINS_HOME/mic-seed.tar by default),
VM_MEMORY(8Mb by default) and VM_CPUS(8 cpus by default).

mic appliance implemented in mic-seed expects configuration file in
mic/out directory. It will automatically run mic and put image and other
build results also into mic/out.

Fixes: #1146

Change-Id: I3a1fd07306372912a1909fd4774d06f5e8523f97
Signed-off-by: Ed Bartosh <eduard.bartosh@intel.com>
Reviewed-on: https://otctools.jf.intel.com/review/6306
Tested-by: OTC Tools Tester <ed.bartosh@linux.intel.com>
Reviewed-by: Hasan Wan <hasan.wan@intel.com>
job_imager.py

index b689078..0c5cc20 100755 (executable)
@@ -20,6 +20,30 @@ def get_xml(image_dir, ks_name):
 
     return ''
 
+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 -hda %s -nographic -virtfs '\
+          'local,id=test_dev,path=%s,security_model=mapped,mount_tag=share' % \
+          (vm_memory, vm_cpus, vm_image, basedir)
+
+    subprocess.call(cmd, stdout=sys.stdout,
+                    stderr=sys.stderr, shell=True)
+    #read mic status from file
+    try:
+        with open(os.path.join(basedir, 'status')) as statusf:
+            status = statusf.read().strip()
+        if status == 'success':
+            return 0
+        else:
+            return -1
+    except IOError, _err:
+        return -1
+
 def main():
     """The main body"""
 
@@ -41,26 +65,50 @@ def main():
     name = fields['name']
 
     # Prepare working directory
-    outdir = os.path.join(os.getenv('WORKSPACE'), 'mic')
-    log = os.path.join(outdir, '%s_%s.log.txt' % (build_id, name))
-    cache = os.path.join(outdir, 'cache')
-    if not os.path.exists(cache):
-        os.makedirs(cache)
+    basedir = os.path.join(os.getenv('WORKSPACE'), 'mic')
+    outdir = os.path.join(basedir, 'out')
+    if not os.path.exists(outdir):
+        os.makedirs(outdir)
 
     ksf = os.path.join(outdir, '%s.ks' % name)
     with open(ksf, 'w') as ks_fh:
         ks_fh.write(fields["kickstart"])
 
-    mic_cmd = "sudo /usr/bin/mic cr auto %s --release %s -o %s -k %s "\
-              '--logfile=%s' % (ksf, build_id, outdir, cache, log)
-
-    sys.stdout.flush()
-    print 'starting mic to create image: %s' % mic_cmd
-    ret = subprocess.call(mic_cmd,
-                          stdout=sys.stdout,
-                          stderr=sys.stderr,
-                          shell=True)
-
+    if int(os.getenv('USE_VM')):
+        vm_image = os.getenv("VM_IMAGE",
+                os.path.join(os.getenv('JENKINS_HOME'), 'mic-seed.tar'))
+        # check if tarball exists
+        if not vm_image or not os.access(vm_image, os.R_OK):
+            print 'VM image %s is not found' % vm_image
+            return -1
+        # untar it
+        ret = subprocess.call("tar -Sxf %s" % vm_image, stdout=sys.stdout,
+                              stderr=sys.stderr, shell=True)
+
+        # check if image exists
+        unpacked_image = os.path.basename(vm_image).split('.tar')[0]
+        if not os.access(unpacked_image, os.R_OK | os.W_OK):
+            print 'Not enough permissions to run VM image %s' % unpacked_image
+            return -1
+
+        print 'starting mic inside VM to create image'
+        ret = run_inside_vm(unpacked_image, os.getenv("VM_MEMORY", 8192),
+                            os.getenv("VM_CPUS", 8), basedir)
+    else:
+        log = os.path.join(outdir, '%s_%s.log.txt' % (build_id, name))
+        cache = os.path.join(basedir, 'cache')
+        if not os.path.exists(cache):
+            os.makedirs(cache)
+
+        mic_cmd = "sudo /usr/bin/mic cr auto %s --release %s -o %s -k %s "\
+                  '--logfile=%s' % (ksf, build_id, outdir, cache, log)
+
+        sys.stdout.flush()
+        print 'starting mic to create image: %s' % mic_cmd
+        ret = subprocess.call(mic_cmd,
+                              stdout=sys.stdout,
+                              stderr=sys.stderr,
+                              shell=True)
     status = 'success'
     if ret:
         print 'Error: mic returned %d' % ret