blob: f65ad4d1ba17ed8324acbd820a1d1409fa1c4c2f [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
Manoj Gupta1282e842017-05-17 12:57:56 -070053def DisableCrosBeeps(chromeos_root, remote, log_level):
54 """Disable annoying chromebooks beeps after reboots."""
55 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
56
57 command = '/usr/share/vboot/bin/set_gbb_flags.sh 0x1'
58 logger.GetLogger().LogOutput('Trying to disable beeping.')
59
60 ret, o, _ = cmd_executer.CrosRunCommandWOutput(
61 command, chromeos_root=chromeos_root, machine=remote)
62 if ret != 0:
63 logger.GetLogger().LogOutput(o)
64 logger.GetLogger().LogOutput('Failed to disable beeps.')
65
66
Ahmad Sharif4467f002012-12-20 12:09:49 -080067def DoImage(argv):
Han Shenba649282015-08-05 17:19:55 -070068 """Image ChromeOS."""
Ahmad Sharif4467f002012-12-20 12:09:49 -080069
Caroline Tice88272d42016-01-13 09:48:29 -080070 parser = argparse.ArgumentParser()
Caroline Ticef6ef4392017-04-06 17:16:05 -070071 parser.add_argument(
72 '-c',
73 '--chromeos_root',
74 dest='chromeos_root',
75 help='Target directory for ChromeOS installation.')
Caroline Tice88272d42016-01-13 09:48:29 -080076 parser.add_argument('-r', '--remote', dest='remote', help='Target device.')
77 parser.add_argument('-i', '--image', dest='image', help='Image binary file.')
Caroline Ticef6ef4392017-04-06 17:16:05 -070078 parser.add_argument(
79 '-b', '--board', dest='board', help='Target board override.')
80 parser.add_argument(
81 '-f',
82 '--force',
83 dest='force',
84 action='store_true',
85 default=False,
86 help='Force an image even if it is non-test.')
87 parser.add_argument(
88 '-n',
89 '--no_lock',
90 dest='no_lock',
91 default=False,
92 action='store_true',
93 help='Do not attempt to lock remote before imaging. '
94 'This option should only be used in cases where the '
95 'exclusive lock has already been acquired (e.g. in '
96 'a script that calls this one).')
97 parser.add_argument(
98 '-l',
99 '--logging_level',
100 dest='log_level',
101 default='verbose',
102 help='Amount of logging to be used. Valid levels are '
103 "'quiet', 'average', and 'verbose'.")
Caroline Tice88272d42016-01-13 09:48:29 -0800104 parser.add_argument('-a', '--image_args', dest='image_args')
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700105
Caroline Tice88272d42016-01-13 09:48:29 -0800106 options = parser.parse_args(argv[1:])
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700107
cmtice13909242014-03-11 13:38:07 -0700108 if not options.log_level in command_executer.LOG_LEVEL:
109 Usage(parser, "--logging_level must be 'quiet', 'average' or 'verbose'")
110 else:
111 log_level = options.log_level
112
113 # Common initializations
114 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
115 l = logger.GetLogger()
116
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700117 if options.chromeos_root is None:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800118 Usage(parser, '--chromeos_root must be set')
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700119
120 if options.remote is None:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800121 Usage(parser, '--remote must be set')
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700122
123 options.chromeos_root = os.path.expanduser(options.chromeos_root)
124
125 if options.board is None:
126 board = cmd_executer.CrosLearnBoard(options.chromeos_root, options.remote)
127 else:
128 board = options.board
129
130 if options.image is None:
Ahmad Shariffd356fb2012-05-07 12:02:16 -0700131 images_dir = misc.GetImageDir(options.chromeos_root, board)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800132 image = os.path.join(images_dir, 'latest', 'chromiumos_test_image.bin')
Ahmad Shariffd356fb2012-05-07 12:02:16 -0700133 if not os.path.exists(image):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800134 image = os.path.join(images_dir, 'latest', 'chromiumos_image.bin')
David Sharpa20e0302016-01-25 16:00:02 -0800135 is_xbuddy_image = False
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700136 else:
137 image = options.image
David Sharpa20e0302016-01-25 16:00:02 -0800138 is_xbuddy_image = image.startswith('xbuddy://')
139 if not is_xbuddy_image:
cmtice0cc4e772014-01-30 15:52:37 -0800140 image = os.path.expanduser(image)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700141
David Sharpa20e0302016-01-25 16:00:02 -0800142 if not is_xbuddy_image:
cmtice0cc4e772014-01-30 15:52:37 -0800143 image = os.path.realpath(image)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700144
David Sharpa20e0302016-01-25 16:00:02 -0800145 if not os.path.exists(image) and not is_xbuddy_image:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800146 Usage(parser, 'Image file: ' + image + ' does not exist!')
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700147
cmticee5bc63b2015-05-27 16:59:37 -0700148 try:
149 should_unlock = False
150 if not options.no_lock:
151 try:
Caroline Tice88272d42016-01-13 09:48:29 -0800152 _ = locks.AcquireLock(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800153 list(options.remote.split()), options.chromeos_root)
cmticee5bc63b2015-05-27 16:59:37 -0700154 should_unlock = True
155 except Exception as e:
Caroline Tice9099a782016-07-22 16:28:12 -0700156 raise RuntimeError('Error acquiring machine: %s' % str(e))
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700157
cmticee5bc63b2015-05-27 16:59:37 -0700158 reimage = False
159 local_image = False
David Sharpa20e0302016-01-25 16:00:02 -0800160 if not is_xbuddy_image:
cmticee5bc63b2015-05-27 16:59:37 -0700161 local_image = True
162 image_checksum = FileUtils().Md5File(image, log_level=log_level)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700163
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800164 command = 'cat ' + checksum_file
Caroline Tice88272d42016-01-13 09:48:29 -0800165 ret, device_checksum, _ = cmd_executer.CrosRunCommandWOutput(
Caroline Ticef6ef4392017-04-06 17:16:05 -0700166 command, chromeos_root=options.chromeos_root, machine=options.remote)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700167
cmticee5bc63b2015-05-27 16:59:37 -0700168 device_checksum = device_checksum.strip()
169 image_checksum = str(image_checksum)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700170
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800171 l.LogOutput('Image checksum: ' + image_checksum)
172 l.LogOutput('Device checksum: ' + device_checksum)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700173
cmticee5bc63b2015-05-27 16:59:37 -0700174 if image_checksum != device_checksum:
Caroline Ticef6ef4392017-04-06 17:16:05 -0700175 [found, located_image] = LocateOrCopyImage(
176 options.chromeos_root, image, board=board)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700177
cmticee5bc63b2015-05-27 16:59:37 -0700178 reimage = True
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800179 l.LogOutput('Checksums do not match. Re-imaging...')
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700180
cmticee5bc63b2015-05-27 16:59:37 -0700181 is_test_image = IsImageModdedForTest(options.chromeos_root,
182 located_image, log_level)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700183
cmticee5bc63b2015-05-27 16:59:37 -0700184 if not is_test_image and not options.force:
Caroline Tice88272d42016-01-13 09:48:29 -0800185 logger.GetLogger().LogFatal('Have to pass --force to image a '
186 'non-test image!')
cmtice0cc4e772014-01-30 15:52:37 -0800187 else:
cmticee5bc63b2015-05-27 16:59:37 -0700188 reimage = True
189 found = True
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800190 l.LogOutput('Using non-local image; Re-imaging...')
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700191
cmticee5bc63b2015-05-27 16:59:37 -0700192 if reimage:
193 # If the device has /tmp mounted as noexec, image_to_live.sh can fail.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800194 command = 'mount -o remount,rw,exec /tmp'
Caroline Ticef6ef4392017-04-06 17:16:05 -0700195 cmd_executer.CrosRunCommand(
196 command, chromeos_root=options.chromeos_root, machine=options.remote)
cmticeb1340082014-01-13 13:22:37 -0800197
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800198 real_src_dir = os.path.join(
199 os.path.realpath(options.chromeos_root), 'src')
200 real_chroot_dir = os.path.join(
201 os.path.realpath(options.chromeos_root), 'chroot')
cmticee5bc63b2015-05-27 16:59:37 -0700202 if local_image:
203 if located_image.find(real_src_dir) != 0:
204 if located_image.find(real_chroot_dir) != 0:
Caroline Tice9099a782016-07-22 16:28:12 -0700205 raise RuntimeError('Located image: %s not in chromeos_root: %s' %
206 (located_image, options.chromeos_root))
cmticee5bc63b2015-05-27 16:59:37 -0700207 else:
208 chroot_image = located_image[len(real_chroot_dir):]
209 else:
210 chroot_image = os.path.join(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800211 '~/trunk/src', located_image[len(real_src_dir):].lstrip('/'))
cmticee5bc63b2015-05-27 16:59:37 -0700212
213 # Check to see if cros flash will work for the remote machine.
214 CheckForCrosFlash(options.chromeos_root, options.remote, log_level)
215
Manoj Gupta1282e842017-05-17 12:57:56 -0700216 # Disable the annoying chromebook beeps after reboot.
217 DisableCrosBeeps(options.chromeos_root, options.remote, log_level)
218
Caroline Ticef6ef4392017-04-06 17:16:05 -0700219 cros_flash_args = [
Manoj Gupta1282e842017-05-17 12:57:56 -0700220 'cros', 'flash',
221 '--board=%s' % board, '--clobber-stateful', options.remote
Caroline Ticef6ef4392017-04-06 17:16:05 -0700222 ]
cmticee5bc63b2015-05-27 16:59:37 -0700223 if local_image:
David Sharpa20e0302016-01-25 16:00:02 -0800224 cros_flash_args.append(chroot_image)
cmticee5bc63b2015-05-27 16:59:37 -0700225 else:
David Sharpa20e0302016-01-25 16:00:02 -0800226 cros_flash_args.append(image)
cmticee5bc63b2015-05-27 16:59:37 -0700227
David Sharpa20e0302016-01-25 16:00:02 -0800228 command = ' '.join(cros_flash_args)
cmticee5bc63b2015-05-27 16:59:37 -0700229
230 # Workaround for crosbug.com/35684.
231 os.chmod(misc.GetChromeOSKeyFile(options.chromeos_root), 0600)
cmticeb1340082014-01-13 13:22:37 -0800232
David Sharpa20e0302016-01-25 16:00:02 -0800233 if log_level == 'average':
234 cmd_executer.SetLogLevel('verbose')
cmticee5bc63b2015-05-27 16:59:37 -0700235 retries = 0
David Sharpa20e0302016-01-25 16:00:02 -0800236 while True:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800237 if log_level == 'quiet':
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800238 l.LogOutput('CMD : %s' % command)
Caroline Ticef6ef4392017-04-06 17:16:05 -0700239 ret = cmd_executer.ChrootRunCommand(
240 options.chromeos_root, command, command_timeout=1800)
David Sharpa20e0302016-01-25 16:00:02 -0800241 if ret == 0 or retries >= 2:
242 break
243 retries += 1
244 if log_level == 'quiet':
245 l.LogOutput('Imaging failed. Retry # %d.' % retries)
cmtice13909242014-03-11 13:38:07 -0700246
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800247 if log_level == 'average':
cmticee5bc63b2015-05-27 16:59:37 -0700248 cmd_executer.SetLogLevel(log_level)
cmtice0cc4e772014-01-30 15:52:37 -0800249
cmticee5bc63b2015-05-27 16:59:37 -0700250 if found == False:
251 temp_dir = os.path.dirname(located_image)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800252 l.LogOutput('Deleting temp image dir: %s' % temp_dir)
cmticee5bc63b2015-05-27 16:59:37 -0700253 shutil.rmtree(temp_dir)
254
Caroline Tice88272d42016-01-13 09:48:29 -0800255 logger.GetLogger().LogFatalIf(ret, 'Image command failed')
cmticee5bc63b2015-05-27 16:59:37 -0700256
257 # Unfortunately cros_image_to_target.py sometimes returns early when the
258 # machine isn't fully up yet.
Caroline Tice88272d42016-01-13 09:48:29 -0800259 ret = EnsureMachineUp(options.chromeos_root, options.remote, log_level)
cmticee5bc63b2015-05-27 16:59:37 -0700260
Caroline Tice88272d42016-01-13 09:48:29 -0800261 # If this is a non-local image, then the ret returned from
cmticee5bc63b2015-05-27 16:59:37 -0700262 # EnsureMachineUp is the one that will be returned by this function;
Caroline Tice88272d42016-01-13 09:48:29 -0800263 # in that case, make sure the value in 'ret' is appropriate.
264 if not local_image and ret == True:
265 ret = 0
cmticee5bc63b2015-05-27 16:59:37 -0700266 else:
Caroline Tice88272d42016-01-13 09:48:29 -0800267 ret = 1
cmticee5bc63b2015-05-27 16:59:37 -0700268
269 if local_image:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800270 if log_level == 'average':
271 l.LogOutput('Verifying image.')
272 command = 'echo %s > %s && chmod -w %s' % (image_checksum,
Caroline Ticef6ef4392017-04-06 17:16:05 -0700273 checksum_file, checksum_file)
Caroline Tice88272d42016-01-13 09:48:29 -0800274 ret = cmd_executer.CrosRunCommand(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800275 command,
276 chromeos_root=options.chromeos_root,
277 machine=options.remote)
Caroline Tice88272d42016-01-13 09:48:29 -0800278 logger.GetLogger().LogFatalIf(ret, 'Writing checksum failed.')
cmticee5bc63b2015-05-27 16:59:37 -0700279
Caroline Ticef6ef4392017-04-06 17:16:05 -0700280 successfully_imaged = VerifyChromeChecksum(options.chromeos_root, image,
281 options.remote, log_level)
cmticee5bc63b2015-05-27 16:59:37 -0700282 logger.GetLogger().LogFatalIf(not successfully_imaged,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800283 'Image verification failed!')
cmticee5bc63b2015-05-27 16:59:37 -0700284 TryRemountPartitionAsRW(options.chromeos_root, options.remote,
285 log_level)
286 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800287 l.LogOutput('Checksums match. Skipping reimage')
Caroline Tice88272d42016-01-13 09:48:29 -0800288 return ret
cmticee5bc63b2015-05-27 16:59:37 -0700289 finally:
290 if should_unlock:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800291 locks.ReleaseLock(list(options.remote.split()), options.chromeos_root)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700292
293
294def LocateOrCopyImage(chromeos_root, image, board=None):
295 l = logger.GetLogger()
296 if board is None:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800297 board_glob = '*'
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700298 else:
299 board_glob = board
300
301 chromeos_root_realpath = os.path.realpath(chromeos_root)
302 image = os.path.realpath(image)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800303
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800304 if image.startswith('%s/' % chromeos_root_realpath):
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700305 return [True, image]
306
307 # First search within the existing build dirs for any matching files.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800308 images_glob = ('%s/src/build/images/%s/*/*.bin' % (chromeos_root_realpath,
309 board_glob))
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700310 images_list = glob.glob(images_glob)
311 for potential_image in images_list:
312 if filecmp.cmp(potential_image, image):
Caroline Ticef6ef4392017-04-06 17:16:05 -0700313 l.LogOutput('Found matching image %s in chromeos_root.' % potential_image)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700314 return [True, potential_image]
cmtice13909242014-03-11 13:38:07 -0700315 # We did not find an image. Copy it in the src dir and return the copied
316 # file.
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700317 if board is None:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800318 board = ''
319 base_dir = ('%s/src/build/images/%s' % (chromeos_root_realpath, board))
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700320 if not os.path.isdir(base_dir):
321 os.makedirs(base_dir)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800322 temp_dir = tempfile.mkdtemp(prefix='%s/tmp' % base_dir)
323 new_image = '%s/%s' % (temp_dir, os.path.basename(image))
324 l.LogOutput('No matching image found. Copying %s to %s' % (image, new_image))
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700325 shutil.copyfile(image, new_image)
326 return [False, new_image]
327
328
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800329def GetImageMountCommand(chromeos_root, image, rootfs_mp, stateful_mp):
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700330 image_dir = os.path.dirname(image)
331 image_file = os.path.basename(image)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800332 mount_command = ('cd %s/src/scripts &&'
333 './mount_gpt_image.sh --from=%s --image=%s'
334 ' --safe --read_only'
335 ' --rootfs_mountpt=%s'
Caroline Ticef6ef4392017-04-06 17:16:05 -0700336 ' --stateful_mountpt=%s' %
337 (chromeos_root, image_dir, image_file, rootfs_mp,
338 stateful_mp))
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700339 return mount_command
340
341
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800342def MountImage(chromeos_root,
343 image,
344 rootfs_mp,
345 stateful_mp,
346 log_level,
cmtice13909242014-03-11 13:38:07 -0700347 unmount=False):
348 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800349 command = GetImageMountCommand(chromeos_root, image, rootfs_mp, stateful_mp)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700350 if unmount:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800351 command = '%s --unmount' % command
Caroline Tice88272d42016-01-13 09:48:29 -0800352 ret = cmd_executer.RunCommand(command)
353 logger.GetLogger().LogFatalIf(ret, 'Mount/unmount command failed!')
354 return ret
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700355
356
cmtice13909242014-03-11 13:38:07 -0700357def IsImageModdedForTest(chromeos_root, image, log_level):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800358 if log_level != 'verbose':
359 log_level = 'quiet'
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 lsb_release_file = os.path.join(rootfs_mp, 'etc/lsb-release')
Ahmad Shariff395c262012-10-09 17:48:09 -0700364 lsb_release_contents = open(lsb_release_file).read()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800365 is_test_image = re.search('test', lsb_release_contents, re.IGNORECASE)
Caroline Ticef6ef4392017-04-06 17:16:05 -0700366 MountImage(
367 chromeos_root, image, rootfs_mp, stateful_mp, log_level, unmount=True)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700368 return is_test_image
369
370
cmtice13909242014-03-11 13:38:07 -0700371def VerifyChromeChecksum(chromeos_root, image, remote, log_level):
372 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800373 rootfs_mp = tempfile.mkdtemp()
374 stateful_mp = tempfile.mkdtemp()
cmtice13909242014-03-11 13:38:07 -0700375 MountImage(chromeos_root, image, rootfs_mp, stateful_mp, log_level)
Caroline Ticef6ef4392017-04-06 17:16:05 -0700376 image_chrome_checksum = FileUtils().Md5File(
377 '%s/opt/google/chrome/chrome' % rootfs_mp, log_level=log_level)
378 MountImage(
379 chromeos_root, image, rootfs_mp, stateful_mp, log_level, unmount=True)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700380
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800381 command = 'md5sum /opt/google/chrome/chrome'
Caroline Ticef6ef4392017-04-06 17:16:05 -0700382 [_, o, _] = cmd_executer.CrosRunCommandWOutput(
383 command, chromeos_root=chromeos_root, machine=remote)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700384 device_chrome_checksum = o.split()[0]
385 if image_chrome_checksum.strip() == device_chrome_checksum.strip():
386 return True
387 else:
388 return False
389
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800390
Luis Lozanof81680c2013-03-15 14:44:13 -0700391# Remount partition as writable.
392# TODO: auto-detect if an image is built using --noenable_rootfs_verification.
cmtice13909242014-03-11 13:38:07 -0700393def TryRemountPartitionAsRW(chromeos_root, remote, log_level):
Luis Lozanof81680c2013-03-15 14:44:13 -0700394 l = logger.GetLogger()
cmtice13909242014-03-11 13:38:07 -0700395 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800396 command = 'sudo mount -o remount,rw /'
Caroline Tice88272d42016-01-13 09:48:29 -0800397 ret = cmd_executer.CrosRunCommand(\
398 command, chromeos_root=chromeos_root, machine=remote,
399 terminated_timeout=10)
400 if ret:
Luis Lozanof81680c2013-03-15 14:44:13 -0700401 ## Safely ignore.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800402 l.LogWarning('Failed to remount partition as rw, '
403 'probably the image was not built with '
Luis Lozanof81680c2013-03-15 14:44:13 -0700404 "\"--noenable_rootfs_verification\", "
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800405 'you can safely ignore this.')
Luis Lozanof81680c2013-03-15 14:44:13 -0700406 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800407 l.LogOutput('Re-mounted partition as writable.')
Luis Lozanof81680c2013-03-15 14:44:13 -0700408
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700409
cmtice13909242014-03-11 13:38:07 -0700410def EnsureMachineUp(chromeos_root, remote, log_level):
Ahmad Sharif4467f002012-12-20 12:09:49 -0800411 l = logger.GetLogger()
cmtice13909242014-03-11 13:38:07 -0700412 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
Ahmad Sharif4467f002012-12-20 12:09:49 -0800413 timeout = 600
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800414 magic = 'abcdefghijklmnopqrstuvwxyz'
415 command = 'echo %s' % magic
Ahmad Sharif4467f002012-12-20 12:09:49 -0800416 start_time = time.time()
417 while True:
418 current_time = time.time()
419 if current_time - start_time > timeout:
Caroline Ticef6ef4392017-04-06 17:16:05 -0700420 l.LogError(
421 'Timeout of %ss reached. Machine still not up. Aborting.' % timeout)
Ahmad Sharif4467f002012-12-20 12:09:49 -0800422 return False
Caroline Ticef6ef4392017-04-06 17:16:05 -0700423 ret = cmd_executer.CrosRunCommand(
424 command, chromeos_root=chromeos_root, machine=remote)
Caroline Tice88272d42016-01-13 09:48:29 -0800425 if not ret:
Ahmad Sharif4467f002012-12-20 12:09:49 -0800426 return True
427
428
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800429if __name__ == '__main__':
cmticee5bc63b2015-05-27 16:59:37 -0700430 retval = DoImage(sys.argv)
431 sys.exit(retval)