asharif | 4d2b716 | 2013-02-15 05:14:57 +0000 | [diff] [blame] | 1 | #!/usr/bin/python2.6 |
| 2 | # |
asharif | fc33cd9 | 2013-02-15 06:31:21 +0000 | [diff] [blame] | 3 | # Copyright 2011 Google Inc. All Rights Reserved. |
asharif | 4d2b716 | 2013-02-15 05:14:57 +0000 | [diff] [blame] | 4 | |
asharif | fc33cd9 | 2013-02-15 06:31:21 +0000 | [diff] [blame] | 5 | """Script to image a ChromeOS device. |
asharif | 4d2b716 | 2013-02-15 05:14:57 +0000 | [diff] [blame] | 6 | |
asharif | fc33cd9 | 2013-02-15 06:31:21 +0000 | [diff] [blame] | 7 | This script images a remote ChromeOS device with a specific image." |
asharif | 4d2b716 | 2013-02-15 05:14:57 +0000 | [diff] [blame] | 8 | """ |
| 9 | |
asharif | fc33cd9 | 2013-02-15 06:31:21 +0000 | [diff] [blame] | 10 | __author__ = "asharif@google.com (Ahmad Sharif)" |
asharif | 4d2b716 | 2013-02-15 05:14:57 +0000 | [diff] [blame] | 11 | |
asharif | fc33cd9 | 2013-02-15 06:31:21 +0000 | [diff] [blame] | 12 | import filecmp |
| 13 | import glob |
asharif | 4d2b716 | 2013-02-15 05:14:57 +0000 | [diff] [blame] | 14 | import optparse |
| 15 | import os |
asharif | fc33cd9 | 2013-02-15 06:31:21 +0000 | [diff] [blame] | 16 | import shutil |
asharif | 4d2b716 | 2013-02-15 05:14:57 +0000 | [diff] [blame] | 17 | import sys |
asharif | fc33cd9 | 2013-02-15 06:31:21 +0000 | [diff] [blame] | 18 | import tempfile |
asharif | 4d2b716 | 2013-02-15 05:14:57 +0000 | [diff] [blame] | 19 | import tc_enter_chroot |
| 20 | from utils import command_executer |
| 21 | from utils import logger |
| 22 | from utils import utils |
| 23 | |
asharif | 0e0e268 | 2013-02-15 05:15:29 +0000 | [diff] [blame] | 24 | checksum_file = "/home/chronos/osimage_checksum_file" |
asharif | 4d2b716 | 2013-02-15 05:14:57 +0000 | [diff] [blame] | 25 | |
| 26 | |
| 27 | def Usage(parser, message): |
| 28 | print "ERROR: " + message |
| 29 | parser.print_help() |
| 30 | sys.exit(0) |
| 31 | |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 32 | def Main(argv): |
asharif | 4d2b716 | 2013-02-15 05:14:57 +0000 | [diff] [blame] | 33 | """Build ChromeOS.""" |
| 34 | # Common initializations |
| 35 | cmd_executer = command_executer.GetCommandExecuter() |
asharif | fc33cd9 | 2013-02-15 06:31:21 +0000 | [diff] [blame] | 36 | l = logger.GetLogger() |
asharif | 4d2b716 | 2013-02-15 05:14:57 +0000 | [diff] [blame] | 37 | |
| 38 | parser = optparse.OptionParser() |
| 39 | parser.add_option("-c", "--chromeos_root", dest="chromeos_root", |
| 40 | help="Target directory for ChromeOS installation.") |
| 41 | parser.add_option("-r", "--remote", dest="remote", |
| 42 | help="Target device.") |
asharif | 8de2c73 | 2013-02-15 05:15:25 +0000 | [diff] [blame] | 43 | parser.add_option("-i", "--image", dest="image", |
| 44 | help="Image binary file.") |
asharif | 7cbe069 | 2013-02-15 06:17:33 +0000 | [diff] [blame] | 45 | parser.add_option("-b", "--board", dest="board", |
| 46 | help="Target board override.") |
asharif | 4d2b716 | 2013-02-15 05:14:57 +0000 | [diff] [blame] | 47 | |
| 48 | options = parser.parse_args(argv[1:])[0] |
| 49 | |
| 50 | if options.chromeos_root is None: |
| 51 | Usage(parser, "--chromeos_root must be set") |
| 52 | |
| 53 | if options.remote is None: |
| 54 | Usage(parser, "--remote must be set") |
| 55 | |
| 56 | options.chromeos_root = os.path.expanduser(options.chromeos_root) |
| 57 | |
asharif | 7cbe069 | 2013-02-15 06:17:33 +0000 | [diff] [blame] | 58 | if options.board is None: |
| 59 | board = cmd_executer.CrosLearnBoard(options.chromeos_root, options.remote) |
| 60 | else: |
| 61 | board = options.board |
asharif | 8de2c73 | 2013-02-15 05:15:25 +0000 | [diff] [blame] | 62 | |
| 63 | if options.image is None: |
asharif | 6796687 | 2013-02-15 05:15:44 +0000 | [diff] [blame] | 64 | image = (options.chromeos_root + |
| 65 | "/src/build/images/" + board + |
asharif | 8de2c73 | 2013-02-15 05:15:25 +0000 | [diff] [blame] | 66 | "/latest/" + |
| 67 | "/chromiumos_image.bin") |
| 68 | else: |
| 69 | image = options.image |
asharif | c380f61 | 2013-02-15 09:13:07 +0000 | [diff] [blame] | 70 | image = os.path.expanduser(image) |
asharif | 8de2c73 | 2013-02-15 05:15:25 +0000 | [diff] [blame] | 71 | |
| 72 | image = os.path.realpath(image) |
| 73 | |
| 74 | if not os.path.exists(image): |
| 75 | Usage(parser, "Image file: " + image + " does not exist!") |
| 76 | |
| 77 | image_checksum = utils.Md5File(image) |
asharif | 4d2b716 | 2013-02-15 05:14:57 +0000 | [diff] [blame] | 78 | |
| 79 | command = "cat " + checksum_file |
| 80 | retval, device_checksum, err = cmd_executer.CrosRunCommand(command, |
| 81 | return_output=True, |
| 82 | chromeos_root=options.chromeos_root, |
| 83 | machine=options.remote) |
| 84 | |
| 85 | device_checksum = device_checksum.strip() |
| 86 | image_checksum = str(image_checksum) |
| 87 | |
asharif | fc33cd9 | 2013-02-15 06:31:21 +0000 | [diff] [blame] | 88 | l.LogOutput("Image checksum: " + image_checksum) |
| 89 | l.LogOutput("Device checksum: " + device_checksum) |
asharif | 4d2b716 | 2013-02-15 05:14:57 +0000 | [diff] [blame] | 90 | |
| 91 | if image_checksum != device_checksum: |
asharif | fc33cd9 | 2013-02-15 06:31:21 +0000 | [diff] [blame] | 92 | [found, located_image] = LocateOrCopyImage(options.chromeos_root, |
| 93 | image, |
| 94 | board=board) |
| 95 | |
| 96 | l.LogOutput("Checksums do not match. Re-imaging...") |
asharif | 4d2b716 | 2013-02-15 05:14:57 +0000 | [diff] [blame] | 97 | command = (options.chromeos_root + |
| 98 | "/src/scripts/image_to_live.sh --remote=" + |
asharif | 8de2c73 | 2013-02-15 05:15:25 +0000 | [diff] [blame] | 99 | options.remote + |
asharif | fc33cd9 | 2013-02-15 06:31:21 +0000 | [diff] [blame] | 100 | " --image=" + located_image) |
asharif | 4d2b716 | 2013-02-15 05:14:57 +0000 | [diff] [blame] | 101 | |
asharif | 7cbe069 | 2013-02-15 06:17:33 +0000 | [diff] [blame] | 102 | retval = cmd_executer.RunCommand(command) |
asharif | fc33cd9 | 2013-02-15 06:31:21 +0000 | [diff] [blame] | 103 | |
| 104 | if found == False: |
| 105 | temp_dir = os.path.dirname(located_image) |
| 106 | l.LogOutput("Deleting temp image dir: %s" % temp_dir) |
| 107 | shutil.rmtree(temp_dir) |
| 108 | |
asharif | 7cbe069 | 2013-02-15 06:17:33 +0000 | [diff] [blame] | 109 | utils.AssertExit(retval == 0, "Image command failed") |
asharif | 4d2b716 | 2013-02-15 05:14:57 +0000 | [diff] [blame] | 110 | command = "'echo " + image_checksum + " > " + checksum_file |
| 111 | command += "&& chmod -w " + checksum_file + "'" |
asharif | 7cbe069 | 2013-02-15 06:17:33 +0000 | [diff] [blame] | 112 | retval = cmd_executer.CrosRunCommand(command, |
| 113 | chromeos_root=options.chromeos_root, |
asharif | 4d2b716 | 2013-02-15 05:14:57 +0000 | [diff] [blame] | 114 | machine=options.remote) |
asharif | 7cbe069 | 2013-02-15 06:17:33 +0000 | [diff] [blame] | 115 | utils.AssertExit(retval == 0, "Writing checksum failed.") |
asharif | 4d2b716 | 2013-02-15 05:14:57 +0000 | [diff] [blame] | 116 | else: |
asharif | fc33cd9 | 2013-02-15 06:31:21 +0000 | [diff] [blame] | 117 | l.LogOutput("Checksums match. Skipping reimage") |
asharif | 4d2b716 | 2013-02-15 05:14:57 +0000 | [diff] [blame] | 118 | |
| 119 | return retval |
| 120 | |
asharif | fc33cd9 | 2013-02-15 06:31:21 +0000 | [diff] [blame] | 121 | |
| 122 | def LocateOrCopyImage(chromeos_root, image, board=None): |
| 123 | l = logger.GetLogger() |
| 124 | if board is None: |
| 125 | board_glob = "*" |
| 126 | else: |
| 127 | board_glob = board |
| 128 | |
| 129 | chromeos_root_realpath = os.path.realpath(chromeos_root) |
| 130 | |
| 131 | |
| 132 | if image.startswith(chromeos_root_realpath): |
| 133 | return [True, image] |
| 134 | |
| 135 | # First search within the existing build dirs for any matching files. |
| 136 | images_glob = ("%s/src/build/images/%s/*/*.bin" % |
| 137 | (chromeos_root_realpath, |
| 138 | board_glob)) |
| 139 | images_list = glob.glob(images_glob) |
| 140 | for potential_image in images_list: |
| 141 | if filecmp.cmp(potential_image, image): |
| 142 | l.LogOutput("Found matching image %s in chromeos_root." % potential_image) |
| 143 | return [True, potential_image] |
| 144 | # We did not find an image. Copy it in the src dir and return the copied file. |
| 145 | if board is None: |
| 146 | board = "" |
| 147 | temp_dir = tempfile.mkdtemp(prefix="%s/src/build/images/%s/tmp" % |
| 148 | (chromeos_root_realpath, |
| 149 | board)) |
| 150 | new_image = "%s/%s" % (temp_dir, os.path.basename(image)) |
| 151 | l.LogOutput("No matching image found. Copying %s to %s" % |
| 152 | (image, new_image)) |
| 153 | shutil.copyfile(image, new_image) |
asharif | edff23f | 2013-02-15 09:14:34 +0000 | [diff] [blame] | 154 | return [False, new_image] |
asharif | fc33cd9 | 2013-02-15 06:31:21 +0000 | [diff] [blame] | 155 | |
| 156 | |
asharif | 4d2b716 | 2013-02-15 05:14:57 +0000 | [diff] [blame] | 157 | if __name__ == "__main__": |
asharif | ad40120 | 2013-02-15 09:21:36 +0000 | [diff] [blame] | 158 | retval = Main(sys.argv) |
asharif | 2198c51 | 2013-02-15 09:21:35 +0000 | [diff] [blame] | 159 | sys.exit(retval) |