blob: d2f3ee003412ccb18f6d346b1d6429c125c4d268 [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.
4
5"""Script to image a ChromeOS device.
6
7This script images a remote ChromeOS device with a specific image."
8"""
9
10__author__ = "asharif@google.com (Ahmad Sharif)"
11
12import filecmp
13import glob
14import optparse
15import os
Ahmad Shariff395c262012-10-09 17:48:09 -070016import re
Ahmad Sharif70de27b2011-06-15 17:51:24 -070017import shutil
18import sys
19import tempfile
Ahmad Sharif4467f002012-12-20 12:09:49 -080020import time
21
Ahmad Sharif70de27b2011-06-15 17:51:24 -070022from utils import command_executer
23from 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
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080027checksum_file = "/usr/local/osimage_checksum_file"
Ahmad Sharif4467f002012-12-20 12:09:49 -080028lock_file = "/tmp/image_chromeos_lock/image_chromeos_lock"
Ahmad Sharif70de27b2011-06-15 17:51:24 -070029
30def Usage(parser, message):
31 print "ERROR: " + message
32 parser.print_help()
33 sys.exit(0)
34
Ahmad Shariff395c262012-10-09 17:48:09 -070035
cmtice13909242014-03-11 13:38:07 -070036def CheckForCrosFlash(chromeos_root, remote, log_level):
37 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
cmtice0cc4e772014-01-30 15:52:37 -080038
39 chroot_has_cros_flash = False
40 remote_has_cherrypy = False
41
42 # Check to see if chroot contains cros flash.
43 cros_flash_path = os.path.join(os.path.realpath(chromeos_root),
44 "chromite/cros/commands/cros_flash.py")
45
46 if os.path.exists(cros_flash_path):
47 chroot_has_cros_flash = True
48
49 # Check to see if remote machine has cherrypy.
Luis Lozanodd75bad2014-04-21 13:58:16 -070050 command = "python -c 'import cherrypy'"
51 retval = cmd_executer.CrosRunCommand (command,
52 chromeos_root=chromeos_root,
53 machine=remote)
Luis Lozano1e462d92014-04-18 17:49:29 -070054 logger.GetLogger().LogFatalIf(retval == 255, "Failed ssh to %s" % remote)
cmtice0cc4e772014-01-30 15:52:37 -080055 if retval == 0:
56 remote_has_cherrypy = True
57
58 return (chroot_has_cros_flash and remote_has_cherrypy)
59
Ahmad Sharif4467f002012-12-20 12:09:49 -080060def DoImage(argv):
Ahmad Sharif70de27b2011-06-15 17:51:24 -070061 """Build ChromeOS."""
Ahmad Sharif4467f002012-12-20 12:09:49 -080062
Ahmad Sharif70de27b2011-06-15 17:51:24 -070063 parser = optparse.OptionParser()
64 parser.add_option("-c", "--chromeos_root", dest="chromeos_root",
65 help="Target directory for ChromeOS installation.")
66 parser.add_option("-r", "--remote", dest="remote",
67 help="Target device.")
68 parser.add_option("-i", "--image", dest="image",
69 help="Image binary file.")
70 parser.add_option("-b", "--board", dest="board",
71 help="Target board override.")
72 parser.add_option("-f", "--force", dest="force",
73 action="store_true",
74 default=False,
75 help="Force an image even if it is non-test.")
cmtice13909242014-03-11 13:38:07 -070076 parser.add_option("-l", "--logging_level", dest="log_level",
77 default="verbose",
78 help="Amount of logging to be used. Valid levels are "
79 "'quiet', 'average', and 'verbose'.")
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080080 parser.add_option("-a",
Ahmad Sharif4467f002012-12-20 12:09:49 -080081 "--image_args",
82 dest="image_args")
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080083
Ahmad Sharif70de27b2011-06-15 17:51:24 -070084
85 options = parser.parse_args(argv[1:])[0]
86
cmtice13909242014-03-11 13:38:07 -070087 if not options.log_level in command_executer.LOG_LEVEL:
88 Usage(parser, "--logging_level must be 'quiet', 'average' or 'verbose'")
89 else:
90 log_level = options.log_level
91
92 # Common initializations
93 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
94 l = logger.GetLogger()
95
Ahmad Sharif70de27b2011-06-15 17:51:24 -070096 if options.chromeos_root is None:
97 Usage(parser, "--chromeos_root must be set")
98
99 if options.remote is None:
100 Usage(parser, "--remote must be set")
101
102 options.chromeos_root = os.path.expanduser(options.chromeos_root)
103
104 if options.board is None:
105 board = cmd_executer.CrosLearnBoard(options.chromeos_root, options.remote)
106 else:
107 board = options.board
108
109 if options.image is None:
Ahmad Shariffd356fb2012-05-07 12:02:16 -0700110 images_dir = misc.GetImageDir(options.chromeos_root, board)
111 image = os.path.join(images_dir,
112 "latest",
113 "chromiumos_test_image.bin")
114 if not os.path.exists(image):
115 image = os.path.join(images_dir,
116 "latest",
117 "chromiumos_image.bin")
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700118 else:
119 image = options.image
cmtice0cc4e772014-01-30 15:52:37 -0800120 if image.find("xbuddy://") < 0:
121 image = os.path.expanduser(image)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700122
cmtice0cc4e772014-01-30 15:52:37 -0800123 if image.find("xbuddy://") < 0:
124 image = os.path.realpath(image)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700125
cmtice0cc4e772014-01-30 15:52:37 -0800126 if not os.path.exists(image) and image.find("xbuddy://") < 0:
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700127 Usage(parser, "Image file: " + image + " does not exist!")
128
cmtice0cc4e772014-01-30 15:52:37 -0800129 reimage = False
130 local_image = False
131 if image.find("xbuddy://") < 0:
132 local_image = True
cmtice13909242014-03-11 13:38:07 -0700133 image_checksum = FileUtils().Md5File(image, log_level=log_level)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700134
cmtice0cc4e772014-01-30 15:52:37 -0800135 command = "cat " + checksum_file
136 retval, device_checksum, err = cmd_executer.CrosRunCommand(command,
137 return_output=True,
138 chromeos_root=options.chromeos_root,
139 machine=options.remote)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700140
cmtice0cc4e772014-01-30 15:52:37 -0800141 device_checksum = device_checksum.strip()
142 image_checksum = str(image_checksum)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700143
cmtice0cc4e772014-01-30 15:52:37 -0800144 l.LogOutput("Image checksum: " + image_checksum)
145 l.LogOutput("Device checksum: " + device_checksum)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700146
cmtice0cc4e772014-01-30 15:52:37 -0800147 if image_checksum != device_checksum:
148 [found, located_image] = LocateOrCopyImage(options.chromeos_root,
149 image,
150 board=board)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700151
cmtice0cc4e772014-01-30 15:52:37 -0800152 reimage = True
153 l.LogOutput("Checksums do not match. Re-imaging...")
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700154
cmtice0cc4e772014-01-30 15:52:37 -0800155 is_test_image = IsImageModdedForTest(options.chromeos_root,
cmtice13909242014-03-11 13:38:07 -0700156 located_image, log_level)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700157
cmtice0cc4e772014-01-30 15:52:37 -0800158 if not is_test_image and not options.force:
159 logger.GetLogger().LogFatal("Have to pass --force to image a non-test "
160 "image!")
161 else:
162 reimage = True
163 found = True
164 l.LogOutput("Using non-local image; Re-imaging...")
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700165
cmtice0cc4e772014-01-30 15:52:37 -0800166
167 if reimage:
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700168 # If the device has /tmp mounted as noexec, image_to_live.sh can fail.
169 command = "mount -o remount,rw,exec /tmp"
170 cmd_executer.CrosRunCommand(command,
171 chromeos_root=options.chromeos_root,
172 machine=options.remote)
173
Ahmad Sharif4467f002012-12-20 12:09:49 -0800174 real_src_dir = os.path.join(os.path.realpath(options.chromeos_root),
175 "src")
cmtice43f1a452014-04-04 13:15:06 -0700176 real_chroot_dir = os.path.join(os.path.realpath(options.chromeos_root),
177 "chroot")
cmtice0cc4e772014-01-30 15:52:37 -0800178 if local_image:
179 if located_image.find(real_src_dir) != 0:
cmtice43f1a452014-04-04 13:15:06 -0700180 if located_image.find(real_chroot_dir) != 0:
181 raise Exception("Located image: %s not in chromeos_root: %s" %
182 (located_image, options.chromeos_root))
183 else:
184 chroot_image = located_image[len(real_chroot_dir):]
185 else:
186 chroot_image = os.path.join(
187 "..",
188 located_image[len(real_src_dir):].lstrip("/"))
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700189
cmticefd06cca2014-01-29 14:21:44 -0800190 # Check to see if cros flash is in the chroot or not.
cmtice0cc4e772014-01-30 15:52:37 -0800191 use_cros_flash = CheckForCrosFlash (options.chromeos_root,
cmtice13909242014-03-11 13:38:07 -0700192 options.remote, log_level)
cmtice0cc4e772014-01-30 15:52:37 -0800193
194 if use_cros_flash:
cmticefd06cca2014-01-29 14:21:44 -0800195 # Use 'cros flash'
cmtice0cc4e772014-01-30 15:52:37 -0800196 if local_image:
197 cros_flash_args = ["--board=%s" % board,
198 "--clobber-stateful",
199 options.remote,
200 chroot_image]
201 else:
cmtice0cc4e772014-01-30 15:52:37 -0800202 cros_flash_args = ["--board=%s" % board,
203 "--clobber-stateful",
204 options.remote,
205 image]
cmticefd06cca2014-01-29 14:21:44 -0800206
207 command = ("cros flash %s" % " ".join(cros_flash_args))
cmtice0cc4e772014-01-30 15:52:37 -0800208 else:
Han Shenf9bb4a92015-03-24 09:54:32 -0700209 raise Exception(("Unable to find 'cros flash' in chroot;"
210 "chromeos tree is too old."))
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700211
Ahmad Sharif4467f002012-12-20 12:09:49 -0800212 # Workaround for crosbug.com/35684.
213 os.chmod(misc.GetChromeOSKeyFile(options.chromeos_root), 0600)
cmtice13909242014-03-11 13:38:07 -0700214 if log_level == "quiet":
215 l.LogOutput("CMD : %s" % command)
216 elif log_level == "average":
217 cmd_executer.SetLogLevel("verbose");
Ahmad Sharif4467f002012-12-20 12:09:49 -0800218 retval = cmd_executer.ChrootRunCommand(options.chromeos_root,
cmtice6de7f8f2014-03-14 14:08:21 -0700219 command, command_timeout=1800)
cmticeb1340082014-01-13 13:22:37 -0800220
221 retries = 0
222 while retval != 0 and retries < 2:
223 retries += 1
cmtice13909242014-03-11 13:38:07 -0700224 if log_level == "quiet":
225 l.LogOutput("Imaging failed. Retry # %d." % retries)
226 l.LogOutput("CMD : %s" % command)
cmticeb1340082014-01-13 13:22:37 -0800227 retval = cmd_executer.ChrootRunCommand(options.chromeos_root,
cmtice6de7f8f2014-03-14 14:08:21 -0700228 command, command_timeout=1800)
cmticeb1340082014-01-13 13:22:37 -0800229
cmtice13909242014-03-11 13:38:07 -0700230 if log_level == "average":
231 cmd_executer.SetLogLevel(log_level)
232
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700233 if found == False:
234 temp_dir = os.path.dirname(located_image)
235 l.LogOutput("Deleting temp image dir: %s" % temp_dir)
236 shutil.rmtree(temp_dir)
237
238 logger.GetLogger().LogFatalIf(retval, "Image command failed")
Ahmad Sharif4467f002012-12-20 12:09:49 -0800239
240 # Unfortunately cros_image_to_target.py sometimes returns early when the
241 # machine isn't fully up yet.
cmtice13909242014-03-11 13:38:07 -0700242 retval = EnsureMachineUp(options.chromeos_root, options.remote,
243 log_level)
Ahmad Sharif4467f002012-12-20 12:09:49 -0800244
cmtice0cc4e772014-01-30 15:52:37 -0800245 # If this is a non-local image, then the retval returned from
246 # EnsureMachineUp is the one that will be returned by this function;
247 # in that case, make sure the value in 'retval' is appropriate.
248 if not local_image and retval == True:
249 retval = 0
250 else:
251 retval = 1
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700252
cmtice0cc4e772014-01-30 15:52:37 -0800253 if local_image:
cmtice13909242014-03-11 13:38:07 -0700254 if log_level == "average":
255 l.LogOutput("Verifying image.")
cmtice0cc4e772014-01-30 15:52:37 -0800256 command = "echo %s > %s && chmod -w %s" % (image_checksum,
257 checksum_file,
258 checksum_file)
259 retval = cmd_executer.CrosRunCommand(command,
260 chromeos_root=options.chromeos_root,
261 machine=options.remote)
262 logger.GetLogger().LogFatalIf(retval, "Writing checksum failed.")
263
264 successfully_imaged = VerifyChromeChecksum(options.chromeos_root,
265 image,
cmtice13909242014-03-11 13:38:07 -0700266 options.remote, log_level)
cmtice0cc4e772014-01-30 15:52:37 -0800267 logger.GetLogger().LogFatalIf(not successfully_imaged,
268 "Image verification failed!")
cmtice13909242014-03-11 13:38:07 -0700269 TryRemountPartitionAsRW(options.chromeos_root, options.remote,
270 log_level)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700271 else:
272 l.LogOutput("Checksums match. Skipping reimage")
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700273 return retval
274
275
276def LocateOrCopyImage(chromeos_root, image, board=None):
277 l = logger.GetLogger()
278 if board is None:
279 board_glob = "*"
280 else:
281 board_glob = board
282
283 chromeos_root_realpath = os.path.realpath(chromeos_root)
284 image = os.path.realpath(image)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800285
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700286 if image.startswith("%s/" % chromeos_root_realpath):
287 return [True, image]
288
289 # First search within the existing build dirs for any matching files.
290 images_glob = ("%s/src/build/images/%s/*/*.bin" %
291 (chromeos_root_realpath,
292 board_glob))
293 images_list = glob.glob(images_glob)
294 for potential_image in images_list:
295 if filecmp.cmp(potential_image, image):
296 l.LogOutput("Found matching image %s in chromeos_root." % potential_image)
297 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:
301 board = ""
302 base_dir = ("%s/src/build/images/%s" %
303 (chromeos_root_realpath,
304 board))
305 if not os.path.isdir(base_dir):
306 os.makedirs(base_dir)
307 temp_dir = tempfile.mkdtemp(prefix="%s/tmp" % base_dir)
308 new_image = "%s/%s" % (temp_dir, os.path.basename(image))
309 l.LogOutput("No matching image found. Copying %s to %s" %
310 (image, new_image))
311 shutil.copyfile(image, new_image)
312 return [False, new_image]
313
314
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800315def GetImageMountCommand(chromeos_root, image, rootfs_mp, stateful_mp):
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700316 image_dir = os.path.dirname(image)
317 image_file = os.path.basename(image)
318 mount_command = ("cd %s/src/scripts &&"
319 "./mount_gpt_image.sh --from=%s --image=%s"
320 " --safe --read_only"
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800321 " --rootfs_mountpt=%s"
322 " --stateful_mountpt=%s" %
323 (chromeos_root, image_dir, image_file, rootfs_mp,
324 stateful_mp))
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700325 return mount_command
326
327
cmtice13909242014-03-11 13:38:07 -0700328def MountImage(chromeos_root, image, rootfs_mp, stateful_mp, log_level,
329 unmount=False):
330 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800331 command = GetImageMountCommand(chromeos_root, image, rootfs_mp, stateful_mp)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700332 if unmount:
333 command = "%s --unmount" % command
334 retval = cmd_executer.RunCommand(command)
335 logger.GetLogger().LogFatalIf(retval, "Mount/unmount command failed!")
336 return retval
337
338
cmtice13909242014-03-11 13:38:07 -0700339def IsImageModdedForTest(chromeos_root, image, log_level):
340 if log_level != "verbose":
341 log_level = "quiet"
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800342 rootfs_mp = tempfile.mkdtemp()
343 stateful_mp = tempfile.mkdtemp()
cmtice13909242014-03-11 13:38:07 -0700344 MountImage(chromeos_root, image, rootfs_mp, stateful_mp, log_level)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800345 lsb_release_file = os.path.join(rootfs_mp, "etc/lsb-release")
Ahmad Shariff395c262012-10-09 17:48:09 -0700346 lsb_release_contents = open(lsb_release_file).read()
347 is_test_image = re.search("test", lsb_release_contents, re.IGNORECASE)
cmtice13909242014-03-11 13:38:07 -0700348 MountImage(chromeos_root, image, rootfs_mp, stateful_mp, log_level,
349 unmount=True)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700350 return is_test_image
351
352
cmtice13909242014-03-11 13:38:07 -0700353def VerifyChromeChecksum(chromeos_root, image, remote, log_level):
354 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800355 rootfs_mp = tempfile.mkdtemp()
356 stateful_mp = tempfile.mkdtemp()
cmtice13909242014-03-11 13:38:07 -0700357 MountImage(chromeos_root, image, rootfs_mp, stateful_mp, log_level)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800358 image_chrome_checksum = FileUtils().Md5File("%s/opt/google/chrome/chrome" %
cmtice13909242014-03-11 13:38:07 -0700359 rootfs_mp,
360 log_level=log_level)
361 MountImage(chromeos_root, image, rootfs_mp, stateful_mp, log_level,
362 unmount=True)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700363
364 command = "md5sum /opt/google/chrome/chrome"
365 [r, o, e] = cmd_executer.CrosRunCommand(command,
366 return_output=True,
367 chromeos_root=chromeos_root,
368 machine=remote)
369 device_chrome_checksum = o.split()[0]
370 if image_chrome_checksum.strip() == device_chrome_checksum.strip():
371 return True
372 else:
373 return False
374
Luis Lozanof81680c2013-03-15 14:44:13 -0700375# Remount partition as writable.
376# TODO: auto-detect if an image is built using --noenable_rootfs_verification.
cmtice13909242014-03-11 13:38:07 -0700377def TryRemountPartitionAsRW(chromeos_root, remote, log_level):
Luis Lozanof81680c2013-03-15 14:44:13 -0700378 l = logger.GetLogger()
cmtice13909242014-03-11 13:38:07 -0700379 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
Luis Lozanof81680c2013-03-15 14:44:13 -0700380 command = "sudo mount -o remount,rw /"
381 retval = cmd_executer.CrosRunCommand(\
382 command, chromeos_root=chromeos_root, machine=remote, terminated_timeout=10)
383 if retval:
384 ## Safely ignore.
385 l.LogWarning("Failed to remount partition as rw, "
386 "probably the image was not built with "
387 "\"--noenable_rootfs_verification\", "
388 "you can safely ignore this.")
389 else:
390 l.LogOutput("Re-mounted partition as writable.")
391
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
397 magic = "abcdefghijklmnopqrstuvwxyz"
398 command = "echo %s" % magic
399 start_time = time.time()
400 while True:
401 current_time = time.time()
402 if current_time - start_time > timeout:
403 l.LogError("Timeout of %ss reached. Machine still not up. Aborting." %
404 timeout)
405 return False
406 retval = cmd_executer.CrosRunCommand(command,
407 chromeos_root=chromeos_root,
408 machine=remote)
409 if not retval:
410 return True
411
412
413def Main(argv):
414 misc.AcquireLock(lock_file)
415 try:
416 return DoImage(argv)
417 finally:
418 misc.ReleaseLock(lock_file)
419
420
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700421if __name__ == "__main__":
422 retval = Main(sys.argv)
423 sys.exit(retval)