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
5import logging
6import os
7
8from autotest_lib.client.bin import utils, test
9from autotest_lib.client.common_lib import error
10
11ROOTFS_SIZE = 2 * 1024 * 1024 * 1024
12
13class platform_PartitionCheck(test.test):
14    """
15    Verify partition size is correct.
16    """
17    version = 1
18
19    def isRemovable(self, device):
20        """
21        Check if the block device is removable.
22
23        Args:
24            @param device: string, name of the block device.
25
26        Returns:
27            bool, True if device is removable.
28        """
29
30        # Construct a pathname to 'removable' for this device
31        removable_file = os.path.join('/sys', 'block', device, 'removable')
32        return int(utils.read_one_line(removable_file)) == 1
33
34    def get_block_size(self, device):
35        """
36        Check the block size of a block device.
37
38        Args:
39            @param device: string, name of the block device.
40
41        Returns:
42            int, size of block in bytes.
43        """
44
45        # Construct a pathname to find the logic block size for this device
46        sysfs_path = os.path.join('/sys', 'block', device,
47                                  'queue', 'logical_block_size')
48
49        return int(utils.read_one_line(sysfs_path))
50
51    def get_partition_size(self, device, partition):
52        """
53        Get the number of blocks in the partition.
54
55        Args:
56            @param device: string, name of the block device.
57            @param partition: string, partition name
58
59        Returns:
60            int, number of blocks
61        """
62
63        part_file = os.path.join('/sys', 'block', device, partition, 'size')
64        part_blocks = int(utils.read_one_line(part_file))
65        return part_blocks
66
67    def run_once(self):
68        errors = []
69        mmcpath = '/sys/block/mmcblk0'
70
71        if os.path.exists(mmcpath) and (not self.isRemovable('mmcblk0')):
72            device = 'mmcblk0'
73            partitions = ['mmcblk0p3', 'mmcblk0p5']
74        else:
75            device = 'sda'
76            partitions = ['sda3', 'sda5']
77
78        block_size = self.get_block_size(device)
79
80        for p in partitions:
81            pblocks = self.get_partition_size(device, p)
82            psize = pblocks * block_size
83            if psize != ROOTFS_SIZE:
84                errmsg = ('%s is %d bytes, expected %d' %
85                          (p, psize, ROOTFS_SIZE))
86                logging.warning(errmsg)
87                errors.append(errmsg)
88
89        # If self.error is not zero, there were errors.
90        if errors:
91            raise error.TestFail('There were %d partition errors: %s' %
92                                 (len(errors), ': '.join(errors)))
93