1 # SPDX-License-Identifier: GPL-2.0
2 # Copyright (C) 2020 Bootlin
3 # Author: Joao Marcos Costa <joaomarcos.costa@bootlin.com>
9 """ standard test images table: Each table item is a key:value pair
10 representing the output image name and its respective mksquashfs options.
11 This table should be modified only when adding support for new compression
12 algorithms. The 'default' case takes no options but the input and output
13 names, so it must be assigned with an empty string.
20 'zstd_comp_frag' : '',
23 'gzip_comp_frag' : '',
28 """ EXTRA_TABLE: Set this table's keys and values if you want to make squashfs
29 images with your own customized options.
33 # path to source directory used to make squashfs test images
34 SQFS_SRC_DIR = 'sqfs_src_dir'
37 """ Combines fragmentation and compression options into a list of strings.
39 opts_list's firts item is an empty string as STANDARD_TABLE's first item is
43 A list of strings whose items are formed by a compression and a
44 fragmentation option joined by a whitespace.
46 # supported compression options only
47 comp_opts = ['-comp lzo', '-comp zstd', '-comp gzip']
48 # file fragmentation options
49 frag_opts = ['-always-use-fragments', '-always-use-fragments -noF', '-no-fragments']
52 for comp_opt in comp_opts:
53 for frag_opt in frag_opts:
54 opts_list.append(' '.join([comp_opt, frag_opt]))
58 def init_standard_table():
59 """ Initializes STANDARD_TABLE values.
61 STANDARD_TABLE's keys are pre-defined, and init_standard_table() assigns
62 the right value for each one of them.
64 opts_list = get_opts_list()
66 for key, value in zip(STANDARD_TABLE.keys(), opts_list):
67 STANDARD_TABLE[key] = value
69 def generate_file(file_name, file_size):
70 """ Generates a file filled with 'x'.
73 file_name: the file's name.
74 file_size: the content's length and therefore the file size.
76 content = 'x' * file_size
78 file = open(file_name, 'w')
82 def generate_sqfs_src_dir(build_dir):
83 """ Generates the source directory used to make the SquashFS images.
85 The source directory is generated at build_dir, and it has the following
96 3 directories, 4 files
98 The files in the root dir. are prefixed with an 'f' followed by its size.
101 build_dir: u-boot's build-sandbox directory.
104 root = os.path.join(build_dir, SQFS_SRC_DIR)
105 # make root directory
108 # 4096: minimum block size
110 generate_file(os.path.join(root, file_name), 4096)
112 # 5096: minimum block size + 1000 chars (fragment)
114 generate_file(os.path.join(root, file_name), 5096)
116 # 1000: less than minimum block size (fragment only)
118 generate_file(os.path.join(root, file_name), 1000)
120 # sub-directory with a single file inside
121 subdir_path = os.path.join(root, 'subdir')
122 os.makedirs(subdir_path)
123 generate_file(os.path.join(subdir_path, 'subdir-file'), 100)
125 # symlink (target: sub-directory)
126 os.symlink('subdir', os.path.join(root, 'sym'))
129 os.makedirs(os.path.join(root, 'empty-dir'))
131 def mksquashfs(args):
132 """ Runs mksquashfs command.
135 args: mksquashfs options (e.g.: compression and fragmentation).
137 subprocess.run(['mksquashfs ' + args], shell=True, check=True,
138 stdout=subprocess.DEVNULL)
140 def get_mksquashfs_version():
141 """ Parses the output of mksquashfs -version.
144 mksquashfs's version as a float.
146 out = subprocess.run(['mksquashfs -version'], shell=True, check=True,
147 capture_output=True, text=True)
148 # 'out' is: mksquashfs version X (yyyy/mm/dd) ...
149 return float(out.stdout.split()[2].split('-')[0])
151 def check_mksquashfs_version():
152 """ Checks if mksquashfs meets the required version. """
154 required_version = 4.4
155 if get_mksquashfs_version() < required_version:
156 print('Error: mksquashfs is too old.')
157 print('Required version: {}'.format(required_version))
160 def make_all_images(build_dir):
161 """ Makes the SquashFS images used in the test suite.
163 The image names and respective mksquashfs options are defined in STANDARD_TABLE
164 and EXTRA_TABLE. The destination is defined by 'build_dir'.
167 build_dir: u-boot's build-sandbox directory.
170 init_standard_table()
171 input_path = os.path.join(build_dir, SQFS_SRC_DIR)
173 # make squashfs images according to STANDARD_TABLE
174 for out, opts in zip(STANDARD_TABLE.keys(), STANDARD_TABLE.values()):
175 output_path = os.path.join(build_dir, out)
176 mksquashfs(' '.join([input_path, output_path, opts]))
178 # make squashfs images according to EXTRA_TABLE
179 for out, opts in zip(EXTRA_TABLE.keys(), EXTRA_TABLE.values()):
180 output_path = os.path.join(build_dir, out)
181 mksquashfs(' '.join([input_path, output_path, opts]))
183 def clean_all_images(build_dir):
184 """ Deletes the SquashFS images at build_dir.
187 build_dir: u-boot's build-sandbox directory.
190 for image_name in STANDARD_TABLE:
191 image_path = os.path.join(build_dir, image_name)
192 os.remove(image_path)
194 for image_name in EXTRA_TABLE:
195 image_path = os.path.join(build_dir, image_name)
196 os.remove(image_path)
198 def clean_sqfs_src_dir(build_dir):
199 """ Deletes the source directory at build_dir.
202 build_dir: u-boot's build-sandbox directory.
204 path = os.path.join(build_dir, SQFS_SRC_DIR)