blob: 60e1e9eb1bae648a481dfefd5080a4ed162ba27b [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.
50 keypath = os.path.join (os.path.realpath(chromeos_root),
cmtice13909242014-03-11 13:38:07 -070051 "src/scripts/mod_for_test_scripts/ssh_keys/"
52 "testing_rsa")
cmtice0cc4e772014-01-30 15:52:37 -080053
Luis Lozano1e462d92014-04-18 17:49:29 -070054 command = ("ssh -i %s -o StrictHostKeyChecking=no -o CheckHostIP=no "
55 "-o BatchMode=yes -o UserKnownHostsFile=/dev/null "
56 "root@%s \"python -c 'import cherrypy'\" " %
cmtice0cc4e772014-01-30 15:52:37 -080057 (keypath,remote) )
58 retval = cmd_executer.RunCommand (command)
Luis Lozano1e462d92014-04-18 17:49:29 -070059 logger.GetLogger().LogFatalIf(retval == 255, "Failed ssh to %s" % remote)
cmtice0cc4e772014-01-30 15:52:37 -080060 if retval == 0:
61 remote_has_cherrypy = True
62
63 return (chroot_has_cros_flash and remote_has_cherrypy)
64
Ahmad Sharif4467f002012-12-20 12:09:49 -080065def DoImage(argv):
Ahmad Sharif70de27b2011-06-15 17:51:24 -070066 """Build ChromeOS."""
Ahmad Sharif4467f002012-12-20 12:09:49 -080067
Ahmad Sharif70de27b2011-06-15 17:51:24 -070068 parser = optparse.OptionParser()
69 parser.add_option("-c", "--chromeos_root", dest="chromeos_root",
70 help="Target directory for ChromeOS installation.")
71 parser.add_option("-r", "--remote", dest="remote",
72 help="Target device.")
73 parser.add_option("-i", "--image", dest="image",
74 help="Image binary file.")
75 parser.add_option("-b", "--board", dest="board",
76 help="Target board override.")
77 parser.add_option("-f", "--force", dest="force",
78 action="store_true",
79 default=False,
80 help="Force an image even if it is non-test.")
cmtice13909242014-03-11 13:38:07 -070081 parser.add_option("-l", "--logging_level", dest="log_level",
82 default="verbose",
83 help="Amount of logging to be used. Valid levels are "
84 "'quiet', 'average', and 'verbose'.")
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080085 parser.add_option("-a",
Ahmad Sharif4467f002012-12-20 12:09:49 -080086 "--image_args",
87 dest="image_args")
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080088
Ahmad Sharif70de27b2011-06-15 17:51:24 -070089
90 options = parser.parse_args(argv[1:])[0]
91
cmtice13909242014-03-11 13:38:07 -070092 if not options.log_level in command_executer.LOG_LEVEL:
93 Usage(parser, "--logging_level must be 'quiet', 'average' or 'verbose'")
94 else:
95 log_level = options.log_level
96
97 # Common initializations
98 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
99 l = logger.GetLogger()
100
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700101 if options.chromeos_root is None:
102 Usage(parser, "--chromeos_root must be set")
103
104 if options.remote is None:
105 Usage(parser, "--remote must be set")
106
107 options.chromeos_root = os.path.expanduser(options.chromeos_root)
108
109 if options.board is None:
110 board = cmd_executer.CrosLearnBoard(options.chromeos_root, options.remote)
111 else:
112 board = options.board
113
114 if options.image is None:
Ahmad Shariffd356fb2012-05-07 12:02:16 -0700115 images_dir = misc.GetImageDir(options.chromeos_root, board)
116 image = os.path.join(images_dir,
117 "latest",
118 "chromiumos_test_image.bin")
119 if not os.path.exists(image):
120 image = os.path.join(images_dir,
121 "latest",
122 "chromiumos_image.bin")
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700123 else:
124 image = options.image
cmtice0cc4e772014-01-30 15:52:37 -0800125 if image.find("xbuddy://") < 0:
126 image = os.path.expanduser(image)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700127
cmtice0cc4e772014-01-30 15:52:37 -0800128 if image.find("xbuddy://") < 0:
129 image = os.path.realpath(image)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700130
cmtice0cc4e772014-01-30 15:52:37 -0800131 if not os.path.exists(image) and image.find("xbuddy://") < 0:
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700132 Usage(parser, "Image file: " + image + " does not exist!")
133
cmtice0cc4e772014-01-30 15:52:37 -0800134 reimage = False
135 local_image = False
136 if image.find("xbuddy://") < 0:
137 local_image = True
cmtice13909242014-03-11 13:38:07 -0700138 image_checksum = FileUtils().Md5File(image, log_level=log_level)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700139
cmtice0cc4e772014-01-30 15:52:37 -0800140 command = "cat " + checksum_file
141 retval, device_checksum, err = cmd_executer.CrosRunCommand(command,
142 return_output=True,
143 chromeos_root=options.chromeos_root,
144 machine=options.remote)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700145
cmtice0cc4e772014-01-30 15:52:37 -0800146 device_checksum = device_checksum.strip()
147 image_checksum = str(image_checksum)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700148
cmtice0cc4e772014-01-30 15:52:37 -0800149 l.LogOutput("Image checksum: " + image_checksum)
150 l.LogOutput("Device checksum: " + device_checksum)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700151
cmtice0cc4e772014-01-30 15:52:37 -0800152 if image_checksum != device_checksum:
153 [found, located_image] = LocateOrCopyImage(options.chromeos_root,
154 image,
155 board=board)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700156
cmtice0cc4e772014-01-30 15:52:37 -0800157 reimage = True
158 l.LogOutput("Checksums do not match. Re-imaging...")
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700159
cmtice0cc4e772014-01-30 15:52:37 -0800160 is_test_image = IsImageModdedForTest(options.chromeos_root,
cmtice13909242014-03-11 13:38:07 -0700161 located_image, log_level)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700162
cmtice0cc4e772014-01-30 15:52:37 -0800163 if not is_test_image and not options.force:
164 logger.GetLogger().LogFatal("Have to pass --force to image a non-test "
165 "image!")
166 else:
167 reimage = True
168 found = True
169 l.LogOutput("Using non-local image; Re-imaging...")
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700170
cmtice0cc4e772014-01-30 15:52:37 -0800171
172 if reimage:
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700173 # If the device has /tmp mounted as noexec, image_to_live.sh can fail.
174 command = "mount -o remount,rw,exec /tmp"
175 cmd_executer.CrosRunCommand(command,
176 chromeos_root=options.chromeos_root,
177 machine=options.remote)
178
Ahmad Sharif4467f002012-12-20 12:09:49 -0800179 real_src_dir = os.path.join(os.path.realpath(options.chromeos_root),
180 "src")
cmtice43f1a452014-04-04 13:15:06 -0700181 real_chroot_dir = os.path.join(os.path.realpath(options.chromeos_root),
182 "chroot")
cmtice0cc4e772014-01-30 15:52:37 -0800183 if local_image:
184 if located_image.find(real_src_dir) != 0:
cmtice43f1a452014-04-04 13:15:06 -0700185 if located_image.find(real_chroot_dir) != 0:
186 raise Exception("Located image: %s not in chromeos_root: %s" %
187 (located_image, options.chromeos_root))
188 else:
189 chroot_image = located_image[len(real_chroot_dir):]
190 else:
191 chroot_image = os.path.join(
192 "..",
193 located_image[len(real_src_dir):].lstrip("/"))
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700194
cmticefd06cca2014-01-29 14:21:44 -0800195 # Check to see if cros flash is in the chroot or not.
cmtice0cc4e772014-01-30 15:52:37 -0800196 use_cros_flash = CheckForCrosFlash (options.chromeos_root,
cmtice13909242014-03-11 13:38:07 -0700197 options.remote, log_level)
cmtice0cc4e772014-01-30 15:52:37 -0800198
199 if use_cros_flash:
cmticefd06cca2014-01-29 14:21:44 -0800200 # Use 'cros flash'
cmtice0cc4e772014-01-30 15:52:37 -0800201 if local_image:
202 cros_flash_args = ["--board=%s" % board,
203 "--clobber-stateful",
204 options.remote,
205 chroot_image]
206 else:
207
208 cros_flash_args = ["--board=%s" % board,
209 "--clobber-stateful",
210 options.remote,
211 image]
cmticefd06cca2014-01-29 14:21:44 -0800212
213 command = ("cros flash %s" % " ".join(cros_flash_args))
cmtice0cc4e772014-01-30 15:52:37 -0800214 elif local_image:
cmticefd06cca2014-01-29 14:21:44 -0800215 # Use 'cros_image_to_target.py'
cmticefd06cca2014-01-29 14:21:44 -0800216 cros_image_to_target_args = ["--remote=%s" % options.remote,
217 "--board=%s" % board,
218 "--from=%s" % os.path.dirname(chroot_image),
219 "--image-name=%s" %
220 os.path.basename(located_image)]
221
222 command = ("./bin/cros_image_to_target.py %s" %
223 " ".join(cros_image_to_target_args))
224 if options.image_args:
225 command += " %s" % options.image_args
cmtice0cc4e772014-01-30 15:52:37 -0800226 else:
227 raise Exception("Unable to find 'cros flash' in chroot; cannot use "
228 "non-local image (%s) with cros_image_to_target.py" %
229 image)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700230
Ahmad Sharif4467f002012-12-20 12:09:49 -0800231 # Workaround for crosbug.com/35684.
232 os.chmod(misc.GetChromeOSKeyFile(options.chromeos_root), 0600)
cmtice13909242014-03-11 13:38:07 -0700233 if log_level == "quiet":
234 l.LogOutput("CMD : %s" % command)
235 elif log_level == "average":
236 cmd_executer.SetLogLevel("verbose");
Ahmad Sharif4467f002012-12-20 12:09:49 -0800237 retval = cmd_executer.ChrootRunCommand(options.chromeos_root,
cmtice6de7f8f2014-03-14 14:08:21 -0700238 command, command_timeout=1800)
cmticeb1340082014-01-13 13:22:37 -0800239
240 retries = 0
241 while retval != 0 and retries < 2:
242 retries += 1
cmtice13909242014-03-11 13:38:07 -0700243 if log_level == "quiet":
244 l.LogOutput("Imaging failed. Retry # %d." % retries)
245 l.LogOutput("CMD : %s" % command)
cmticeb1340082014-01-13 13:22:37 -0800246 retval = cmd_executer.ChrootRunCommand(options.chromeos_root,
cmtice6de7f8f2014-03-14 14:08:21 -0700247 command, command_timeout=1800)
cmticeb1340082014-01-13 13:22:37 -0800248
cmtice13909242014-03-11 13:38:07 -0700249 if log_level == "average":
250 cmd_executer.SetLogLevel(log_level)
251
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700252 if found == False:
253 temp_dir = os.path.dirname(located_image)
254 l.LogOutput("Deleting temp image dir: %s" % temp_dir)
255 shutil.rmtree(temp_dir)
256
257 logger.GetLogger().LogFatalIf(retval, "Image command failed")
Ahmad Sharif4467f002012-12-20 12:09:49 -0800258
259 # Unfortunately cros_image_to_target.py sometimes returns early when the
260 # machine isn't fully up yet.
cmtice13909242014-03-11 13:38:07 -0700261 retval = EnsureMachineUp(options.chromeos_root, options.remote,
262 log_level)
Ahmad Sharif4467f002012-12-20 12:09:49 -0800263
cmtice0cc4e772014-01-30 15:52:37 -0800264 # If this is a non-local image, then the retval returned from
265 # EnsureMachineUp is the one that will be returned by this function;
266 # in that case, make sure the value in 'retval' is appropriate.
267 if not local_image and retval == True:
268 retval = 0
269 else:
270 retval = 1
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700271
cmtice0cc4e772014-01-30 15:52:37 -0800272 if local_image:
cmtice13909242014-03-11 13:38:07 -0700273 if log_level == "average":
274 l.LogOutput("Verifying image.")
cmtice0cc4e772014-01-30 15:52:37 -0800275 command = "echo %s > %s && chmod -w %s" % (image_checksum,
276 checksum_file,
277 checksum_file)
278 retval = cmd_executer.CrosRunCommand(command,
279 chromeos_root=options.chromeos_root,
280 machine=options.remote)
281 logger.GetLogger().LogFatalIf(retval, "Writing checksum failed.")
282
283 successfully_imaged = VerifyChromeChecksum(options.chromeos_root,
284 image,
cmtice13909242014-03-11 13:38:07 -0700285 options.remote, log_level)
cmtice0cc4e772014-01-30 15:52:37 -0800286 logger.GetLogger().LogFatalIf(not successfully_imaged,
287 "Image verification failed!")
cmtice13909242014-03-11 13:38:07 -0700288 TryRemountPartitionAsRW(options.chromeos_root, options.remote,
289 log_level)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700290 else:
291 l.LogOutput("Checksums match. Skipping reimage")
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700292 return retval
293
294
295def LocateOrCopyImage(chromeos_root, image, board=None):
296 l = logger.GetLogger()
297 if board is None:
298 board_glob = "*"
299 else:
300 board_glob = board
301
302 chromeos_root_realpath = os.path.realpath(chromeos_root)
303 image = os.path.realpath(image)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800304
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700305 if image.startswith("%s/" % chromeos_root_realpath):
306 return [True, image]
307
308 # First search within the existing build dirs for any matching files.
309 images_glob = ("%s/src/build/images/%s/*/*.bin" %
310 (chromeos_root_realpath,
311 board_glob))
312 images_list = glob.glob(images_glob)
313 for potential_image in images_list:
314 if filecmp.cmp(potential_image, image):
315 l.LogOutput("Found matching image %s in chromeos_root." % potential_image)
316 return [True, potential_image]
cmtice13909242014-03-11 13:38:07 -0700317 # We did not find an image. Copy it in the src dir and return the copied
318 # file.
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700319 if board is None:
320 board = ""
321 base_dir = ("%s/src/build/images/%s" %
322 (chromeos_root_realpath,
323 board))
324 if not os.path.isdir(base_dir):
325 os.makedirs(base_dir)
326 temp_dir = tempfile.mkdtemp(prefix="%s/tmp" % base_dir)
327 new_image = "%s/%s" % (temp_dir, os.path.basename(image))
328 l.LogOutput("No matching image found. Copying %s to %s" %
329 (image, new_image))
330 shutil.copyfile(image, new_image)
331 return [False, new_image]
332
333
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800334def GetImageMountCommand(chromeos_root, image, rootfs_mp, stateful_mp):
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700335 image_dir = os.path.dirname(image)
336 image_file = os.path.basename(image)
337 mount_command = ("cd %s/src/scripts &&"
338 "./mount_gpt_image.sh --from=%s --image=%s"
339 " --safe --read_only"
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800340 " --rootfs_mountpt=%s"
341 " --stateful_mountpt=%s" %
342 (chromeos_root, image_dir, image_file, rootfs_mp,
343 stateful_mp))
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700344 return mount_command
345
346
cmtice13909242014-03-11 13:38:07 -0700347def MountImage(chromeos_root, image, rootfs_mp, stateful_mp, log_level,
348 unmount=False):
349 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800350 command = GetImageMountCommand(chromeos_root, image, rootfs_mp, stateful_mp)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700351 if unmount:
352 command = "%s --unmount" % command
353 retval = cmd_executer.RunCommand(command)
354 logger.GetLogger().LogFatalIf(retval, "Mount/unmount command failed!")
355 return retval
356
357
cmtice13909242014-03-11 13:38:07 -0700358def IsImageModdedForTest(chromeos_root, image, log_level):
359 if log_level != "verbose":
360 log_level = "quiet"
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800361 rootfs_mp = tempfile.mkdtemp()
362 stateful_mp = tempfile.mkdtemp()
cmtice13909242014-03-11 13:38:07 -0700363 MountImage(chromeos_root, image, rootfs_mp, stateful_mp, log_level)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800364 lsb_release_file = os.path.join(rootfs_mp, "etc/lsb-release")
Ahmad Shariff395c262012-10-09 17:48:09 -0700365 lsb_release_contents = open(lsb_release_file).read()
366 is_test_image = re.search("test", lsb_release_contents, re.IGNORECASE)
cmtice13909242014-03-11 13:38:07 -0700367 MountImage(chromeos_root, image, rootfs_mp, stateful_mp, log_level,
368 unmount=True)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700369 return is_test_image
370
371
cmtice13909242014-03-11 13:38:07 -0700372def VerifyChromeChecksum(chromeos_root, image, remote, log_level):
373 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800374 rootfs_mp = tempfile.mkdtemp()
375 stateful_mp = tempfile.mkdtemp()
cmtice13909242014-03-11 13:38:07 -0700376 MountImage(chromeos_root, image, rootfs_mp, stateful_mp, log_level)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800377 image_chrome_checksum = FileUtils().Md5File("%s/opt/google/chrome/chrome" %
cmtice13909242014-03-11 13:38:07 -0700378 rootfs_mp,
379 log_level=log_level)
380 MountImage(chromeos_root, image, rootfs_mp, stateful_mp, log_level,
381 unmount=True)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700382
383 command = "md5sum /opt/google/chrome/chrome"
384 [r, o, e] = cmd_executer.CrosRunCommand(command,
385 return_output=True,
386 chromeos_root=chromeos_root,
387 machine=remote)
388 device_chrome_checksum = o.split()[0]
389 if image_chrome_checksum.strip() == device_chrome_checksum.strip():
390 return True
391 else:
392 return False
393
Luis Lozanof81680c2013-03-15 14:44:13 -0700394# Remount partition as writable.
395# TODO: auto-detect if an image is built using --noenable_rootfs_verification.
cmtice13909242014-03-11 13:38:07 -0700396def TryRemountPartitionAsRW(chromeos_root, remote, log_level):
Luis Lozanof81680c2013-03-15 14:44:13 -0700397 l = logger.GetLogger()
cmtice13909242014-03-11 13:38:07 -0700398 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
Luis Lozanof81680c2013-03-15 14:44:13 -0700399 command = "sudo mount -o remount,rw /"
400 retval = cmd_executer.CrosRunCommand(\
401 command, chromeos_root=chromeos_root, machine=remote, terminated_timeout=10)
402 if retval:
403 ## Safely ignore.
404 l.LogWarning("Failed to remount partition as rw, "
405 "probably the image was not built with "
406 "\"--noenable_rootfs_verification\", "
407 "you can safely ignore this.")
408 else:
409 l.LogOutput("Re-mounted partition as writable.")
410
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700411
cmtice13909242014-03-11 13:38:07 -0700412def EnsureMachineUp(chromeos_root, remote, log_level):
Ahmad Sharif4467f002012-12-20 12:09:49 -0800413 l = logger.GetLogger()
cmtice13909242014-03-11 13:38:07 -0700414 cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
Ahmad Sharif4467f002012-12-20 12:09:49 -0800415 timeout = 600
416 magic = "abcdefghijklmnopqrstuvwxyz"
417 command = "echo %s" % magic
418 start_time = time.time()
419 while True:
420 current_time = time.time()
421 if current_time - start_time > timeout:
422 l.LogError("Timeout of %ss reached. Machine still not up. Aborting." %
423 timeout)
424 return False
425 retval = cmd_executer.CrosRunCommand(command,
426 chromeos_root=chromeos_root,
427 machine=remote)
428 if not retval:
429 return True
430
431
432def Main(argv):
433 misc.AcquireLock(lock_file)
434 try:
435 return DoImage(argv)
436 finally:
437 misc.ReleaseLock(lock_file)
438
439
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700440if __name__ == "__main__":
441 retval = Main(sys.argv)
442 sys.exit(retval)