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        @param job: The job object.
34        """
35
36        self.env_names = env_names
37        self.fs_types = fs_types
38        for name in self.env_names:
39            file_name = 'xfstests_%s' % name
40            file_img = os.path.join(
41                self.XFS_MOUNT_PATH, '%s.img' % file_name)
42            self.env_vp[name] = partition.virtual_partition(
43                file_img=file_img, file_size=1024)
44            self.env_device[name] = self.env_vp[name].device
45
46            # You can use real block devices, such as /dev/sdc1 by populating
47            # env_device directly, but going through the virtual partition
48            # object.
49
50            # By default, we create a directory under autotest
51            mountpoint = os.path.join(job.tmpdir, file_name)
52            if not os.path.isdir(mountpoint):
53                os.makedirs(mountpoint)
54
55            self.env_partition[name] = job.partition(
56                device=self.env_device[name], mountpoint=mountpoint)
57
58        #
59        # Job configuration, instead of editing xfstests config files, set them
60        # right here as environment variables
61        #
62        for name in self.env_names:
63            os.environ['%s_DEV' % name] = self.env_partition[name].device
64
65        test_dir = self.env_partition['TEST'].mountpoint
66
67        os.environ['TEST_DIR'] = test_dir
68        os.environ['SCRATCH_MNT'] = self.env_partition['SCRATCH'].mountpoint
69
70        # ChromeOS does not need special option when SELinux is enabled.
71        os.environ['SELINUX_MOUNT_OPTIONS'] = ' '
72
73        mkfs_args = ''
74        mnt_options = ''
75        if crypto:
76            mkfs_args += '-O encrypt'
77            mnt_options += '-o test_dummy_encryption'
78
79        for fs_type in self.fs_types:
80            for name in self.env_names:
81                self.env_partition[name].mkfs(fstype=fs_type, args=mkfs_args)
82
83        os.environ['EXT_MOUNT_OPTIONS'] = mnt_options
84
85    def unmount_partitions(self):
86        """
87        Unmount the partition created.
88        """
89        for name in self.env_names:
90            self.env_partition[name].unmount(ignore_status=True)
91            self.env_vp[name].destroy()
92