blob: f28998dc468fe9e7183cd47c9529b5aef261c985 [file] [log] [blame]
Caroline Tice88272d42016-01-13 09:48:29 -08001#!/usr/bin/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 Tice88272d42016-01-13 09:48:29 -080044 ret = cmd_executer.CrosRunCommand(command,
45 chromeos_root=chromeos_root,
46 machine=remote)
Han Shen96d936c2015-03-25 12:03:12 -070047 logger.GetLogger().LogFatalIf(
Caroline Tice88272d42016-01-13 09:48:29 -080048 ret == 255, 'Failed ssh to %s (for checking cherrypy)' % remote)
Luis Lozano54db5382015-05-20 15:57:19 -070049 logger.GetLogger().LogFatalIf(
Caroline Tice88272d42016-01-13 09:48:29 -080050 ret != 0, "Failed to find cherrypy or ctypes on remote '{}', "
Luis Lozanof2a3ef42015-12-15 13:49:30 -080051 'cros flash cannot work.'.format(remote))
Luis Lozano54db5382015-05-20 15:57:19 -070052
cmtice0cc4e772014-01-30 15:52:37 -080053
Ahmad Sharif4467f002012-12-20 12:09:49 -080054def DoImage(argv):
Han Shenba649282015-08-05 17:19:55 -070055 """Image ChromeOS."""
Ahmad Sharif4467f002012-12-20 12:09:49 -080056
Caroline Tice88272d42016-01-13 09:48:29 -080057 parser = argparse.ArgumentParser()
58 parser.add_argument('-c',
59 '--chromeos_root',
60 dest='chromeos_root',
61 help='Target directory for ChromeOS installation.')
62 parser.add_argument('-r', '--remote', dest='remote', help='Target device.')
63 parser.add_argument('-i', '--image', dest='image', help='Image binary file.')
64 parser.add_argument('-b',
65 '--board',
66 dest='board',
67 help='Target board override.')
68 parser.add_argument('-f',
69 '--force',
70 dest='force',
71 action='store_true',
72 default=False,
73 help='Force an image even if it is non-test.')
74 parser.add_argument('-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('-l',
84 '--logging_level',
85 dest='log_level',
86 default='verbose',
87 help='Amount of logging to be used. Valid levels are '
88 "'quiet', 'average', and 'verbose'.")
89 parser.add_argument('-a', '--image_args', dest='image_args')
Ahmad Sharif70de27b2011-06-15 17:51:24 -070090
Caroline Tice88272d42016-01-13 09:48:29 -080091 options = parser.parse_args(argv[1:])
Ahmad Sharif70de27b2011-06-15 17:51:24 -070092
cmtice13909242014-03-11 13:38:07 -070093 if not options.log_level in command_executer.LOG_LEVEL:
94 Usage(parser, "--logging_level must be 'quiet', 'average' or 'verbose'")
95 else:
96 log_level = options.log_level
97
98 # Common initializations
99 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
100 l = logger.GetLogger()
101
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700102 if options.chromeos_root is None:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800103 Usage(parser, '--chromeos_root must be set')
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700104
105 if options.remote is None:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800106 Usage(parser, '--remote must be set')
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700107
108 options.chromeos_root = os.path.expanduser(options.chromeos_root)
109
110 if options.board is None:
111 board = cmd_executer.CrosLearnBoard(options.chromeos_root, options.remote)
112 else:
113 board = options.board
114
115 if options.image is None:
Ahmad Shariffd356fb2012-05-07 12:02:16 -0700116 images_dir = misc.GetImageDir(options.chromeos_root, board)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800117 image = os.path.join(images_dir, 'latest', 'chromiumos_test_image.bin')
Ahmad Shariffd356fb2012-05-07 12:02:16 -0700118 if not os.path.exists(image):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800119 image = os.path.join(images_dir, 'latest', 'chromiumos_image.bin')
David Sharpa20e0302016-01-25 16:00:02 -0800120 is_xbuddy_image = False
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700121 else:
122 image = options.image
David Sharpa20e0302016-01-25 16:00:02 -0800123 is_xbuddy_image = image.startswith('xbuddy://')
124 if not is_xbuddy_image:
cmtice0cc4e772014-01-30 15:52:37 -0800125 image = os.path.expanduser(image)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700126
David Sharpa20e0302016-01-25 16:00:02 -0800127 if not is_xbuddy_image:
cmtice0cc4e772014-01-30 15:52:37 -0800128 image = os.path.realpath(image)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700129
David Sharpa20e0302016-01-25 16:00:02 -0800130 if not os.path.exists(image) and not is_xbuddy_image:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800131 Usage(parser, 'Image file: ' + image + ' does not exist!')
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700132
cmticee5bc63b2015-05-27 16:59:37 -0700133 try:
134 should_unlock = False
135 if not options.no_lock:
136 try:
Caroline Tice88272d42016-01-13 09:48:29 -0800137 _ = locks.AcquireLock(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800138 list(options.remote.split()), options.chromeos_root)
cmticee5bc63b2015-05-27 16:59:37 -0700139 should_unlock = True
140 except Exception as e:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800141 raise Exception('Error acquiring machine: %s' % str(e))
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700142
cmticee5bc63b2015-05-27 16:59:37 -0700143 reimage = False
144 local_image = False
David Sharpa20e0302016-01-25 16:00:02 -0800145 if not is_xbuddy_image:
cmticee5bc63b2015-05-27 16:59:37 -0700146 local_image = True
147 image_checksum = FileUtils().Md5File(image, log_level=log_level)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700148
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800149 command = 'cat ' + checksum_file
Caroline Tice88272d42016-01-13 09:48:29 -0800150 ret, device_checksum, _ = cmd_executer.CrosRunCommandWOutput(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800151 command,
152 chromeos_root=options.chromeos_root,
153 machine=options.remote)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700154
cmticee5bc63b2015-05-27 16:59:37 -0700155 device_checksum = device_checksum.strip()
156 image_checksum = str(image_checksum)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700157
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800158 l.LogOutput('Image checksum: ' + image_checksum)
159 l.LogOutput('Device checksum: ' + device_checksum)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700160
cmticee5bc63b2015-05-27 16:59:37 -0700161 if image_checksum != device_checksum:
162 [found, located_image] = LocateOrCopyImage(options.chromeos_root,
163 image,
164 board=board)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700165
cmticee5bc63b2015-05-27 16:59:37 -0700166 reimage = True
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800167 l.LogOutput('Checksums do not match. Re-imaging...')
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700168
cmticee5bc63b2015-05-27 16:59:37 -0700169 is_test_image = IsImageModdedForTest(options.chromeos_root,
170 located_image, log_level)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700171
cmticee5bc63b2015-05-27 16:59:37 -0700172 if not is_test_image and not options.force:
Caroline Tice88272d42016-01-13 09:48:29 -0800173 logger.GetLogger().LogFatal('Have to pass --force to image a '
174 'non-test image!')
cmtice0cc4e772014-01-30 15:52:37 -0800175 else:
cmticee5bc63b2015-05-27 16:59:37 -0700176 reimage = True
177 found = True
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800178 l.LogOutput('Using non-local image; Re-imaging...')
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700179
cmticee5bc63b2015-05-27 16:59:37 -0700180 if reimage:
181 # If the device has /tmp mounted as noexec, image_to_live.sh can fail.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800182 command = 'mount -o remount,rw,exec /tmp'
cmticee5bc63b2015-05-27 16:59:37 -0700183 cmd_executer.CrosRunCommand(command,
184 chromeos_root=options.chromeos_root,
185 machine=options.remote)
cmticeb1340082014-01-13 13:22:37 -0800186
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800187 real_src_dir = os.path.join(
188 os.path.realpath(options.chromeos_root), 'src')
189 real_chroot_dir = os.path.join(
190 os.path.realpath(options.chromeos_root), 'chroot')
cmticee5bc63b2015-05-27 16:59:37 -0700191 if local_image:
192 if located_image.find(real_src_dir) != 0:
193 if located_image.find(real_chroot_dir) != 0:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800194 raise Exception('Located image: %s not in chromeos_root: %s' %
cmticee5bc63b2015-05-27 16:59:37 -0700195 (located_image, options.chromeos_root))
196 else:
197 chroot_image = located_image[len(real_chroot_dir):]
198 else:
199 chroot_image = os.path.join(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800200 '~/trunk/src', located_image[len(real_src_dir):].lstrip('/'))
cmticee5bc63b2015-05-27 16:59:37 -0700201
202 # Check to see if cros flash will work for the remote machine.
203 CheckForCrosFlash(options.chromeos_root, options.remote, log_level)
204
David Sharpa20e0302016-01-25 16:00:02 -0800205 cros_flash_args = ['cros', 'flash', '--board=%s' % board,
206 '--clobber-stateful', options.remote]
cmticee5bc63b2015-05-27 16:59:37 -0700207 if local_image:
David Sharpa20e0302016-01-25 16:00:02 -0800208 cros_flash_args.append(chroot_image)
cmticee5bc63b2015-05-27 16:59:37 -0700209 else:
David Sharpa20e0302016-01-25 16:00:02 -0800210 cros_flash_args.append(image)
cmticee5bc63b2015-05-27 16:59:37 -0700211
David Sharpa20e0302016-01-25 16:00:02 -0800212 command = ' '.join(cros_flash_args)
cmticee5bc63b2015-05-27 16:59:37 -0700213
214 # Workaround for crosbug.com/35684.
215 os.chmod(misc.GetChromeOSKeyFile(options.chromeos_root), 0600)
cmticeb1340082014-01-13 13:22:37 -0800216
David Sharpa20e0302016-01-25 16:00:02 -0800217 if log_level == 'average':
218 cmd_executer.SetLogLevel('verbose')
cmticee5bc63b2015-05-27 16:59:37 -0700219 retries = 0
David Sharpa20e0302016-01-25 16:00:02 -0800220 while True:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800221 if log_level == 'quiet':
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800222 l.LogOutput('CMD : %s' % command)
Caroline Tice88272d42016-01-13 09:48:29 -0800223 ret = cmd_executer.ChrootRunCommand(options.chromeos_root,
224 command,
225 command_timeout=1800)
David Sharpa20e0302016-01-25 16:00:02 -0800226 if ret == 0 or retries >= 2:
227 break
228 retries += 1
229 if log_level == 'quiet':
230 l.LogOutput('Imaging failed. Retry # %d.' % retries)
cmtice13909242014-03-11 13:38:07 -0700231
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800232 if log_level == 'average':
cmticee5bc63b2015-05-27 16:59:37 -0700233 cmd_executer.SetLogLevel(log_level)
cmtice0cc4e772014-01-30 15:52:37 -0800234
cmticee5bc63b2015-05-27 16:59:37 -0700235 if found == False:
236 temp_dir = os.path.dirname(located_image)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800237 l.LogOutput('Deleting temp image dir: %s' % temp_dir)
cmticee5bc63b2015-05-27 16:59:37 -0700238 shutil.rmtree(temp_dir)
239
Caroline Tice88272d42016-01-13 09:48:29 -0800240 logger.GetLogger().LogFatalIf(ret, 'Image command failed')
cmticee5bc63b2015-05-27 16:59:37 -0700241
242 # Unfortunately cros_image_to_target.py sometimes returns early when the
243 # machine isn't fully up yet.
Caroline Tice88272d42016-01-13 09:48:29 -0800244 ret = EnsureMachineUp(options.chromeos_root, options.remote, log_level)
cmticee5bc63b2015-05-27 16:59:37 -0700245
Caroline Tice88272d42016-01-13 09:48:29 -0800246 # If this is a non-local image, then the ret returned from
cmticee5bc63b2015-05-27 16:59:37 -0700247 # EnsureMachineUp is the one that will be returned by this function;
Caroline Tice88272d42016-01-13 09:48:29 -0800248 # in that case, make sure the value in 'ret' is appropriate.
249 if not local_image and ret == True:
250 ret = 0
cmticee5bc63b2015-05-27 16:59:37 -0700251 else:
Caroline Tice88272d42016-01-13 09:48:29 -0800252 ret = 1
cmticee5bc63b2015-05-27 16:59:37 -0700253
254 if local_image:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800255 if log_level == 'average':
256 l.LogOutput('Verifying image.')
257 command = 'echo %s > %s && chmod -w %s' % (image_checksum,
Caroline Tice88272d42016-01-13 09:48:29 -0800258 checksum_file,
259 checksum_file)
260 ret = cmd_executer.CrosRunCommand(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800261 command,
262 chromeos_root=options.chromeos_root,
263 machine=options.remote)
Caroline Tice88272d42016-01-13 09:48:29 -0800264 logger.GetLogger().LogFatalIf(ret, 'Writing checksum failed.')
cmticee5bc63b2015-05-27 16:59:37 -0700265
Caroline Tice88272d42016-01-13 09:48:29 -0800266 successfully_imaged = VerifyChromeChecksum(options.chromeos_root,
267 image, options.remote,
268 log_level)
cmticee5bc63b2015-05-27 16:59:37 -0700269 logger.GetLogger().LogFatalIf(not successfully_imaged,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800270 'Image verification failed!')
cmticee5bc63b2015-05-27 16:59:37 -0700271 TryRemountPartitionAsRW(options.chromeos_root, options.remote,
272 log_level)
273 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800274 l.LogOutput('Checksums match. Skipping reimage')
Caroline Tice88272d42016-01-13 09:48:29 -0800275 return ret
cmticee5bc63b2015-05-27 16:59:37 -0700276 finally:
277 if should_unlock:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800278 locks.ReleaseLock(list(options.remote.split()), options.chromeos_root)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700279
280
281def LocateOrCopyImage(chromeos_root, image, board=None):
282 l = logger.GetLogger()
283 if board is None:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800284 board_glob = '*'
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700285 else:
286 board_glob = board
287
288 chromeos_root_realpath = os.path.realpath(chromeos_root)
289 image = os.path.realpath(image)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800290
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800291 if image.startswith('%s/' % chromeos_root_realpath):
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700292 return [True, image]
293
294 # First search within the existing build dirs for any matching files.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800295 images_glob = ('%s/src/build/images/%s/*/*.bin' % (chromeos_root_realpath,
296 board_glob))
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700297 images_list = glob.glob(images_glob)
298 for potential_image in images_list:
299 if filecmp.cmp(potential_image, image):
Caroline Tice88272d42016-01-13 09:48:29 -0800300 l.LogOutput('Found matching image %s in chromeos_root.' %
301 potential_image)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700302 return [True, potential_image]
cmtice13909242014-03-11 13:38:07 -0700303 # We did not find an image. Copy it in the src dir and return the copied
304 # file.
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700305 if board is None:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800306 board = ''
307 base_dir = ('%s/src/build/images/%s' % (chromeos_root_realpath, board))
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700308 if not os.path.isdir(base_dir):
309 os.makedirs(base_dir)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800310 temp_dir = tempfile.mkdtemp(prefix='%s/tmp' % base_dir)
311 new_image = '%s/%s' % (temp_dir, os.path.basename(image))
312 l.LogOutput('No matching image found. Copying %s to %s' % (image, new_image))
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700313 shutil.copyfile(image, new_image)
314 return [False, new_image]
315
316
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800317def GetImageMountCommand(chromeos_root, image, rootfs_mp, stateful_mp):
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700318 image_dir = os.path.dirname(image)
319 image_file = os.path.basename(image)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800320 mount_command = ('cd %s/src/scripts &&'
321 './mount_gpt_image.sh --from=%s --image=%s'
322 ' --safe --read_only'
323 ' --rootfs_mountpt=%s'
324 ' --stateful_mountpt=%s' % (chromeos_root, image_dir,
325 image_file, rootfs_mp,
326 stateful_mp))
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700327 return mount_command
328
329
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800330def MountImage(chromeos_root,
331 image,
332 rootfs_mp,
333 stateful_mp,
334 log_level,
cmtice13909242014-03-11 13:38:07 -0700335 unmount=False):
336 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800337 command = GetImageMountCommand(chromeos_root, image, rootfs_mp, stateful_mp)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700338 if unmount:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800339 command = '%s --unmount' % command
Caroline Tice88272d42016-01-13 09:48:29 -0800340 ret = cmd_executer.RunCommand(command)
341 logger.GetLogger().LogFatalIf(ret, 'Mount/unmount command failed!')
342 return ret
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700343
344
cmtice13909242014-03-11 13:38:07 -0700345def IsImageModdedForTest(chromeos_root, image, log_level):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800346 if log_level != 'verbose':
347 log_level = 'quiet'
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800348 rootfs_mp = tempfile.mkdtemp()
349 stateful_mp = tempfile.mkdtemp()
cmtice13909242014-03-11 13:38:07 -0700350 MountImage(chromeos_root, image, rootfs_mp, stateful_mp, log_level)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800351 lsb_release_file = os.path.join(rootfs_mp, 'etc/lsb-release')
Ahmad Shariff395c262012-10-09 17:48:09 -0700352 lsb_release_contents = open(lsb_release_file).read()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800353 is_test_image = re.search('test', lsb_release_contents, re.IGNORECASE)
354 MountImage(chromeos_root,
355 image,
356 rootfs_mp,
357 stateful_mp,
358 log_level,
cmtice13909242014-03-11 13:38:07 -0700359 unmount=True)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700360 return is_test_image
361
362
cmtice13909242014-03-11 13:38:07 -0700363def VerifyChromeChecksum(chromeos_root, image, remote, log_level):
364 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800365 rootfs_mp = tempfile.mkdtemp()
366 stateful_mp = tempfile.mkdtemp()
cmtice13909242014-03-11 13:38:07 -0700367 MountImage(chromeos_root, image, rootfs_mp, stateful_mp, log_level)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800368 image_chrome_checksum = FileUtils().Md5File('%s/opt/google/chrome/chrome' %
cmtice13909242014-03-11 13:38:07 -0700369 rootfs_mp,
370 log_level=log_level)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800371 MountImage(chromeos_root,
372 image,
373 rootfs_mp,
374 stateful_mp,
375 log_level,
cmtice13909242014-03-11 13:38:07 -0700376 unmount=True)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700377
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800378 command = 'md5sum /opt/google/chrome/chrome'
379 [_, o, _] = cmd_executer.CrosRunCommandWOutput(command,
380 chromeos_root=chromeos_root,
381 machine=remote)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700382 device_chrome_checksum = o.split()[0]
383 if image_chrome_checksum.strip() == device_chrome_checksum.strip():
384 return True
385 else:
386 return False
387
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800388
Luis Lozanof81680c2013-03-15 14:44:13 -0700389# Remount partition as writable.
390# TODO: auto-detect if an image is built using --noenable_rootfs_verification.
cmtice13909242014-03-11 13:38:07 -0700391def TryRemountPartitionAsRW(chromeos_root, remote, log_level):
Luis Lozanof81680c2013-03-15 14:44:13 -0700392 l = logger.GetLogger()
cmtice13909242014-03-11 13:38:07 -0700393 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800394 command = 'sudo mount -o remount,rw /'
Caroline Tice88272d42016-01-13 09:48:29 -0800395 ret = cmd_executer.CrosRunCommand(\
396 command, chromeos_root=chromeos_root, machine=remote,
397 terminated_timeout=10)
398 if ret:
Luis Lozanof81680c2013-03-15 14:44:13 -0700399 ## Safely ignore.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800400 l.LogWarning('Failed to remount partition as rw, '
401 'probably the image was not built with '
Luis Lozanof81680c2013-03-15 14:44:13 -0700402 "\"--noenable_rootfs_verification\", "
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800403 'you can safely ignore this.')
Luis Lozanof81680c2013-03-15 14:44:13 -0700404 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800405 l.LogOutput('Re-mounted partition as writable.')
Luis Lozanof81680c2013-03-15 14:44:13 -0700406
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700407
cmtice13909242014-03-11 13:38:07 -0700408def EnsureMachineUp(chromeos_root, remote, log_level):
Ahmad Sharif4467f002012-12-20 12:09:49 -0800409 l = logger.GetLogger()
cmtice13909242014-03-11 13:38:07 -0700410 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
Ahmad Sharif4467f002012-12-20 12:09:49 -0800411 timeout = 600
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800412 magic = 'abcdefghijklmnopqrstuvwxyz'
413 command = 'echo %s' % magic
Ahmad Sharif4467f002012-12-20 12:09:49 -0800414 start_time = time.time()
415 while True:
416 current_time = time.time()
417 if current_time - start_time > timeout:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800418 l.LogError('Timeout of %ss reached. Machine still not up. Aborting.' %
Ahmad Sharif4467f002012-12-20 12:09:49 -0800419 timeout)
420 return False
Caroline Tice88272d42016-01-13 09:48:29 -0800421 ret = cmd_executer.CrosRunCommand(command,
422 chromeos_root=chromeos_root,
423 machine=remote)
424 if not ret:
Ahmad Sharif4467f002012-12-20 12:09:49 -0800425 return True
426
427
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800428if __name__ == '__main__':
cmticee5bc63b2015-05-27 16:59:37 -0700429 retval = DoImage(sys.argv)
430 sys.exit(retval)