• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2014 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
5from autotest_lib.client.common_lib import error
6from autotest_lib.server import test
7
8import logging
9
10
11POWERWASH_COUNT = '/mnt/stateful_partition/unencrypted/preserve/powerwash_count'
12
13POWERWASH_MARKER_FILE = '/mnt/stateful_partition/factory_install_reset'
14
15POWERWASH_COMMAND = 'safe fast keepimg'
16
17STATEFUL_MARKER_FILE = '/mnt/stateful_partition/platform_Powerwash_flag'
18
19# Log files to help debugging what happened during last clobbering (powerwash).
20CLOBBER_STATE_LOG_FILE = '/mnt/stateful_partition/unencrypted/clobber-state.log'
21CLOBBER_LOG_FILE = '/mnt/stateful_partition/unencrypted/clobber.log'
22
23
24class platform_Powerwash(test.test):
25    """Powerwash a device."""
26    version = 1
27
28    def run_once(self, host):
29        self._host = host
30
31        count_before = self._powerwash_count()
32
33        # We create a file on the stateful partition to test if it is deleted
34        # during the powerwash.
35        self._host.run('echo car > %s' % STATEFUL_MARKER_FILE)
36
37        logging.debug('Signaling powerwash on the device.')
38        self._mark_powerwash()
39        self._host.reboot()
40
41        # Check if the marker file still exists on the stateful partition.
42        # The powerwash cycle should remove it.
43        marker = self._host.run('[ -e %s ]' % STATEFUL_MARKER_FILE,
44                                ignore_status=True, ignore_timeout=True)
45
46        # If "[ -e file ]" finishes with status 0, the file is present.
47        if marker is None or marker.exit_status == 0:
48            raise error.TestFail("Powerwash cycle didn't remove the marker "
49                                 "file on the stateful partition.")
50
51        # Capture powerwash logs.
52        logging.debug('Powerwash logs: %r', self._host.run(
53                'cat %s %s 2>/dev/null' % (CLOBBER_LOG_FILE,
54                                           CLOBBER_STATE_LOG_FILE),
55                ignore_status=True).stdout.strip())
56
57        # Check the powerwash counter before and after the powerwash to verify
58        # it was incremented. This file should be preserved by the powerwash.
59        count_after = self._powerwash_count()
60        if count_after != count_before + 1:
61            raise error.TestFail("Powerwash count didn't increase after "
62                                 "powerwash cycle.")
63
64
65    def _mark_powerwash(self, command=None):
66        """Creates the Powerwash marker file on the host with the given command.
67
68        @param command: The text to include on the marker file, *not* including
69                        the '\n' at the end.
70        """
71        if command is None:
72            command = POWERWASH_COMMAND
73        self._host.run("echo '%s' > %s" % (command, POWERWASH_MARKER_FILE))
74
75
76    def _powerwash_count(self):
77        """Return the powerwash count from the DUT."""
78        count = self._host.run('test -e %s && cat %s || true' %
79                               (POWERWASH_COUNT,
80                                POWERWASH_COUNT)).stdout.strip()
81        logging.debug('Powerwash count is: %r', count)
82        if count:
83            return int(count)
84        return 0
85