xref: /aosp_15_r20/external/autotest/client/bin/xfstest_util.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1# Copyright (c) 2017 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"""Library to run xfstests in autotest.
6
7"""
8
9import os
10from autotest_lib.client.bin import partition
11
12class xfstests_env:
13    """
14    Setup the environment for running xfstests.
15    """
16    XFS_MOUNT_PATH = '/mnt/stateful_partition/unencrypted/cache'
17
18    env_names={}
19    env_partition={}
20    env_vp={}
21    env_device={}
22    fs_types={}
23
24    def setup_partitions(self, job, fs_types, crypto=False,
25                         env_names=['TEST', 'SCRATCH']):
26        """
27        xfttests needs 2 partitions: TEST and SCRATCH.
28        - TEST_DEV: "device containing TEST PARTITION"
29        - TEST_DIR: "mount point of TEST PARTITION"
30        - SCRATCH_DEV "device containing SCRATCH PARTITION"
31        - SCRATCH_MNT "mount point for SCRATCH PARTITION"
32
33        - SCRATCH_DIR "directory on a large partition"
34
35        @param job: The job object.
36        """
37
38        self.env_names = env_names
39        self.fs_types = fs_types
40        for name in self.env_names:
41            file_name = 'xfstests_%s' % name
42            file_img = os.path.join(
43                self.XFS_MOUNT_PATH, '%s.img' % file_name)
44            self.env_vp[name] = partition.virtual_partition(
45                file_img=file_img, file_size=4096)
46            self.env_device[name] = self.env_vp[name].device
47
48            # You can use real block devices, such as /dev/sdc1 by populating
49            # env_device directly, but going through the virtual partition
50            # object.
51
52            # By default, we create a directory under autotest
53            mountpoint = os.path.join(job.tmpdir, file_name)
54            if not os.path.isdir(mountpoint):
55                os.makedirs(mountpoint)
56
57            self.env_partition[name] = job.partition(
58                device=self.env_device[name], mountpoint=mountpoint)
59
60        #
61        # Job configuration, instead of editing xfstests config files, set them
62        # right here as environment variables
63        #
64        for name in self.env_names:
65            os.environ['%s_DEV' % name] = self.env_partition[name].device
66
67        test_dir = self.env_partition['TEST'].mountpoint
68
69        os.environ['TEST_DIR'] = test_dir
70        os.environ['SCRATCH_MNT'] = self.env_partition['SCRATCH'].mountpoint
71        os.environ['SCRATCH_DIR'] = self.XFS_MOUNT_PATH
72
73        # ChromeOS does not need special option when SELinux is enabled.
74        os.environ['SELINUX_MOUNT_OPTIONS'] = ' '
75
76        mkfs_args = ''
77        mnt_options = ''
78        if crypto:
79            mkfs_args += '-O encrypt'
80            # TODO: update this when blockers are updated b:169251326
81            mnt_options += '-o test_dummy_encryption'
82
83        for fs_type in self.fs_types:
84            for name in self.env_names:
85                self.env_partition[name].mkfs(fstype=fs_type, args=mkfs_args)
86
87        os.environ['EXT_MOUNT_OPTIONS'] = mnt_options
88
89    def unmount_partitions(self):
90        """
91        Unmount the partition created.
92        """
93        for name in self.env_names:
94            self.env_partition[name].unmount(ignore_status=True)
95            self.env_vp[name].destroy()
96