blob: 562731a6e7a40ee74a427528f55ba36df117ab1d [file] [log] [blame]
Ahmad Sharif4467f002012-12-20 12:09:49 -08001#!/usr/bin/python
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
Luis Lozanof2a3ef42015-12-15 13:49:30 -08009__author__ = 'asharif@google.com (Ahmad Sharif)'
Ahmad Sharif70de27b2011-06-15 17:51:24 -070010
11import filecmp
12import glob
13import optparse
14import os
Ahmad Shariff395c262012-10-09 17:48:09 -070015import re
Ahmad Sharif70de27b2011-06-15 17:51:24 -070016import shutil
17import sys
18import tempfile
Ahmad Sharif4467f002012-12-20 12:09:49 -080019import time
20
Ahmad Sharif70de27b2011-06-15 17:51:24 -070021from utils import command_executer
cmticee5bc63b2015-05-27 16:59:37 -070022from utils import locks
Ahmad Sharif70de27b2011-06-15 17:51:24 -070023from utils import logger
Ahmad Shariffd356fb2012-05-07 12:02:16 -070024from utils import misc
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080025from utils.file_utils import FileUtils
Ahmad Sharif70de27b2011-06-15 17:51:24 -070026
Luis Lozanof2a3ef42015-12-15 13:49:30 -080027checksum_file = '/usr/local/osimage_checksum_file'
28lock_file = '/tmp/image_chromeos_lock/image_chromeos_lock'
29
Ahmad Sharif70de27b2011-06-15 17:51:24 -070030
31def Usage(parser, message):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080032 print 'ERROR: ' + message
Ahmad Sharif70de27b2011-06-15 17:51:24 -070033 parser.print_help()
34 sys.exit(0)
35
Ahmad Shariff395c262012-10-09 17:48:09 -070036
cmtice13909242014-03-11 13:38:07 -070037def CheckForCrosFlash(chromeos_root, remote, log_level):
38 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
cmtice0cc4e772014-01-30 15:52:37 -080039
Luis Lozano54db5382015-05-20 15:57:19 -070040 # 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 Shen96d936c2015-03-25 12:03:12 -070045 logger.GetLogger().LogFatalIf(
Luis Lozanof2a3ef42015-12-15 13:49:30 -080046 retval == 255, 'Failed ssh to %s (for checking cherrypy)' % remote)
Luis Lozano54db5382015-05-20 15:57:19 -070047 logger.GetLogger().LogFatalIf(
48 retval != 0, "Failed to find cherrypy or ctypes on remote '{}', "
Luis Lozanof2a3ef42015-12-15 13:49:30 -080049 'cros flash cannot work.'.format(remote))
Luis Lozano54db5382015-05-20 15:57:19 -070050
cmtice0cc4e772014-01-30 15:52:37 -080051
Ahmad Sharif4467f002012-12-20 12:09:49 -080052def DoImage(argv):
Han Shenba649282015-08-05 17:19:55 -070053 """Image ChromeOS."""
Ahmad Sharif4467f002012-12-20 12:09:49 -080054
Ahmad Sharif70de27b2011-06-15 17:51:24 -070055 parser = optparse.OptionParser()
Luis Lozanof2a3ef42015-12-15 13:49:30 -080056 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 Sharif70de27b2011-06-15 17:51:24 -070070 default=False,
Luis Lozanof2a3ef42015-12-15 13:49:30 -080071 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 '
cmtice13909242014-03-11 13:38:07 -070086 "'quiet', 'average', and 'verbose'.")
Luis Lozanof2a3ef42015-12-15 13:49:30 -080087 parser.add_option('-a', '--image_args', dest='image_args')
Ahmad Sharif70de27b2011-06-15 17:51:24 -070088
89 options = parser.parse_args(argv[1:])[0]
90
cmtice13909242014-03-11 13:38:07 -070091 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 Sharif70de27b2011-06-15 17:51:24 -0700100 if options.chromeos_root is None:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800101 Usage(parser, '--chromeos_root must be set')
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700102
103 if options.remote is None:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800104 Usage(parser, '--remote must be set')
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700105
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 Shariffd356fb2012-05-07 12:02:16 -0700114 images_dir = misc.GetImageDir(options.chromeos_root, board)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800115 image = os.path.join(images_dir, 'latest', 'chromiumos_test_image.bin')
Ahmad Shariffd356fb2012-05-07 12:02:16 -0700116 if not os.path.exists(image):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800117 image = os.path.join(images_dir, 'latest', 'chromiumos_image.bin')
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700118 else:
119 image = options.image
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800120 if image.find('xbuddy://') < 0:
cmtice0cc4e772014-01-30 15:52:37 -0800121 image = os.path.expanduser(image)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700122
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800123 if image.find('xbuddy://') < 0:
cmtice0cc4e772014-01-30 15:52:37 -0800124 image = os.path.realpath(image)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700125
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800126 if not os.path.exists(image) and image.find('xbuddy://') < 0:
127 Usage(parser, 'Image file: ' + image + ' does not exist!')
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700128
cmticee5bc63b2015-05-27 16:59:37 -0700129 try:
130 should_unlock = False
131 if not options.no_lock:
132 try:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800133 status = locks.AcquireLock(
134 list(options.remote.split()), options.chromeos_root)
cmticee5bc63b2015-05-27 16:59:37 -0700135 should_unlock = True
136 except Exception as e:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800137 raise Exception('Error acquiring machine: %s' % str(e))
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700138
cmticee5bc63b2015-05-27 16:59:37 -0700139 reimage = False
140 local_image = False
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800141 if image.find('xbuddy://') < 0:
cmticee5bc63b2015-05-27 16:59:37 -0700142 local_image = True
143 image_checksum = FileUtils().Md5File(image, log_level=log_level)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700144
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800145 command = 'cat ' + checksum_file
Luis Lozano036c9232015-12-10 10:47:01 -0800146 retval, device_checksum, _ = cmd_executer.CrosRunCommandWOutput(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800147 command,
148 chromeos_root=options.chromeos_root,
149 machine=options.remote)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700150
cmticee5bc63b2015-05-27 16:59:37 -0700151 device_checksum = device_checksum.strip()
152 image_checksum = str(image_checksum)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700153
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800154 l.LogOutput('Image checksum: ' + image_checksum)
155 l.LogOutput('Device checksum: ' + device_checksum)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700156
cmticee5bc63b2015-05-27 16:59:37 -0700157 if image_checksum != device_checksum:
158 [found, located_image] = LocateOrCopyImage(options.chromeos_root,
159 image,
160 board=board)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700161
cmticee5bc63b2015-05-27 16:59:37 -0700162 reimage = True
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800163 l.LogOutput('Checksums do not match. Re-imaging...')
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700164
cmticee5bc63b2015-05-27 16:59:37 -0700165 is_test_image = IsImageModdedForTest(options.chromeos_root,
166 located_image, log_level)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700167
cmticee5bc63b2015-05-27 16:59:37 -0700168 if not is_test_image and not options.force:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800169 logger.GetLogger().LogFatal('Have to pass --force to image a non-test'
170 ' image!')
cmtice0cc4e772014-01-30 15:52:37 -0800171 else:
cmticee5bc63b2015-05-27 16:59:37 -0700172 reimage = True
173 found = True
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800174 l.LogOutput('Using non-local image; Re-imaging...')
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700175
cmticee5bc63b2015-05-27 16:59:37 -0700176 if reimage:
177 # If the device has /tmp mounted as noexec, image_to_live.sh can fail.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800178 command = 'mount -o remount,rw,exec /tmp'
cmticee5bc63b2015-05-27 16:59:37 -0700179 cmd_executer.CrosRunCommand(command,
180 chromeos_root=options.chromeos_root,
181 machine=options.remote)
cmticeb1340082014-01-13 13:22:37 -0800182
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800183 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')
cmticee5bc63b2015-05-27 16:59:37 -0700187 if local_image:
188 if located_image.find(real_src_dir) != 0:
189 if located_image.find(real_chroot_dir) != 0:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800190 raise Exception('Located image: %s not in chromeos_root: %s' %
cmticee5bc63b2015-05-27 16:59:37 -0700191 (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 Lozanof2a3ef42015-12-15 13:49:30 -0800196 '~/trunk/src', located_image[len(real_src_dir):].lstrip('/'))
cmticee5bc63b2015-05-27 16:59:37 -0700197
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 Lozanof2a3ef42015-12-15 13:49:30 -0800202 cros_flash_args = ['--board=%s' % board, '--clobber-stateful',
203 options.remote, chroot_image]
cmticee5bc63b2015-05-27 16:59:37 -0700204 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800205 cros_flash_args = ['--board=%s' % board, '--clobber-stateful',
206 options.remote, image]
cmticee5bc63b2015-05-27 16:59:37 -0700207
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800208 command = ('cros flash %s' % ' '.join(cros_flash_args))
cmticee5bc63b2015-05-27 16:59:37 -0700209
210 # Workaround for crosbug.com/35684.
211 os.chmod(misc.GetChromeOSKeyFile(options.chromeos_root), 0600)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800212 if log_level == 'quiet':
213 l.LogOutput('CMD : %s' % command)
214 elif log_level == 'average':
215 cmd_executer.SetLogLevel('verbose')
cmticeb1340082014-01-13 13:22:37 -0800216 retval = cmd_executer.ChrootRunCommand(options.chromeos_root,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800217 command,
218 command_timeout=1800)
cmticeb1340082014-01-13 13:22:37 -0800219
cmticee5bc63b2015-05-27 16:59:37 -0700220 retries = 0
221 while retval != 0 and retries < 2:
222 retries += 1
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800223 if log_level == 'quiet':
224 l.LogOutput('Imaging failed. Retry # %d.' % retries)
225 l.LogOutput('CMD : %s' % command)
cmticee5bc63b2015-05-27 16:59:37 -0700226 retval = cmd_executer.ChrootRunCommand(options.chromeos_root,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800227 command,
228 command_timeout=1800)
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
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800238 logger.GetLogger().LogFatalIf(retval, '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.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800242 retval = EnsureMachineUp(options.chromeos_root, options.remote, log_level)
cmticee5bc63b2015-05-27 16:59:37 -0700243
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 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,
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.')
cmticee5bc63b2015-05-27 16:59:37 -0700262
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800263 successfully_imaged = VerifyChromeChecksum(options.chromeos_root, image,
cmticee5bc63b2015-05-27 16:59:37 -0700264 options.remote, log_level)
265 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')
cmticee5bc63b2015-05-27 16:59:37 -0700271 return retval
272 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):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800296 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'
319 ' --stateful_mountpt=%s' % (chromeos_root, image_dir,
320 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
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700335 retval = cmd_executer.RunCommand(command)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800336 logger.GetLogger().LogFatalIf(retval, 'Mount/unmount command failed!')
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700337 return retval
338
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)
349 MountImage(chromeos_root,
350 image,
351 rootfs_mp,
352 stateful_mp,
353 log_level,
cmtice13909242014-03-11 13:38:07 -0700354 unmount=True)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700355 return is_test_image
356
357
cmtice13909242014-03-11 13:38:07 -0700358def VerifyChromeChecksum(chromeos_root, image, remote, log_level):
359 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800360 rootfs_mp = tempfile.mkdtemp()
361 stateful_mp = tempfile.mkdtemp()
cmtice13909242014-03-11 13:38:07 -0700362 MountImage(chromeos_root, image, rootfs_mp, stateful_mp, log_level)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800363 image_chrome_checksum = FileUtils().Md5File('%s/opt/google/chrome/chrome' %
cmtice13909242014-03-11 13:38:07 -0700364 rootfs_mp,
365 log_level=log_level)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800366 MountImage(chromeos_root,
367 image,
368 rootfs_mp,
369 stateful_mp,
370 log_level,
cmtice13909242014-03-11 13:38:07 -0700371 unmount=True)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700372
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800373 command = 'md5sum /opt/google/chrome/chrome'
374 [_, o, _] = cmd_executer.CrosRunCommandWOutput(command,
375 chromeos_root=chromeos_root,
376 machine=remote)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700377 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 Lozanof2a3ef42015-12-15 13:49:30 -0800383
Luis Lozanof81680c2013-03-15 14:44:13 -0700384# Remount partition as writable.
385# TODO: auto-detect if an image is built using --noenable_rootfs_verification.
cmtice13909242014-03-11 13:38:07 -0700386def TryRemountPartitionAsRW(chromeos_root, remote, log_level):
Luis Lozanof81680c2013-03-15 14:44:13 -0700387 l = logger.GetLogger()
cmtice13909242014-03-11 13:38:07 -0700388 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800389 command = 'sudo mount -o remount,rw /'
Luis Lozanof81680c2013-03-15 14:44:13 -0700390 retval = cmd_executer.CrosRunCommand(\
391 command, chromeos_root=chromeos_root, machine=remote, terminated_timeout=10)
392 if retval:
393 ## Safely ignore.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800394 l.LogWarning('Failed to remount partition as rw, '
395 'probably the image was not built with '
Luis Lozanof81680c2013-03-15 14:44:13 -0700396 "\"--noenable_rootfs_verification\", "
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800397 'you can safely ignore this.')
Luis Lozanof81680c2013-03-15 14:44:13 -0700398 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800399 l.LogOutput('Re-mounted partition as writable.')
Luis Lozanof81680c2013-03-15 14:44:13 -0700400
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700401
cmtice13909242014-03-11 13:38:07 -0700402def EnsureMachineUp(chromeos_root, remote, log_level):
Ahmad Sharif4467f002012-12-20 12:09:49 -0800403 l = logger.GetLogger()
cmtice13909242014-03-11 13:38:07 -0700404 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
Ahmad Sharif4467f002012-12-20 12:09:49 -0800405 timeout = 600
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800406 magic = 'abcdefghijklmnopqrstuvwxyz'
407 command = 'echo %s' % magic
Ahmad Sharif4467f002012-12-20 12:09:49 -0800408 start_time = time.time()
409 while True:
410 current_time = time.time()
411 if current_time - start_time > timeout:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800412 l.LogError('Timeout of %ss reached. Machine still not up. Aborting.' %
Ahmad Sharif4467f002012-12-20 12:09:49 -0800413 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 Lozanof2a3ef42015-12-15 13:49:30 -0800422if __name__ == '__main__':
cmticee5bc63b2015-05-27 16:59:37 -0700423 retval = DoImage(sys.argv)
424 sys.exit(retval)