blob: cd013ad94c9c3e7844c9ec8990dae326c75e7217 [file] [log] [blame]
Wei-Han Chene97d3532016-03-31 19:22:01 +08001#!/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 Kitching679a00b2016-08-03 11:42:58 +08007"""Transition to release state directly without reboot."""
Wei-Han Chene97d3532016-03-31 19:22:01 +08008
Wei-Han Chenbe1355a2016-04-24 19:31:03 +08009import json
Wei-Han Chene97d3532016-03-31 19:22:01 +080010import logging
Wei-Han Chene97d3532016-03-31 19:22:01 +080011import os
Shun-Hsing Oub5724832016-07-21 11:45:58 +080012import re
Wei-Han Chene97d3532016-03-31 19:22:01 +080013import resource
14import shutil
15import signal
Wei-Han Chenbe1355a2016-04-24 19:31:03 +080016import socket
Wei-Han Chene97d3532016-03-31 19:22:01 +080017import tempfile
18import textwrap
19import time
20
21import factory_common # pylint: disable=unused-import
22from cros.factory.gooftool import chroot
Wei-Han Chen0a3320e2016-04-23 01:32:07 +080023from cros.factory.gooftool.common import ExecFactoryPar
Wei-Han Chen9adf9de2016-04-01 19:35:41 +080024from cros.factory.gooftool.common import Util
Wei-Han Chen0a3320e2016-04-23 01:32:07 +080025from cros.factory.test.env import paths
Wei-Han Chene97d3532016-03-31 19:22:01 +080026from cros.factory.utils import process_utils
27from cros.factory.utils import sync_utils
28from cros.factory.utils import sys_utils
29
30
Hung-Te Lina3195462016-10-14 15:48:29 +080031"""Directory of scripts for device cut-off"""
32CUTOFF_SCRIPT_DIR = '/usr/local/factory/sh/cutoff'
Wei-Han Chen9adf9de2016-04-01 19:35:41 +080033
Wei-Han Chenbe1355a2016-04-24 19:31:03 +080034WIPE_IN_TMPFS_LOG = 'wipe_in_tmpfs.log'
Wei-Han Chene97d3532016-03-31 19:22:01 +080035
Joel Kitching679a00b2016-08-03 11:42:58 +080036
Wei-Han Chenbe1355a2016-04-24 19:31:03 +080037def _CopyLogFileToStateDev(state_dev, logfile):
Wei-Han Chene97d3532016-03-31 19:22:01 +080038 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 Chenbe1355a2016-04-24 19:31:03 +080045def _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 Chene97d3532016-03-31 19:22:01 +080057def 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
111def 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 Lin7b27f0c2016-10-18 18:41:29 +0800118def WipeInTmpFs(is_fast=None, shopfloor_url=None, station_ip=None,
119 station_port=None, wipe_finish_token=None):
Wei-Han Chene97d3532016-03-31 19:22:01 +0800120 """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 Chene97d3532016-03-31 19:22:01 +0800124 shopfloor_url: for inform_shopfloor.sh
125 """
126
Wei-Han Chene97d3532016-03-31 19:22:01 +0800127 Daemonize()
128
Shun-Hsing Oub5724832016-07-21 11:45:58 +0800129 # Set the defual umask.
130 os.umask(0022)
131
Wei-Han Chenbe1355a2016-04-24 19:31:03 +0800132 logfile = os.path.join('/tmp', WIPE_IN_TMPFS_LOG)
Wei-Han Chene97d3532016-03-31 19:22:01 +0800133 ResetLog(logfile)
134
Wei-Han Chen0a3320e2016-04-23 01:32:07 +0800135 factory_par = paths.GetFactoryPythonArchivePath()
Wei-Han Chene97d3532016-03-31 19:22:01 +0800136
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 Lin0ce13392016-12-30 12:36:29 +0800144 'shutdown', 'stop', 'umount', 'vpd', 'wget', 'lsof', 'jq']
Wei-Han Chene97d3532016-03-31 19:22:01 +0800145 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 Chen0a3320e2016-04-23 01:32:07 +0800164 util = Util()
165
166 root_disk = util.GetPrimaryDevicePath()
167 release_rootfs = util.GetReleaseRootPartitionPath()
168 state_dev = util.GetPrimaryDevicePath(1)
Wei-Han Chene97d3532016-03-31 19:22:01 +0800169 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 Oub5724832016-07-21 11:45:58 +0800177 # 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 Chene97d3532016-03-31 19:22:01 +0800185 with chroot.TmpChroot(
186 new_root,
187 file_dir_list=[
Shun-Hsing Oub5724832016-07-21 11:45:58 +0800188 # Basic rootfs.
Wei-Ning Huang71f94e12016-07-17 23:21:41 +0800189 '/bin', '/etc', '/lib', '/lib64', '/root', '/sbin',
Shun-Hsing Oub5724832016-07-21 11:45:58 +0800190 # Factory related scripts.
191 factory_par,
192 '/usr/local/factory/sh',
193 # Fonts and assets required for showing message.
194 pango_module,
Wei-Han Chene97d3532016-03-31 19:22:01 +0800195 '/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 Oub5724832016-07-21 11:45:58 +0800200 # 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 Chene97d3532016-03-31 19:22:01 +0800206 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 Chene97d3532016-03-31 19:22:01 +0800211 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 Chenbe1355a2016-04-24 19:31:03 +0800218 args = []
219 if wipe_args:
220 args += ['--wipe_args', wipe_args]
Wei-Han Chenbe1355a2016-04-24 19:31:03 +0800221 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 Chene97d3532016-03-31 19:22:01 +0800235 raise RuntimeError('Should not reach here')
236 except: # pylint: disable=bare-except
237 logging.exception('wipe_in_place failed')
Wei-Han Chenbe1355a2016-04-24 19:31:03 +0800238 _OnError(station_ip, station_port, wipe_finish_token, state_dev,
239 wipe_in_tmpfs_log=logfile, wipe_init_log=None)
Wei-Han Chene97d3532016-03-31 19:22:01 +0800240 raise
241
242
243def _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 Chenc8f24562016-04-23 19:42:42 +0800258 if service in exclude_list or service.startswith('console-'):
Wei-Han Chene97d3532016-03-31 19:22:01 +0800259 continue
Wei-Han Chenc8f24562016-04-23 19:42:42 +0800260 process_utils.Spawn(['stop', service], call=True, log=True)
Wei-Han Chene97d3532016-03-31 19:22:01 +0800261
262
Wei-Han Chenc8f24562016-04-23 19:42:42 +0800263def _UnmountStatefulPartition(root, state_dev):
Wei-Han Chene97d3532016-03-31 19:22:01 +0800264 logging.debug('unmount stateful partition')
Wei-Han Chene97d3532016-03-31 19:22:01 +0800265 # mount points that need chromeos_shutdown to umount
266
267 # 1. find mount points on stateful partition
Wei-Han Chenc8f24562016-04-23 19:42:42 +0800268 mount_output = process_utils.SpawnOutput(['mount'], log=True)
Wei-Han Chene97d3532016-03-31 19:22:01 +0800269
Wei-Han Chenc8f24562016-04-23 19:42:42 +0800270 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 Chene97d3532016-03-31 19:22:01 +0800277 # 2. find processes that are using stateful partitions
278
Wei-Han Chen0a3320e2016-04-23 01:32:07 +0800279 def _ListProcOpening(path_list):
280 lsof_cmd = ['lsof', '-t'] + path_list
Wei-Han Chene97d3532016-03-31 19:22:01 +0800281 return [int(line)
282 for line in process_utils.SpawnOutput(lsof_cmd).splitlines()]
283
Hung-Te Lin09226ef2017-01-11 18:00:19 +0800284 def _ListMinijail():
285 # Not sure why, but if we use 'minijail0', then we can't find processes that
286 # starts with /sbin/minijail0.
287 list_cmd = ['pgrep', 'minijail']
288 return [int(line)
289 for line in process_utils.SpawnOutput(list_cmd).splitlines()]
290
Wei-Han Chene97d3532016-03-31 19:22:01 +0800291 proc_list = _ListProcOpening(mount_point_list)
292
Wei-Han Chenc8f24562016-04-23 19:42:42 +0800293 if os.getpid() in proc_list:
Wei-Han Chene97d3532016-03-31 19:22:01 +0800294 logging.error('wipe_init itself is using stateful partition')
295 logging.error(
296 'lsof: %s',
297 process_utils.SpawnOutput('lsof -p %d' % os.getpid(), shell=True))
298 raise RuntimeError('using stateful partition')
299
300 def _KillOpeningBySignal(sig):
301 proc_list = _ListProcOpening(mount_point_list)
302 if not proc_list:
303 return True # we are done
304 for pid in proc_list:
Wei-Han Chenc8f24562016-04-23 19:42:42 +0800305 try:
306 os.kill(pid, sig)
307 except: # pylint: disable=bare-except
308 logging.exception('killing process %d failed', pid)
Wei-Han Chene97d3532016-03-31 19:22:01 +0800309 return False # need to check again
310
311 sync_utils.Retry(10, 0.1, None, _KillOpeningBySignal, signal.SIGTERM)
312 sync_utils.Retry(10, 0.1, None, _KillOpeningBySignal, signal.SIGKILL)
313
314 proc_list = _ListProcOpening(mount_point_list)
315 assert not proc_list, "processes using stateful partition: %s" % proc_list
316
You-Cheng Syu2ea26dd2016-12-06 20:50:05 +0800317 def _Unmount(mount_point, critical):
Hung-Te Lin09226ef2017-01-11 18:00:19 +0800318 logging.info('try to unmount %s', mount_point)
You-Cheng Syu2ea26dd2016-12-06 20:50:05 +0800319 for unused_i in xrange(10):
320 output = process_utils.Spawn(['umount', '-n', '-R', mount_point],
321 read_stderr=True, log=True).stderr_data
322 # some mount points need to be unmounted multiple times.
323 if (output.endswith(': not mounted\n') or
324 output.endswith(': not found\n')):
325 return
326 time.sleep(0.5)
Hung-Te Lin09226ef2017-01-11 18:00:19 +0800327 logging.error('failed to unmount %s', mount_point)
You-Cheng Syu2ea26dd2016-12-06 20:50:05 +0800328 if critical:
329 raise RuntimeError('Unmounting %s is critical. Stop.')
330
Wei-Han Chene97d3532016-03-31 19:22:01 +0800331 if os.path.exists(os.path.join(root, 'dev', 'mapper', 'encstateful')):
Hung-Te Lin09226ef2017-01-11 18:00:19 +0800332
333 # minijail will make encstateful busy, but usually we can't just kill them.
334 # Need to list the processes and solve each-by-each.
335 proc_list = _ListMinijail()
336 assert not proc_list, "processes still using minijail: %s" % proc_list
337
You-Cheng Syuf0990462016-09-07 14:56:19 +0800338 # Doing what 'mount-encrypted umount' should do.
339 for mount_point in mount_point_list:
You-Cheng Syu2ea26dd2016-12-06 20:50:05 +0800340 _Unmount(mount_point, False)
341 _Unmount(os.path.join(root, 'var'), True)
You-Cheng Syuf0990462016-09-07 14:56:19 +0800342 process_utils.Spawn(['dmsetup', 'remove', 'encstateful'], check_call=True)
343 process_utils.Spawn(['losetup', '-D'], check_call=True)
Wei-Han Chene97d3532016-03-31 19:22:01 +0800344
345 for mount_point in mount_point_list:
You-Cheng Syu2ea26dd2016-12-06 20:50:05 +0800346 _Unmount(mount_point, True)
Wei-Han Chene97d3532016-03-31 19:22:01 +0800347 process_utils.Spawn(['sync'], call=True)
348
You-Cheng Syuf0990462016-09-07 14:56:19 +0800349 # Check if the stateful partition is unmounted successfully.
350 process_utils.Spawn(r'mount | grep -c "^\S*stateful" | grep -q ^0$',
351 shell=True, check_call=True)
352
Wei-Han Chene97d3532016-03-31 19:22:01 +0800353
Wei-Han Chenbe1355a2016-04-24 19:31:03 +0800354def _InformStation(ip, port, token, wipe_init_log=None,
355 wipe_in_tmpfs_log=None, success=True):
356 if not ip:
357 return
358 port = int(port)
359
360 logging.debug('inform station %s:%d', ip, port)
361
362 try:
363 sync_utils.WaitFor(
364 lambda: 0 == process_utils.Spawn(['ping', '-w1', '-c1', ip],
365 call=True).returncode,
366 timeout_secs=180, poll_interval=1)
367 except: # pylint: disable=bare-except
368 logging.exception('cannot get network connection...')
369 else:
370 sock = socket.socket()
371 sock.connect((ip, port))
372
373 response = dict(token=token, success=success)
374
375 if wipe_init_log:
376 with open(wipe_init_log) as f:
377 response['wipe_init_log'] = f.read()
378
379 if wipe_in_tmpfs_log:
380 with open(wipe_in_tmpfs_log) as f:
381 response['wipe_in_tmpfs_log'] = f.read()
382
383 sock.sendall(json.dumps(response) + '\n')
384 sock.close()
385
386
387def _WipeStateDev(release_rootfs, root_disk, wipe_args):
Wei-Han Chen9adf9de2016-04-01 19:35:41 +0800388 stateful_partition_path = '/mnt/stateful_partition'
389
Wei-Han Chen9adf9de2016-04-01 19:35:41 +0800390 clobber_state_env = os.environ.copy()
Wei-Han Chen0a3320e2016-04-23 01:32:07 +0800391 clobber_state_env.update(ROOT_DEV=release_rootfs,
Earl Oueeb289d2016-11-04 14:36:40 +0800392 ROOT_DISK=root_disk)
Wei-Han Chen9adf9de2016-04-01 19:35:41 +0800393 logging.debug('clobber-state: root_dev=%s, root_disk=%s',
Wei-Han Chen0a3320e2016-04-23 01:32:07 +0800394 release_rootfs, root_disk)
Wei-Han Chen9adf9de2016-04-01 19:35:41 +0800395 process_utils.Spawn(['clobber-state', wipe_args], env=clobber_state_env,
396 call=True)
Wei-Han Chen9adf9de2016-04-01 19:35:41 +0800397 # remove developer flag, which is created by clobber-state after wiping.
398 try:
Wei-Han Chene5b19f42016-10-21 14:45:10 +0800399 os.unlink(os.path.join(stateful_partition_path, '.developer_mode'))
Wei-Han Chen9adf9de2016-04-01 19:35:41 +0800400 except OSError:
401 pass
Wei-Han Chene5b19f42016-10-21 14:45:10 +0800402 # make sure that everything is synced
403 for unused_i in xrange(3):
404 process_utils.Spawn(['sync'], call=True)
405 time.sleep(1)
Wei-Han Chen9adf9de2016-04-01 19:35:41 +0800406
Joel Kitching679a00b2016-08-03 11:42:58 +0800407
Earl Ou564a7872016-10-05 10:22:00 +0800408def EnableReleasePartition(release_rootfs):
409 """Enables a release image partition on disk."""
Wei-Han Chen0a3320e2016-04-23 01:32:07 +0800410 logging.debug('enable release partition: %s', release_rootfs)
411 Util().EnableReleasePartition(release_rootfs)
Earl Ou564a7872016-10-05 10:22:00 +0800412 logging.debug('Device will boot from %s after reboot.', release_rootfs)
Wei-Han Chen9adf9de2016-04-01 19:35:41 +0800413
Wei-Han Chenbe1355a2016-04-24 19:31:03 +0800414
415def _InformShopfloor(shopfloor_url):
Wei-Han Chen9adf9de2016-04-01 19:35:41 +0800416 if shopfloor_url:
417 logging.debug('inform shopfloor %s', shopfloor_url)
Hung-Te Lina3195462016-10-14 15:48:29 +0800418 proc = process_utils.Spawn(
419 [os.path.join(CUTOFF_SCRIPT_DIR, 'inform_shopfloor.sh'), shopfloor_url,
420 'factory_wipe'], check_call=True)
Wei-Han Chen9adf9de2016-04-01 19:35:41 +0800421 logging.debug('stdout: %s', proc.stdout_data)
422 logging.debug('stderr: %s', proc.stderr_data)
423
Wei-Han Chenbe1355a2016-04-24 19:31:03 +0800424
Hung-Te Lin7b27f0c2016-10-18 18:41:29 +0800425def _Cutoff():
426 logging.debug('cutoff')
Hung-Te Lina3195462016-10-14 15:48:29 +0800427 cutoff_script = os.path.join(CUTOFF_SCRIPT_DIR, 'cutoff.sh')
Hung-Te Lin7b27f0c2016-10-18 18:41:29 +0800428 process_utils.Spawn('%s' % (cutoff_script), shell=True, check_call=True)
Wei-Han Chen9adf9de2016-04-01 19:35:41 +0800429
430
Hung-Te Lin7b27f0c2016-10-18 18:41:29 +0800431def WipeInit(wipe_args, shopfloor_url, state_dev, release_rootfs,
Wei-Han Chenbe1355a2016-04-24 19:31:03 +0800432 root_disk, old_root, station_ip, station_port, finish_token):
Wei-Han Chenc8f24562016-04-23 19:42:42 +0800433 Daemonize()
Wei-Han Chene97d3532016-03-31 19:22:01 +0800434 logfile = '/tmp/wipe_init.log'
Wei-Han Chenbe1355a2016-04-24 19:31:03 +0800435 wipe_in_tmpfs_log = os.path.join(old_root, 'tmp', WIPE_IN_TMPFS_LOG)
Wei-Han Chene97d3532016-03-31 19:22:01 +0800436 ResetLog(logfile)
437
Wei-Han Chen0a3320e2016-04-23 01:32:07 +0800438 logging.debug('wipe_args: %s', wipe_args)
Wei-Han Chen0a3320e2016-04-23 01:32:07 +0800439 logging.debug('shopfloor_url: %s', shopfloor_url)
440 logging.debug('state_dev: %s', state_dev)
441 logging.debug('release_rootfs: %s', release_rootfs)
442 logging.debug('root_disk: %s', root_disk)
443 logging.debug('old_root: %s', old_root)
Wei-Han Chene97d3532016-03-31 19:22:01 +0800444
Wei-Han Chene97d3532016-03-31 19:22:01 +0800445 try:
Shun-Hsing Oub5724832016-07-21 11:45:58 +0800446 _StopAllUpstartJobs(exclude_list=[
447 # Milestone marker that use to determine the running of other services.
448 'boot-services',
449 'system-services',
450 'failsafe',
451 # Keep dbus to make sure we can shutdown the device.
452 'dbus',
453 # Keep shill for connecting to shopfloor or stations.
454 'shill',
455 # Keep openssh-server for debugging purpose.
456 'openssh-server',
457 # sslh is a service in ARC++ for muxing between ssh and adb.
458 'sslh'
459 ])
Wei-Han Chenc8f24562016-04-23 19:42:42 +0800460 _UnmountStatefulPartition(old_root, state_dev)
Wei-Han Chen9adf9de2016-04-01 19:35:41 +0800461
Hung-Te Lina3195462016-10-14 15:48:29 +0800462 process_utils.Spawn(
463 [os.path.join(CUTOFF_SCRIPT_DIR, 'display_wipe_message.sh'), 'wipe'],
464 call=True)
Wei-Han Chen9adf9de2016-04-01 19:35:41 +0800465
Wei-Han Chenbe1355a2016-04-24 19:31:03 +0800466 _WipeStateDev(release_rootfs, root_disk, wipe_args)
467
Earl Ou564a7872016-10-05 10:22:00 +0800468 EnableReleasePartition(release_rootfs)
Wei-Han Chenbe1355a2016-04-24 19:31:03 +0800469
470 _InformShopfloor(shopfloor_url)
471
472 _InformStation(station_ip, station_port, finish_token,
473 wipe_init_log=logfile,
474 wipe_in_tmpfs_log=wipe_in_tmpfs_log,
475 success=True)
476
Hung-Te Lin7b27f0c2016-10-18 18:41:29 +0800477 _Cutoff()
Wei-Han Chenbe1355a2016-04-24 19:31:03 +0800478
479 # should not reach here
480 time.sleep(1e8)
Wei-Han Chene97d3532016-03-31 19:22:01 +0800481 except: # pylint: disable=bare-except
482 logging.exception('wipe_init failed')
Wei-Han Chenbe1355a2016-04-24 19:31:03 +0800483 _OnError(station_ip, station_port, finish_token, state_dev,
484 wipe_in_tmpfs_log=wipe_in_tmpfs_log, wipe_init_log=logfile)
Wei-Han Chene97d3532016-03-31 19:22:01 +0800485 raise