Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame^] | 1 | #!/usr/bin/env python2 |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 2 | # |
| 3 | # Copyright 2011 Google Inc. All Rights Reserved. |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 4 | """Script to image a ChromeOS device. |
| 5 | |
| 6 | This script images a remote ChromeOS device with a specific image." |
| 7 | """ |
| 8 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 9 | from __future__ import print_function |
| 10 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 11 | __author__ = 'asharif@google.com (Ahmad Sharif)' |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 12 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 13 | import argparse |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 14 | import filecmp |
| 15 | import glob |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 16 | import os |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 17 | import re |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 18 | import shutil |
| 19 | import sys |
| 20 | import tempfile |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 21 | import time |
| 22 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 23 | from cros_utils import command_executer |
| 24 | from cros_utils import locks |
| 25 | from cros_utils import logger |
| 26 | from cros_utils import misc |
| 27 | from cros_utils.file_utils import FileUtils |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 28 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 29 | checksum_file = '/usr/local/osimage_checksum_file' |
| 30 | lock_file = '/tmp/image_chromeos_lock/image_chromeos_lock' |
| 31 | |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 32 | |
| 33 | def Usage(parser, message): |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 34 | print('ERROR: %s' % message) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 35 | parser.print_help() |
| 36 | sys.exit(0) |
| 37 | |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 38 | |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 39 | def CheckForCrosFlash(chromeos_root, remote, log_level): |
| 40 | cmd_executer = command_executer.GetCommandExecuter(log_level=log_level) |
cmtice | 0cc4e77 | 2014-01-30 15:52:37 -0800 | [diff] [blame] | 41 | |
Luis Lozano | 54db538 | 2015-05-20 15:57:19 -0700 | [diff] [blame] | 42 | # Check to see if remote machine has cherrypy, ctypes |
| 43 | command = "python -c 'import cherrypy, ctypes'" |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame^] | 44 | ret = cmd_executer.CrosRunCommand( |
| 45 | command, chromeos_root=chromeos_root, machine=remote) |
Han Shen | 96d936c | 2015-03-25 12:03:12 -0700 | [diff] [blame] | 46 | logger.GetLogger().LogFatalIf( |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 47 | ret == 255, 'Failed ssh to %s (for checking cherrypy)' % remote) |
Luis Lozano | 54db538 | 2015-05-20 15:57:19 -0700 | [diff] [blame] | 48 | logger.GetLogger().LogFatalIf( |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 49 | ret != 0, "Failed to find cherrypy or ctypes on remote '{}', " |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 50 | 'cros flash cannot work.'.format(remote)) |
Luis Lozano | 54db538 | 2015-05-20 15:57:19 -0700 | [diff] [blame] | 51 | |
cmtice | 0cc4e77 | 2014-01-30 15:52:37 -0800 | [diff] [blame] | 52 | |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 53 | def DoImage(argv): |
Han Shen | ba64928 | 2015-08-05 17:19:55 -0700 | [diff] [blame] | 54 | """Image ChromeOS.""" |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 55 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 56 | parser = argparse.ArgumentParser() |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame^] | 57 | parser.add_argument( |
| 58 | '-c', |
| 59 | '--chromeos_root', |
| 60 | dest='chromeos_root', |
| 61 | help='Target directory for ChromeOS installation.') |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 62 | parser.add_argument('-r', '--remote', dest='remote', help='Target device.') |
| 63 | parser.add_argument('-i', '--image', dest='image', help='Image binary file.') |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame^] | 64 | parser.add_argument( |
| 65 | '-b', '--board', dest='board', help='Target board override.') |
| 66 | parser.add_argument( |
| 67 | '-f', |
| 68 | '--force', |
| 69 | dest='force', |
| 70 | action='store_true', |
| 71 | default=False, |
| 72 | help='Force an image even if it is non-test.') |
| 73 | parser.add_argument( |
| 74 | '-n', |
| 75 | '--no_lock', |
| 76 | dest='no_lock', |
| 77 | default=False, |
| 78 | action='store_true', |
| 79 | help='Do not attempt to lock remote before imaging. ' |
| 80 | 'This option should only be used in cases where the ' |
| 81 | 'exclusive lock has already been acquired (e.g. in ' |
| 82 | 'a script that calls this one).') |
| 83 | parser.add_argument( |
| 84 | '-l', |
| 85 | '--logging_level', |
| 86 | dest='log_level', |
| 87 | default='verbose', |
| 88 | help='Amount of logging to be used. Valid levels are ' |
| 89 | "'quiet', 'average', and 'verbose'.") |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 90 | parser.add_argument('-a', '--image_args', dest='image_args') |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 91 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 92 | options = parser.parse_args(argv[1:]) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 93 | |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 94 | if not options.log_level in command_executer.LOG_LEVEL: |
| 95 | Usage(parser, "--logging_level must be 'quiet', 'average' or 'verbose'") |
| 96 | else: |
| 97 | log_level = options.log_level |
| 98 | |
| 99 | # Common initializations |
| 100 | cmd_executer = command_executer.GetCommandExecuter(log_level=log_level) |
| 101 | l = logger.GetLogger() |
| 102 | |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 103 | if options.chromeos_root is None: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 104 | Usage(parser, '--chromeos_root must be set') |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 105 | |
| 106 | if options.remote is None: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 107 | Usage(parser, '--remote must be set') |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 108 | |
| 109 | options.chromeos_root = os.path.expanduser(options.chromeos_root) |
| 110 | |
| 111 | if options.board is None: |
| 112 | board = cmd_executer.CrosLearnBoard(options.chromeos_root, options.remote) |
| 113 | else: |
| 114 | board = options.board |
| 115 | |
| 116 | if options.image is None: |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 117 | images_dir = misc.GetImageDir(options.chromeos_root, board) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 118 | image = os.path.join(images_dir, 'latest', 'chromiumos_test_image.bin') |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 119 | if not os.path.exists(image): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 120 | image = os.path.join(images_dir, 'latest', 'chromiumos_image.bin') |
David Sharp | a20e030 | 2016-01-25 16:00:02 -0800 | [diff] [blame] | 121 | is_xbuddy_image = False |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 122 | else: |
| 123 | image = options.image |
David Sharp | a20e030 | 2016-01-25 16:00:02 -0800 | [diff] [blame] | 124 | is_xbuddy_image = image.startswith('xbuddy://') |
| 125 | if not is_xbuddy_image: |
cmtice | 0cc4e77 | 2014-01-30 15:52:37 -0800 | [diff] [blame] | 126 | image = os.path.expanduser(image) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 127 | |
David Sharp | a20e030 | 2016-01-25 16:00:02 -0800 | [diff] [blame] | 128 | if not is_xbuddy_image: |
cmtice | 0cc4e77 | 2014-01-30 15:52:37 -0800 | [diff] [blame] | 129 | image = os.path.realpath(image) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 130 | |
David Sharp | a20e030 | 2016-01-25 16:00:02 -0800 | [diff] [blame] | 131 | if not os.path.exists(image) and not is_xbuddy_image: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 132 | Usage(parser, 'Image file: ' + image + ' does not exist!') |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 133 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 134 | try: |
| 135 | should_unlock = False |
| 136 | if not options.no_lock: |
| 137 | try: |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 138 | _ = locks.AcquireLock( |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 139 | list(options.remote.split()), options.chromeos_root) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 140 | should_unlock = True |
| 141 | except Exception as e: |
Caroline Tice | 9099a78 | 2016-07-22 16:28:12 -0700 | [diff] [blame] | 142 | raise RuntimeError('Error acquiring machine: %s' % str(e)) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 143 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 144 | reimage = False |
| 145 | local_image = False |
David Sharp | a20e030 | 2016-01-25 16:00:02 -0800 | [diff] [blame] | 146 | if not is_xbuddy_image: |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 147 | local_image = True |
| 148 | image_checksum = FileUtils().Md5File(image, log_level=log_level) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 149 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 150 | command = 'cat ' + checksum_file |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 151 | ret, device_checksum, _ = cmd_executer.CrosRunCommandWOutput( |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame^] | 152 | command, chromeos_root=options.chromeos_root, machine=options.remote) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 153 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 154 | device_checksum = device_checksum.strip() |
| 155 | image_checksum = str(image_checksum) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 156 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 157 | l.LogOutput('Image checksum: ' + image_checksum) |
| 158 | l.LogOutput('Device checksum: ' + device_checksum) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 159 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 160 | if image_checksum != device_checksum: |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame^] | 161 | [found, located_image] = LocateOrCopyImage( |
| 162 | options.chromeos_root, image, board=board) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 163 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 164 | reimage = True |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 165 | l.LogOutput('Checksums do not match. Re-imaging...') |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 166 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 167 | is_test_image = IsImageModdedForTest(options.chromeos_root, |
| 168 | located_image, log_level) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 169 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 170 | if not is_test_image and not options.force: |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 171 | logger.GetLogger().LogFatal('Have to pass --force to image a ' |
| 172 | 'non-test image!') |
cmtice | 0cc4e77 | 2014-01-30 15:52:37 -0800 | [diff] [blame] | 173 | else: |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 174 | reimage = True |
| 175 | found = True |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 176 | l.LogOutput('Using non-local image; Re-imaging...') |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 177 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 178 | if reimage: |
| 179 | # If the device has /tmp mounted as noexec, image_to_live.sh can fail. |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 180 | command = 'mount -o remount,rw,exec /tmp' |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame^] | 181 | cmd_executer.CrosRunCommand( |
| 182 | command, chromeos_root=options.chromeos_root, machine=options.remote) |
cmtice | b134008 | 2014-01-13 13:22:37 -0800 | [diff] [blame] | 183 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 184 | real_src_dir = os.path.join( |
| 185 | os.path.realpath(options.chromeos_root), 'src') |
| 186 | real_chroot_dir = os.path.join( |
| 187 | os.path.realpath(options.chromeos_root), 'chroot') |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 188 | if local_image: |
| 189 | if located_image.find(real_src_dir) != 0: |
| 190 | if located_image.find(real_chroot_dir) != 0: |
Caroline Tice | 9099a78 | 2016-07-22 16:28:12 -0700 | [diff] [blame] | 191 | raise RuntimeError('Located image: %s not in chromeos_root: %s' % |
| 192 | (located_image, options.chromeos_root)) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 193 | else: |
| 194 | chroot_image = located_image[len(real_chroot_dir):] |
| 195 | else: |
| 196 | chroot_image = os.path.join( |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 197 | '~/trunk/src', located_image[len(real_src_dir):].lstrip('/')) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 198 | |
| 199 | # Check to see if cros flash will work for the remote machine. |
| 200 | CheckForCrosFlash(options.chromeos_root, options.remote, log_level) |
| 201 | |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame^] | 202 | cros_flash_args = [ |
| 203 | 'cros', 'flash', '--board=%s' % board, '--clobber-stateful', |
| 204 | options.remote |
| 205 | ] |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 206 | if local_image: |
David Sharp | a20e030 | 2016-01-25 16:00:02 -0800 | [diff] [blame] | 207 | cros_flash_args.append(chroot_image) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 208 | else: |
David Sharp | a20e030 | 2016-01-25 16:00:02 -0800 | [diff] [blame] | 209 | cros_flash_args.append(image) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 210 | |
David Sharp | a20e030 | 2016-01-25 16:00:02 -0800 | [diff] [blame] | 211 | command = ' '.join(cros_flash_args) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 212 | |
| 213 | # Workaround for crosbug.com/35684. |
| 214 | os.chmod(misc.GetChromeOSKeyFile(options.chromeos_root), 0600) |
cmtice | b134008 | 2014-01-13 13:22:37 -0800 | [diff] [blame] | 215 | |
David Sharp | a20e030 | 2016-01-25 16:00:02 -0800 | [diff] [blame] | 216 | if log_level == 'average': |
| 217 | cmd_executer.SetLogLevel('verbose') |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 218 | retries = 0 |
David Sharp | a20e030 | 2016-01-25 16:00:02 -0800 | [diff] [blame] | 219 | while True: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 220 | if log_level == 'quiet': |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 221 | l.LogOutput('CMD : %s' % command) |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame^] | 222 | ret = cmd_executer.ChrootRunCommand( |
| 223 | options.chromeos_root, command, command_timeout=1800) |
David Sharp | a20e030 | 2016-01-25 16:00:02 -0800 | [diff] [blame] | 224 | if ret == 0 or retries >= 2: |
| 225 | break |
| 226 | retries += 1 |
| 227 | if log_level == 'quiet': |
| 228 | l.LogOutput('Imaging failed. Retry # %d.' % retries) |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 229 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 230 | if log_level == 'average': |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 231 | cmd_executer.SetLogLevel(log_level) |
cmtice | 0cc4e77 | 2014-01-30 15:52:37 -0800 | [diff] [blame] | 232 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 233 | if found == False: |
| 234 | temp_dir = os.path.dirname(located_image) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 235 | l.LogOutput('Deleting temp image dir: %s' % temp_dir) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 236 | shutil.rmtree(temp_dir) |
| 237 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 238 | logger.GetLogger().LogFatalIf(ret, 'Image command failed') |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 239 | |
| 240 | # Unfortunately cros_image_to_target.py sometimes returns early when the |
| 241 | # machine isn't fully up yet. |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 242 | ret = EnsureMachineUp(options.chromeos_root, options.remote, log_level) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 243 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 244 | # If this is a non-local image, then the ret returned from |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 245 | # EnsureMachineUp is the one that will be returned by this function; |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 246 | # in that case, make sure the value in 'ret' is appropriate. |
| 247 | if not local_image and ret == True: |
| 248 | ret = 0 |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 249 | else: |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 250 | ret = 1 |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 251 | |
| 252 | if local_image: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 253 | if log_level == 'average': |
| 254 | l.LogOutput('Verifying image.') |
| 255 | command = 'echo %s > %s && chmod -w %s' % (image_checksum, |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame^] | 256 | checksum_file, checksum_file) |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 257 | ret = cmd_executer.CrosRunCommand( |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 258 | command, |
| 259 | chromeos_root=options.chromeos_root, |
| 260 | machine=options.remote) |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 261 | logger.GetLogger().LogFatalIf(ret, 'Writing checksum failed.') |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 262 | |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame^] | 263 | successfully_imaged = VerifyChromeChecksum(options.chromeos_root, image, |
| 264 | options.remote, log_level) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 265 | logger.GetLogger().LogFatalIf(not successfully_imaged, |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 266 | 'Image verification failed!') |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 267 | TryRemountPartitionAsRW(options.chromeos_root, options.remote, |
| 268 | log_level) |
| 269 | else: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 270 | l.LogOutput('Checksums match. Skipping reimage') |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 271 | return ret |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 272 | finally: |
| 273 | if should_unlock: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 274 | locks.ReleaseLock(list(options.remote.split()), options.chromeos_root) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 275 | |
| 276 | |
| 277 | def LocateOrCopyImage(chromeos_root, image, board=None): |
| 278 | l = logger.GetLogger() |
| 279 | if board is None: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 280 | board_glob = '*' |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 281 | else: |
| 282 | board_glob = board |
| 283 | |
| 284 | chromeos_root_realpath = os.path.realpath(chromeos_root) |
| 285 | image = os.path.realpath(image) |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 286 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 287 | if image.startswith('%s/' % chromeos_root_realpath): |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 288 | return [True, image] |
| 289 | |
| 290 | # First search within the existing build dirs for any matching files. |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 291 | images_glob = ('%s/src/build/images/%s/*/*.bin' % (chromeos_root_realpath, |
| 292 | board_glob)) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 293 | images_list = glob.glob(images_glob) |
| 294 | for potential_image in images_list: |
| 295 | if filecmp.cmp(potential_image, image): |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame^] | 296 | l.LogOutput('Found matching image %s in chromeos_root.' % potential_image) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 297 | return [True, potential_image] |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 298 | # We did not find an image. Copy it in the src dir and return the copied |
| 299 | # file. |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 300 | if board is None: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 301 | board = '' |
| 302 | base_dir = ('%s/src/build/images/%s' % (chromeos_root_realpath, board)) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 303 | if not os.path.isdir(base_dir): |
| 304 | os.makedirs(base_dir) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 305 | temp_dir = tempfile.mkdtemp(prefix='%s/tmp' % base_dir) |
| 306 | new_image = '%s/%s' % (temp_dir, os.path.basename(image)) |
| 307 | l.LogOutput('No matching image found. Copying %s to %s' % (image, new_image)) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 308 | shutil.copyfile(image, new_image) |
| 309 | return [False, new_image] |
| 310 | |
| 311 | |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 312 | def GetImageMountCommand(chromeos_root, image, rootfs_mp, stateful_mp): |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 313 | image_dir = os.path.dirname(image) |
| 314 | image_file = os.path.basename(image) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 315 | mount_command = ('cd %s/src/scripts &&' |
| 316 | './mount_gpt_image.sh --from=%s --image=%s' |
| 317 | ' --safe --read_only' |
| 318 | ' --rootfs_mountpt=%s' |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame^] | 319 | ' --stateful_mountpt=%s' % |
| 320 | (chromeos_root, image_dir, image_file, rootfs_mp, |
| 321 | stateful_mp)) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 322 | return mount_command |
| 323 | |
| 324 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 325 | def MountImage(chromeos_root, |
| 326 | image, |
| 327 | rootfs_mp, |
| 328 | stateful_mp, |
| 329 | log_level, |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 330 | unmount=False): |
| 331 | cmd_executer = command_executer.GetCommandExecuter(log_level=log_level) |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 332 | command = GetImageMountCommand(chromeos_root, image, rootfs_mp, stateful_mp) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 333 | if unmount: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 334 | command = '%s --unmount' % command |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 335 | ret = cmd_executer.RunCommand(command) |
| 336 | logger.GetLogger().LogFatalIf(ret, 'Mount/unmount command failed!') |
| 337 | return ret |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 338 | |
| 339 | |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 340 | def IsImageModdedForTest(chromeos_root, image, log_level): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 341 | if log_level != 'verbose': |
| 342 | log_level = 'quiet' |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 343 | rootfs_mp = tempfile.mkdtemp() |
| 344 | stateful_mp = tempfile.mkdtemp() |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 345 | MountImage(chromeos_root, image, rootfs_mp, stateful_mp, log_level) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 346 | lsb_release_file = os.path.join(rootfs_mp, 'etc/lsb-release') |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 347 | lsb_release_contents = open(lsb_release_file).read() |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 348 | is_test_image = re.search('test', lsb_release_contents, re.IGNORECASE) |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame^] | 349 | MountImage( |
| 350 | chromeos_root, image, rootfs_mp, stateful_mp, log_level, unmount=True) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 351 | return is_test_image |
| 352 | |
| 353 | |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 354 | def VerifyChromeChecksum(chromeos_root, image, remote, log_level): |
| 355 | cmd_executer = command_executer.GetCommandExecuter(log_level=log_level) |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 356 | rootfs_mp = tempfile.mkdtemp() |
| 357 | stateful_mp = tempfile.mkdtemp() |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 358 | MountImage(chromeos_root, image, rootfs_mp, stateful_mp, log_level) |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame^] | 359 | image_chrome_checksum = FileUtils().Md5File( |
| 360 | '%s/opt/google/chrome/chrome' % rootfs_mp, log_level=log_level) |
| 361 | MountImage( |
| 362 | chromeos_root, image, rootfs_mp, stateful_mp, log_level, unmount=True) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 363 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 364 | command = 'md5sum /opt/google/chrome/chrome' |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame^] | 365 | [_, o, _] = cmd_executer.CrosRunCommandWOutput( |
| 366 | command, chromeos_root=chromeos_root, machine=remote) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 367 | device_chrome_checksum = o.split()[0] |
| 368 | if image_chrome_checksum.strip() == device_chrome_checksum.strip(): |
| 369 | return True |
| 370 | else: |
| 371 | return False |
| 372 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 373 | |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 374 | # Remount partition as writable. |
| 375 | # TODO: auto-detect if an image is built using --noenable_rootfs_verification. |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 376 | def TryRemountPartitionAsRW(chromeos_root, remote, log_level): |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 377 | l = logger.GetLogger() |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 378 | cmd_executer = command_executer.GetCommandExecuter(log_level=log_level) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 379 | command = 'sudo mount -o remount,rw /' |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 380 | ret = cmd_executer.CrosRunCommand(\ |
| 381 | command, chromeos_root=chromeos_root, machine=remote, |
| 382 | terminated_timeout=10) |
| 383 | if ret: |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 384 | ## Safely ignore. |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 385 | l.LogWarning('Failed to remount partition as rw, ' |
| 386 | 'probably the image was not built with ' |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 387 | "\"--noenable_rootfs_verification\", " |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 388 | 'you can safely ignore this.') |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 389 | else: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 390 | l.LogOutput('Re-mounted partition as writable.') |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 391 | |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 392 | |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 393 | def EnsureMachineUp(chromeos_root, remote, log_level): |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 394 | l = logger.GetLogger() |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 395 | cmd_executer = command_executer.GetCommandExecuter(log_level=log_level) |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 396 | timeout = 600 |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 397 | magic = 'abcdefghijklmnopqrstuvwxyz' |
| 398 | command = 'echo %s' % magic |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 399 | start_time = time.time() |
| 400 | while True: |
| 401 | current_time = time.time() |
| 402 | if current_time - start_time > timeout: |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame^] | 403 | l.LogError( |
| 404 | 'Timeout of %ss reached. Machine still not up. Aborting.' % timeout) |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 405 | return False |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame^] | 406 | ret = cmd_executer.CrosRunCommand( |
| 407 | command, chromeos_root=chromeos_root, machine=remote) |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 408 | if not ret: |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 409 | return True |
| 410 | |
| 411 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 412 | if __name__ == '__main__': |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 413 | retval = DoImage(sys.argv) |
| 414 | sys.exit(retval) |