Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 1 | # Copyright 2016 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Joel Kitching | 679a00b | 2016-08-03 11:42:58 +0800 | [diff] [blame] | 5 | """Transition to release state directly without reboot.""" |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 6 | |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 7 | import json |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 8 | import logging |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 9 | import os |
| 10 | import resource |
| 11 | import shutil |
| 12 | import signal |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 13 | import socket |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 14 | import tempfile |
| 15 | import textwrap |
| 16 | import time |
| 17 | |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 18 | from cros.factory.gooftool import chroot |
Wei-Han Chen | 0a3320e | 2016-04-23 01:32:07 +0800 | [diff] [blame] | 19 | from cros.factory.gooftool.common import ExecFactoryPar |
Shen-En Shih | 502b310 | 2018-04-24 11:12:01 +0800 | [diff] [blame] | 20 | from cros.factory.gooftool.common import Shell |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 21 | from cros.factory.gooftool.common import Util |
Wei-Han Chen | 0a3320e | 2016-04-23 01:32:07 +0800 | [diff] [blame] | 22 | from cros.factory.test.env import paths |
Wei-Han Chen | b05699a | 2017-07-12 16:37:47 +0800 | [diff] [blame] | 23 | from cros.factory.utils import file_utils |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 24 | from cros.factory.utils import process_utils |
| 25 | from cros.factory.utils import sync_utils |
| 26 | from cros.factory.utils import sys_utils |
| 27 | |
| 28 | |
Hung-Te Lin | a319546 | 2016-10-14 15:48:29 +0800 | [diff] [blame] | 29 | CUTOFF_SCRIPT_DIR = '/usr/local/factory/sh/cutoff' |
Peter Shih | 1889830 | 2018-03-05 15:32:58 +0800 | [diff] [blame] | 30 | """Directory of scripts for device cut-off""" |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 31 | |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 32 | WIPE_IN_TMPFS_LOG = 'wipe_in_tmpfs.log' |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 33 | |
Wei-Han Chen | b05699a | 2017-07-12 16:37:47 +0800 | [diff] [blame] | 34 | STATEFUL_PARTITION_PATH = '/mnt/stateful_partition/' |
| 35 | |
| 36 | WIPE_MARK_FILE = 'wipe_mark_file' |
| 37 | |
Cheng Yueh | e84775f | 2020-02-12 14:03:35 +0800 | [diff] [blame] | 38 | CRX_CACHE_PAYLOAD_NAME = 'dev_image/opt/cros_payloads/release_image.crx_cache' |
Hung-Te Lin | dd3425d | 2017-07-12 20:10:52 +0800 | [diff] [blame] | 39 | CRX_CACHE_TAR_PATH = '/tmp/crx_cache.tar' |
Wei-Han Chen | b05699a | 2017-07-12 16:37:47 +0800 | [diff] [blame] | 40 | |
Peter Shih | e6afab3 | 2018-09-11 17:16:48 +0800 | [diff] [blame] | 41 | class WipeError(Exception): |
Wei-Han Chen | b05699a | 2017-07-12 16:37:47 +0800 | [diff] [blame] | 42 | """Failed to complete wiping.""" |
| 43 | |
Joel Kitching | 679a00b | 2016-08-03 11:42:58 +0800 | [diff] [blame] | 44 | |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 45 | def _CopyLogFileToStateDev(state_dev, logfile): |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 46 | with sys_utils.MountPartition(state_dev, |
| 47 | rw=True, |
| 48 | fstype='ext4') as mount_point: |
| 49 | shutil.copyfile(logfile, |
| 50 | os.path.join(mount_point, os.path.basename(logfile))) |
| 51 | |
| 52 | |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 53 | def _OnError(ip, port, token, state_dev, wipe_in_tmpfs_log=None, |
| 54 | wipe_init_log=None): |
| 55 | if wipe_in_tmpfs_log: |
| 56 | _CopyLogFileToStateDev(state_dev, wipe_in_tmpfs_log) |
| 57 | if wipe_init_log: |
| 58 | _CopyLogFileToStateDev(state_dev, wipe_init_log) |
| 59 | _InformStation(ip, port, token, |
| 60 | wipe_in_tmpfs_log=wipe_in_tmpfs_log, |
| 61 | wipe_init_log=wipe_init_log, |
| 62 | success=False) |
| 63 | |
| 64 | |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 65 | def Daemonize(logfile=None): |
| 66 | """Starts a daemon process and terminates current process. |
| 67 | |
You-Cheng Syu | 461ec03 | 2017-03-06 15:56:58 +0800 | [diff] [blame] | 68 | A daemon process will be started, and continue executing the following codes. |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 69 | The original process that calls this function will be terminated. |
| 70 | |
| 71 | Example:: |
| 72 | |
| 73 | def DaemonFunc(): |
| 74 | Daemonize() |
| 75 | # the process calling DaemonFunc is terminated. |
| 76 | # the following codes will be executed in a daemon process |
| 77 | ... |
| 78 | |
| 79 | If you would like to keep the original process alive, you could fork a child |
| 80 | process and let child process start the daemon. |
| 81 | """ |
| 82 | # fork from parent process |
| 83 | if os.fork(): |
| 84 | # stop parent process |
| 85 | os._exit(0) # pylint: disable=protected-access |
| 86 | |
| 87 | # decouple from parent process |
| 88 | os.chdir('/') |
| 89 | os.umask(0) |
| 90 | os.setsid() |
| 91 | |
| 92 | # fork again |
| 93 | if os.fork(): |
| 94 | os._exit(0) # pylint: disable=protected-access |
| 95 | |
| 96 | maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1] |
| 97 | if maxfd == resource.RLIM_INFINITY: |
| 98 | maxfd = 1024 |
| 99 | |
Yilin Yang | bf84d2e | 2020-05-13 10:34:46 +0800 | [diff] [blame] | 100 | for fd in range(maxfd): |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 101 | try: |
| 102 | os.close(fd) |
| 103 | except OSError: |
| 104 | pass |
| 105 | |
| 106 | # Reopen fd 0 (stdin), 1 (stdout), 2 (stderr) to prevent errors from reading |
| 107 | # or writing to these files. |
| 108 | # Since we have closed all file descriptors, os.open should open a file with |
| 109 | # file descriptor equals to 0 |
| 110 | os.open('/dev/null', os.O_RDWR) |
| 111 | if logfile is None: |
| 112 | os.dup2(0, 1) # stdout |
| 113 | os.dup2(0, 2) # stderr |
| 114 | else: |
| 115 | os.open(logfile, os.O_RDWR | os.O_CREAT) |
| 116 | os.dup2(1, 2) # stderr |
| 117 | |
| 118 | |
| 119 | def ResetLog(logfile=None): |
Peter Shih | 19a938f | 2018-02-26 14:26:16 +0800 | [diff] [blame] | 120 | if logging.getLogger().handlers: |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 121 | for handler in logging.getLogger().handlers: |
| 122 | logging.getLogger().removeHandler(handler) |
Meng-Huan Yu | 345698d | 2020-04-30 15:18:44 +0800 | [diff] [blame] | 123 | log_format = '[%(asctime)-15s] %(levelname)s:%(name)s:%(message)s' |
| 124 | # logging.NOTSET is the lowerest level. |
| 125 | logging.basicConfig(filename=logfile, level=logging.NOTSET, format=log_format) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 126 | |
| 127 | |
Hung-Te Lin | 7b27f0c | 2016-10-18 18:41:29 +0800 | [diff] [blame] | 128 | def WipeInTmpFs(is_fast=None, shopfloor_url=None, station_ip=None, |
Wei-Han Chen | f392411 | 2019-02-25 14:52:58 +0800 | [diff] [blame] | 129 | station_port=None, wipe_finish_token=None, |
Meng-Huan Yu | 7a4f0f5 | 2020-01-07 20:11:01 +0800 | [diff] [blame] | 130 | keep_developer_mode_flag=False, test_umount=False): |
You-Cheng Syu | 461ec03 | 2017-03-06 15:56:58 +0800 | [diff] [blame] | 131 | """prepare to wipe by pivot root to tmpfs and unmount stateful partition. |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 132 | |
| 133 | Args: |
| 134 | is_fast: whether or not to apply fast wipe. |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 135 | shopfloor_url: for inform_shopfloor.sh |
| 136 | """ |
| 137 | |
Shen-En Shih | 502b310 | 2018-04-24 11:12:01 +0800 | [diff] [blame] | 138 | def _CheckBug78323428(): |
| 139 | # b/78323428: Check if dhcpcd is locking /var/run. If dhcpcd is locking |
| 140 | # /var/run, unmount will fail. Need CL:1021611 to use /run instead. |
| 141 | for pid in Shell('pgrep dhcpcd').stdout.splitlines(): |
| 142 | lock_result = Shell('ls -al /proc/%s/fd | grep /var/run' % pid) |
| 143 | if lock_result.stdout: |
| 144 | raise WipeError('dhcpcd is still locking on /var/run. Please use a ' |
| 145 | 'newer ChromeOS image with CL:1021611 included. ' |
| 146 | 'Lock info: "%s"' % lock_result.stdout) |
| 147 | _CheckBug78323428() |
| 148 | |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 149 | Daemonize() |
| 150 | |
You-Cheng Syu | 461ec03 | 2017-03-06 15:56:58 +0800 | [diff] [blame] | 151 | # Set the default umask. |
Peter Shih | e6afab3 | 2018-09-11 17:16:48 +0800 | [diff] [blame] | 152 | os.umask(0o022) |
Shun-Hsing Ou | b572483 | 2016-07-21 11:45:58 +0800 | [diff] [blame] | 153 | |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 154 | logfile = os.path.join('/tmp', WIPE_IN_TMPFS_LOG) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 155 | ResetLog(logfile) |
| 156 | |
Wei-Han Chen | 0a3320e | 2016-04-23 01:32:07 +0800 | [diff] [blame] | 157 | factory_par = paths.GetFactoryPythonArchivePath() |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 158 | |
| 159 | new_root = tempfile.mkdtemp(prefix='tmpfs.') |
| 160 | binary_deps = [ |
| 161 | 'activate_date', 'backlight_tool', 'busybox', 'cgpt', 'cgpt.bin', |
| 162 | 'clobber-log', 'clobber-state', 'coreutils', 'crossystem', 'dd', |
| 163 | 'display_boot_message', 'dumpe2fs', 'ectool', 'flashrom', 'halt', |
| 164 | 'initctl', 'mkfs.ext4', 'mktemp', 'mosys', 'mount', 'mount-encrypted', |
| 165 | 'od', 'pango-view', 'pkill', 'pv', 'python', 'reboot', 'setterm', 'sh', |
Cheng-Han Yang | 6f12dc4 | 2017-11-30 15:28:38 +0800 | [diff] [blame] | 166 | 'shutdown', 'stop', 'umount', 'vpd', 'curl', 'lsof', 'jq', '/sbin/frecon', |
Cheng Yueh | 3569b75 | 2020-05-18 11:41:17 +0800 | [diff] [blame] | 167 | 'stressapptest', 'fuser', 'login'] |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 168 | |
| 169 | etc_issue = textwrap.dedent(""" |
| 170 | You are now in tmp file system created for in-place wiping. |
| 171 | |
| 172 | For debugging wiping fails, see log files under |
| 173 | /tmp |
| 174 | /mnt/stateful_partition/unencrypted |
| 175 | |
| 176 | The log file name should be |
| 177 | - wipe_in_tmpfs.log |
| 178 | - wipe_init.log |
| 179 | |
| 180 | You can also run scripts under /usr/local/factory/sh for wiping process. |
| 181 | """) |
| 182 | |
Wei-Han Chen | 0a3320e | 2016-04-23 01:32:07 +0800 | [diff] [blame] | 183 | util = Util() |
| 184 | |
| 185 | root_disk = util.GetPrimaryDevicePath() |
| 186 | release_rootfs = util.GetReleaseRootPartitionPath() |
| 187 | state_dev = util.GetPrimaryDevicePath(1) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 188 | wipe_args = 'factory' + (' fast' if is_fast else '') |
| 189 | |
| 190 | logging.debug('state_dev: %s', state_dev) |
| 191 | logging.debug('factory_par: %s', factory_par) |
| 192 | |
| 193 | old_root = 'old_root' |
| 194 | |
| 195 | try: |
| 196 | with chroot.TmpChroot( |
| 197 | new_root, |
| 198 | file_dir_list=[ |
Shun-Hsing Ou | b572483 | 2016-07-21 11:45:58 +0800 | [diff] [blame] | 199 | # Basic rootfs. |
Wei-Ning Huang | 71f94e1 | 2016-07-17 23:21:41 +0800 | [diff] [blame] | 200 | '/bin', '/etc', '/lib', '/lib64', '/root', '/sbin', |
Wei-Han Chen | b05699a | 2017-07-12 16:37:47 +0800 | [diff] [blame] | 201 | '/usr/sbin', '/usr/bin', |
Shun-Hsing Ou | b572483 | 2016-07-21 11:45:58 +0800 | [diff] [blame] | 202 | # Factory related scripts. |
| 203 | factory_par, |
| 204 | '/usr/local/factory/sh', |
Wei-Han Chen | 85ace05 | 2017-06-24 15:39:50 +0800 | [diff] [blame] | 205 | # Factory config files |
| 206 | '/usr/local/factory/py/config', |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 207 | '/usr/share/fonts/notocjk', |
| 208 | '/usr/share/cache/fontconfig', |
| 209 | '/usr/share/chromeos-assets/images', |
| 210 | '/usr/share/chromeos-assets/text/boot_messages', |
| 211 | '/usr/share/misc/chromeos-common.sh', |
Shun-Hsing Ou | b572483 | 2016-07-21 11:45:58 +0800 | [diff] [blame] | 212 | # File required for enable ssh connection. |
| 213 | '/mnt/stateful_partition/etc/ssh', |
| 214 | '/root/.ssh', |
Meng-Huan Yu | 35af9ae | 2020-06-01 16:54:47 +0800 | [diff] [blame] | 215 | '/usr/share/chromeos-ssh-config', |
| 216 | # /mnt/empty is required by openssh server. |
| 217 | '/mnt/empty'], |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 218 | binary_list=binary_deps, etc_issue=etc_issue).PivotRoot(old_root): |
Meng-Huan Yu | 9eca908 | 2020-06-23 16:42:32 +0800 | [diff] [blame^] | 219 | logging.debug('ps -aux: %s', process_utils.SpawnOutput(['ps', '-aux'])) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 220 | logging.debug( |
| 221 | 'lsof: %s', |
| 222 | process_utils.SpawnOutput('lsof -p %d' % os.getpid(), shell=True)) |
| 223 | |
Hung-Te Lin | 6ce54bd | 2017-06-27 16:20:36 +0800 | [diff] [blame] | 224 | # Modify display_wipe_message so we have shells in VT2. |
| 225 | # --dev-mode provides shell with etc-issue. |
| 226 | # --enable-vt1 allows drawing escapes (OSC) on VT1 but it'll also display |
| 227 | # etc-issue and login prompt. |
| 228 | # For now we only want login prompts on VT2+. |
| 229 | process_utils.Spawn(['sed', '-i', |
| 230 | 's/--no-login/--dev-mode/g;s/--enable-vt1//g', |
| 231 | '/usr/sbin/display_boot_message'], |
| 232 | call=True) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 233 | |
| 234 | # Restart gooftool under new root. Since current gooftool might be using |
| 235 | # some resource under stateful partition, restarting gooftool ensures that |
| 236 | # everything new gooftool is using comes from tmpfs and we can safely |
| 237 | # unmount stateful partition. |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 238 | args = [] |
| 239 | if wipe_args: |
| 240 | args += ['--wipe_args', wipe_args] |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 241 | if shopfloor_url: |
| 242 | args += ['--shopfloor_url', shopfloor_url] |
| 243 | if station_ip: |
| 244 | args += ['--station_ip', station_ip] |
| 245 | if station_port: |
| 246 | args += ['--station_port', station_port] |
| 247 | if wipe_finish_token: |
| 248 | args += ['--wipe_finish_token', wipe_finish_token] |
Meng-Huan Yu | 7a4f0f5 | 2020-01-07 20:11:01 +0800 | [diff] [blame] | 249 | if test_umount: |
| 250 | args += ['--test_umount'] |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 251 | args += ['--state_dev', state_dev] |
| 252 | args += ['--release_rootfs', release_rootfs] |
| 253 | args += ['--root_disk', root_disk] |
| 254 | args += ['--old_root', old_root] |
Wei-Han Chen | f392411 | 2019-02-25 14:52:58 +0800 | [diff] [blame] | 255 | if keep_developer_mode_flag: |
| 256 | args += ['--keep_developer_mode_flag_after_clobber_state'] |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 257 | |
| 258 | ExecFactoryPar('gooftool', 'wipe_init', *args) |
Wei-Han Chen | b05699a | 2017-07-12 16:37:47 +0800 | [diff] [blame] | 259 | raise WipeError('Should not reach here') |
Hung-Te Lin | c8174b5 | 2017-06-02 11:11:45 +0800 | [diff] [blame] | 260 | except Exception: |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 261 | logging.exception('wipe_in_place failed') |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 262 | _OnError(station_ip, station_port, wipe_finish_token, state_dev, |
| 263 | wipe_in_tmpfs_log=logfile, wipe_init_log=None) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 264 | raise |
| 265 | |
| 266 | |
| 267 | def _StopAllUpstartJobs(exclude_list=None): |
| 268 | logging.debug('stopping upstart jobs') |
| 269 | |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 270 | if exclude_list is None: |
| 271 | exclude_list = [] |
| 272 | |
Meng-Huan Yu | 54835f2 | 2020-03-04 20:01:47 +0800 | [diff] [blame] | 273 | # Try three times to stop running services because some service will respawn |
| 274 | # one time after being stopped, e.g. shill_respawn. Two times should be enough |
| 275 | # to stop shill. Adding one more try for safety. |
Yilin Yang | bf84d2e | 2020-05-13 10:34:46 +0800 | [diff] [blame] | 276 | for unused_tries in range(3): |
Meng-Huan Yu | 54835f2 | 2020-03-04 20:01:47 +0800 | [diff] [blame] | 277 | |
| 278 | # There may be LOG_PATH optional parameter for upstart job, the initctl |
| 279 | # output may different. The possible output: |
| 280 | # "service_name start/running" |
| 281 | # "service_name ($LOG_PATH) start/running" |
| 282 | initctl_output = process_utils.SpawnOutput(['initctl', 'list']).splitlines() |
| 283 | |
| 284 | running_service_list = [] |
| 285 | for line in initctl_output: |
| 286 | if 'start/running' not in line: |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 287 | continue |
Meng-Huan Yu | 54835f2 | 2020-03-04 20:01:47 +0800 | [diff] [blame] | 288 | |
| 289 | service_name = line.split()[0] |
| 290 | log_path = line.split()[1][1:-1] if '(' in line.split()[1] else '' |
| 291 | running_service_list.append((service_name, log_path)) |
| 292 | |
| 293 | logging.info('Running services (service_name, LOG_PATH): %r', |
| 294 | running_service_list) |
| 295 | |
| 296 | to_stop_service_list = [ |
| 297 | service for service in running_service_list |
| 298 | if not (service[0] in exclude_list or service[0].startswith('console-')) |
| 299 | ] |
| 300 | logging.info('Going to stop services (service_name, LOG_PATH): %r', |
| 301 | to_stop_service_list) |
| 302 | |
| 303 | for service, log_path in to_stop_service_list: |
| 304 | stop_cmd = ['stop', service] |
| 305 | stop_cmd += ["LOG_PATH=" + log_path] if log_path else [] |
Meng-Huan Yu | f2172b7 | 2020-03-04 20:12:09 +0800 | [diff] [blame] | 306 | process_utils.Spawn(stop_cmd, log=True, log_stderr_on_error=True) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 307 | |
| 308 | |
Wei-Han Chen | c8f2456 | 2016-04-23 19:42:42 +0800 | [diff] [blame] | 309 | def _UnmountStatefulPartition(root, state_dev): |
Hung-Te Lin | dd3425d | 2017-07-12 20:10:52 +0800 | [diff] [blame] | 310 | logging.debug('Unmount stateful partition.') |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 311 | |
Hung-Te Lin | dd3425d | 2017-07-12 20:10:52 +0800 | [diff] [blame] | 312 | # Expected stateful partition mount point. |
| 313 | state_dir = os.path.join(root, STATEFUL_PARTITION_PATH.strip(os.path.sep)) |
Wei-Han Chen | b05699a | 2017-07-12 16:37:47 +0800 | [diff] [blame] | 314 | |
Hung-Te Lin | dd3425d | 2017-07-12 20:10:52 +0800 | [diff] [blame] | 315 | # Touch a mark file so we can check if the stateful partition is wiped |
| 316 | # successfully. |
| 317 | file_utils.WriteFile(os.path.join(state_dir, WIPE_MARK_FILE), '') |
| 318 | |
| 319 | # Backup extension cache (crx_cache) if available (will be restored after |
| 320 | # wiping by clobber-state). |
| 321 | crx_cache_path = os.path.join(state_dir, CRX_CACHE_PAYLOAD_NAME) |
| 322 | if os.path.exists(crx_cache_path): |
| 323 | shutil.copyfile(crx_cache_path, CRX_CACHE_TAR_PATH) |
| 324 | |
| 325 | # Find mount points on stateful partition. |
Wei-Han Chen | c8f2456 | 2016-04-23 19:42:42 +0800 | [diff] [blame] | 326 | mount_output = process_utils.SpawnOutput(['mount'], log=True) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 327 | |
Wei-Han Chen | c8f2456 | 2016-04-23 19:42:42 +0800 | [diff] [blame] | 328 | mount_point_list = [] |
| 329 | for line in mount_output.splitlines(): |
| 330 | fields = line.split() |
| 331 | if fields[0] == state_dev: |
| 332 | mount_point_list.append(fields[2]) |
Wei-Han Chen | c8f2456 | 2016-04-23 19:42:42 +0800 | [diff] [blame] | 333 | logging.debug('stateful partitions mounted on: %s', mount_point_list) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 334 | |
Wei-Han Chen | 0a3320e | 2016-04-23 01:32:07 +0800 | [diff] [blame] | 335 | def _ListProcOpening(path_list): |
| 336 | lsof_cmd = ['lsof', '-t'] + path_list |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 337 | return [int(line) |
| 338 | for line in process_utils.SpawnOutput(lsof_cmd).splitlines()] |
| 339 | |
Hung-Te Lin | 09226ef | 2017-01-11 18:00:19 +0800 | [diff] [blame] | 340 | def _ListMinijail(): |
| 341 | # Not sure why, but if we use 'minijail0', then we can't find processes that |
| 342 | # starts with /sbin/minijail0. |
| 343 | list_cmd = ['pgrep', 'minijail'] |
| 344 | return [int(line) |
| 345 | for line in process_utils.SpawnOutput(list_cmd).splitlines()] |
| 346 | |
Hung-Te Lin | dd3425d | 2017-07-12 20:10:52 +0800 | [diff] [blame] | 347 | # Find processes that are using stateful partitions. |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 348 | proc_list = _ListProcOpening(mount_point_list) |
| 349 | |
Wei-Han Chen | c8f2456 | 2016-04-23 19:42:42 +0800 | [diff] [blame] | 350 | if os.getpid() in proc_list: |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 351 | logging.error('wipe_init itself is using stateful partition') |
| 352 | logging.error( |
| 353 | 'lsof: %s', |
| 354 | process_utils.SpawnOutput('lsof -p %d' % os.getpid(), shell=True)) |
Wei-Han Chen | b05699a | 2017-07-12 16:37:47 +0800 | [diff] [blame] | 355 | raise WipeError('wipe_init itself is using stateful partition') |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 356 | |
| 357 | def _KillOpeningBySignal(sig): |
chuntsen | 421b6e2 | 2019-02-19 19:51:24 +0800 | [diff] [blame] | 358 | for mount_point in mount_point_list: |
| 359 | cmd = ['fuser', '-k', '-%d' % sig, '-m', mount_point] |
Meng-Huan Yu | f2172b7 | 2020-03-04 20:12:09 +0800 | [diff] [blame] | 360 | process_utils.Spawn(cmd, call=True, log=True) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 361 | proc_list = _ListProcOpening(mount_point_list) |
| 362 | if not proc_list: |
| 363 | return True # we are done |
| 364 | for pid in proc_list: |
Wei-Han Chen | c8f2456 | 2016-04-23 19:42:42 +0800 | [diff] [blame] | 365 | try: |
| 366 | os.kill(pid, sig) |
Hung-Te Lin | c8174b5 | 2017-06-02 11:11:45 +0800 | [diff] [blame] | 367 | except Exception: |
Wei-Han Chen | c8f2456 | 2016-04-23 19:42:42 +0800 | [diff] [blame] | 368 | logging.exception('killing process %d failed', pid) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 369 | return False # need to check again |
| 370 | |
Hung-Te Lin | dd3425d | 2017-07-12 20:10:52 +0800 | [diff] [blame] | 371 | # Try to kill processes using stateful partition gracefully. |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 372 | sync_utils.Retry(10, 0.1, None, _KillOpeningBySignal, signal.SIGTERM) |
| 373 | sync_utils.Retry(10, 0.1, None, _KillOpeningBySignal, signal.SIGKILL) |
| 374 | |
| 375 | proc_list = _ListProcOpening(mount_point_list) |
| 376 | assert not proc_list, "processes using stateful partition: %s" % proc_list |
| 377 | |
You-Cheng Syu | 2ea26dd | 2016-12-06 20:50:05 +0800 | [diff] [blame] | 378 | def _Unmount(mount_point, critical): |
Hung-Te Lin | 09226ef | 2017-01-11 18:00:19 +0800 | [diff] [blame] | 379 | logging.info('try to unmount %s', mount_point) |
Yilin Yang | bf84d2e | 2020-05-13 10:34:46 +0800 | [diff] [blame] | 380 | for unused_i in range(10): |
You-Cheng Syu | 2ea26dd | 2016-12-06 20:50:05 +0800 | [diff] [blame] | 381 | output = process_utils.Spawn(['umount', '-n', '-R', mount_point], |
Meng-Huan Yu | f2172b7 | 2020-03-04 20:12:09 +0800 | [diff] [blame] | 382 | log=True, |
Meng-Huan Yu | 98f7823 | 2020-02-19 17:40:54 +0800 | [diff] [blame] | 383 | log_stderr_on_error=True).stderr_data |
You-Cheng Syu | 2ea26dd | 2016-12-06 20:50:05 +0800 | [diff] [blame] | 384 | # some mount points need to be unmounted multiple times. |
| 385 | if (output.endswith(': not mounted\n') or |
| 386 | output.endswith(': not found\n')): |
| 387 | return |
| 388 | time.sleep(0.5) |
Hung-Te Lin | 09226ef | 2017-01-11 18:00:19 +0800 | [diff] [blame] | 389 | logging.error('failed to unmount %s', mount_point) |
You-Cheng Syu | 2ea26dd | 2016-12-06 20:50:05 +0800 | [diff] [blame] | 390 | if critical: |
Wei-Han Chen | b05699a | 2017-07-12 16:37:47 +0800 | [diff] [blame] | 391 | raise WipeError('Unmounting %s is critical. Stop.' % mount_point) |
You-Cheng Syu | 2ea26dd | 2016-12-06 20:50:05 +0800 | [diff] [blame] | 392 | |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 393 | if os.path.exists(os.path.join(root, 'dev', 'mapper', 'encstateful')): |
Hung-Te Lin | 09226ef | 2017-01-11 18:00:19 +0800 | [diff] [blame] | 394 | |
| 395 | # minijail will make encstateful busy, but usually we can't just kill them. |
| 396 | # Need to list the processes and solve each-by-each. |
| 397 | proc_list = _ListMinijail() |
| 398 | assert not proc_list, "processes still using minijail: %s" % proc_list |
| 399 | |
You-Cheng Syu | f099046 | 2016-09-07 14:56:19 +0800 | [diff] [blame] | 400 | # Doing what 'mount-encrypted umount' should do. |
| 401 | for mount_point in mount_point_list: |
You-Cheng Syu | 2ea26dd | 2016-12-06 20:50:05 +0800 | [diff] [blame] | 402 | _Unmount(mount_point, False) |
| 403 | _Unmount(os.path.join(root, 'var'), True) |
Jeffy Chen | d5b08e1 | 2017-03-06 10:22:59 +0800 | [diff] [blame] | 404 | process_utils.Spawn(['dmsetup', 'remove', 'encstateful', |
| 405 | '--noudevrules', '--noudevsync'], check_call=True) |
You-Cheng Syu | f099046 | 2016-09-07 14:56:19 +0800 | [diff] [blame] | 406 | process_utils.Spawn(['losetup', '-D'], check_call=True) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 407 | |
Hung-Te Lin | dd3425d | 2017-07-12 20:10:52 +0800 | [diff] [blame] | 408 | # Try to unmount all known mount points. |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 409 | for mount_point in mount_point_list: |
You-Cheng Syu | 2ea26dd | 2016-12-06 20:50:05 +0800 | [diff] [blame] | 410 | _Unmount(mount_point, True) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 411 | process_utils.Spawn(['sync'], call=True) |
| 412 | |
You-Cheng Syu | f099046 | 2016-09-07 14:56:19 +0800 | [diff] [blame] | 413 | # Check if the stateful partition is unmounted successfully. |
Wei-Han Chen | b05699a | 2017-07-12 16:37:47 +0800 | [diff] [blame] | 414 | if _IsStateDevMounted(state_dev): |
| 415 | raise WipeError('Failed to unmount stateful_partition') |
| 416 | |
| 417 | |
| 418 | def _IsStateDevMounted(state_dev): |
| 419 | try: |
| 420 | output = process_utils.CheckOutput(['df', state_dev]) |
| 421 | return output.splitlines()[-1].split()[0] == state_dev |
| 422 | except Exception: |
| 423 | return False |
You-Cheng Syu | f099046 | 2016-09-07 14:56:19 +0800 | [diff] [blame] | 424 | |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 425 | |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 426 | def _InformStation(ip, port, token, wipe_init_log=None, |
| 427 | wipe_in_tmpfs_log=None, success=True): |
| 428 | if not ip: |
| 429 | return |
| 430 | port = int(port) |
| 431 | |
| 432 | logging.debug('inform station %s:%d', ip, port) |
| 433 | |
| 434 | try: |
| 435 | sync_utils.WaitFor( |
Peter Shih | 1445873 | 2018-02-26 14:40:15 +0800 | [diff] [blame] | 436 | lambda: process_utils.Spawn(['ping', '-w1', '-c1', ip], |
| 437 | call=True).returncode == 0, |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 438 | timeout_secs=180, poll_interval=1) |
Hung-Te Lin | c8174b5 | 2017-06-02 11:11:45 +0800 | [diff] [blame] | 439 | except Exception: |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 440 | logging.exception('cannot get network connection...') |
| 441 | else: |
| 442 | sock = socket.socket() |
| 443 | sock.connect((ip, port)) |
| 444 | |
| 445 | response = dict(token=token, success=success) |
| 446 | |
| 447 | if wipe_init_log: |
| 448 | with open(wipe_init_log) as f: |
| 449 | response['wipe_init_log'] = f.read() |
| 450 | |
| 451 | if wipe_in_tmpfs_log: |
| 452 | with open(wipe_in_tmpfs_log) as f: |
| 453 | response['wipe_in_tmpfs_log'] = f.read() |
| 454 | |
| 455 | sock.sendall(json.dumps(response) + '\n') |
| 456 | sock.close() |
| 457 | |
| 458 | |
Wei-Han Chen | f392411 | 2019-02-25 14:52:58 +0800 | [diff] [blame] | 459 | def _WipeStateDev(release_rootfs, root_disk, wipe_args, state_dev, |
| 460 | keep_developer_mode_flag): |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 461 | clobber_state_env = os.environ.copy() |
Wei-Han Chen | 0a3320e | 2016-04-23 01:32:07 +0800 | [diff] [blame] | 462 | clobber_state_env.update(ROOT_DEV=release_rootfs, |
Earl Ou | eeb289d | 2016-11-04 14:36:40 +0800 | [diff] [blame] | 463 | ROOT_DISK=root_disk) |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 464 | logging.debug('clobber-state: root_dev=%s, root_disk=%s', |
Wei-Han Chen | 0a3320e | 2016-04-23 01:32:07 +0800 | [diff] [blame] | 465 | release_rootfs, root_disk) |
Wei-Han Chen | b05699a | 2017-07-12 16:37:47 +0800 | [diff] [blame] | 466 | process_utils.Spawn( |
| 467 | ['clobber-state', wipe_args], env=clobber_state_env, check_call=True) |
| 468 | |
Hung-Te Lin | dd3425d | 2017-07-12 20:10:52 +0800 | [diff] [blame] | 469 | logging.info('Checking if stateful partition is mounted...') |
Wei-Han Chen | b05699a | 2017-07-12 16:37:47 +0800 | [diff] [blame] | 470 | # Check if the stateful partition is wiped. |
| 471 | if not _IsStateDevMounted(state_dev): |
| 472 | process_utils.Spawn(['mount', state_dev, STATEFUL_PARTITION_PATH], |
Hung-Te Lin | dd3425d | 2017-07-12 20:10:52 +0800 | [diff] [blame] | 473 | check_call=True, log=True) |
Wei-Han Chen | b05699a | 2017-07-12 16:37:47 +0800 | [diff] [blame] | 474 | |
Hung-Te Lin | dd3425d | 2017-07-12 20:10:52 +0800 | [diff] [blame] | 475 | logging.info('Checking wipe mark file %s...', WIPE_MARK_FILE) |
Wei-Han Chen | b05699a | 2017-07-12 16:37:47 +0800 | [diff] [blame] | 476 | if os.path.exists( |
| 477 | os.path.join(STATEFUL_PARTITION_PATH, WIPE_MARK_FILE)): |
| 478 | raise WipeError(WIPE_MARK_FILE + ' still exists') |
Hung-Te Lin | dd3425d | 2017-07-12 20:10:52 +0800 | [diff] [blame] | 479 | |
| 480 | # Restore CRX cache. |
| 481 | logging.info('Checking CRX cache %s...', CRX_CACHE_TAR_PATH) |
| 482 | if os.path.exists(CRX_CACHE_TAR_PATH): |
| 483 | process_utils.Spawn(['tar', '-xpvf', CRX_CACHE_TAR_PATH, '-C', |
| 484 | STATEFUL_PARTITION_PATH], check_call=True, log=True) |
Wei-Han Chen | b05699a | 2017-07-12 16:37:47 +0800 | [diff] [blame] | 485 | |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 486 | try: |
Wei-Han Chen | f392411 | 2019-02-25 14:52:58 +0800 | [diff] [blame] | 487 | if not keep_developer_mode_flag: |
| 488 | # Remove developer flag, which is created by clobber-state after wiping. |
| 489 | os.unlink(os.path.join(STATEFUL_PARTITION_PATH, '.developer_mode')) |
| 490 | # Otherwise we don't care. |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 491 | except OSError: |
| 492 | pass |
Hung-Te Lin | dd3425d | 2017-07-12 20:10:52 +0800 | [diff] [blame] | 493 | |
Wei-Han Chen | b05699a | 2017-07-12 16:37:47 +0800 | [diff] [blame] | 494 | process_utils.Spawn(['umount', STATEFUL_PARTITION_PATH], call=True) |
| 495 | # Make sure that everything is synced. |
| 496 | process_utils.Spawn(['sync'], call=True) |
| 497 | time.sleep(3) |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 498 | |
Joel Kitching | 679a00b | 2016-08-03 11:42:58 +0800 | [diff] [blame] | 499 | |
Earl Ou | 564a787 | 2016-10-05 10:22:00 +0800 | [diff] [blame] | 500 | def EnableReleasePartition(release_rootfs): |
| 501 | """Enables a release image partition on disk.""" |
Wei-Han Chen | 0a3320e | 2016-04-23 01:32:07 +0800 | [diff] [blame] | 502 | logging.debug('enable release partition: %s', release_rootfs) |
| 503 | Util().EnableReleasePartition(release_rootfs) |
Earl Ou | 564a787 | 2016-10-05 10:22:00 +0800 | [diff] [blame] | 504 | logging.debug('Device will boot from %s after reboot.', release_rootfs) |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 505 | |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 506 | |
| 507 | def _InformShopfloor(shopfloor_url): |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 508 | if shopfloor_url: |
| 509 | logging.debug('inform shopfloor %s', shopfloor_url) |
Hung-Te Lin | a319546 | 2016-10-14 15:48:29 +0800 | [diff] [blame] | 510 | proc = process_utils.Spawn( |
Yilun Lin | dbb8af7 | 2018-01-31 16:01:17 +0800 | [diff] [blame] | 511 | [ |
| 512 | os.path.join(CUTOFF_SCRIPT_DIR, 'inform_shopfloor.sh'), |
| 513 | shopfloor_url, 'factory_wipe' |
| 514 | ], |
| 515 | read_stdout=True, |
| 516 | read_stderr=True) |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 517 | logging.debug('stdout: %s', proc.stdout_data) |
| 518 | logging.debug('stderr: %s', proc.stderr_data) |
Yilun Lin | dbb8af7 | 2018-01-31 16:01:17 +0800 | [diff] [blame] | 519 | if proc.returncode != 0: |
Peter Shih | bf6f22b | 2018-02-26 14:05:28 +0800 | [diff] [blame] | 520 | raise RuntimeError('InformShopfloor failed.') |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 521 | |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 522 | |
Hung-Te Lin | 7b27f0c | 2016-10-18 18:41:29 +0800 | [diff] [blame] | 523 | def _Cutoff(): |
| 524 | logging.debug('cutoff') |
Hung-Te Lin | a319546 | 2016-10-14 15:48:29 +0800 | [diff] [blame] | 525 | cutoff_script = os.path.join(CUTOFF_SCRIPT_DIR, 'cutoff.sh') |
You-Cheng Syu | e684417 | 2017-11-28 16:39:32 +0800 | [diff] [blame] | 526 | process_utils.Spawn([cutoff_script], check_call=True) |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 527 | |
| 528 | |
Hung-Te Lin | 7b27f0c | 2016-10-18 18:41:29 +0800 | [diff] [blame] | 529 | def WipeInit(wipe_args, shopfloor_url, state_dev, release_rootfs, |
Wei-Han Chen | f392411 | 2019-02-25 14:52:58 +0800 | [diff] [blame] | 530 | root_disk, old_root, station_ip, station_port, finish_token, |
Meng-Huan Yu | 7a4f0f5 | 2020-01-07 20:11:01 +0800 | [diff] [blame] | 531 | keep_developer_mode_flag, test_umount): |
Wei-Han Chen | c8f2456 | 2016-04-23 19:42:42 +0800 | [diff] [blame] | 532 | Daemonize() |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 533 | logfile = '/tmp/wipe_init.log' |
| 534 | ResetLog(logfile) |
Meng-Huan Yu | 7e530ce | 2019-12-23 17:35:57 +0800 | [diff] [blame] | 535 | 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] | 536 | |
Wei-Han Chen | 0a3320e | 2016-04-23 01:32:07 +0800 | [diff] [blame] | 537 | logging.debug('wipe_args: %s', wipe_args) |
Wei-Han Chen | 0a3320e | 2016-04-23 01:32:07 +0800 | [diff] [blame] | 538 | logging.debug('shopfloor_url: %s', shopfloor_url) |
| 539 | logging.debug('state_dev: %s', state_dev) |
| 540 | logging.debug('release_rootfs: %s', release_rootfs) |
| 541 | logging.debug('root_disk: %s', root_disk) |
| 542 | logging.debug('old_root: %s', old_root) |
Meng-Huan Yu | 7a4f0f5 | 2020-01-07 20:11:01 +0800 | [diff] [blame] | 543 | logging.debug('test_umount: %s', test_umount) |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 544 | |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 545 | try: |
Meng-Huan Yu | d4a2c80 | 2020-05-13 15:46:31 +0800 | [diff] [blame] | 546 | # Enable upstart log under /var/log/upstart.log for Tast. |
| 547 | process_utils.Spawn(['initctl', 'log-priority', 'info'], |
| 548 | log=True, |
| 549 | log_stderr_on_error=True) |
| 550 | |
Shun-Hsing Ou | b572483 | 2016-07-21 11:45:58 +0800 | [diff] [blame] | 551 | _StopAllUpstartJobs(exclude_list=[ |
| 552 | # Milestone marker that use to determine the running of other services. |
| 553 | 'boot-services', |
| 554 | 'system-services', |
| 555 | 'failsafe', |
| 556 | # Keep dbus to make sure we can shutdown the device. |
| 557 | 'dbus', |
| 558 | # Keep shill for connecting to shopfloor or stations. |
| 559 | 'shill', |
cyueh | efe3cf9 | 2020-01-07 12:01:22 +0800 | [diff] [blame] | 560 | # Keep wpasupplicant since shopfloor may connect over WiFi. |
| 561 | 'wpasupplicant', |
Shun-Hsing Ou | b572483 | 2016-07-21 11:45:58 +0800 | [diff] [blame] | 562 | # Keep openssh-server for debugging purpose. |
| 563 | 'openssh-server', |
| 564 | # sslh is a service in ARC++ for muxing between ssh and adb. |
| 565 | 'sslh' |
Peter Shih | e6afab3 | 2018-09-11 17:16:48 +0800 | [diff] [blame] | 566 | ]) |
Wei-Han Chen | c8f2456 | 2016-04-23 19:42:42 +0800 | [diff] [blame] | 567 | _UnmountStatefulPartition(old_root, state_dev) |
Wei-Han Chen | 9adf9de | 2016-04-01 19:35:41 +0800 | [diff] [blame] | 568 | |
Meng-Huan Yu | 7a4f0f5 | 2020-01-07 20:11:01 +0800 | [diff] [blame] | 569 | # When testing, stop the wiping process with no error. In normal |
| 570 | # process, this function will run forever until reboot. |
| 571 | if test_umount: |
| 572 | logging.info('Finished unmount, stop wiping process because test_umount ' |
| 573 | 'is set.') |
| 574 | return |
| 575 | |
Meng-Huan Yu | a97e44b | 2020-02-14 17:07:36 +0800 | [diff] [blame] | 576 | # The following code could not be executed when factory is not installed |
| 577 | # due to lacking of CUTOFF_SCRIPT_DIR. |
| 578 | process_utils.Spawn( |
| 579 | [os.path.join(CUTOFF_SCRIPT_DIR, 'display_wipe_message.sh'), 'wipe'], |
| 580 | call=True) |
| 581 | |
Wei-Han Chen | b05699a | 2017-07-12 16:37:47 +0800 | [diff] [blame] | 582 | try: |
Wei-Han Chen | f392411 | 2019-02-25 14:52:58 +0800 | [diff] [blame] | 583 | _WipeStateDev(release_rootfs, root_disk, wipe_args, state_dev, |
| 584 | keep_developer_mode_flag) |
Wei-Han Chen | b05699a | 2017-07-12 16:37:47 +0800 | [diff] [blame] | 585 | except Exception: |
| 586 | process_utils.Spawn( |
| 587 | [os.path.join(CUTOFF_SCRIPT_DIR, 'display_wipe_message.sh'), |
| 588 | 'wipe_failed'], call=True) |
| 589 | raise |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 590 | |
Earl Ou | 564a787 | 2016-10-05 10:22:00 +0800 | [diff] [blame] | 591 | EnableReleasePartition(release_rootfs) |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 592 | |
| 593 | _InformShopfloor(shopfloor_url) |
| 594 | |
| 595 | _InformStation(station_ip, station_port, finish_token, |
| 596 | wipe_init_log=logfile, |
| 597 | wipe_in_tmpfs_log=wipe_in_tmpfs_log, |
| 598 | success=True) |
| 599 | |
Hung-Te Lin | 7b27f0c | 2016-10-18 18:41:29 +0800 | [diff] [blame] | 600 | _Cutoff() |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 601 | |
| 602 | # should not reach here |
Hung-Te Lin | dd3425d | 2017-07-12 20:10:52 +0800 | [diff] [blame] | 603 | logging.info('Going to sleep forever!') |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 604 | time.sleep(1e8) |
Hung-Te Lin | c8174b5 | 2017-06-02 11:11:45 +0800 | [diff] [blame] | 605 | except Exception: |
Meng-Huan Yu | d4a2c80 | 2020-05-13 15:46:31 +0800 | [diff] [blame] | 606 | # This error message is used to detect error in Factory.Finalize Tast test. |
| 607 | # Keep sync if changed this. |
Wei-Han Chen | e97d353 | 2016-03-31 19:22:01 +0800 | [diff] [blame] | 608 | logging.exception('wipe_init failed') |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 609 | _OnError(station_ip, station_port, finish_token, state_dev, |
| 610 | 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] | 611 | raise |