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.
6 """Installs deps for using SDK emulator for testing.
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.
18 from pylib import cmd_helper
19 from pylib import constants
20 from pylib.utils import run_tests_helper
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-20130729.zip'
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-18_r01.zip'
31 API_TARGET = 'android-%s' % constants.ANDROID_SDK_VERSION
35 """Check if SDK is already installed.
38 True if the emulator SDK directory (src/android_emulator_sdk/) exists.
40 return os.path.exists(constants.EMULATOR_SDK_ROOT)
44 """Check if Android system images have been installed.
47 True if sdk/system-images/<API TARGET>/x86 exists inside EMULATOR_SDK_ROOT.
49 return os.path.exists(os.path.join(constants.EMULATOR_SDK_ROOT,
50 'sdk', 'system-images',
55 """Check if KVM is enabled.
58 True if kvm-ok returns 0 (already enabled)
61 return not cmd_helper.RunCmd(['kvm-ok'])
63 logging.info('kvm-ok not installed')
68 """Download the SDK and unzip it into EMULATOR_SDK_ROOT."""
69 logging.info('Download Android SDK.')
70 sdk_url = '%s/%s' % (SDK_BASE_URL, SDK_ZIP)
72 cmd_helper.RunCmd(['curl', '-o', '/tmp/sdk.zip', sdk_url])
73 print 'curled unzipping...'
74 rc = cmd_helper.RunCmd(['unzip', '-o', '/tmp/sdk.zip', '-d', '/tmp/'])
76 raise Exception('ERROR: could not download/unzip Android SDK.')
77 # Get the name of the sub-directory that everything will be extracted to.
78 dirname, _ = os.path.splitext(SDK_ZIP)
79 zip_dir = '/tmp/%s' % dirname
80 # Move the extracted directory to EMULATOR_SDK_ROOT
81 shutil.move(zip_dir, constants.EMULATOR_SDK_ROOT)
83 os.unlink('/tmp/sdk.zip')
87 """Installs KVM packages."""
88 rc = cmd_helper.RunCmd(['sudo', 'apt-get', 'install', 'kvm'])
90 logging.critical('ERROR: Did not install KVM. Make sure hardware '
91 'virtualization is enabled in BIOS (i.e. Intel VT-x or '
93 # TODO(navabi): Use modprobe kvm-amd on AMD processors.
94 rc = cmd_helper.RunCmd(['sudo', 'modprobe', 'kvm-intel'])
96 logging.critical('ERROR: Did not add KVM module to Linux Kernal. Make sure '
97 'hardware virtualization is enabled in BIOS.')
98 # Now check to ensure KVM acceleration can be used.
99 rc = cmd_helper.RunCmd(['kvm-ok'])
101 logging.critical('ERROR: Can not use KVM acceleration. Make sure hardware '
102 'virtualization is enabled in BIOS (i.e. Intel VT-x or '
107 """Download x86 system image from Intel's website."""
108 logging.info('Download x86 system image directory into sdk directory.')
110 cmd_helper.RunCmd(['curl', '-o', '/tmp/x86_img.zip', X86_IMG_URL])
111 rc = cmd_helper.RunCmd(['unzip', '-o', '/tmp/x86_img.zip', '-d', '/tmp/'])
113 raise Exception('ERROR: Could not download/unzip image zip.')
114 sys_imgs = os.path.join(constants.EMULATOR_SDK_ROOT, 'sdk',
115 'system-images', API_TARGET, 'x86')
116 shutil.move('/tmp/x86', sys_imgs)
118 os.unlink('/tmp/x86_img.zip')
122 logging.basicConfig(level=logging.INFO,
123 format='# %(asctime)-15s: %(message)s')
124 run_tests_helper.SetLogLevel(verbose_count=1)
126 # Calls below will download emulator SDK and/or system images only if needed.
128 logging.info('android_emulator_sdk/ already exists, skipping download.')
132 # Download the x86 system image only if needed.
134 logging.info('The x86 image is already present, skipping download.')
138 # Make sure KVM packages are installed and enabled.
140 logging.info('KVM already installed and enabled.')
145 if __name__ == '__main__':
146 sys.exit(main(sys.argv))