Zhizhou Yang | 5534af8 | 2020-01-15 16:25:04 -0800 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
Manoj Gupta | 1d1de43 | 2019-04-26 11:30:14 -0700 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 3 | # |
Manoj Gupta | 1d1de43 | 2019-04-26 11:30:14 -0700 | [diff] [blame] | 4 | # Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 5 | # Use of this source code is governed by a BSD-style license that can be |
| 6 | # found in the LICENSE file. |
| 7 | |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 8 | """Script to image a ChromeOS device. |
| 9 | |
| 10 | This script images a remote ChromeOS device with a specific image." |
| 11 | """ |
| 12 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 13 | from __future__ import print_function |
| 14 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 15 | __author__ = 'asharif@google.com (Ahmad Sharif)' |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 16 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 17 | import argparse |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 18 | import filecmp |
Caroline Tice | d0f7a73 | 2018-03-02 16:31:17 -0800 | [diff] [blame] | 19 | import getpass |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 20 | import glob |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 21 | import os |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 22 | import re |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 23 | import shutil |
| 24 | import sys |
| 25 | import tempfile |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 26 | import time |
| 27 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 28 | from cros_utils import command_executer |
| 29 | from cros_utils import locks |
| 30 | from cros_utils import logger |
| 31 | from cros_utils import misc |
| 32 | from cros_utils.file_utils import FileUtils |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 33 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 34 | checksum_file = '/usr/local/osimage_checksum_file' |
| 35 | lock_file = '/tmp/image_chromeos_lock/image_chromeos_lock' |
| 36 | |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 37 | |
| 38 | def Usage(parser, message): |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 39 | print('ERROR: %s' % message) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 40 | parser.print_help() |
| 41 | sys.exit(0) |
| 42 | |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 43 | |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 44 | def CheckForCrosFlash(chromeos_root, remote, log_level): |
| 45 | cmd_executer = command_executer.GetCommandExecuter(log_level=log_level) |
cmtice | 0cc4e77 | 2014-01-30 15:52:37 -0800 | [diff] [blame] | 46 | |
Luis Lozano | 54db538 | 2015-05-20 15:57:19 -0700 | [diff] [blame] | 47 | # Check to see if remote machine has cherrypy, ctypes |
| 48 | command = "python -c 'import cherrypy, ctypes'" |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 49 | ret = cmd_executer.CrosRunCommand( |
| 50 | command, chromeos_root=chromeos_root, machine=remote) |
Han Shen | 96d936c | 2015-03-25 12:03:12 -0700 | [diff] [blame] | 51 | logger.GetLogger().LogFatalIf( |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 52 | ret == 255, 'Failed ssh to %s (for checking cherrypy)' % remote) |
Luis Lozano | 54db538 | 2015-05-20 15:57:19 -0700 | [diff] [blame] | 53 | logger.GetLogger().LogFatalIf( |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 54 | ret != 0, "Failed to find cherrypy or ctypes on remote '{}', " |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 55 | 'cros flash cannot work.'.format(remote)) |
Luis Lozano | 54db538 | 2015-05-20 15:57:19 -0700 | [diff] [blame] | 56 | |
cmtice | 0cc4e77 | 2014-01-30 15:52:37 -0800 | [diff] [blame] | 57 | |
Manoj Gupta | 1282e84 | 2017-05-17 12:57:56 -0700 | [diff] [blame] | 58 | def DisableCrosBeeps(chromeos_root, remote, log_level): |
| 59 | """Disable annoying chromebooks beeps after reboots.""" |
| 60 | cmd_executer = command_executer.GetCommandExecuter(log_level=log_level) |
| 61 | |
| 62 | command = '/usr/share/vboot/bin/set_gbb_flags.sh 0x1' |
| 63 | logger.GetLogger().LogOutput('Trying to disable beeping.') |
| 64 | |
| 65 | ret, o, _ = cmd_executer.CrosRunCommandWOutput( |
| 66 | command, chromeos_root=chromeos_root, machine=remote) |
| 67 | if ret != 0: |
| 68 | logger.GetLogger().LogOutput(o) |
| 69 | logger.GetLogger().LogOutput('Failed to disable beeps.') |
| 70 | |
| 71 | |
Caroline Tice | d0f7a73 | 2018-03-02 16:31:17 -0800 | [diff] [blame] | 72 | def FindChromeOSImage(image_file, chromeos_root): |
| 73 | """Find path for ChromeOS image inside chroot. |
| 74 | |
| 75 | This function could be called with image paths that are either inside |
| 76 | or outside the chroot. In either case the path needs to be translated |
| 77 | to an real/absolute path inside the chroot. |
| 78 | Example input paths: |
| 79 | /usr/local/google/home/uname/chromeos/chroot/tmp/my-test-images/image |
| 80 | ~/trunk/src/build/images/board/latest/image |
| 81 | /tmp/peppy-release/R67-1235.0.0/image |
| 82 | |
| 83 | Corresponding example output paths: |
| 84 | /tmp/my-test-images/image |
| 85 | /home/uname/trunk/src/build/images/board/latest/image |
| 86 | /tmp/peppy-release/R67-1235.0,0/image |
| 87 | """ |
| 88 | |
| 89 | # Get the name of the user, for "/home/<user>" part of the path. |
| 90 | whoami = getpass.getuser() |
| 91 | # Get the full path for the chroot dir, including 'chroot' |
| 92 | real_chroot_dir = os.path.join(os.path.realpath(chromeos_root), 'chroot') |
| 93 | # Get the full path for the chromeos root, excluding 'chroot' |
| 94 | real_chromeos_root = os.path.realpath(chromeos_root) |
| 95 | |
| 96 | # If path name starts with real_chroot_dir, remove that piece, but assume |
| 97 | # the rest of the path is correct. |
| 98 | if image_file.find(real_chroot_dir) != -1: |
| 99 | chroot_image = image_file[len(real_chroot_dir):] |
| 100 | # If path name starts with chromeos_root, excluding 'chroot', replace the |
| 101 | # chromeos_root with the prefix: '/home/<username>/trunk'. |
| 102 | elif image_file.find(real_chromeos_root) != -1: |
| 103 | chroot_image = image_file[len(real_chromeos_root):] |
| 104 | chroot_image = '/home/%s/trunk%s' % (whoami, chroot_image) |
| 105 | # Else assume the path is already internal, so leave it alone. |
| 106 | else: |
| 107 | chroot_image = image_file |
| 108 | |
| 109 | return chroot_image |
| 110 | |
| 111 | |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 112 | def DoImage(argv): |
Han Shen | ba64928 | 2015-08-05 17:19:55 -0700 | [diff] [blame] | 113 | """Image ChromeOS.""" |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 114 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 115 | parser = argparse.ArgumentParser() |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 116 | parser.add_argument( |
| 117 | '-c', |
| 118 | '--chromeos_root', |
| 119 | dest='chromeos_root', |
| 120 | help='Target directory for ChromeOS installation.') |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 121 | parser.add_argument('-r', '--remote', dest='remote', help='Target device.') |
| 122 | parser.add_argument('-i', '--image', dest='image', help='Image binary file.') |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 123 | parser.add_argument( |
| 124 | '-b', '--board', dest='board', help='Target board override.') |
| 125 | parser.add_argument( |
| 126 | '-f', |
| 127 | '--force', |
| 128 | dest='force', |
| 129 | action='store_true', |
| 130 | default=False, |
| 131 | help='Force an image even if it is non-test.') |
| 132 | parser.add_argument( |
| 133 | '-n', |
| 134 | '--no_lock', |
| 135 | dest='no_lock', |
| 136 | default=False, |
| 137 | action='store_true', |
| 138 | help='Do not attempt to lock remote before imaging. ' |
| 139 | 'This option should only be used in cases where the ' |
| 140 | 'exclusive lock has already been acquired (e.g. in ' |
| 141 | 'a script that calls this one).') |
| 142 | parser.add_argument( |
| 143 | '-l', |
| 144 | '--logging_level', |
| 145 | dest='log_level', |
| 146 | default='verbose', |
| 147 | help='Amount of logging to be used. Valid levels are ' |
| 148 | "'quiet', 'average', and 'verbose'.") |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 149 | parser.add_argument('-a', '--image_args', dest='image_args') |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 150 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 151 | options = parser.parse_args(argv[1:]) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 152 | |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 153 | if not options.log_level in command_executer.LOG_LEVEL: |
| 154 | Usage(parser, "--logging_level must be 'quiet', 'average' or 'verbose'") |
| 155 | else: |
| 156 | log_level = options.log_level |
| 157 | |
| 158 | # Common initializations |
| 159 | cmd_executer = command_executer.GetCommandExecuter(log_level=log_level) |
| 160 | l = logger.GetLogger() |
| 161 | |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 162 | if options.chromeos_root is None: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 163 | Usage(parser, '--chromeos_root must be set') |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 164 | |
| 165 | if options.remote is None: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 166 | Usage(parser, '--remote must be set') |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 167 | |
| 168 | options.chromeos_root = os.path.expanduser(options.chromeos_root) |
| 169 | |
| 170 | if options.board is None: |
| 171 | board = cmd_executer.CrosLearnBoard(options.chromeos_root, options.remote) |
| 172 | else: |
| 173 | board = options.board |
| 174 | |
| 175 | if options.image is None: |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 176 | images_dir = misc.GetImageDir(options.chromeos_root, board) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 177 | image = os.path.join(images_dir, 'latest', 'chromiumos_test_image.bin') |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 178 | if not os.path.exists(image): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 179 | image = os.path.join(images_dir, 'latest', 'chromiumos_image.bin') |
David Sharp | a20e030 | 2016-01-25 16:00:02 -0800 | [diff] [blame] | 180 | is_xbuddy_image = False |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 181 | else: |
| 182 | image = options.image |
David Sharp | a20e030 | 2016-01-25 16:00:02 -0800 | [diff] [blame] | 183 | is_xbuddy_image = image.startswith('xbuddy://') |
| 184 | if not is_xbuddy_image: |
cmtice | 0cc4e77 | 2014-01-30 15:52:37 -0800 | [diff] [blame] | 185 | image = os.path.expanduser(image) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 186 | |
David Sharp | a20e030 | 2016-01-25 16:00:02 -0800 | [diff] [blame] | 187 | if not is_xbuddy_image: |
cmtice | 0cc4e77 | 2014-01-30 15:52:37 -0800 | [diff] [blame] | 188 | image = os.path.realpath(image) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 189 | |
David Sharp | a20e030 | 2016-01-25 16:00:02 -0800 | [diff] [blame] | 190 | if not os.path.exists(image) and not is_xbuddy_image: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 191 | Usage(parser, 'Image file: ' + image + ' does not exist!') |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 192 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 193 | try: |
| 194 | should_unlock = False |
| 195 | if not options.no_lock: |
| 196 | try: |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 197 | _ = locks.AcquireLock( |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 198 | list(options.remote.split()), options.chromeos_root) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 199 | should_unlock = True |
| 200 | except Exception as e: |
Caroline Tice | 9099a78 | 2016-07-22 16:28:12 -0700 | [diff] [blame] | 201 | raise RuntimeError('Error acquiring machine: %s' % str(e)) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 202 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 203 | reimage = False |
| 204 | local_image = False |
David Sharp | a20e030 | 2016-01-25 16:00:02 -0800 | [diff] [blame] | 205 | if not is_xbuddy_image: |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 206 | local_image = True |
| 207 | image_checksum = FileUtils().Md5File(image, log_level=log_level) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 208 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 209 | command = 'cat ' + checksum_file |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 210 | ret, device_checksum, _ = cmd_executer.CrosRunCommandWOutput( |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 211 | command, chromeos_root=options.chromeos_root, machine=options.remote) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 212 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 213 | device_checksum = device_checksum.strip() |
| 214 | image_checksum = str(image_checksum) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 215 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 216 | l.LogOutput('Image checksum: ' + image_checksum) |
| 217 | l.LogOutput('Device checksum: ' + device_checksum) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 218 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 219 | if image_checksum != device_checksum: |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 220 | [found, located_image] = LocateOrCopyImage( |
| 221 | options.chromeos_root, image, board=board) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 222 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 223 | reimage = True |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 224 | l.LogOutput('Checksums do not match. Re-imaging...') |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 225 | |
Caroline Tice | d0f7a73 | 2018-03-02 16:31:17 -0800 | [diff] [blame] | 226 | chroot_image = FindChromeOSImage(located_image, options.chromeos_root) |
| 227 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 228 | is_test_image = IsImageModdedForTest(options.chromeos_root, |
Caroline Tice | d0f7a73 | 2018-03-02 16:31:17 -0800 | [diff] [blame] | 229 | chroot_image, log_level) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 230 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 231 | if not is_test_image and not options.force: |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 232 | logger.GetLogger().LogFatal('Have to pass --force to image a ' |
| 233 | 'non-test image!') |
cmtice | 0cc4e77 | 2014-01-30 15:52:37 -0800 | [diff] [blame] | 234 | else: |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 235 | reimage = True |
| 236 | found = True |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 237 | l.LogOutput('Using non-local image; Re-imaging...') |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 238 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 239 | if reimage: |
| 240 | # 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] | 241 | command = 'mount -o remount,rw,exec /tmp' |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 242 | cmd_executer.CrosRunCommand( |
| 243 | command, chromeos_root=options.chromeos_root, machine=options.remote) |
cmtice | b134008 | 2014-01-13 13:22:37 -0800 | [diff] [blame] | 244 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 245 | # Check to see if cros flash will work for the remote machine. |
| 246 | CheckForCrosFlash(options.chromeos_root, options.remote, log_level) |
| 247 | |
Manoj Gupta | 1282e84 | 2017-05-17 12:57:56 -0700 | [diff] [blame] | 248 | # Disable the annoying chromebook beeps after reboot. |
| 249 | DisableCrosBeeps(options.chromeos_root, options.remote, log_level) |
| 250 | |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 251 | cros_flash_args = [ |
Manoj Gupta | 1282e84 | 2017-05-17 12:57:56 -0700 | [diff] [blame] | 252 | 'cros', 'flash', |
| 253 | '--board=%s' % board, '--clobber-stateful', options.remote |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 254 | ] |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 255 | if local_image: |
David Sharp | a20e030 | 2016-01-25 16:00:02 -0800 | [diff] [blame] | 256 | cros_flash_args.append(chroot_image) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 257 | else: |
David Sharp | a20e030 | 2016-01-25 16:00:02 -0800 | [diff] [blame] | 258 | cros_flash_args.append(image) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 259 | |
David Sharp | a20e030 | 2016-01-25 16:00:02 -0800 | [diff] [blame] | 260 | command = ' '.join(cros_flash_args) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 261 | |
| 262 | # Workaround for crosbug.com/35684. |
Zhizhou Yang | 5534af8 | 2020-01-15 16:25:04 -0800 | [diff] [blame^] | 263 | os.chmod(misc.GetChromeOSKeyFile(options.chromeos_root), 0o600) |
cmtice | b134008 | 2014-01-13 13:22:37 -0800 | [diff] [blame] | 264 | |
David Sharp | a20e030 | 2016-01-25 16:00:02 -0800 | [diff] [blame] | 265 | if log_level == 'average': |
| 266 | cmd_executer.SetLogLevel('verbose') |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 267 | retries = 0 |
David Sharp | a20e030 | 2016-01-25 16:00:02 -0800 | [diff] [blame] | 268 | while True: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 269 | if log_level == 'quiet': |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 270 | l.LogOutput('CMD : %s' % command) |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 271 | ret = cmd_executer.ChrootRunCommand( |
| 272 | options.chromeos_root, command, command_timeout=1800) |
David Sharp | a20e030 | 2016-01-25 16:00:02 -0800 | [diff] [blame] | 273 | if ret == 0 or retries >= 2: |
| 274 | break |
| 275 | retries += 1 |
| 276 | if log_level == 'quiet': |
| 277 | l.LogOutput('Imaging failed. Retry # %d.' % retries) |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 278 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 279 | if log_level == 'average': |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 280 | cmd_executer.SetLogLevel(log_level) |
cmtice | 0cc4e77 | 2014-01-30 15:52:37 -0800 | [diff] [blame] | 281 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 282 | logger.GetLogger().LogFatalIf(ret, 'Image command failed') |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 283 | |
| 284 | # Unfortunately cros_image_to_target.py sometimes returns early when the |
| 285 | # machine isn't fully up yet. |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 286 | ret = EnsureMachineUp(options.chromeos_root, options.remote, log_level) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 287 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 288 | # If this is a non-local image, then the ret returned from |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 289 | # EnsureMachineUp is the one that will be returned by this function; |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 290 | # in that case, make sure the value in 'ret' is appropriate. |
Denis Nikitin | 144f699 | 2019-07-20 20:25:16 -0700 | [diff] [blame] | 291 | if not local_image and ret: |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 292 | ret = 0 |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 293 | else: |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 294 | ret = 1 |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 295 | |
| 296 | if local_image: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 297 | if log_level == 'average': |
| 298 | l.LogOutput('Verifying image.') |
| 299 | command = 'echo %s > %s && chmod -w %s' % (image_checksum, |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 300 | checksum_file, checksum_file) |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 301 | ret = cmd_executer.CrosRunCommand( |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 302 | command, |
| 303 | chromeos_root=options.chromeos_root, |
| 304 | machine=options.remote) |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 305 | logger.GetLogger().LogFatalIf(ret, 'Writing checksum failed.') |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 306 | |
Caroline Tice | d0f7a73 | 2018-03-02 16:31:17 -0800 | [diff] [blame] | 307 | successfully_imaged = VerifyChromeChecksum( |
| 308 | options.chromeos_root, chroot_image, options.remote, log_level) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 309 | logger.GetLogger().LogFatalIf(not successfully_imaged, |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 310 | 'Image verification failed!') |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 311 | TryRemountPartitionAsRW(options.chromeos_root, options.remote, |
| 312 | log_level) |
Gabriel Marin | e2a920a | 2018-05-16 11:31:07 -0700 | [diff] [blame] | 313 | |
Denis Nikitin | 144f699 | 2019-07-20 20:25:16 -0700 | [diff] [blame] | 314 | if not found: |
Gabriel Marin | e2a920a | 2018-05-16 11:31:07 -0700 | [diff] [blame] | 315 | temp_dir = os.path.dirname(located_image) |
| 316 | l.LogOutput('Deleting temp image dir: %s' % temp_dir) |
| 317 | shutil.rmtree(temp_dir) |
Denis Nikitin | 144f699 | 2019-07-20 20:25:16 -0700 | [diff] [blame] | 318 | l.LogOutput('Image updated.') |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 319 | else: |
Denis Nikitin | 144f699 | 2019-07-20 20:25:16 -0700 | [diff] [blame] | 320 | l.LogOutput('Checksums match, skip image update and reboot.') |
Manoj Gupta | 1d1de43 | 2019-04-26 11:30:14 -0700 | [diff] [blame] | 321 | command = 'reboot && exit' |
| 322 | _ = cmd_executer.CrosRunCommand( |
| 323 | command, chromeos_root=options.chromeos_root, machine=options.remote) |
| 324 | # Wait 30s after reboot. |
| 325 | time.sleep(30) |
| 326 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 327 | finally: |
| 328 | if should_unlock: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 329 | locks.ReleaseLock(list(options.remote.split()), options.chromeos_root) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 330 | |
Denis Nikitin | 144f699 | 2019-07-20 20:25:16 -0700 | [diff] [blame] | 331 | return ret |
| 332 | |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 333 | |
| 334 | def LocateOrCopyImage(chromeos_root, image, board=None): |
| 335 | l = logger.GetLogger() |
| 336 | if board is None: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 337 | board_glob = '*' |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 338 | else: |
| 339 | board_glob = board |
| 340 | |
| 341 | chromeos_root_realpath = os.path.realpath(chromeos_root) |
| 342 | image = os.path.realpath(image) |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 343 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 344 | if image.startswith('%s/' % chromeos_root_realpath): |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 345 | return [True, image] |
| 346 | |
| 347 | # First search within the existing build dirs for any matching files. |
Manoj Gupta | 1d1de43 | 2019-04-26 11:30:14 -0700 | [diff] [blame] | 348 | images_glob = ( |
| 349 | '%s/src/build/images/%s/*/*.bin' % (chromeos_root_realpath, board_glob)) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 350 | images_list = glob.glob(images_glob) |
| 351 | for potential_image in images_list: |
| 352 | if filecmp.cmp(potential_image, image): |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 353 | l.LogOutput('Found matching image %s in chromeos_root.' % potential_image) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 354 | return [True, potential_image] |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 355 | # We did not find an image. Copy it in the src dir and return the copied |
| 356 | # file. |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 357 | if board is None: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 358 | board = '' |
| 359 | base_dir = ('%s/src/build/images/%s' % (chromeos_root_realpath, board)) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 360 | if not os.path.isdir(base_dir): |
| 361 | os.makedirs(base_dir) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 362 | temp_dir = tempfile.mkdtemp(prefix='%s/tmp' % base_dir) |
| 363 | new_image = '%s/%s' % (temp_dir, os.path.basename(image)) |
| 364 | 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] | 365 | shutil.copyfile(image, new_image) |
| 366 | return [False, new_image] |
| 367 | |
| 368 | |
Manoj Gupta | 1d1de43 | 2019-04-26 11:30:14 -0700 | [diff] [blame] | 369 | def GetImageMountCommand(image, rootfs_mp, stateful_mp): |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 370 | image_dir = os.path.dirname(image) |
| 371 | image_file = os.path.basename(image) |
Zhizhou Yang | 5534af8 | 2020-01-15 16:25:04 -0800 | [diff] [blame^] | 372 | mount_command = ('cd /mnt/host/source/src/scripts &&' |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 373 | './mount_gpt_image.sh --from=%s --image=%s' |
| 374 | ' --safe --read_only' |
| 375 | ' --rootfs_mountpt=%s' |
Caroline Tice | d0f7a73 | 2018-03-02 16:31:17 -0800 | [diff] [blame] | 376 | ' --stateful_mountpt=%s' % (image_dir, image_file, rootfs_mp, |
| 377 | stateful_mp)) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 378 | return mount_command |
| 379 | |
| 380 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 381 | def MountImage(chromeos_root, |
| 382 | image, |
| 383 | rootfs_mp, |
| 384 | stateful_mp, |
| 385 | log_level, |
Caroline Tice | d0f7a73 | 2018-03-02 16:31:17 -0800 | [diff] [blame] | 386 | unmount=False, |
| 387 | extra_commands=''): |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 388 | cmd_executer = command_executer.GetCommandExecuter(log_level=log_level) |
Manoj Gupta | 1d1de43 | 2019-04-26 11:30:14 -0700 | [diff] [blame] | 389 | command = GetImageMountCommand(image, rootfs_mp, stateful_mp) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 390 | if unmount: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 391 | command = '%s --unmount' % command |
Caroline Tice | d0f7a73 | 2018-03-02 16:31:17 -0800 | [diff] [blame] | 392 | if extra_commands: |
| 393 | command = '%s ; %s' % (command, extra_commands) |
| 394 | ret, out, _ = cmd_executer.ChrootRunCommandWOutput(chromeos_root, command) |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 395 | logger.GetLogger().LogFatalIf(ret, 'Mount/unmount command failed!') |
Caroline Tice | d0f7a73 | 2018-03-02 16:31:17 -0800 | [diff] [blame] | 396 | return out |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 397 | |
| 398 | |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 399 | def IsImageModdedForTest(chromeos_root, image, log_level): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 400 | if log_level != 'verbose': |
| 401 | log_level = 'quiet' |
Caroline Tice | d0f7a73 | 2018-03-02 16:31:17 -0800 | [diff] [blame] | 402 | command = 'mktemp -d' |
| 403 | cmd_executer = command_executer.GetCommandExecuter(log_level=log_level) |
| 404 | _, rootfs_mp, _ = cmd_executer.ChrootRunCommandWOutput(chromeos_root, command) |
| 405 | _, stateful_mp, _ = cmd_executer.ChrootRunCommandWOutput( |
| 406 | chromeos_root, command) |
| 407 | rootfs_mp = rootfs_mp.strip() |
| 408 | stateful_mp = stateful_mp.strip() |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 409 | lsb_release_file = os.path.join(rootfs_mp, 'etc/lsb-release') |
Jian Cai | 5a14390 | 2019-06-21 14:39:42 -0700 | [diff] [blame] | 410 | extra = ('grep CHROMEOS_RELEASE_TRACK %s | grep -i test' % lsb_release_file) |
Caroline Tice | d0f7a73 | 2018-03-02 16:31:17 -0800 | [diff] [blame] | 411 | output = MountImage( |
| 412 | chromeos_root, |
| 413 | image, |
| 414 | rootfs_mp, |
| 415 | stateful_mp, |
| 416 | log_level, |
| 417 | extra_commands=extra) |
| 418 | is_test_image = re.search('test', output, re.IGNORECASE) |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 419 | MountImage( |
| 420 | chromeos_root, image, rootfs_mp, stateful_mp, log_level, unmount=True) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 421 | return is_test_image |
| 422 | |
| 423 | |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 424 | def VerifyChromeChecksum(chromeos_root, image, remote, log_level): |
Caroline Tice | d0f7a73 | 2018-03-02 16:31:17 -0800 | [diff] [blame] | 425 | command = 'mktemp -d' |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 426 | cmd_executer = command_executer.GetCommandExecuter(log_level=log_level) |
Caroline Tice | d0f7a73 | 2018-03-02 16:31:17 -0800 | [diff] [blame] | 427 | _, rootfs_mp, _ = cmd_executer.ChrootRunCommandWOutput(chromeos_root, command) |
| 428 | _, stateful_mp, _ = cmd_executer.ChrootRunCommandWOutput( |
| 429 | chromeos_root, command) |
| 430 | rootfs_mp = rootfs_mp.strip() |
| 431 | stateful_mp = stateful_mp.strip() |
| 432 | chrome_file = '%s/opt/google/chrome/chrome' % rootfs_mp |
| 433 | extra = 'md5sum %s' % chrome_file |
| 434 | out = MountImage( |
| 435 | chromeos_root, |
| 436 | image, |
| 437 | rootfs_mp, |
| 438 | stateful_mp, |
| 439 | log_level, |
| 440 | extra_commands=extra) |
| 441 | image_chrome_checksum = out.strip().split()[0] |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 442 | MountImage( |
| 443 | chromeos_root, image, rootfs_mp, stateful_mp, log_level, unmount=True) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 444 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 445 | command = 'md5sum /opt/google/chrome/chrome' |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 446 | [_, o, _] = cmd_executer.CrosRunCommandWOutput( |
| 447 | command, chromeos_root=chromeos_root, machine=remote) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 448 | device_chrome_checksum = o.split()[0] |
Manoj Gupta | 1d1de43 | 2019-04-26 11:30:14 -0700 | [diff] [blame] | 449 | return image_chrome_checksum.strip() == device_chrome_checksum.strip() |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 450 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 451 | |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 452 | # Remount partition as writable. |
| 453 | # TODO: auto-detect if an image is built using --noenable_rootfs_verification. |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 454 | def TryRemountPartitionAsRW(chromeos_root, remote, log_level): |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 455 | l = logger.GetLogger() |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 456 | cmd_executer = command_executer.GetCommandExecuter(log_level=log_level) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 457 | command = 'sudo mount -o remount,rw /' |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 458 | ret = cmd_executer.CrosRunCommand(\ |
| 459 | command, chromeos_root=chromeos_root, machine=remote, |
| 460 | terminated_timeout=10) |
| 461 | if ret: |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 462 | ## Safely ignore. |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 463 | l.LogWarning('Failed to remount partition as rw, ' |
| 464 | 'probably the image was not built with ' |
Zhizhou Yang | 5534af8 | 2020-01-15 16:25:04 -0800 | [diff] [blame^] | 465 | '"--noenable_rootfs_verification", ' |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 466 | 'you can safely ignore this.') |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 467 | else: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 468 | l.LogOutput('Re-mounted partition as writable.') |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 469 | |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 470 | |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 471 | def EnsureMachineUp(chromeos_root, remote, log_level): |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 472 | l = logger.GetLogger() |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 473 | cmd_executer = command_executer.GetCommandExecuter(log_level=log_level) |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 474 | timeout = 600 |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 475 | magic = 'abcdefghijklmnopqrstuvwxyz' |
| 476 | command = 'echo %s' % magic |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 477 | start_time = time.time() |
| 478 | while True: |
| 479 | current_time = time.time() |
| 480 | if current_time - start_time > timeout: |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 481 | l.LogError( |
| 482 | 'Timeout of %ss reached. Machine still not up. Aborting.' % timeout) |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 483 | return False |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 484 | ret = cmd_executer.CrosRunCommand( |
| 485 | command, chromeos_root=chromeos_root, machine=remote) |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 486 | if not ret: |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 487 | return True |
| 488 | |
| 489 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 490 | if __name__ == '__main__': |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 491 | retval = DoImage(sys.argv) |
| 492 | sys.exit(retval) |