1# Copyright (c) 2010 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
5import os
6import re
7
8from autotest_lib.client.bin import site_utils, test, utils
9from autotest_lib.client.common_lib import error
10
11class hardware_SsdDetection(test.test):
12    version = 1
13    # Keep a list of boards that are expected to ship with hard drive.
14    boards_with_hdd = ['butterfly', 'kiev', 'parrot', 'stout']
15
16    def setup(self):
17        # create a empty srcdir to prevent the error that checks .version file
18        if not os.path.exists(self.srcdir):
19            utils.system('mkdir %s' % self.srcdir)
20
21
22    def run_once(self, check_link_speed=()):
23        # Use rootdev to find the underlying block device even if the
24        # system booted to /dev/dm-0.
25        device = site_utils.get_root_device()
26
27        # Check the device is fixed
28
29        def IsFixed(dev):
30            sysfs_path = '/sys/block/%s/removable' % dev
31            return (os.path.exists(sysfs_path) and
32                    open(sysfs_path).read().strip() == '0')
33
34        alpha_re = re.compile(r'^/dev/([a-zA-Z]+)$')
35        alnum_re = re.compile(r'^/dev/([a-zA-Z]+[0-9]+)$')
36        dev = alpha_re.findall(device) + alnum_re.findall(device)
37        if len(dev) != 1 or not IsFixed(dev[0]):
38            raise error.TestFail('The main disk %s is not fixed' % dev)
39
40        # If it is an mmcblk device, then it is SSD.
41        # Else run hdparm to check for SSD.
42
43        if re.search("mmcblk", device):
44            return
45
46        hdparm = utils.run('/sbin/hdparm -I %s' % device)
47
48        # Check if device is a SSD
49        match = re.search(r'Nominal Media Rotation Rate: (.+)$',
50                          hdparm.stdout, re.MULTILINE)
51        if match and match.group(1):
52            if match.group(1) != 'Solid State Device':
53                if utils.get_board() in self.boards_with_hdd:
54                    return
55                raise error.TestFail('The main disk is not a SSD, '
56                    'Rotation Rate: %s' % match.group(1))
57        else:
58            raise error.TestFail(
59                'Rotation Rate not reported from the device, '
60                'unable to ensure it is a SSD')
61
62        # Check if SSD is > 8GB in size
63        match = re.search("device size with M = 1000\*1000: (.+) MBytes",
64                          hdparm.stdout, re.MULTILINE)
65        if match and match.group(1):
66            size = int(match.group(1))
67            self.write_perf_keyval({"mb_ssd_device_size" : size})
68        else:
69            raise error.TestFail(
70                'Device size info missing from the device')
71
72        # Check supported link speed.
73        #
74        # check_link_speed is an empty tuple by default, which does not perform
75        # link speed checking.  You can run the test while specifying
76        # check_link_speed=('1.5Gb/s', '3.0Gb/s') to check the 2 signaling
77        # speeds are both supported.
78        for link_speed in check_link_speed:
79            if not re.search(r'Gen. signaling speed \(%s\)' % link_speed,
80                             hdparm.stdout, re.MULTILINE):
81                raise error.TestFail('Link speed %s not supported' % link_speed)
82