blob: 001f4a6781487e5f89e2dec1eaa9fea3e38838c9 [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
cmtice0cc4e772014-01-30 15:52:37 -080036def CheckForCrosFlash (chromeos_root, remote):
37 cmd_executer = command_executer.GetCommandExecuter()
38
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),
51 "src/scripts/mod_for_test_scripts/ssh_keys/testing_rsa")
52
53 command = ("ssh -i %s -o StrictHostKeyChecking=no -o CheckHostIP=no "
54 "-o BatchMode=yes root@%s \"python -c 'import cherrypy'\" " %
55 (keypath,remote) )
56 retval = cmd_executer.RunCommand (command)
57 if retval == 0:
58 remote_has_cherrypy = True
59
60 return (chroot_has_cros_flash and remote_has_cherrypy)
61
Ahmad Sharif4467f002012-12-20 12:09:49 -080062def DoImage(argv):
Ahmad Sharif70de27b2011-06-15 17:51:24 -070063 """Build ChromeOS."""
Ahmad Sharif4467f002012-12-20 12:09:49 -080064
Ahmad Sharif70de27b2011-06-15 17:51:24 -070065 # Common initializations
66 cmd_executer = command_executer.GetCommandExecuter()
67 l = logger.GetLogger()
68
69 parser = optparse.OptionParser()
70 parser.add_option("-c", "--chromeos_root", dest="chromeos_root",
71 help="Target directory for ChromeOS installation.")
72 parser.add_option("-r", "--remote", dest="remote",
73 help="Target device.")
74 parser.add_option("-i", "--image", dest="image",
75 help="Image binary file.")
76 parser.add_option("-b", "--board", dest="board",
77 help="Target board override.")
78 parser.add_option("-f", "--force", dest="force",
79 action="store_true",
80 default=False,
81 help="Force an image even if it is non-test.")
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080082 parser.add_option("-a",
Ahmad Sharif4467f002012-12-20 12:09:49 -080083 "--image_args",
84 dest="image_args")
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080085
Ahmad Sharif70de27b2011-06-15 17:51:24 -070086
87 options = parser.parse_args(argv[1:])[0]
88
89 if options.chromeos_root is None:
90 Usage(parser, "--chromeos_root must be set")
91
92 if options.remote is None:
93 Usage(parser, "--remote must be set")
94
95 options.chromeos_root = os.path.expanduser(options.chromeos_root)
96
97 if options.board is None:
98 board = cmd_executer.CrosLearnBoard(options.chromeos_root, options.remote)
99 else:
100 board = options.board
101
102 if options.image is None:
Ahmad Shariffd356fb2012-05-07 12:02:16 -0700103 images_dir = misc.GetImageDir(options.chromeos_root, board)
104 image = os.path.join(images_dir,
105 "latest",
106 "chromiumos_test_image.bin")
107 if not os.path.exists(image):
108 image = os.path.join(images_dir,
109 "latest",
110 "chromiumos_image.bin")
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700111 else:
112 image = options.image
cmtice0cc4e772014-01-30 15:52:37 -0800113 if image.find("xbuddy://") < 0:
114 image = os.path.expanduser(image)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700115
cmtice0cc4e772014-01-30 15:52:37 -0800116 if image.find("xbuddy://") < 0:
117 image = os.path.realpath(image)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700118
cmtice0cc4e772014-01-30 15:52:37 -0800119 if not os.path.exists(image) and image.find("xbuddy://") < 0:
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700120 Usage(parser, "Image file: " + image + " does not exist!")
121
cmtice0cc4e772014-01-30 15:52:37 -0800122 reimage = False
123 local_image = False
124 if image.find("xbuddy://") < 0:
125 local_image = True
126 image_checksum = FileUtils().Md5File(image)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700127
cmtice0cc4e772014-01-30 15:52:37 -0800128 command = "cat " + checksum_file
129 retval, device_checksum, err = cmd_executer.CrosRunCommand(command,
130 return_output=True,
131 chromeos_root=options.chromeos_root,
132 machine=options.remote)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700133
cmtice0cc4e772014-01-30 15:52:37 -0800134 device_checksum = device_checksum.strip()
135 image_checksum = str(image_checksum)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700136
cmtice0cc4e772014-01-30 15:52:37 -0800137 l.LogOutput("Image checksum: " + image_checksum)
138 l.LogOutput("Device checksum: " + device_checksum)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700139
cmtice0cc4e772014-01-30 15:52:37 -0800140 if image_checksum != device_checksum:
141 [found, located_image] = LocateOrCopyImage(options.chromeos_root,
142 image,
143 board=board)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700144
cmtice0cc4e772014-01-30 15:52:37 -0800145 reimage = True
146 l.LogOutput("Checksums do not match. Re-imaging...")
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700147
cmtice0cc4e772014-01-30 15:52:37 -0800148 is_test_image = IsImageModdedForTest(options.chromeos_root,
149 located_image)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700150
cmtice0cc4e772014-01-30 15:52:37 -0800151 if not is_test_image and not options.force:
152 logger.GetLogger().LogFatal("Have to pass --force to image a non-test "
153 "image!")
154 else:
155 reimage = True
156 found = True
157 l.LogOutput("Using non-local image; Re-imaging...")
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700158
cmtice0cc4e772014-01-30 15:52:37 -0800159
160 if reimage:
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700161 # If the device has /tmp mounted as noexec, image_to_live.sh can fail.
162 command = "mount -o remount,rw,exec /tmp"
163 cmd_executer.CrosRunCommand(command,
164 chromeos_root=options.chromeos_root,
165 machine=options.remote)
166
Ahmad Sharif4467f002012-12-20 12:09:49 -0800167 real_src_dir = os.path.join(os.path.realpath(options.chromeos_root),
168 "src")
cmtice0cc4e772014-01-30 15:52:37 -0800169 if local_image:
170 if located_image.find(real_src_dir) != 0:
171 raise Exception("Located image: %s not in chromeos_root: %s" %
172 (located_image, options.chromeos_root))
173 chroot_image = os.path.join(
174 "..",
175 located_image[len(real_src_dir):].lstrip("/"))
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700176
cmticefd06cca2014-01-29 14:21:44 -0800177 # Check to see if cros flash is in the chroot or not.
cmtice0cc4e772014-01-30 15:52:37 -0800178 use_cros_flash = CheckForCrosFlash (options.chromeos_root,
179 options.remote)
180
181 if use_cros_flash:
cmticefd06cca2014-01-29 14:21:44 -0800182 # Use 'cros flash'
cmtice0cc4e772014-01-30 15:52:37 -0800183 if local_image:
184 cros_flash_args = ["--board=%s" % board,
185 "--clobber-stateful",
186 options.remote,
187 chroot_image]
188 else:
189
190 cros_flash_args = ["--board=%s" % board,
191 "--clobber-stateful",
192 options.remote,
193 image]
cmticefd06cca2014-01-29 14:21:44 -0800194
195 command = ("cros flash %s" % " ".join(cros_flash_args))
cmtice0cc4e772014-01-30 15:52:37 -0800196 elif local_image:
cmticefd06cca2014-01-29 14:21:44 -0800197 # Use 'cros_image_to_target.py'
cmticefd06cca2014-01-29 14:21:44 -0800198 cros_image_to_target_args = ["--remote=%s" % options.remote,
199 "--board=%s" % board,
200 "--from=%s" % os.path.dirname(chroot_image),
201 "--image-name=%s" %
202 os.path.basename(located_image)]
203
204 command = ("./bin/cros_image_to_target.py %s" %
205 " ".join(cros_image_to_target_args))
206 if options.image_args:
207 command += " %s" % options.image_args
cmtice0cc4e772014-01-30 15:52:37 -0800208 else:
209 raise Exception("Unable to find 'cros flash' in chroot; cannot use "
210 "non-local image (%s) with cros_image_to_target.py" %
211 image)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700212
Ahmad Sharif4467f002012-12-20 12:09:49 -0800213 # Workaround for crosbug.com/35684.
214 os.chmod(misc.GetChromeOSKeyFile(options.chromeos_root), 0600)
215 retval = cmd_executer.ChrootRunCommand(options.chromeos_root,
cmticeb1340082014-01-13 13:22:37 -0800216 command, command_timeout=600)
217
218 retries = 0
219 while retval != 0 and retries < 2:
220 retries += 1
221 retval = cmd_executer.ChrootRunCommand(options.chromeos_root,
222 command, command_timeout=600)
223
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700224 if found == False:
225 temp_dir = os.path.dirname(located_image)
226 l.LogOutput("Deleting temp image dir: %s" % temp_dir)
227 shutil.rmtree(temp_dir)
228
229 logger.GetLogger().LogFatalIf(retval, "Image command failed")
Ahmad Sharif4467f002012-12-20 12:09:49 -0800230
231 # Unfortunately cros_image_to_target.py sometimes returns early when the
232 # machine isn't fully up yet.
233 retval = EnsureMachineUp(options.chromeos_root, options.remote)
234
cmtice0cc4e772014-01-30 15:52:37 -0800235 # If this is a non-local image, then the retval returned from
236 # EnsureMachineUp is the one that will be returned by this function;
237 # in that case, make sure the value in 'retval' is appropriate.
238 if not local_image and retval == True:
239 retval = 0
240 else:
241 retval = 1
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700242
cmtice0cc4e772014-01-30 15:52:37 -0800243 if local_image:
244 command = "echo %s > %s && chmod -w %s" % (image_checksum,
245 checksum_file,
246 checksum_file)
247 retval = cmd_executer.CrosRunCommand(command,
248 chromeos_root=options.chromeos_root,
249 machine=options.remote)
250 logger.GetLogger().LogFatalIf(retval, "Writing checksum failed.")
251
252 successfully_imaged = VerifyChromeChecksum(options.chromeos_root,
253 image,
254 options.remote)
255 logger.GetLogger().LogFatalIf(not successfully_imaged,
256 "Image verification failed!")
257 TryRemountPartitionAsRW(options.chromeos_root, options.remote)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700258 else:
259 l.LogOutput("Checksums match. Skipping reimage")
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700260 return retval
261
262
263def LocateOrCopyImage(chromeos_root, image, board=None):
264 l = logger.GetLogger()
265 if board is None:
266 board_glob = "*"
267 else:
268 board_glob = board
269
270 chromeos_root_realpath = os.path.realpath(chromeos_root)
271 image = os.path.realpath(image)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800272
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700273 if image.startswith("%s/" % chromeos_root_realpath):
274 return [True, image]
275
276 # First search within the existing build dirs for any matching files.
277 images_glob = ("%s/src/build/images/%s/*/*.bin" %
278 (chromeos_root_realpath,
279 board_glob))
280 images_list = glob.glob(images_glob)
281 for potential_image in images_list:
282 if filecmp.cmp(potential_image, image):
283 l.LogOutput("Found matching image %s in chromeos_root." % potential_image)
284 return [True, potential_image]
285 # We did not find an image. Copy it in the src dir and return the copied file.
286 if board is None:
287 board = ""
288 base_dir = ("%s/src/build/images/%s" %
289 (chromeos_root_realpath,
290 board))
291 if not os.path.isdir(base_dir):
292 os.makedirs(base_dir)
293 temp_dir = tempfile.mkdtemp(prefix="%s/tmp" % base_dir)
294 new_image = "%s/%s" % (temp_dir, os.path.basename(image))
295 l.LogOutput("No matching image found. Copying %s to %s" %
296 (image, new_image))
297 shutil.copyfile(image, new_image)
298 return [False, new_image]
299
300
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800301def GetImageMountCommand(chromeos_root, image, rootfs_mp, stateful_mp):
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700302 image_dir = os.path.dirname(image)
303 image_file = os.path.basename(image)
304 mount_command = ("cd %s/src/scripts &&"
305 "./mount_gpt_image.sh --from=%s --image=%s"
306 " --safe --read_only"
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800307 " --rootfs_mountpt=%s"
308 " --stateful_mountpt=%s" %
309 (chromeos_root, image_dir, image_file, rootfs_mp,
310 stateful_mp))
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700311 return mount_command
312
313
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800314def MountImage(chromeos_root, image, rootfs_mp, stateful_mp, unmount=False):
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700315 cmd_executer = command_executer.GetCommandExecuter()
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800316 command = GetImageMountCommand(chromeos_root, image, rootfs_mp, stateful_mp)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700317 if unmount:
318 command = "%s --unmount" % command
319 retval = cmd_executer.RunCommand(command)
320 logger.GetLogger().LogFatalIf(retval, "Mount/unmount command failed!")
321 return retval
322
323
324def IsImageModdedForTest(chromeos_root, image):
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800325 rootfs_mp = tempfile.mkdtemp()
326 stateful_mp = tempfile.mkdtemp()
327 MountImage(chromeos_root, image, rootfs_mp, stateful_mp)
328 lsb_release_file = os.path.join(rootfs_mp, "etc/lsb-release")
Ahmad Shariff395c262012-10-09 17:48:09 -0700329 lsb_release_contents = open(lsb_release_file).read()
330 is_test_image = re.search("test", lsb_release_contents, re.IGNORECASE)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800331 MountImage(chromeos_root, image, rootfs_mp, stateful_mp, unmount=True)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700332 return is_test_image
333
334
335def VerifyChromeChecksum(chromeos_root, image, remote):
336 cmd_executer = command_executer.GetCommandExecuter()
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800337 rootfs_mp = tempfile.mkdtemp()
338 stateful_mp = tempfile.mkdtemp()
339 MountImage(chromeos_root, image, rootfs_mp, stateful_mp)
340 image_chrome_checksum = FileUtils().Md5File("%s/opt/google/chrome/chrome" %
341 rootfs_mp)
342 MountImage(chromeos_root, image, rootfs_mp, stateful_mp, unmount=True)
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700343
344 command = "md5sum /opt/google/chrome/chrome"
345 [r, o, e] = cmd_executer.CrosRunCommand(command,
346 return_output=True,
347 chromeos_root=chromeos_root,
348 machine=remote)
349 device_chrome_checksum = o.split()[0]
350 if image_chrome_checksum.strip() == device_chrome_checksum.strip():
351 return True
352 else:
353 return False
354
Luis Lozanof81680c2013-03-15 14:44:13 -0700355# Remount partition as writable.
356# TODO: auto-detect if an image is built using --noenable_rootfs_verification.
357def TryRemountPartitionAsRW(chromeos_root, remote):
358 l = logger.GetLogger()
359 cmd_executer = command_executer.GetCommandExecuter()
360 command = "sudo mount -o remount,rw /"
361 retval = cmd_executer.CrosRunCommand(\
362 command, chromeos_root=chromeos_root, machine=remote, terminated_timeout=10)
363 if retval:
364 ## Safely ignore.
365 l.LogWarning("Failed to remount partition as rw, "
366 "probably the image was not built with "
367 "\"--noenable_rootfs_verification\", "
368 "you can safely ignore this.")
369 else:
370 l.LogOutput("Re-mounted partition as writable.")
371
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700372
Ahmad Sharif4467f002012-12-20 12:09:49 -0800373def EnsureMachineUp(chromeos_root, remote):
374 l = logger.GetLogger()
375 cmd_executer = command_executer.GetCommandExecuter()
376 timeout = 600
377 magic = "abcdefghijklmnopqrstuvwxyz"
378 command = "echo %s" % magic
379 start_time = time.time()
380 while True:
381 current_time = time.time()
382 if current_time - start_time > timeout:
383 l.LogError("Timeout of %ss reached. Machine still not up. Aborting." %
384 timeout)
385 return False
386 retval = cmd_executer.CrosRunCommand(command,
387 chromeos_root=chromeos_root,
388 machine=remote)
389 if not retval:
390 return True
391
392
393def Main(argv):
394 misc.AcquireLock(lock_file)
395 try:
396 return DoImage(argv)
397 finally:
398 misc.ReleaseLock(lock_file)
399
400
Ahmad Sharif70de27b2011-06-15 17:51:24 -0700401if __name__ == "__main__":
402 retval = Main(sys.argv)
403 sys.exit(retval)