Home | History | Annotate | Download | only in cros
      1 # Copyright (c) 2018 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 re
      7 
      8 from autotest_lib.client.common_lib import error
      9 
     10 
     11 def make_rootfs_writable(host):
     12     """
     13     Checks and makes root filesystem writable.
     14 
     15     Checks if root filesytem is writable. If not
     16     it converts root filesystem to writable.
     17     This function will reboot the DUT
     18 
     19     @param host: An Autotest host object
     20 
     21     @raises TestError: If making root fs  writable fails.
     22 
     23     """
     24 
     25     if  is_rootfs_writable(host):
     26         logging.info('DUT root file system is writable.')
     27     else:
     28         logging.info('DUT root file system is not writable. '
     29                      'Converting it writable...')
     30         convert_rootfs_writable(host)
     31         if not is_rootfs_writable(host):
     32             raise error.TestError('Failed to make root filesystem writable')
     33         logging.info('DUT root filesystem converted to writable')
     34 
     35 
     36 def convert_rootfs_writable(host):
     37     """
     38     Makes CrOS rootfs writable.
     39 
     40     Remove root fs verification, reboot host
     41     and remounts the root fs
     42 
     43     @param host: An Autotest host object.
     44 
     45     @raises TestError: If executing the command on CrOS fails.
     46     """
     47 
     48     logging.info('Disabling rootfs verification.')
     49     remove_rootfs_verification(host)
     50 
     51     logging.info('Rebooting the host')
     52     host.reboot()
     53 
     54     logging.info('Remounting root filesystem')
     55     cmd = 'mount -o remount,rw /'
     56     res = host.run(cmd)
     57 
     58     if res.exit_status != 0:
     59         raise error.TestError('Executing remount command on DUT failed')
     60 
     61 
     62 def remove_rootfs_verification(host):
     63     """
     64     Removes root fs verification from CrOS host.
     65 
     66     @param host: an Autotest host object.
     67 
     68     @raises TestError: if executing the command on CrOS fails.
     69     """
     70 
     71     # 2 & 4 are default partitions, and the system boots from one of them.
     72     # Code from chromite/scripts/deploy_chrome.py
     73     KERNEL_A_PARTITION = 2
     74     KERNEL_B_PARTITION = 4
     75 
     76     cmd_template = ('/usr/share/vboot/bin/make_dev_ssd.sh'
     77                     ' --partitions "%d %d"'
     78                     ' --remove_rootfs_verification --force')
     79     cmd = cmd_template % (KERNEL_A_PARTITION, KERNEL_B_PARTITION)
     80     res = host.run(cmd)
     81     if res.exit_status != 0:
     82         raise error.TestError('Executing command on DUT failed')
     83 
     84 
     85 def is_rootfs_writable(host):
     86     """
     87     Checks if the root file system is writable.
     88 
     89     @param host: an Autotest host object.
     90 
     91     @returns Boolean denoting where root filesytem is writable or not.
     92 
     93     @raises TestError: if executing the command on CrOS fails.
     94     """
     95 
     96     # Query the DUT's filesystem /dev/root and check whether it is rw
     97 
     98     cmd = 'cat /proc/mounts | grep "/dev/root"'
     99     result = host.run(cmd)
    100     if result.exit_status > 0:
    101         raise error.TestError('Executing command on DUT failed')
    102     fields = re.split(' |,', result.stdout)
    103 
    104     # Result of grep will be of the following format
    105     # /dev/root / ext2 ro,seclabel <....truncated...> => readonly
    106     # /dev/root / ext2 rw,seclabel <....truncated...> => readwrite
    107     if fields.__len__() < 4 or fields[3] not in ['rw', 'ro']:
    108         raise error.TestError('Command output not in expected format')
    109 
    110     return fields[3] == 'rw'
    111