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