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 test, utils
9from autotest_lib.client.common_lib import error
10
11ROOTFS_SIZE_2G = 2 * 1024 * 1024 * 1024
12ROOTFS_SIZE_4G = 4 * 1024 * 1024 * 1024
13
14class platform_PartitionCheck(test.test):
15    """
16    Verify partition size is correct.
17    """
18    version = 1
19
20    def get_block_size(self, device):
21        """
22        Check the block size of a block device.
23
24        Args:
25            @param device: string, name of the block device.
26
27        Returns:
28            int, size of block in bytes.
29        """
30
31        # Construct a pathname to find the logic block size for this device
32        sysfs_path = os.path.join('/sys', 'block', device,
33                                  'queue', 'logical_block_size')
34
35        return int(utils.read_one_line(sysfs_path))
36
37    def get_partition_size(self, device, partition):
38        """
39        Get the number of blocks in the partition.
40
41        Args:
42            @param device: string, name of the block device.
43            @param partition: string, partition name
44
45        Returns:
46            int, number of blocks
47        """
48
49        part_file = os.path.join('/sys', 'block', device, partition, 'size')
50        part_blocks = int(utils.read_one_line(part_file))
51        return part_blocks
52
53    def run_once(self):
54        errors = []
55        device = os.path.basename(utils.get_fixed_dst_drive())
56        partitions = [utils.concat_partition(device, i) for i in (3, 5)]
57        block_size = self.get_block_size(device)
58
59        for p in partitions:
60            pblocks = self.get_partition_size(device, p)
61            psize = pblocks * block_size
62            if psize != ROOTFS_SIZE_2G and psize != ROOTFS_SIZE_4G:
63                errmsg = ('%s is %d bytes, expected %d or %d' %
64                          (p, psize, ROOTFS_SIZE_2G, ROOTFS_SIZE_4G))
65                logging.warning(errmsg)
66                errors.append(errmsg)
67
68        # If self.error is not zero, there were errors.
69        if errors:
70            raise error.TestFail('There were %d partition errors: %s' %
71                                 (len(errors), ': '.join(errors)))
72