1 #!/usr/bin/env python 2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. 3 # Use of this source code is governed by a BSD-style license that can be 4 # found in the LICENSE file. 5 6 """Installs deps for using SDK emulator for testing. 7 8 The script will download the SDK and system images, if they are not present, and 9 install and enable KVM, if virtualization has been enabled in the BIOS. 10 """ 11 12 13 import logging 14 import os 15 import shutil 16 import sys 17 18 from pylib import cmd_helper 19 from pylib import constants 20 from pylib.utils import run_tests_helper 21 22 # From the Android Developer's website. 23 SDK_BASE_URL = 'http://dl.google.com/android/adt' 24 SDK_ZIP = 'adt-bundle-linux-x86_64-20130522.zip' 25 26 # Android x86 system image from the Intel website: 27 # http://software.intel.com/en-us/articles/intel-eula-x86-android-4-2-jelly-bean-bin 28 X86_IMG_URL = 'http://download-software.intel.com/sites/landingpage/android/sysimg_x86-17_r01.zip' 29 30 # Android API level 31 API_TARGET = 'android-%s' % constants.ANDROID_SDK_VERSION 32 33 34 def CheckSDK(): 35 """Check if SDK is already installed. 36 37 Returns: 38 True if android_tools directory exists in current directory. 39 """ 40 return os.path.exists(os.path.join(constants.EMULATOR_SDK_ROOT, 41 'android_tools')) 42 43 44 def CheckX86Image(): 45 """Check if Android system images have been installed. 46 47 Returns: 48 True if android_tools/sdk/system-images directory exists. 49 """ 50 return os.path.exists(os.path.join(constants.EMULATOR_SDK_ROOT, 51 'android_tools', 'sdk', 'system-images', 52 API_TARGET, 'x86')) 53 54 55 def CheckKVM(): 56 """Check if KVM is enabled. 57 58 Returns: 59 True if kvm-ok returns 0 (already enabled) 60 """ 61 try: 62 return not cmd_helper.RunCmd(['kvm-ok']) 63 except OSError: 64 logging.info('kvm-ok not installed') 65 return False 66 67 68 def GetSDK(): 69 """Download the SDK and unzip in android_tools directory.""" 70 logging.info('Download Android SDK.') 71 sdk_url = '%s/%s' % (SDK_BASE_URL, SDK_ZIP) 72 try: 73 cmd_helper.RunCmd(['curl', '-o', '/tmp/sdk.zip', sdk_url]) 74 print 'curled unzipping...' 75 rc = cmd_helper.RunCmd(['unzip', '-o', '/tmp/sdk.zip', '-d', '/tmp/']) 76 if rc: 77 logging.critical('ERROR: could not download/unzip Android SDK.') 78 raise 79 # Get the name of the sub-directory that everything will be extracted to. 80 dirname, _ = os.path.splitext(SDK_ZIP) 81 zip_dir = '/tmp/%s' % dirname 82 # Move the extracted directory to EMULATOR_SDK_ROOT 83 dst = os.path.join(constants.EMULATOR_SDK_ROOT, 'android_tools') 84 shutil.move(zip_dir, dst) 85 finally: 86 os.unlink('/tmp/sdk.zip') 87 88 89 def InstallKVM(): 90 """Installs KVM packages.""" 91 rc = cmd_helper.RunCmd(['sudo', 'apt-get', 'install', 'kvm']) 92 if rc: 93 logging.critical('ERROR: Did not install KVM. Make sure hardware ' 94 'virtualization is enabled in BIOS (i.e. Intel VT-x or ' 95 'AMD SVM).') 96 # TODO(navabi): Use modprobe kvm-amd on AMD processors. 97 rc = cmd_helper.RunCmd(['sudo', 'modprobe', 'kvm-intel']) 98 if rc: 99 logging.critical('ERROR: Did not add KVM module to Linux Kernal. Make sure ' 100 'hardware virtualization is enabled in BIOS.') 101 # Now check to ensure KVM acceleration can be used. 102 rc = cmd_helper.RunCmd(['kvm-ok']) 103 if rc: 104 logging.critical('ERROR: Can not use KVM acceleration. Make sure hardware ' 105 'virtualization is enabled in BIOS (i.e. Intel VT-x or ' 106 'AMD SVM).') 107 108 109 def GetX86Image(): 110 """Download x86 system image from Intel's website.""" 111 logging.info('Download x86 system image directory into sdk directory.') 112 try: 113 cmd_helper.RunCmd(['curl', '-o', '/tmp/x86_img.zip', X86_IMG_URL]) 114 rc = cmd_helper.RunCmd(['unzip', '-o', '/tmp/x86_img.zip', '-d', '/tmp/']) 115 if rc: 116 logging.critical('ERROR: Could not download/unzip image zip.') 117 raise 118 sys_imgs = os.path.join(constants.EMULATOR_SDK_ROOT, 'android_tools', 'sdk', 119 'system-images', API_TARGET, 'x86') 120 shutil.move('/tmp/x86', sys_imgs) 121 finally: 122 os.unlink('/tmp/x86_img.zip') 123 124 125 def main(argv): 126 logging.basicConfig(level=logging.INFO, 127 format='# %(asctime)-15s: %(message)s') 128 run_tests_helper.SetLogLevel(verbose_count=1) 129 130 # Calls below will download emulator SDK and/or system images only if needed. 131 if CheckSDK(): 132 logging.info('android_tools directory already exists (not downloading).') 133 else: 134 GetSDK() 135 136 logging.info('Emulator deps for ARM emulator complete.') 137 138 if CheckX86Image(): 139 logging.info('system-images directory already exists.') 140 else: 141 GetX86Image() 142 143 # Make sure KVM packages are installed and enabled. 144 if CheckKVM(): 145 logging.info('KVM already installed and enabled.') 146 else: 147 InstallKVM() 148 149 150 if __name__ == '__main__': 151 sys.exit(main(sys.argv)) 152