blob: 0ea6d390716b7fc1af61448e9eb4e7b00244b73e [file] [log] [blame]
Caroline Ticef6ef4392017-04-06 17:16:05 -07001#!/usr/bin/env python2
Ahmad Sharif70de27b2011-06-15 17:51:24 -07002#
3# Copyright 2011 Google Inc. All Rights Reserved.
Ahmad Sharif70de27b2011-06-15 17:51:24 -07004"""Script to image a ChromeOS device.
5
6This script images a remote ChromeOS device with a specific image."
7"""
8
Caroline Tice88272d42016-01-13 09:48:29 -08009from __future__ import print_function
10
Luis Lozanof2a3ef42015-12-15 13:49:30 -080011__author__ = 'asharif@google.com (Ahmad Sharif)'
Ahmad Sharif70de27b2011-06-15 17:51:24 -070012
Caroline Tice88272d42016-01-13 09:48:29 -080013import argparse
Ahmad Sharif70de27b2011-06-15 17:51:24 -070014import filecmp
15import glob
Ahmad Sharif70de27b2011-06-15 17:51:24 -070016import os
Ahmad Shariff395c262012-10-09 17:48:09 -070017import re
Ahmad Sharif70de27b2011-06-15 17:51:24 -070018import shutil
19import sys
20import tempfile
Ahmad Sharif4467f002012-12-20 12:09:49 -080021import time
22
Caroline Tice88272d42016-01-13 09:48:29 -080023from cros_utils import command_executer
24from cros_utils import locks
25from cros_utils import logger
26from cros_utils import misc
27from cros_utils.file_utils import FileUtils
Ahmad Sharif70de27b2011-06-15 17:51:24 -070028
Luis Lozanof2a3ef42015-12-15 13:49:30 -080029checksum_file = '/usr/local/osimage_checksum_file'
30lock_file = '/tmp/image_chromeos_lock/image_chromeos_lock'
31
Ahmad Sharif70de27b2011-06-15 17:51:24 -070032
33def Usage(parser, message):
Caroline Tice88272d42016-01-13 09:48:29 -080034 print('ERROR: %s' % message)
Ahmad Sharif70de27b2011-06-15 17:51:24 -070035 parser.print_help()
36 sys.exit(0)
37
Ahmad Shariff395c262012-10-09 17:48:09 -070038
cmtice13909242014-03-11 13:38:07 -070039def CheckForCrosFlash(chromeos_root, remote, log_level):
40 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
cmtice0cc4e772014-01-30 15:52:37 -080041
Luis Lozano54db5382015-05-20 15:57:19 -070042 # Check to see if remote machine has cherrypy, ctypes
43 command = "python -c 'import cherrypy, ctypes'"
Caroline Ticef6ef4392017-04-06 17:16:05 -070044 ret = cmd_executer.CrosRunCommand(
45 command, chromeos_root=chromeos_root, machine=remote)
Han Shen96d936c2015-03-25 12:03:12 -070046 logger.GetLogger().LogFatalIf(
Caroline Tice88272d42016-01-13 09:48:29 -080047 ret == 255, 'Failed ssh to %s (for checking cherrypy)' % remote)
Luis Lozano54db5382015-05-20 15:57:19 -070048 logger.GetLogger().LogFatalIf(
Caroline Tice88272d42016-01-13 09:48:29 -080049 ret != 0, "Failed to find cherrypy or ctypes on remote '{}', "
Luis Lozanof2a3ef42015-12-15 13:49:30 -080050 'cros flash cannot work.'.format(remote))
Luis Lozano54db5382015-05-20 15:57:19 -070051
cmtice0cc4e772014-01-30 15:52:37 -080052
Ahmad Sharif4467f002012-12-20 12:09:49 -080053def DoImage(argv):
Han Shenba649282015-08-05 17:19:55 -070054 """Image ChromeOS."""
Ahmad Sharif4467f002012-12-20 12:09:49 -080055
Caroline Tice88272d42016-01-13 09:48:29 -080056 parser = argparse.ArgumentParser()
Caroline Ticef6ef4392017-04-06 17:16:05 -070057 parser.add_argument(
58 '-c',
59 '--chromeos_root',
60 dest='chromeos_root',
61 help='Target directory for ChromeOS installation.')
Caroline Tice88272d42016-01-13 09:48:29 -080062 parser.add_argument('-r', '--remote', dest='remote', help='Target device.')
63 parser.add_argument('-i', '--image', dest='image', help='Image binary file.')
Caroline Ticef6ef4392017-04-06 17:16:05 -070064 parser.add_argument(
65 '-b', '--board', dest='board', help='Target board override.')
66 parser.add_argument(
67 '-f',
68 '--force',
69 dest='force',
70 action='store_true',
71 default=False,
72 help='Force an image even if it is non-test.')
73 parser.add_argument(
74 '-n',
75 '--no_lock',
76 dest='no_lock',
77 default=False,
78 action='store_true',
79 help='Do not attempt to lock remote before imaging. '
80 'This option should only be used in cases where the '
81 'exclusive lock has already been acquired (e.g. in '
82 'a script that calls this one).')
83 parser.add_argument(
84 '-l',
85 '--logging_level',
86 dest='log_level',
87 default='verbose',
88 help='Amount of logging to be used. Valid levels are '
89 "'quiet', 'average', and 'verbose'.")
Caroline Tice88272d42016-01-13 09:48:29 -080090 parser.add_argument('-a', '--image_args', dest='image_args')
Ahmad Sharif70de27b2011-06-15 17:51:24 -070091
Caroline Tice88272d42016-01-13 09:48:29 -080092 options = parser.parse_args(argv[1:])
Ahmad Sharif70de27b2011-06-15 17:51:24 -070093
cmtice13909242014-03-11 13:38:07 -070094 if not options.log_level in command_executer.LOG_LEVEL:
95 Usage(parser, "--logging_level must be 'quiet', 'average' or 'verbose'")
96 else:
97 log_level = options.log_level
98
99 # Common initializations
100 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
101 l = logger.GetLogger()
102
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700103 if options.chromeos_root is None:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800104 Usage(parser, '--chromeos_root must be set')
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700105
106 if options.remote is None:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800107 Usage(parser, '--remote must be set')
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700108
109 options.chromeos_root = os.path.expanduser(options.chromeos_root)
110
111 if options.board is None:
112 board = cmd_executer.CrosLearnBoard(options.chromeos_root, options.remote)
113 else:
114 board = options.board
115
116 if options.image is None:
Ahmad Shariffd356fb2012-05-07 12:02:16 -0700117 images_dir = misc.GetImageDir(options.chromeos_root, board)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800118 image = os.path.join(images_dir, 'latest', 'chromiumos_test_image.bin')
Ahmad Shariffd356fb2012-05-07 12:02:16 -0700119 if not os.path.exists(image):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800120 image = os.path.join(images_dir, 'latest', 'chromiumos_image.bin')
David Sharpa20e0302016-01-25 16:00:02 -0800121 is_xbuddy_image = False
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700122 else:
123 image = options.image
David Sharpa20e0302016-01-25 16:00:02 -0800124 is_xbuddy_image = image.startswith('xbuddy://')
125 if not is_xbuddy_image:
cmtice0cc4e772014-01-30 15:52:37 -0800126 image = os.path.expanduser(image)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700127
David Sharpa20e0302016-01-25 16:00:02 -0800128 if not is_xbuddy_image:
cmtice0cc4e772014-01-30 15:52:37 -0800129 image = os.path.realpath(image)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700130
David Sharpa20e0302016-01-25 16:00:02 -0800131 if not os.path.exists(image) and not is_xbuddy_image:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800132 Usage(parser, 'Image file: ' + image + ' does not exist!')
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700133
cmticee5bc63b2015-05-27 16:59:37 -0700134 try:
135 should_unlock = False
136 if not options.no_lock:
137 try:
Caroline Tice88272d42016-01-13 09:48:29 -0800138 _ = locks.AcquireLock(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800139 list(options.remote.split()), options.chromeos_root)
cmticee5bc63b2015-05-27 16:59:37 -0700140 should_unlock = True
141 except Exception as e:
Caroline Tice9099a782016-07-22 16:28:12 -0700142 raise RuntimeError('Error acquiring machine: %s' % str(e))
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700143
cmticee5bc63b2015-05-27 16:59:37 -0700144 reimage = False
145 local_image = False
David Sharpa20e0302016-01-25 16:00:02 -0800146 if not is_xbuddy_image:
cmticee5bc63b2015-05-27 16:59:37 -0700147 local_image = True
148 image_checksum = FileUtils().Md5File(image, log_level=log_level)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700149
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800150 command = 'cat ' + checksum_file
Caroline Tice88272d42016-01-13 09:48:29 -0800151 ret, device_checksum, _ = cmd_executer.CrosRunCommandWOutput(
Caroline Ticef6ef4392017-04-06 17:16:05 -0700152 command, chromeos_root=options.chromeos_root, machine=options.remote)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700153
cmticee5bc63b2015-05-27 16:59:37 -0700154 device_checksum = device_checksum.strip()
155 image_checksum = str(image_checksum)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700156
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800157 l.LogOutput('Image checksum: ' + image_checksum)
158 l.LogOutput('Device checksum: ' + device_checksum)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700159
cmticee5bc63b2015-05-27 16:59:37 -0700160 if image_checksum != device_checksum:
Caroline Ticef6ef4392017-04-06 17:16:05 -0700161 [found, located_image] = LocateOrCopyImage(
162 options.chromeos_root, image, board=board)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700163
cmticee5bc63b2015-05-27 16:59:37 -0700164 reimage = True
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800165 l.LogOutput('Checksums do not match. Re-imaging...')
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700166
cmticee5bc63b2015-05-27 16:59:37 -0700167 is_test_image = IsImageModdedForTest(options.chromeos_root,
168 located_image, log_level)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700169
cmticee5bc63b2015-05-27 16:59:37 -0700170 if not is_test_image and not options.force:
Caroline Tice88272d42016-01-13 09:48:29 -0800171 logger.GetLogger().LogFatal('Have to pass --force to image a '
172 'non-test image!')
cmtice0cc4e772014-01-30 15:52:37 -0800173 else:
cmticee5bc63b2015-05-27 16:59:37 -0700174 reimage = True
175 found = True
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800176 l.LogOutput('Using non-local image; Re-imaging...')
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700177
cmticee5bc63b2015-05-27 16:59:37 -0700178 if reimage:
179 # If the device has /tmp mounted as noexec, image_to_live.sh can fail.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800180 command = 'mount -o remount,rw,exec /tmp'
Caroline Ticef6ef4392017-04-06 17:16:05 -0700181 cmd_executer.CrosRunCommand(
182 command, chromeos_root=options.chromeos_root, machine=options.remote)
cmticeb1340082014-01-13 13:22:37 -0800183
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800184 real_src_dir = os.path.join(
185 os.path.realpath(options.chromeos_root), 'src')
186 real_chroot_dir = os.path.join(
187 os.path.realpath(options.chromeos_root), 'chroot')
cmticee5bc63b2015-05-27 16:59:37 -0700188 if local_image:
189 if located_image.find(real_src_dir) != 0:
190 if located_image.find(real_chroot_dir) != 0:
Caroline Tice9099a782016-07-22 16:28:12 -0700191 raise RuntimeError('Located image: %s not in chromeos_root: %s' %
192 (located_image, options.chromeos_root))
cmticee5bc63b2015-05-27 16:59:37 -0700193 else:
194 chroot_image = located_image[len(real_chroot_dir):]
195 else:
196 chroot_image = os.path.join(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800197 '~/trunk/src', located_image[len(real_src_dir):].lstrip('/'))
cmticee5bc63b2015-05-27 16:59:37 -0700198
199 # Check to see if cros flash will work for the remote machine.
200 CheckForCrosFlash(options.chromeos_root, options.remote, log_level)
201
Caroline Ticef6ef4392017-04-06 17:16:05 -0700202 cros_flash_args = [
203 'cros', 'flash', '--board=%s' % board, '--clobber-stateful',
204 options.remote
205 ]
cmticee5bc63b2015-05-27 16:59:37 -0700206 if local_image:
David Sharpa20e0302016-01-25 16:00:02 -0800207 cros_flash_args.append(chroot_image)
cmticee5bc63b2015-05-27 16:59:37 -0700208 else:
David Sharpa20e0302016-01-25 16:00:02 -0800209 cros_flash_args.append(image)
cmticee5bc63b2015-05-27 16:59:37 -0700210
David Sharpa20e0302016-01-25 16:00:02 -0800211 command = ' '.join(cros_flash_args)
cmticee5bc63b2015-05-27 16:59:37 -0700212
213 # Workaround for crosbug.com/35684.
214 os.chmod(misc.GetChromeOSKeyFile(options.chromeos_root), 0600)
cmticeb1340082014-01-13 13:22:37 -0800215
David Sharpa20e0302016-01-25 16:00:02 -0800216 if log_level == 'average':
217 cmd_executer.SetLogLevel('verbose')
cmticee5bc63b2015-05-27 16:59:37 -0700218 retries = 0
David Sharpa20e0302016-01-25 16:00:02 -0800219 while True:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800220 if log_level == 'quiet':
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800221 l.LogOutput('CMD : %s' % command)
Caroline Ticef6ef4392017-04-06 17:16:05 -0700222 ret = cmd_executer.ChrootRunCommand(
223 options.chromeos_root, command, command_timeout=1800)
David Sharpa20e0302016-01-25 16:00:02 -0800224 if ret == 0 or retries >= 2:
225 break
226 retries += 1
227 if log_level == 'quiet':
228 l.LogOutput('Imaging failed. Retry # %d.' % retries)
cmtice13909242014-03-11 13:38:07 -0700229
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800230 if log_level == 'average':
cmticee5bc63b2015-05-27 16:59:37 -0700231 cmd_executer.SetLogLevel(log_level)
cmtice0cc4e772014-01-30 15:52:37 -0800232
cmticee5bc63b2015-05-27 16:59:37 -0700233 if found == False:
234 temp_dir = os.path.dirname(located_image)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800235 l.LogOutput('Deleting temp image dir: %s' % temp_dir)
cmticee5bc63b2015-05-27 16:59:37 -0700236 shutil.rmtree(temp_dir)
237
Caroline Tice88272d42016-01-13 09:48:29 -0800238 logger.GetLogger().LogFatalIf(ret, 'Image command failed')
cmticee5bc63b2015-05-27 16:59:37 -0700239
240 # Unfortunately cros_image_to_target.py sometimes returns early when the
241 # machine isn't fully up yet.
Caroline Tice88272d42016-01-13 09:48:29 -0800242 ret = EnsureMachineUp(options.chromeos_root, options.remote, log_level)
cmticee5bc63b2015-05-27 16:59:37 -0700243
Caroline Tice88272d42016-01-13 09:48:29 -0800244 # If this is a non-local image, then the ret returned from
cmticee5bc63b2015-05-27 16:59:37 -0700245 # EnsureMachineUp is the one that will be returned by this function;
Caroline Tice88272d42016-01-13 09:48:29 -0800246 # in that case, make sure the value in 'ret' is appropriate.
247 if not local_image and ret == True:
248 ret = 0
cmticee5bc63b2015-05-27 16:59:37 -0700249 else:
Caroline Tice88272d42016-01-13 09:48:29 -0800250 ret = 1
cmticee5bc63b2015-05-27 16:59:37 -0700251
252 if local_image:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800253 if log_level == 'average':
254 l.LogOutput('Verifying image.')
255 command = 'echo %s > %s && chmod -w %s' % (image_checksum,
Caroline Ticef6ef4392017-04-06 17:16:05 -0700256 checksum_file, checksum_file)
Caroline Tice88272d42016-01-13 09:48:29 -0800257 ret = cmd_executer.CrosRunCommand(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800258 command,
259 chromeos_root=options.chromeos_root,
260 machine=options.remote)
Caroline Tice88272d42016-01-13 09:48:29 -0800261 logger.GetLogger().LogFatalIf(ret, 'Writing checksum failed.')
cmticee5bc63b2015-05-27 16:59:37 -0700262
Caroline Ticef6ef4392017-04-06 17:16:05 -0700263 successfully_imaged = VerifyChromeChecksum(options.chromeos_root, image,
264 options.remote, log_level)
cmticee5bc63b2015-05-27 16:59:37 -0700265 logger.GetLogger().LogFatalIf(not successfully_imaged,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800266 'Image verification failed!')
cmticee5bc63b2015-05-27 16:59:37 -0700267 TryRemountPartitionAsRW(options.chromeos_root, options.remote,
268 log_level)
269 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800270 l.LogOutput('Checksums match. Skipping reimage')
Caroline Tice88272d42016-01-13 09:48:29 -0800271 return ret
cmticee5bc63b2015-05-27 16:59:37 -0700272 finally:
273 if should_unlock:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800274 locks.ReleaseLock(list(options.remote.split()), options.chromeos_root)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700275
276
277def LocateOrCopyImage(chromeos_root, image, board=None):
278 l = logger.GetLogger()
279 if board is None:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800280 board_glob = '*'
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700281 else:
282 board_glob = board
283
284 chromeos_root_realpath = os.path.realpath(chromeos_root)
285 image = os.path.realpath(image)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800286
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800287 if image.startswith('%s/' % chromeos_root_realpath):
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700288 return [True, image]
289
290 # First search within the existing build dirs for any matching files.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800291 images_glob = ('%s/src/build/images/%s/*/*.bin' % (chromeos_root_realpath,
292 board_glob))
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700293 images_list = glob.glob(images_glob)
294 for potential_image in images_list:
295 if filecmp.cmp(potential_image, image):
Caroline Ticef6ef4392017-04-06 17:16:05 -0700296 l.LogOutput('Found matching image %s in chromeos_root.' % potential_image)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700297 return [True, potential_image]
cmtice13909242014-03-11 13:38:07 -0700298 # We did not find an image. Copy it in the src dir and return the copied
299 # file.
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700300 if board is None:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800301 board = ''
302 base_dir = ('%s/src/build/images/%s' % (chromeos_root_realpath, board))
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700303 if not os.path.isdir(base_dir):
304 os.makedirs(base_dir)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800305 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 Sharif70de27b2011-06-15 17:51:24 -0700308 shutil.copyfile(image, new_image)
309 return [False, new_image]
310
311
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800312def GetImageMountCommand(chromeos_root, image, rootfs_mp, stateful_mp):
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700313 image_dir = os.path.dirname(image)
314 image_file = os.path.basename(image)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800315 mount_command = ('cd %s/src/scripts &&'
316 './mount_gpt_image.sh --from=%s --image=%s'
317 ' --safe --read_only'
318 ' --rootfs_mountpt=%s'
Caroline Ticef6ef4392017-04-06 17:16:05 -0700319 ' --stateful_mountpt=%s' %
320 (chromeos_root, image_dir, image_file, rootfs_mp,
321 stateful_mp))
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700322 return mount_command
323
324
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800325def MountImage(chromeos_root,
326 image,
327 rootfs_mp,
328 stateful_mp,
329 log_level,
cmtice13909242014-03-11 13:38:07 -0700330 unmount=False):
331 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800332 command = GetImageMountCommand(chromeos_root, image, rootfs_mp, stateful_mp)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700333 if unmount:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800334 command = '%s --unmount' % command
Caroline Tice88272d42016-01-13 09:48:29 -0800335 ret = cmd_executer.RunCommand(command)
336 logger.GetLogger().LogFatalIf(ret, 'Mount/unmount command failed!')
337 return ret
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700338
339
cmtice13909242014-03-11 13:38:07 -0700340def IsImageModdedForTest(chromeos_root, image, log_level):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800341 if log_level != 'verbose':
342 log_level = 'quiet'
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800343 rootfs_mp = tempfile.mkdtemp()
344 stateful_mp = tempfile.mkdtemp()
cmtice13909242014-03-11 13:38:07 -0700345 MountImage(chromeos_root, image, rootfs_mp, stateful_mp, log_level)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800346 lsb_release_file = os.path.join(rootfs_mp, 'etc/lsb-release')
Ahmad Shariff395c262012-10-09 17:48:09 -0700347 lsb_release_contents = open(lsb_release_file).read()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800348 is_test_image = re.search('test', lsb_release_contents, re.IGNORECASE)
Caroline Ticef6ef4392017-04-06 17:16:05 -0700349 MountImage(
350 chromeos_root, image, rootfs_mp, stateful_mp, log_level, unmount=True)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700351 return is_test_image
352
353
cmtice13909242014-03-11 13:38:07 -0700354def VerifyChromeChecksum(chromeos_root, image, remote, log_level):
355 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800356 rootfs_mp = tempfile.mkdtemp()
357 stateful_mp = tempfile.mkdtemp()
cmtice13909242014-03-11 13:38:07 -0700358 MountImage(chromeos_root, image, rootfs_mp, stateful_mp, log_level)
Caroline Ticef6ef4392017-04-06 17:16:05 -0700359 image_chrome_checksum = FileUtils().Md5File(
360 '%s/opt/google/chrome/chrome' % rootfs_mp, log_level=log_level)
361 MountImage(
362 chromeos_root, image, rootfs_mp, stateful_mp, log_level, unmount=True)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700363
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800364 command = 'md5sum /opt/google/chrome/chrome'
Caroline Ticef6ef4392017-04-06 17:16:05 -0700365 [_, o, _] = cmd_executer.CrosRunCommandWOutput(
366 command, chromeos_root=chromeos_root, machine=remote)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700367 device_chrome_checksum = o.split()[0]
368 if image_chrome_checksum.strip() == device_chrome_checksum.strip():
369 return True
370 else:
371 return False
372
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800373
Luis Lozanof81680c2013-03-15 14:44:13 -0700374# Remount partition as writable.
375# TODO: auto-detect if an image is built using --noenable_rootfs_verification.
cmtice13909242014-03-11 13:38:07 -0700376def TryRemountPartitionAsRW(chromeos_root, remote, log_level):
Luis Lozanof81680c2013-03-15 14:44:13 -0700377 l = logger.GetLogger()
cmtice13909242014-03-11 13:38:07 -0700378 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800379 command = 'sudo mount -o remount,rw /'
Caroline Tice88272d42016-01-13 09:48:29 -0800380 ret = cmd_executer.CrosRunCommand(\
381 command, chromeos_root=chromeos_root, machine=remote,
382 terminated_timeout=10)
383 if ret:
Luis Lozanof81680c2013-03-15 14:44:13 -0700384 ## Safely ignore.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800385 l.LogWarning('Failed to remount partition as rw, '
386 'probably the image was not built with '
Luis Lozanof81680c2013-03-15 14:44:13 -0700387 "\"--noenable_rootfs_verification\", "
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800388 'you can safely ignore this.')
Luis Lozanof81680c2013-03-15 14:44:13 -0700389 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800390 l.LogOutput('Re-mounted partition as writable.')
Luis Lozanof81680c2013-03-15 14:44:13 -0700391
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700392
cmtice13909242014-03-11 13:38:07 -0700393def EnsureMachineUp(chromeos_root, remote, log_level):
Ahmad Sharif4467f002012-12-20 12:09:49 -0800394 l = logger.GetLogger()
cmtice13909242014-03-11 13:38:07 -0700395 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
Ahmad Sharif4467f002012-12-20 12:09:49 -0800396 timeout = 600
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800397 magic = 'abcdefghijklmnopqrstuvwxyz'
398 command = 'echo %s' % magic
Ahmad Sharif4467f002012-12-20 12:09:49 -0800399 start_time = time.time()
400 while True:
401 current_time = time.time()
402 if current_time - start_time > timeout:
Caroline Ticef6ef4392017-04-06 17:16:05 -0700403 l.LogError(
404 'Timeout of %ss reached. Machine still not up. Aborting.' % timeout)
Ahmad Sharif4467f002012-12-20 12:09:49 -0800405 return False
Caroline Ticef6ef4392017-04-06 17:16:05 -0700406 ret = cmd_executer.CrosRunCommand(
407 command, chromeos_root=chromeos_root, machine=remote)
Caroline Tice88272d42016-01-13 09:48:29 -0800408 if not ret:
Ahmad Sharif4467f002012-12-20 12:09:49 -0800409 return True
410
411
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800412if __name__ == '__main__':
cmticee5bc63b2015-05-27 16:59:37 -0700413 retval = DoImage(sys.argv)
414 sys.exit(retval)