image_chromeos.py revision 6de7f8fe91a1a36d0e7c578ac8d170bf483fcfbe
1#!/usr/bin/python
2#
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
16import re
17import shutil
18import sys
19import tempfile
20import time
21
22from utils import command_executer
23from utils import logger
24from utils import misc
25from utils.file_utils import FileUtils
26
27checksum_file = "/usr/local/osimage_checksum_file"
28lock_file = "/tmp/image_chromeos_lock/image_chromeos_lock"
29
30def Usage(parser, message):
31  print "ERROR: " + message
32  parser.print_help()
33  sys.exit(0)
34
35
36def CheckForCrosFlash(chromeos_root, remote, log_level):
37  cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
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/"
52                          "testing_rsa")
53
54  command = ("ssh -i %s -o StrictHostKeyChecking=no -o CheckHostIP=no "
55             "-o BatchMode=yes root@%s \"python -c 'import cherrypy'\" " %
56             (keypath,remote) )
57  retval = cmd_executer.RunCommand (command)
58  if retval == 0:
59    remote_has_cherrypy = True
60
61  return (chroot_has_cros_flash and remote_has_cherrypy)
62
63def DoImage(argv):
64  """Build ChromeOS."""
65
66  parser = optparse.OptionParser()
67  parser.add_option("-c", "--chromeos_root", dest="chromeos_root",
68                    help="Target directory for ChromeOS installation.")
69  parser.add_option("-r", "--remote", dest="remote",
70                    help="Target device.")
71  parser.add_option("-i", "--image", dest="image",
72                    help="Image binary file.")
73  parser.add_option("-b", "--board", dest="board",
74                    help="Target board override.")
75  parser.add_option("-f", "--force", dest="force",
76                    action="store_true",
77                    default=False,
78                    help="Force an image even if it is non-test.")
79  parser.add_option("-l", "--logging_level", dest="log_level",
80                    default="verbose",
81                    help="Amount of logging to be used. Valid levels are "
82                    "'quiet', 'average', and 'verbose'.")
83  parser.add_option("-a",
84                    "--image_args",
85                    dest="image_args")
86
87
88  options = parser.parse_args(argv[1:])[0]
89
90  if not options.log_level in command_executer.LOG_LEVEL:
91    Usage(parser, "--logging_level must be 'quiet', 'average' or 'verbose'")
92  else:
93    log_level = options.log_level
94
95  # Common initializations
96  cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
97  l = logger.GetLogger()
98
99  if options.chromeos_root is None:
100    Usage(parser, "--chromeos_root must be set")
101
102  if options.remote is None:
103    Usage(parser, "--remote must be set")
104
105  options.chromeos_root = os.path.expanduser(options.chromeos_root)
106
107  if options.board is None:
108    board = cmd_executer.CrosLearnBoard(options.chromeos_root, options.remote)
109  else:
110    board = options.board
111
112  if options.image is None:
113    images_dir = misc.GetImageDir(options.chromeos_root, board)
114    image = os.path.join(images_dir,
115                         "latest",
116                         "chromiumos_test_image.bin")
117    if not os.path.exists(image):
118      image = os.path.join(images_dir,
119                           "latest",
120                           "chromiumos_image.bin")
121  else:
122    image = options.image
123    if image.find("xbuddy://") < 0:
124      image = os.path.expanduser(image)
125
126  if image.find("xbuddy://") < 0:
127    image = os.path.realpath(image)
128
129  if not os.path.exists(image) and image.find("xbuddy://") < 0:
130    Usage(parser, "Image file: " + image + " does not exist!")
131
132  reimage = False
133  local_image = False
134  if image.find("xbuddy://") < 0:
135    local_image = True
136    image_checksum = FileUtils().Md5File(image, log_level=log_level)
137
138    command = "cat " + checksum_file
139    retval, device_checksum, err = cmd_executer.CrosRunCommand(command,
140                                         return_output=True,
141                                         chromeos_root=options.chromeos_root,
142                                         machine=options.remote)
143
144    device_checksum = device_checksum.strip()
145    image_checksum = str(image_checksum)
146
147    l.LogOutput("Image checksum: " + image_checksum)
148    l.LogOutput("Device checksum: " + device_checksum)
149
150    if image_checksum != device_checksum:
151      [found, located_image] = LocateOrCopyImage(options.chromeos_root,
152                                                 image,
153                                                 board=board)
154
155      reimage = True
156      l.LogOutput("Checksums do not match. Re-imaging...")
157
158      is_test_image = IsImageModdedForTest(options.chromeos_root,
159                                           located_image, log_level)
160
161      if not is_test_image and not options.force:
162        logger.GetLogger().LogFatal("Have to pass --force to image a non-test "
163                                    "image!")
164  else:
165    reimage = True
166    found = True
167    l.LogOutput("Using non-local image; Re-imaging...")
168
169
170  if reimage:
171    # If the device has /tmp mounted as noexec, image_to_live.sh can fail.
172    command = "mount -o remount,rw,exec /tmp"
173    cmd_executer.CrosRunCommand(command,
174                                chromeos_root=options.chromeos_root,
175                                machine=options.remote)
176
177    real_src_dir = os.path.join(os.path.realpath(options.chromeos_root),
178                                "src")
179    if local_image:
180      if located_image.find(real_src_dir) != 0:
181        raise Exception("Located image: %s not in chromeos_root: %s" %
182                        (located_image, options.chromeos_root))
183      chroot_image = os.path.join(
184          "..",
185          located_image[len(real_src_dir):].lstrip("/"))
186
187    # Check to see if cros flash is in the chroot or not.
188    use_cros_flash = CheckForCrosFlash (options.chromeos_root,
189                                        options.remote, log_level)
190
191    if use_cros_flash:
192      # Use 'cros flash'
193      if local_image:
194        cros_flash_args = ["--board=%s" % board,
195                           "--clobber-stateful",
196                           options.remote,
197                           chroot_image]
198      else:
199
200        cros_flash_args = ["--board=%s" % board,
201                           "--clobber-stateful",
202                           options.remote,
203                           image]
204
205      command = ("cros flash %s" % " ".join(cros_flash_args))
206    elif local_image:
207      # Use 'cros_image_to_target.py'
208      cros_image_to_target_args = ["--remote=%s" % options.remote,
209                                   "--board=%s" % board,
210                                   "--from=%s" % os.path.dirname(chroot_image),
211                                   "--image-name=%s" %
212                                   os.path.basename(located_image)]
213
214      command = ("./bin/cros_image_to_target.py %s" %
215                 " ".join(cros_image_to_target_args))
216      if options.image_args:
217        command += " %s" % options.image_args
218    else:
219      raise Exception("Unable to find 'cros flash' in chroot; cannot use "
220                      "non-local image (%s) with cros_image_to_target.py" %
221                      image)
222
223    # Workaround for crosbug.com/35684.
224    os.chmod(misc.GetChromeOSKeyFile(options.chromeos_root), 0600)
225    if log_level == "quiet":
226      l.LogOutput("CMD : %s" % command)
227    elif log_level == "average":
228      cmd_executer.SetLogLevel("verbose");
229    retval = cmd_executer.ChrootRunCommand(options.chromeos_root,
230                                           command, command_timeout=1800)
231
232    retries = 0
233    while retval != 0 and retries < 2:
234      retries += 1
235      if log_level == "quiet":
236        l.LogOutput("Imaging failed. Retry # %d." % retries)
237        l.LogOutput("CMD : %s" % command)
238      retval = cmd_executer.ChrootRunCommand(options.chromeos_root,
239                                             command, command_timeout=1800)
240
241    if log_level == "average":
242      cmd_executer.SetLogLevel(log_level)
243
244    if found == False:
245      temp_dir = os.path.dirname(located_image)
246      l.LogOutput("Deleting temp image dir: %s" % temp_dir)
247      shutil.rmtree(temp_dir)
248
249    logger.GetLogger().LogFatalIf(retval, "Image command failed")
250
251    # Unfortunately cros_image_to_target.py sometimes returns early when the
252    # machine isn't fully up yet.
253    retval = EnsureMachineUp(options.chromeos_root, options.remote,
254                             log_level)
255
256    # If this is a non-local image, then the retval returned from
257    # EnsureMachineUp is the one that will be returned by this function;
258    # in that case, make sure the value in 'retval' is appropriate.
259    if not local_image and retval == True:
260      retval = 0
261    else:
262      retval = 1
263
264    if local_image:
265      if log_level == "average":
266        l.LogOutput("Verifying image.")
267      command = "echo %s > %s && chmod -w %s" % (image_checksum,
268                                                 checksum_file,
269                                                 checksum_file)
270      retval = cmd_executer.CrosRunCommand(command,
271                                          chromeos_root=options.chromeos_root,
272                                          machine=options.remote)
273      logger.GetLogger().LogFatalIf(retval, "Writing checksum failed.")
274
275      successfully_imaged = VerifyChromeChecksum(options.chromeos_root,
276                                                 image,
277                                                 options.remote, log_level)
278      logger.GetLogger().LogFatalIf(not successfully_imaged,
279                                    "Image verification failed!")
280      TryRemountPartitionAsRW(options.chromeos_root, options.remote,
281                              log_level)
282  else:
283    l.LogOutput("Checksums match. Skipping reimage")
284  return retval
285
286
287def LocateOrCopyImage(chromeos_root, image, board=None):
288  l = logger.GetLogger()
289  if board is None:
290    board_glob = "*"
291  else:
292    board_glob = board
293
294  chromeos_root_realpath = os.path.realpath(chromeos_root)
295  image = os.path.realpath(image)
296
297  if image.startswith("%s/" % chromeos_root_realpath):
298    return [True, image]
299
300  # First search within the existing build dirs for any matching files.
301  images_glob = ("%s/src/build/images/%s/*/*.bin" %
302                 (chromeos_root_realpath,
303                  board_glob))
304  images_list = glob.glob(images_glob)
305  for potential_image in images_list:
306    if filecmp.cmp(potential_image, image):
307      l.LogOutput("Found matching image %s in chromeos_root." % potential_image)
308      return [True, potential_image]
309  # We did not find an image. Copy it in the src dir and return the copied
310  # file.
311  if board is None:
312    board = ""
313  base_dir = ("%s/src/build/images/%s" %
314              (chromeos_root_realpath,
315               board))
316  if not os.path.isdir(base_dir):
317    os.makedirs(base_dir)
318  temp_dir = tempfile.mkdtemp(prefix="%s/tmp" % base_dir)
319  new_image = "%s/%s" % (temp_dir, os.path.basename(image))
320  l.LogOutput("No matching image found. Copying %s to %s" %
321              (image, new_image))
322  shutil.copyfile(image, new_image)
323  return [False, new_image]
324
325
326def GetImageMountCommand(chromeos_root, image, rootfs_mp, stateful_mp):
327  image_dir = os.path.dirname(image)
328  image_file = os.path.basename(image)
329  mount_command = ("cd %s/src/scripts &&"
330                   "./mount_gpt_image.sh --from=%s --image=%s"
331                   " --safe --read_only"
332                   " --rootfs_mountpt=%s"
333                   " --stateful_mountpt=%s" %
334                   (chromeos_root, image_dir, image_file, rootfs_mp,
335                    stateful_mp))
336  return mount_command
337
338
339def MountImage(chromeos_root, image, rootfs_mp, stateful_mp, log_level,
340               unmount=False):
341  cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
342  command = GetImageMountCommand(chromeos_root, image, rootfs_mp, stateful_mp)
343  if unmount:
344    command = "%s --unmount" % command
345  retval = cmd_executer.RunCommand(command)
346  logger.GetLogger().LogFatalIf(retval, "Mount/unmount command failed!")
347  return retval
348
349
350def IsImageModdedForTest(chromeos_root, image, log_level):
351  if log_level != "verbose":
352    log_level = "quiet"
353  rootfs_mp = tempfile.mkdtemp()
354  stateful_mp = tempfile.mkdtemp()
355  MountImage(chromeos_root, image, rootfs_mp, stateful_mp, log_level)
356  lsb_release_file = os.path.join(rootfs_mp, "etc/lsb-release")
357  lsb_release_contents = open(lsb_release_file).read()
358  is_test_image = re.search("test", lsb_release_contents, re.IGNORECASE)
359  MountImage(chromeos_root, image, rootfs_mp, stateful_mp, log_level,
360             unmount=True)
361  return is_test_image
362
363
364def VerifyChromeChecksum(chromeos_root, image, remote, log_level):
365  cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
366  rootfs_mp = tempfile.mkdtemp()
367  stateful_mp = tempfile.mkdtemp()
368  MountImage(chromeos_root, image, rootfs_mp, stateful_mp, log_level)
369  image_chrome_checksum = FileUtils().Md5File("%s/opt/google/chrome/chrome" %
370                                              rootfs_mp,
371                                              log_level=log_level)
372  MountImage(chromeos_root, image, rootfs_mp, stateful_mp, log_level,
373             unmount=True)
374
375  command = "md5sum /opt/google/chrome/chrome"
376  [r, o, e] = cmd_executer.CrosRunCommand(command,
377                                          return_output=True,
378                                          chromeos_root=chromeos_root,
379                                          machine=remote)
380  device_chrome_checksum = o.split()[0]
381  if image_chrome_checksum.strip() == device_chrome_checksum.strip():
382    return True
383  else:
384    return False
385
386# Remount partition as writable.
387# TODO: auto-detect if an image is built using --noenable_rootfs_verification.
388def TryRemountPartitionAsRW(chromeos_root, remote, log_level):
389  l = logger.GetLogger()
390  cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
391  command = "sudo mount -o remount,rw /"
392  retval = cmd_executer.CrosRunCommand(\
393    command, chromeos_root=chromeos_root, machine=remote, terminated_timeout=10)
394  if retval:
395    ## Safely ignore.
396    l.LogWarning("Failed to remount partition as rw, "
397                 "probably the image was not built with "
398                 "\"--noenable_rootfs_verification\", "
399                 "you can safely ignore this.")
400  else:
401    l.LogOutput("Re-mounted partition as writable.")
402
403
404def EnsureMachineUp(chromeos_root, remote, log_level):
405  l = logger.GetLogger()
406  cmd_executer = command_executer.GetCommandExecuter(log_level=log_level)
407  timeout = 600
408  magic = "abcdefghijklmnopqrstuvwxyz"
409  command = "echo %s" % magic
410  start_time = time.time()
411  while True:
412    current_time = time.time()
413    if current_time - start_time > timeout:
414      l.LogError("Timeout of %ss reached. Machine still not up. Aborting." %
415                 timeout)
416      return False
417    retval = cmd_executer.CrosRunCommand(command,
418                                         chromeos_root=chromeos_root,
419                                         machine=remote)
420    if not retval:
421      return True
422
423
424def Main(argv):
425  misc.AcquireLock(lock_file)
426  try:
427    return DoImage(argv)
428  finally:
429    misc.ReleaseLock(lock_file)
430
431
432if __name__ == "__main__":
433  retval = Main(sys.argv)
434  sys.exit(retval)
435