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