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