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