blob: 380f4c4dca3063dd2137399d5e06e7895b2b7dc0 [file] [log] [blame]
Simon Glass1d5006c2022-10-29 19:47:05 -06001# SPDX-License-Identifier: GPL-2.0+
2#
Simon Glass1d5006c2022-10-29 19:47:05 -06003# Copyright (c) 2018, Linaro Limited
4# Author: Takahiro Akashi <takahiro.akashi@linaro.org>
5
Simon Glassfbd95502022-10-29 19:47:06 -06006"""Helper functions for dealing with filesystems"""
7
Simon Glass1d5006c2022-10-29 19:47:05 -06008import re
9import os
10from subprocess import call, check_call, check_output, CalledProcessError
11
Christian Taedcked6048802023-11-15 13:44:23 +010012def mk_fs(config, fs_type, size, prefix, size_gran = 0x100000):
Simon Glass1d5006c2022-10-29 19:47:05 -060013 """Create a file system volume
14
15 Args:
Simon Glassfbd95502022-10-29 19:47:06 -060016 config (u_boot_config): U-Boot configuration
Simon Glass1d5006c2022-10-29 19:47:05 -060017 fs_type (str): File system type, e.g. 'ext4'
18 size (int): Size of file system in bytes
19 prefix (str): Prefix string of volume's file name
Christian Taedcked6048802023-11-15 13:44:23 +010020 size_gran (int): Size granularity of file system image in bytes
Simon Glassfbd95502022-10-29 19:47:06 -060021
22 Raises:
23 CalledProcessError: if any error occurs when creating the filesystem
Simon Glass1d5006c2022-10-29 19:47:05 -060024 """
Simon Glassfbd95502022-10-29 19:47:06 -060025 fs_img = f'{prefix}.{fs_type}.img'
Simon Glass08709212023-08-24 13:55:38 -060026 fs_img = os.path.join(config.persistent_data_dir, fs_img)
Simon Glass1d5006c2022-10-29 19:47:05 -060027
Christian Taedckeff0ceca2023-11-15 13:44:21 +010028 if fs_type == 'fat12':
29 mkfs_opt = '-F 12'
30 elif fs_type == 'fat16':
Simon Glass1d5006c2022-10-29 19:47:05 -060031 mkfs_opt = '-F 16'
32 elif fs_type == 'fat32':
33 mkfs_opt = '-F 32'
34 else:
35 mkfs_opt = ''
36
37 if re.match('fat', fs_type):
38 fs_lnxtype = 'vfat'
39 else:
40 fs_lnxtype = fs_type
41
Christian Taedcked6048802023-11-15 13:44:23 +010042 count = (size + size_gran - 1) // size_gran
Simon Glass1d5006c2022-10-29 19:47:05 -060043
44 # Some distributions do not add /sbin to the default PATH, where mkfs lives
45 if '/sbin' not in os.environ["PATH"].split(os.pathsep):
46 os.environ["PATH"] += os.pathsep + '/sbin'
47
48 try:
Simon Glassfbd95502022-10-29 19:47:06 -060049 check_call(f'rm -f {fs_img}', shell=True)
Christian Taedcked6048802023-11-15 13:44:23 +010050 check_call(f'dd if=/dev/zero of={fs_img} bs={size_gran} count={count}',
Simon Glassfbd95502022-10-29 19:47:06 -060051 shell=True)
52 check_call(f'mkfs.{fs_lnxtype} {mkfs_opt} {fs_img}', shell=True)
Simon Glass1d5006c2022-10-29 19:47:05 -060053 if fs_type == 'ext4':
Simon Glassfbd95502022-10-29 19:47:06 -060054 sb_content = check_output(f'tune2fs -l {fs_img}',
55 shell=True).decode()
Simon Glass1d5006c2022-10-29 19:47:05 -060056 if 'metadata_csum' in sb_content:
Simon Glassfbd95502022-10-29 19:47:06 -060057 check_call(f'tune2fs -O ^metadata_csum {fs_img}', shell=True)
Simon Glass1d5006c2022-10-29 19:47:05 -060058 return fs_img
59 except CalledProcessError:
Simon Glassfbd95502022-10-29 19:47:06 -060060 call(f'rm -f {fs_img}', shell=True)
Simon Glass1d5006c2022-10-29 19:47:05 -060061 raise
Simon Glassfbd95502022-10-29 19:47:06 -060062
63# Just for trying out
64if __name__ == "__main__":
65 import collections
66
67 CNF= collections.namedtuple('config', 'persistent_data_dir')
68
69 mk_fs(CNF('.'), 'ext4', 0x1000000, 'pref')