Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
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 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 9 | __author__ = 'asharif@google.com (Ahmad Sharif)' |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 10 | |
| 11 | import filecmp |
| 12 | import glob |
| 13 | import optparse |
| 14 | import os |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 15 | import re |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 16 | import shutil |
| 17 | import sys |
| 18 | import tempfile |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 19 | import time |
| 20 | |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 21 | from utils import command_executer |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 22 | from utils import locks |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 23 | from utils import logger |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 24 | from utils import misc |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 25 | from utils.file_utils import FileUtils |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 26 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 27 | checksum_file = '/usr/local/osimage_checksum_file' |
| 28 | lock_file = '/tmp/image_chromeos_lock/image_chromeos_lock' |
| 29 | |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 30 | |
| 31 | def Usage(parser, message): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 32 | print 'ERROR: ' + message |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 33 | parser.print_help() |
| 34 | sys.exit(0) |
| 35 | |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 36 | |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 37 | def CheckForCrosFlash(chromeos_root, remote, log_level): |
| 38 | cmd_executer = command_executer.GetCommandExecuter(log_level=log_level) |
cmtice | 0cc4e77 | 2014-01-30 15:52:37 -0800 | [diff] [blame] | 39 | |
Luis Lozano | 54db538 | 2015-05-20 15:57:19 -0700 | [diff] [blame] | 40 | # Check to see if remote machine has cherrypy, ctypes |
| 41 | command = "python -c 'import cherrypy, ctypes'" |
| 42 | retval = cmd_executer.CrosRunCommand(command, |
| 43 | chromeos_root=chromeos_root, |
| 44 | machine=remote) |
Han Shen | 96d936c | 2015-03-25 12:03:12 -0700 | [diff] [blame] | 45 | logger.GetLogger().LogFatalIf( |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 46 | retval == 255, 'Failed ssh to %s (for checking cherrypy)' % remote) |
Luis Lozano | 54db538 | 2015-05-20 15:57:19 -0700 | [diff] [blame] | 47 | logger.GetLogger().LogFatalIf( |
| 48 | retval != 0, "Failed to find cherrypy or ctypes on remote '{}', " |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 49 | 'cros flash cannot work.'.format(remote)) |
Luis Lozano | 54db538 | 2015-05-20 15:57:19 -0700 | [diff] [blame] | 50 | |
cmtice | 0cc4e77 | 2014-01-30 15:52:37 -0800 | [diff] [blame] | 51 | |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 52 | def DoImage(argv): |
Han Shen | ba64928 | 2015-08-05 17:19:55 -0700 | [diff] [blame] | 53 | """Image ChromeOS.""" |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 54 | |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 55 | parser = optparse.OptionParser() |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 56 | parser.add_option('-c', |
| 57 | '--chromeos_root', |
| 58 | dest='chromeos_root', |
| 59 | help='Target directory for ChromeOS installation.') |
| 60 | parser.add_option('-r', '--remote', dest='remote', help='Target device.') |
| 61 | parser.add_option('-i', '--image', dest='image', help='Image binary file.') |
| 62 | parser.add_option('-b', |
| 63 | '--board', |
| 64 | dest='board', |
| 65 | help='Target board override.') |
| 66 | parser.add_option('-f', |
| 67 | '--force', |
| 68 | dest='force', |
| 69 | action='store_true', |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 70 | default=False, |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 71 | help='Force an image even if it is non-test.') |
| 72 | parser.add_option('-n', |
| 73 | '--no_lock', |
| 74 | dest='no_lock', |
| 75 | default=False, |
| 76 | action='store_true', |
| 77 | help='Do not attempt to lock remote before imaging. ' |
| 78 | 'This option should only be used in cases where the ' |
| 79 | 'exclusive lock has already been acquired (e.g. in ' |
| 80 | 'a script that calls this one).') |
| 81 | parser.add_option('-l', |
| 82 | '--logging_level', |
| 83 | dest='log_level', |
| 84 | default='verbose', |
| 85 | help='Amount of logging to be used. Valid levels are ' |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 86 | "'quiet', 'average', and 'verbose'.") |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 87 | parser.add_option('-a', '--image_args', dest='image_args') |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 88 | |
| 89 | options = parser.parse_args(argv[1:])[0] |
| 90 | |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 91 | if not options.log_level in command_executer.LOG_LEVEL: |
| 92 | Usage(parser, "--logging_level must be 'quiet', 'average' or 'verbose'") |
| 93 | else: |
| 94 | log_level = options.log_level |
| 95 | |
| 96 | # Common initializations |
| 97 | cmd_executer = command_executer.GetCommandExecuter(log_level=log_level) |
| 98 | l = logger.GetLogger() |
| 99 | |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 100 | if options.chromeos_root is None: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 101 | Usage(parser, '--chromeos_root must be set') |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 102 | |
| 103 | if options.remote is None: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 104 | Usage(parser, '--remote must be set') |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 105 | |
| 106 | options.chromeos_root = os.path.expanduser(options.chromeos_root) |
| 107 | |
| 108 | if options.board is None: |
| 109 | board = cmd_executer.CrosLearnBoard(options.chromeos_root, options.remote) |
| 110 | else: |
| 111 | board = options.board |
| 112 | |
| 113 | if options.image is None: |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 114 | images_dir = misc.GetImageDir(options.chromeos_root, board) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 115 | image = os.path.join(images_dir, 'latest', 'chromiumos_test_image.bin') |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 116 | if not os.path.exists(image): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 117 | image = os.path.join(images_dir, 'latest', 'chromiumos_image.bin') |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 118 | else: |
| 119 | image = options.image |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 120 | if image.find('xbuddy://') < 0: |
cmtice | 0cc4e77 | 2014-01-30 15:52:37 -0800 | [diff] [blame] | 121 | image = os.path.expanduser(image) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 122 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 123 | if image.find('xbuddy://') < 0: |
cmtice | 0cc4e77 | 2014-01-30 15:52:37 -0800 | [diff] [blame] | 124 | image = os.path.realpath(image) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 125 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 126 | if not os.path.exists(image) and image.find('xbuddy://') < 0: |
| 127 | Usage(parser, 'Image file: ' + image + ' does not exist!') |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 128 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 129 | try: |
| 130 | should_unlock = False |
| 131 | if not options.no_lock: |
| 132 | try: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 133 | status = locks.AcquireLock( |
| 134 | list(options.remote.split()), options.chromeos_root) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 135 | should_unlock = True |
| 136 | except Exception as e: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 137 | raise Exception('Error acquiring machine: %s' % str(e)) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 138 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 139 | reimage = False |
| 140 | local_image = False |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 141 | if image.find('xbuddy://') < 0: |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 142 | local_image = True |
| 143 | image_checksum = FileUtils().Md5File(image, log_level=log_level) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 144 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 145 | command = 'cat ' + checksum_file |
Luis Lozano | 036c923 | 2015-12-10 10:47:01 -0800 | [diff] [blame] | 146 | retval, device_checksum, _ = cmd_executer.CrosRunCommandWOutput( |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 147 | command, |
| 148 | chromeos_root=options.chromeos_root, |
| 149 | machine=options.remote) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 150 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 151 | device_checksum = device_checksum.strip() |
| 152 | image_checksum = str(image_checksum) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 153 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 154 | l.LogOutput('Image checksum: ' + image_checksum) |
| 155 | l.LogOutput('Device checksum: ' + device_checksum) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 156 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 157 | if image_checksum != device_checksum: |
| 158 | [found, located_image] = LocateOrCopyImage(options.chromeos_root, |
| 159 | image, |
| 160 | board=board) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 161 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 162 | reimage = True |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 163 | l.LogOutput('Checksums do not match. Re-imaging...') |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 164 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 165 | is_test_image = IsImageModdedForTest(options.chromeos_root, |
| 166 | located_image, log_level) |
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 | if not is_test_image and not options.force: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 169 | logger.GetLogger().LogFatal('Have to pass --force to image a non-test' |
| 170 | ' image!') |
cmtice | 0cc4e77 | 2014-01-30 15:52:37 -0800 | [diff] [blame] | 171 | else: |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 172 | reimage = True |
| 173 | found = True |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 174 | l.LogOutput('Using non-local image; Re-imaging...') |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 175 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 176 | if reimage: |
| 177 | # 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] | 178 | command = 'mount -o remount,rw,exec /tmp' |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 179 | cmd_executer.CrosRunCommand(command, |
| 180 | chromeos_root=options.chromeos_root, |
| 181 | machine=options.remote) |
cmtice | b134008 | 2014-01-13 13:22:37 -0800 | [diff] [blame] | 182 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 183 | real_src_dir = os.path.join( |
| 184 | os.path.realpath(options.chromeos_root), 'src') |
| 185 | real_chroot_dir = os.path.join( |
| 186 | os.path.realpath(options.chromeos_root), 'chroot') |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 187 | if local_image: |
| 188 | if located_image.find(real_src_dir) != 0: |
| 189 | if located_image.find(real_chroot_dir) != 0: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 190 | raise Exception('Located image: %s not in chromeos_root: %s' % |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 191 | (located_image, options.chromeos_root)) |
| 192 | else: |
| 193 | chroot_image = located_image[len(real_chroot_dir):] |
| 194 | else: |
| 195 | chroot_image = os.path.join( |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 196 | '~/trunk/src', located_image[len(real_src_dir):].lstrip('/')) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 197 | |
| 198 | # Check to see if cros flash will work for the remote machine. |
| 199 | CheckForCrosFlash(options.chromeos_root, options.remote, log_level) |
| 200 | |
| 201 | if local_image: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 202 | cros_flash_args = ['--board=%s' % board, '--clobber-stateful', |
| 203 | options.remote, chroot_image] |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 204 | else: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 205 | cros_flash_args = ['--board=%s' % board, '--clobber-stateful', |
| 206 | options.remote, image] |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 207 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 208 | command = ('cros flash %s' % ' '.join(cros_flash_args)) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 209 | |
| 210 | # Workaround for crosbug.com/35684. |
| 211 | os.chmod(misc.GetChromeOSKeyFile(options.chromeos_root), 0600) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 212 | if log_level == 'quiet': |
| 213 | l.LogOutput('CMD : %s' % command) |
| 214 | elif log_level == 'average': |
| 215 | cmd_executer.SetLogLevel('verbose') |
cmtice | b134008 | 2014-01-13 13:22:37 -0800 | [diff] [blame] | 216 | retval = cmd_executer.ChrootRunCommand(options.chromeos_root, |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 217 | command, |
| 218 | command_timeout=1800) |
cmtice | b134008 | 2014-01-13 13:22:37 -0800 | [diff] [blame] | 219 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 220 | retries = 0 |
| 221 | while retval != 0 and retries < 2: |
| 222 | retries += 1 |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 223 | if log_level == 'quiet': |
| 224 | l.LogOutput('Imaging failed. Retry # %d.' % retries) |
| 225 | l.LogOutput('CMD : %s' % command) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 226 | retval = cmd_executer.ChrootRunCommand(options.chromeos_root, |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 227 | command, |
| 228 | command_timeout=1800) |
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 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 238 | logger.GetLogger().LogFatalIf(retval, '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. |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 242 | retval = EnsureMachineUp(options.chromeos_root, options.remote, log_level) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 243 | |
| 244 | # If this is a non-local image, then the retval returned from |
| 245 | # EnsureMachineUp is the one that will be returned by this function; |
| 246 | # in that case, make sure the value in 'retval' is appropriate. |
| 247 | if not local_image and retval == True: |
| 248 | retval = 0 |
| 249 | else: |
| 250 | retval = 1 |
| 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, |
| 256 | checksum_file, checksum_file) |
| 257 | retval = cmd_executer.CrosRunCommand( |
| 258 | command, |
| 259 | chromeos_root=options.chromeos_root, |
| 260 | machine=options.remote) |
| 261 | logger.GetLogger().LogFatalIf(retval, 'Writing checksum failed.') |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 262 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 263 | successfully_imaged = VerifyChromeChecksum(options.chromeos_root, image, |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 264 | options.remote, log_level) |
| 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') |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 271 | return retval |
| 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): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [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' |
| 319 | ' --stateful_mountpt=%s' % (chromeos_root, image_dir, |
| 320 | 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 |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 335 | retval = cmd_executer.RunCommand(command) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 336 | logger.GetLogger().LogFatalIf(retval, 'Mount/unmount command failed!') |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 337 | return retval |
| 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) |
| 349 | MountImage(chromeos_root, |
| 350 | image, |
| 351 | rootfs_mp, |
| 352 | stateful_mp, |
| 353 | log_level, |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 354 | unmount=True) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 355 | return is_test_image |
| 356 | |
| 357 | |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 358 | def VerifyChromeChecksum(chromeos_root, image, remote, log_level): |
| 359 | cmd_executer = command_executer.GetCommandExecuter(log_level=log_level) |
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 | image_chrome_checksum = FileUtils().Md5File('%s/opt/google/chrome/chrome' % |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 364 | rootfs_mp, |
| 365 | log_level=log_level) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 366 | MountImage(chromeos_root, |
| 367 | image, |
| 368 | rootfs_mp, |
| 369 | stateful_mp, |
| 370 | log_level, |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 371 | unmount=True) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 372 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 373 | command = 'md5sum /opt/google/chrome/chrome' |
| 374 | [_, o, _] = cmd_executer.CrosRunCommandWOutput(command, |
| 375 | chromeos_root=chromeos_root, |
| 376 | machine=remote) |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 377 | device_chrome_checksum = o.split()[0] |
| 378 | if image_chrome_checksum.strip() == device_chrome_checksum.strip(): |
| 379 | return True |
| 380 | else: |
| 381 | return False |
| 382 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 383 | |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 384 | # Remount partition as writable. |
| 385 | # TODO: auto-detect if an image is built using --noenable_rootfs_verification. |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 386 | def TryRemountPartitionAsRW(chromeos_root, remote, log_level): |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 387 | l = logger.GetLogger() |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 388 | cmd_executer = command_executer.GetCommandExecuter(log_level=log_level) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 389 | command = 'sudo mount -o remount,rw /' |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 390 | retval = cmd_executer.CrosRunCommand(\ |
| 391 | command, chromeos_root=chromeos_root, machine=remote, terminated_timeout=10) |
| 392 | if retval: |
| 393 | ## Safely ignore. |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 394 | l.LogWarning('Failed to remount partition as rw, ' |
| 395 | 'probably the image was not built with ' |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 396 | "\"--noenable_rootfs_verification\", " |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 397 | 'you can safely ignore this.') |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 398 | else: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 399 | l.LogOutput('Re-mounted partition as writable.') |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 400 | |
Ahmad Sharif | 70de27b | 2011-06-15 17:51:24 -0700 | [diff] [blame] | 401 | |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 402 | def EnsureMachineUp(chromeos_root, remote, log_level): |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 403 | l = logger.GetLogger() |
cmtice | 1390924 | 2014-03-11 13:38:07 -0700 | [diff] [blame] | 404 | cmd_executer = command_executer.GetCommandExecuter(log_level=log_level) |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 405 | timeout = 600 |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 406 | magic = 'abcdefghijklmnopqrstuvwxyz' |
| 407 | command = 'echo %s' % magic |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 408 | start_time = time.time() |
| 409 | while True: |
| 410 | current_time = time.time() |
| 411 | if current_time - start_time > timeout: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 412 | l.LogError('Timeout of %ss reached. Machine still not up. Aborting.' % |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 413 | timeout) |
| 414 | return False |
| 415 | retval = cmd_executer.CrosRunCommand(command, |
| 416 | chromeos_root=chromeos_root, |
| 417 | machine=remote) |
| 418 | if not retval: |
| 419 | return True |
| 420 | |
| 421 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 422 | if __name__ == '__main__': |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 423 | retval = DoImage(sys.argv) |
| 424 | sys.exit(retval) |