Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # -*- coding: UTF-8 -*- |
| 3 | # Copyright 2016 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
Joel Kitching | 679a00b | 2016-08-03 11:42:58 +0800 | [diff] [blame] | 7 | """Transition to release state directly without reboot.""" |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 8 | |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 9 | import json |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 10 | import logging |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 11 | import os |
Shun-Hsing Ou | b572483 | 2016-07-21 11:45:58 +0800 | [diff] [blame] | 12 | import re |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 13 | import resource |
| 14 | import shutil |
| 15 | import signal |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 16 | import socket |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 17 | import tempfile |
| 18 | import textwrap |
| 19 | import time |
| 20 | |
| 21 | import factory_common # pylint: disable=unused-import |
| 22 | from cros.factory.gooftool import chroot |
Wei-Han Chen | 0a3320e | 2016-04-23 01:32:07 +0800 | [diff] [blame] | 23 | from cros.factory.gooftool.common import ExecFactoryPar |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 24 | from cros.factory.gooftool.common import Util |
Wei-Han Chen | 0a3320e | 2016-04-23 01:32:07 +0800 | [diff] [blame] | 25 | from cros.factory.test.env import paths |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 26 | from cros.factory.utils import process_utils |
| 27 | from cros.factory.utils import sync_utils |
| 28 | from cros.factory.utils import sys_utils |
| 29 | |
| 30 | |
Hung-Te Lin | a319546 | 2016-10-14 15:48:29 +0800 | [diff] [blame] | 31 | """Directory of scripts for device cut-off""" |
| 32 | CUTOFF_SCRIPT_DIR = '/usr/local/factory/sh/cutoff' |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 33 | |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 34 | WIPE_IN_TMPFS_LOG = 'wipe_in_tmpfs.log' |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 35 | |
Joel Kitching | 679a00b | 2016-08-03 11:42:58 +0800 | [diff] [blame] | 36 | |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 37 | def _CopyLogFileToStateDev(state_dev, logfile): |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 38 | with sys_utils.MountPartition(state_dev, |
| 39 | rw=True, |
| 40 | fstype='ext4') as mount_point: |
| 41 | shutil.copyfile(logfile, |
| 42 | os.path.join(mount_point, os.path.basename(logfile))) |
| 43 | |
| 44 | |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 45 | def _OnError(ip, port, token, state_dev, wipe_in_tmpfs_log=None, |
| 46 | wipe_init_log=None): |
| 47 | if wipe_in_tmpfs_log: |
| 48 | _CopyLogFileToStateDev(state_dev, wipe_in_tmpfs_log) |
| 49 | if wipe_init_log: |
| 50 | _CopyLogFileToStateDev(state_dev, wipe_init_log) |
| 51 | _InformStation(ip, port, token, |
| 52 | wipe_in_tmpfs_log=wipe_in_tmpfs_log, |
| 53 | wipe_init_log=wipe_init_log, |
| 54 | success=False) |
| 55 | |
| 56 | |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 57 | def Daemonize(logfile=None): |
| 58 | """Starts a daemon process and terminates current process. |
| 59 | |
| 60 | A daemon process will be started, and continue excuting the following codes. |
| 61 | The original process that calls this function will be terminated. |
| 62 | |
| 63 | Example:: |
| 64 | |
| 65 | def DaemonFunc(): |
| 66 | Daemonize() |
| 67 | # the process calling DaemonFunc is terminated. |
| 68 | # the following codes will be executed in a daemon process |
| 69 | ... |
| 70 | |
| 71 | If you would like to keep the original process alive, you could fork a child |
| 72 | process and let child process start the daemon. |
| 73 | """ |
| 74 | # fork from parent process |
| 75 | if os.fork(): |
| 76 | # stop parent process |
| 77 | os._exit(0) # pylint: disable=protected-access |
| 78 | |
| 79 | # decouple from parent process |
| 80 | os.chdir('/') |
| 81 | os.umask(0) |
| 82 | os.setsid() |
| 83 | |
| 84 | # fork again |
| 85 | if os.fork(): |
| 86 | os._exit(0) # pylint: disable=protected-access |
| 87 | |
| 88 | maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1] |
| 89 | if maxfd == resource.RLIM_INFINITY: |
| 90 | maxfd = 1024 |
| 91 | |
| 92 | for fd in xrange(maxfd): |
| 93 | try: |
| 94 | os.close(fd) |
| 95 | except OSError: |
| 96 | pass |
| 97 | |
| 98 | # Reopen fd 0 (stdin), 1 (stdout), 2 (stderr) to prevent errors from reading |
| 99 | # or writing to these files. |
| 100 | # Since we have closed all file descriptors, os.open should open a file with |
| 101 | # file descriptor equals to 0 |
| 102 | os.open('/dev/null', os.O_RDWR) |
| 103 | if logfile is None: |
| 104 | os.dup2(0, 1) # stdout |
| 105 | os.dup2(0, 2) # stderr |
| 106 | else: |
| 107 | os.open(logfile, os.O_RDWR | os.O_CREAT) |
| 108 | os.dup2(1, 2) # stderr |
| 109 | |
| 110 | |
| 111 | def ResetLog(logfile=None): |
| 112 | if len(logging.getLogger().handlers) > 0: |
| 113 | for handler in logging.getLogger().handlers: |
| 114 | logging.getLogger().removeHandler(handler) |
| 115 | logging.basicConfig(filename=logfile, level=logging.NOTSET) |
| 116 | |
| 117 | |
Hung-Te Lin | 7b27f0c | 2016-10-18 18:41:29 +0800 | [diff] [blame] | 118 | def WipeInTmpFs(is_fast=None, shopfloor_url=None, station_ip=None, |
| 119 | station_port=None, wipe_finish_token=None): |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 120 | """prepare to wipe by pivot root to tmpfs and unmount statefull partition. |
| 121 | |
| 122 | Args: |
| 123 | is_fast: whether or not to apply fast wipe. |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 124 | shopfloor_url: for inform_shopfloor.sh |
| 125 | """ |
| 126 | |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 127 | Daemonize() |
| 128 | |
Shun-Hsing Ou | b572483 | 2016-07-21 11:45:58 +0800 | [diff] [blame] | 129 | # Set the defual umask. |
| 130 | os.umask(0022) |
| 131 | |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 132 | logfile = os.path.join('/tmp', WIPE_IN_TMPFS_LOG) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 133 | ResetLog(logfile) |
| 134 | |
Wei-Han Chen | 0a3320e | 2016-04-23 01:32:07 +0800 | [diff] [blame] | 135 | factory_par = paths.GetFactoryPythonArchivePath() |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 136 | |
| 137 | new_root = tempfile.mkdtemp(prefix='tmpfs.') |
| 138 | binary_deps = [ |
| 139 | 'activate_date', 'backlight_tool', 'busybox', 'cgpt', 'cgpt.bin', |
| 140 | 'clobber-log', 'clobber-state', 'coreutils', 'crossystem', 'dd', |
| 141 | 'display_boot_message', 'dumpe2fs', 'ectool', 'flashrom', 'halt', |
| 142 | 'initctl', 'mkfs.ext4', 'mktemp', 'mosys', 'mount', 'mount-encrypted', |
| 143 | 'od', 'pango-view', 'pkill', 'pv', 'python', 'reboot', 'setterm', 'sh', |
Hung-Te Lin | 0ce1339 | 2016-12-30 12:36:29 +0800 | [diff] [blame] | 144 | 'shutdown', 'stop', 'umount', 'vpd', 'wget', 'lsof', 'jq'] |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 145 | if os.path.exists('/sbin/frecon'): |
| 146 | binary_deps.append('/sbin/frecon') |
| 147 | else: |
| 148 | binary_deps.append('/usr/bin/ply-image') |
| 149 | |
| 150 | etc_issue = textwrap.dedent(""" |
| 151 | You are now in tmp file system created for in-place wiping. |
| 152 | |
| 153 | For debugging wiping fails, see log files under |
| 154 | /tmp |
| 155 | /mnt/stateful_partition/unencrypted |
| 156 | |
| 157 | The log file name should be |
| 158 | - wipe_in_tmpfs.log |
| 159 | - wipe_init.log |
| 160 | |
| 161 | You can also run scripts under /usr/local/factory/sh for wiping process. |
| 162 | """) |
| 163 | |
Wei-Han Chen | 0a3320e | 2016-04-23 01:32:07 +0800 | [diff] [blame] | 164 | util = Util() |
| 165 | |
| 166 | root_disk = util.GetPrimaryDevicePath() |
| 167 | release_rootfs = util.GetReleaseRootPartitionPath() |
| 168 | state_dev = util.GetPrimaryDevicePath(1) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 169 | wipe_args = 'factory' + (' fast' if is_fast else '') |
| 170 | |
| 171 | logging.debug('state_dev: %s', state_dev) |
| 172 | logging.debug('factory_par: %s', factory_par) |
| 173 | |
| 174 | old_root = 'old_root' |
| 175 | |
| 176 | try: |
Shun-Hsing Ou | b572483 | 2016-07-21 11:45:58 +0800 | [diff] [blame] | 177 | # pango load library module dynamically. Therefore we need to query it |
| 178 | # first. |
| 179 | pango_query_output = process_utils.SpawnOutput( |
| 180 | ['pango-querymodules', '--system']) |
| 181 | m = re.search(r'^# ModulesPath = (.+)$', pango_query_output, re.M) |
| 182 | assert m != None, 'Failed to find pango module path.' |
| 183 | pango_module = m.group(1) |
| 184 | |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 185 | with chroot.TmpChroot( |
| 186 | new_root, |
| 187 | file_dir_list=[ |
Shun-Hsing Ou | b572483 | 2016-07-21 11:45:58 +0800 | [diff] [blame] | 188 | # Basic rootfs. |
Wei-Ning Huang | 71f94e1 | 2016-07-17 23:21:41 +0800 | [diff] [blame] | 189 | '/bin', '/etc', '/lib', '/lib64', '/root', '/sbin', |
Shun-Hsing Ou | b572483 | 2016-07-21 11:45:58 +0800 | [diff] [blame] | 190 | # Factory related scripts. |
| 191 | factory_par, |
| 192 | '/usr/local/factory/sh', |
| 193 | # Fonts and assets required for showing message. |
| 194 | pango_module, |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 195 | '/usr/share/fonts/notocjk', |
| 196 | '/usr/share/cache/fontconfig', |
| 197 | '/usr/share/chromeos-assets/images', |
| 198 | '/usr/share/chromeos-assets/text/boot_messages', |
| 199 | '/usr/share/misc/chromeos-common.sh', |
Shun-Hsing Ou | b572483 | 2016-07-21 11:45:58 +0800 | [diff] [blame] | 200 | # File required for enable ssh connection. |
| 201 | '/mnt/stateful_partition/etc/ssh', |
| 202 | '/root/.ssh', |
| 203 | '/usr/share/chromeos-ssh-config', |
| 204 | # /var/empty is required by openssh server. |
| 205 | '/var/empty'], |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 206 | binary_list=binary_deps, etc_issue=etc_issue).PivotRoot(old_root): |
| 207 | logging.debug( |
| 208 | 'lsof: %s', |
| 209 | process_utils.SpawnOutput('lsof -p %d' % os.getpid(), shell=True)) |
| 210 | |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 211 | process_utils.Spawn(['sync'], call=True) |
| 212 | time.sleep(3) |
| 213 | |
| 214 | # Restart gooftool under new root. Since current gooftool might be using |
| 215 | # some resource under stateful partition, restarting gooftool ensures that |
| 216 | # everything new gooftool is using comes from tmpfs and we can safely |
| 217 | # unmount stateful partition. |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 218 | args = [] |
| 219 | if wipe_args: |
| 220 | args += ['--wipe_args', wipe_args] |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 221 | if shopfloor_url: |
| 222 | args += ['--shopfloor_url', shopfloor_url] |
| 223 | if station_ip: |
| 224 | args += ['--station_ip', station_ip] |
| 225 | if station_port: |
| 226 | args += ['--station_port', station_port] |
| 227 | if wipe_finish_token: |
| 228 | args += ['--wipe_finish_token', wipe_finish_token] |
| 229 | args += ['--state_dev', state_dev] |
| 230 | args += ['--release_rootfs', release_rootfs] |
| 231 | args += ['--root_disk', root_disk] |
| 232 | args += ['--old_root', old_root] |
| 233 | |
| 234 | ExecFactoryPar('gooftool', 'wipe_init', *args) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 235 | raise RuntimeError('Should not reach here') |
| 236 | except: # pylint: disable=bare-except |
| 237 | logging.exception('wipe_in_place failed') |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 238 | _OnError(station_ip, station_port, wipe_finish_token, state_dev, |
| 239 | wipe_in_tmpfs_log=logfile, wipe_init_log=None) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 240 | raise |
| 241 | |
| 242 | |
| 243 | def _StopAllUpstartJobs(exclude_list=None): |
| 244 | logging.debug('stopping upstart jobs') |
| 245 | |
| 246 | # Try three times to stop running services because some service will respawn |
| 247 | # one time after being stopped, e.g. shill_respawn. Two times should be enough |
| 248 | # to stop shill. Adding one more try for safety. |
| 249 | |
| 250 | if exclude_list is None: |
| 251 | exclude_list = [] |
| 252 | |
| 253 | for unused_tries in xrange(3): |
| 254 | service_list = process_utils.SpawnOutput(['initctl', 'list']).splitlines() |
| 255 | service_list = [ |
| 256 | line.split()[0] for line in service_list if 'start/running' in line] |
| 257 | for service in service_list: |
Wei-Han Chen | c8f2456 | 2016-04-23 19:42:42 +0800 | [diff] [blame] | 258 | if service in exclude_list or service.startswith('console-'): |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 259 | continue |
Wei-Han Chen | c8f2456 | 2016-04-23 19:42:42 +0800 | [diff] [blame] | 260 | process_utils.Spawn(['stop', service], call=True, log=True) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 261 | |
| 262 | |
Wei-Han Chen | c8f2456 | 2016-04-23 19:42:42 +0800 | [diff] [blame] | 263 | def _UnmountStatefulPartition(root, state_dev): |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 264 | logging.debug('unmount stateful partition') |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 265 | # mount points that need chromeos_shutdown to umount |
| 266 | |
| 267 | # 1. find mount points on stateful partition |
Wei-Han Chen | c8f2456 | 2016-04-23 19:42:42 +0800 | [diff] [blame] | 268 | mount_output = process_utils.SpawnOutput(['mount'], log=True) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 269 | |
Wei-Han Chen | c8f2456 | 2016-04-23 19:42:42 +0800 | [diff] [blame] | 270 | mount_point_list = [] |
| 271 | for line in mount_output.splitlines(): |
| 272 | fields = line.split() |
| 273 | if fields[0] == state_dev: |
| 274 | mount_point_list.append(fields[2]) |
| 275 | |
| 276 | logging.debug('stateful partitions mounted on: %s', mount_point_list) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 277 | # 2. find processes that are using stateful partitions |
| 278 | |
Wei-Han Chen | 0a3320e | 2016-04-23 01:32:07 +0800 | [diff] [blame] | 279 | def _ListProcOpening(path_list): |
| 280 | lsof_cmd = ['lsof', '-t'] + path_list |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 281 | return [int(line) |
| 282 | for line in process_utils.SpawnOutput(lsof_cmd).splitlines()] |
| 283 | |
| 284 | proc_list = _ListProcOpening(mount_point_list) |
| 285 | |
Wei-Han Chen | c8f2456 | 2016-04-23 19:42:42 +0800 | [diff] [blame] | 286 | if os.getpid() in proc_list: |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 287 | logging.error('wipe_init itself is using stateful partition') |
| 288 | logging.error( |
| 289 | 'lsof: %s', |
| 290 | process_utils.SpawnOutput('lsof -p %d' % os.getpid(), shell=True)) |
| 291 | raise RuntimeError('using stateful partition') |
| 292 | |
| 293 | def _KillOpeningBySignal(sig): |
| 294 | proc_list = _ListProcOpening(mount_point_list) |
| 295 | if not proc_list: |
| 296 | return True # we are done |
| 297 | for pid in proc_list: |
Wei-Han Chen | c8f2456 | 2016-04-23 19:42:42 +0800 | [diff] [blame] | 298 | try: |
| 299 | os.kill(pid, sig) |
| 300 | except: # pylint: disable=bare-except |
| 301 | logging.exception('killing process %d failed', pid) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 302 | return False # need to check again |
| 303 | |
| 304 | sync_utils.Retry(10, 0.1, None, _KillOpeningBySignal, signal.SIGTERM) |
| 305 | sync_utils.Retry(10, 0.1, None, _KillOpeningBySignal, signal.SIGKILL) |
| 306 | |
| 307 | proc_list = _ListProcOpening(mount_point_list) |
| 308 | assert not proc_list, "processes using stateful partition: %s" % proc_list |
| 309 | |
| 310 | os.unlink(os.path.join(root, 'var', 'run')) |
| 311 | os.unlink(os.path.join(root, 'var', 'lock')) |
| 312 | |
You-Cheng Syu | 2ea26dd | 2016-12-06 20:50:05 +0800 | [diff] [blame^] | 313 | def _Unmount(mount_point, critical): |
| 314 | logging.info('try to unmount %s' % mount_point) |
| 315 | for unused_i in xrange(10): |
| 316 | output = process_utils.Spawn(['umount', '-n', '-R', mount_point], |
| 317 | read_stderr=True, log=True).stderr_data |
| 318 | # some mount points need to be unmounted multiple times. |
| 319 | if (output.endswith(': not mounted\n') or |
| 320 | output.endswith(': not found\n')): |
| 321 | return |
| 322 | time.sleep(0.5) |
| 323 | logging.error('failed to unmount %s' % mount_point) |
| 324 | if critical: |
| 325 | raise RuntimeError('Unmounting %s is critical. Stop.') |
| 326 | |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 327 | if os.path.exists(os.path.join(root, 'dev', 'mapper', 'encstateful')): |
You-Cheng Syu | f099046 | 2016-09-07 14:56:19 +0800 | [diff] [blame] | 328 | # Doing what 'mount-encrypted umount' should do. |
| 329 | for mount_point in mount_point_list: |
You-Cheng Syu | 2ea26dd | 2016-12-06 20:50:05 +0800 | [diff] [blame^] | 330 | _Unmount(mount_point, False) |
| 331 | _Unmount(os.path.join(root, 'var'), True) |
You-Cheng Syu | f099046 | 2016-09-07 14:56:19 +0800 | [diff] [blame] | 332 | process_utils.Spawn(['dmsetup', 'remove', 'encstateful'], check_call=True) |
| 333 | process_utils.Spawn(['losetup', '-D'], check_call=True) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 334 | |
| 335 | for mount_point in mount_point_list: |
You-Cheng Syu | 2ea26dd | 2016-12-06 20:50:05 +0800 | [diff] [blame^] | 336 | _Unmount(mount_point, True) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 337 | process_utils.Spawn(['sync'], call=True) |
| 338 | |
You-Cheng Syu | f099046 | 2016-09-07 14:56:19 +0800 | [diff] [blame] | 339 | # Check if the stateful partition is unmounted successfully. |
| 340 | process_utils.Spawn(r'mount | grep -c "^\S*stateful" | grep -q ^0$', |
| 341 | shell=True, check_call=True) |
| 342 | |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 343 | |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 344 | def _InformStation(ip, port, token, wipe_init_log=None, |
| 345 | wipe_in_tmpfs_log=None, success=True): |
| 346 | if not ip: |
| 347 | return |
| 348 | port = int(port) |
| 349 | |
| 350 | logging.debug('inform station %s:%d', ip, port) |
| 351 | |
| 352 | try: |
| 353 | sync_utils.WaitFor( |
| 354 | lambda: 0 == process_utils.Spawn(['ping', '-w1', '-c1', ip], |
| 355 | call=True).returncode, |
| 356 | timeout_secs=180, poll_interval=1) |
| 357 | except: # pylint: disable=bare-except |
| 358 | logging.exception('cannot get network connection...') |
| 359 | else: |
| 360 | sock = socket.socket() |
| 361 | sock.connect((ip, port)) |
| 362 | |
| 363 | response = dict(token=token, success=success) |
| 364 | |
| 365 | if wipe_init_log: |
| 366 | with open(wipe_init_log) as f: |
| 367 | response['wipe_init_log'] = f.read() |
| 368 | |
| 369 | if wipe_in_tmpfs_log: |
| 370 | with open(wipe_in_tmpfs_log) as f: |
| 371 | response['wipe_in_tmpfs_log'] = f.read() |
| 372 | |
| 373 | sock.sendall(json.dumps(response) + '\n') |
| 374 | sock.close() |
| 375 | |
| 376 | |
| 377 | def _WipeStateDev(release_rootfs, root_disk, wipe_args): |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 378 | stateful_partition_path = '/mnt/stateful_partition' |
| 379 | |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 380 | clobber_state_env = os.environ.copy() |
Wei-Han Chen | 0a3320e | 2016-04-23 01:32:07 +0800 | [diff] [blame] | 381 | clobber_state_env.update(ROOT_DEV=release_rootfs, |
Earl Ou | eeb289d | 2016-11-04 14:36:40 +0800 | [diff] [blame] | 382 | ROOT_DISK=root_disk) |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 383 | logging.debug('clobber-state: root_dev=%s, root_disk=%s', |
Wei-Han Chen | 0a3320e | 2016-04-23 01:32:07 +0800 | [diff] [blame] | 384 | release_rootfs, root_disk) |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 385 | process_utils.Spawn(['clobber-state', wipe_args], env=clobber_state_env, |
| 386 | call=True) |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 387 | # remove developer flag, which is created by clobber-state after wiping. |
| 388 | try: |
Wei-Han Chen | e5b19f4 | 2016-10-21 14:45:10 +0800 | [diff] [blame] | 389 | os.unlink(os.path.join(stateful_partition_path, '.developer_mode')) |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 390 | except OSError: |
| 391 | pass |
Wei-Han Chen | e5b19f4 | 2016-10-21 14:45:10 +0800 | [diff] [blame] | 392 | # make sure that everything is synced |
| 393 | for unused_i in xrange(3): |
| 394 | process_utils.Spawn(['sync'], call=True) |
| 395 | time.sleep(1) |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 396 | |
Joel Kitching | 679a00b | 2016-08-03 11:42:58 +0800 | [diff] [blame] | 397 | |
Earl Ou | 564a787 | 2016-10-05 10:22:00 +0800 | [diff] [blame] | 398 | def EnableReleasePartition(release_rootfs): |
| 399 | """Enables a release image partition on disk.""" |
Wei-Han Chen | 0a3320e | 2016-04-23 01:32:07 +0800 | [diff] [blame] | 400 | logging.debug('enable release partition: %s', release_rootfs) |
| 401 | Util().EnableReleasePartition(release_rootfs) |
Earl Ou | 564a787 | 2016-10-05 10:22:00 +0800 | [diff] [blame] | 402 | logging.debug('Device will boot from %s after reboot.', release_rootfs) |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 403 | |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 404 | |
| 405 | def _InformShopfloor(shopfloor_url): |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 406 | if shopfloor_url: |
| 407 | logging.debug('inform shopfloor %s', shopfloor_url) |
Hung-Te Lin | a319546 | 2016-10-14 15:48:29 +0800 | [diff] [blame] | 408 | proc = process_utils.Spawn( |
| 409 | [os.path.join(CUTOFF_SCRIPT_DIR, 'inform_shopfloor.sh'), shopfloor_url, |
| 410 | 'factory_wipe'], check_call=True) |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 411 | logging.debug('stdout: %s', proc.stdout_data) |
| 412 | logging.debug('stderr: %s', proc.stderr_data) |
| 413 | |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 414 | |
Hung-Te Lin | 7b27f0c | 2016-10-18 18:41:29 +0800 | [diff] [blame] | 415 | def _Cutoff(): |
| 416 | logging.debug('cutoff') |
Hung-Te Lin | a319546 | 2016-10-14 15:48:29 +0800 | [diff] [blame] | 417 | cutoff_script = os.path.join(CUTOFF_SCRIPT_DIR, 'cutoff.sh') |
Hung-Te Lin | 7b27f0c | 2016-10-18 18:41:29 +0800 | [diff] [blame] | 418 | process_utils.Spawn('%s' % (cutoff_script), shell=True, check_call=True) |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 419 | |
| 420 | |
Hung-Te Lin | 7b27f0c | 2016-10-18 18:41:29 +0800 | [diff] [blame] | 421 | def WipeInit(wipe_args, shopfloor_url, state_dev, release_rootfs, |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 422 | root_disk, old_root, station_ip, station_port, finish_token): |
Wei-Han Chen | c8f2456 | 2016-04-23 19:42:42 +0800 | [diff] [blame] | 423 | Daemonize() |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 424 | logfile = '/tmp/wipe_init.log' |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 425 | wipe_in_tmpfs_log = os.path.join(old_root, 'tmp', WIPE_IN_TMPFS_LOG) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 426 | ResetLog(logfile) |
| 427 | |
Wei-Han Chen | 0a3320e | 2016-04-23 01:32:07 +0800 | [diff] [blame] | 428 | logging.debug('wipe_args: %s', wipe_args) |
Wei-Han Chen | 0a3320e | 2016-04-23 01:32:07 +0800 | [diff] [blame] | 429 | logging.debug('shopfloor_url: %s', shopfloor_url) |
| 430 | logging.debug('state_dev: %s', state_dev) |
| 431 | logging.debug('release_rootfs: %s', release_rootfs) |
| 432 | logging.debug('root_disk: %s', root_disk) |
| 433 | logging.debug('old_root: %s', old_root) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 434 | |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 435 | try: |
Shun-Hsing Ou | b572483 | 2016-07-21 11:45:58 +0800 | [diff] [blame] | 436 | _StopAllUpstartJobs(exclude_list=[ |
| 437 | # Milestone marker that use to determine the running of other services. |
| 438 | 'boot-services', |
| 439 | 'system-services', |
| 440 | 'failsafe', |
| 441 | # Keep dbus to make sure we can shutdown the device. |
| 442 | 'dbus', |
| 443 | # Keep shill for connecting to shopfloor or stations. |
| 444 | 'shill', |
| 445 | # Keep openssh-server for debugging purpose. |
| 446 | 'openssh-server', |
| 447 | # sslh is a service in ARC++ for muxing between ssh and adb. |
| 448 | 'sslh' |
| 449 | ]) |
Wei-Han Chen | c8f2456 | 2016-04-23 19:42:42 +0800 | [diff] [blame] | 450 | _UnmountStatefulPartition(old_root, state_dev) |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 451 | |
Hung-Te Lin | a319546 | 2016-10-14 15:48:29 +0800 | [diff] [blame] | 452 | process_utils.Spawn( |
| 453 | [os.path.join(CUTOFF_SCRIPT_DIR, 'display_wipe_message.sh'), 'wipe'], |
| 454 | call=True) |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 455 | |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 456 | _WipeStateDev(release_rootfs, root_disk, wipe_args) |
| 457 | |
Earl Ou | 564a787 | 2016-10-05 10:22:00 +0800 | [diff] [blame] | 458 | EnableReleasePartition(release_rootfs) |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 459 | |
| 460 | _InformShopfloor(shopfloor_url) |
| 461 | |
| 462 | _InformStation(station_ip, station_port, finish_token, |
| 463 | wipe_init_log=logfile, |
| 464 | wipe_in_tmpfs_log=wipe_in_tmpfs_log, |
| 465 | success=True) |
| 466 | |
Hung-Te Lin | 7b27f0c | 2016-10-18 18:41:29 +0800 | [diff] [blame] | 467 | _Cutoff() |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 468 | |
| 469 | # should not reach here |
| 470 | time.sleep(1e8) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 471 | except: # pylint: disable=bare-except |
| 472 | logging.exception('wipe_init failed') |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 473 | _OnError(station_ip, station_port, finish_token, state_dev, |
| 474 | wipe_in_tmpfs_log=wipe_in_tmpfs_log, wipe_init_log=logfile) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 475 | raise |