Simon Glass | 1d5006c | 2022-10-29 19:47:05 -0600 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
| 2 | # |
Simon Glass | 1d5006c | 2022-10-29 19:47:05 -0600 | [diff] [blame] | 3 | # Copyright (c) 2018, Linaro Limited |
| 4 | # Author: Takahiro Akashi <takahiro.akashi@linaro.org> |
| 5 | |
Simon Glass | fbd9550 | 2022-10-29 19:47:06 -0600 | [diff] [blame] | 6 | """Helper functions for dealing with filesystems""" |
| 7 | |
Simon Glass | 1d5006c | 2022-10-29 19:47:05 -0600 | [diff] [blame] | 8 | import re |
| 9 | import os |
| 10 | from subprocess import call, check_call, check_output, CalledProcessError |
| 11 | |
Simon Glass | 0870921 | 2023-08-24 13:55:38 -0600 | [diff] [blame] | 12 | def mk_fs(config, fs_type, size, prefix): |
Simon Glass | 1d5006c | 2022-10-29 19:47:05 -0600 | [diff] [blame] | 13 | """Create a file system volume |
| 14 | |
| 15 | Args: |
Simon Glass | fbd9550 | 2022-10-29 19:47:06 -0600 | [diff] [blame] | 16 | config (u_boot_config): U-Boot configuration |
Simon Glass | 1d5006c | 2022-10-29 19:47:05 -0600 | [diff] [blame] | 17 | 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 |
Simon Glass | fbd9550 | 2022-10-29 19:47:06 -0600 | [diff] [blame] | 20 | |
| 21 | Raises: |
| 22 | CalledProcessError: if any error occurs when creating the filesystem |
Simon Glass | 1d5006c | 2022-10-29 19:47:05 -0600 | [diff] [blame] | 23 | """ |
Simon Glass | fbd9550 | 2022-10-29 19:47:06 -0600 | [diff] [blame] | 24 | fs_img = f'{prefix}.{fs_type}.img' |
Simon Glass | 0870921 | 2023-08-24 13:55:38 -0600 | [diff] [blame] | 25 | fs_img = os.path.join(config.persistent_data_dir, fs_img) |
Simon Glass | 1d5006c | 2022-10-29 19:47:05 -0600 | [diff] [blame] | 26 | |
| 27 | if fs_type == 'fat16': |
| 28 | mkfs_opt = '-F 16' |
| 29 | elif fs_type == 'fat32': |
| 30 | mkfs_opt = '-F 32' |
| 31 | else: |
| 32 | mkfs_opt = '' |
| 33 | |
| 34 | if re.match('fat', fs_type): |
| 35 | fs_lnxtype = 'vfat' |
| 36 | else: |
| 37 | fs_lnxtype = fs_type |
| 38 | |
Simon Glass | fbd9550 | 2022-10-29 19:47:06 -0600 | [diff] [blame] | 39 | count = (size + 0x100000 - 1) // 0x100000 |
Simon Glass | 1d5006c | 2022-10-29 19:47:05 -0600 | [diff] [blame] | 40 | |
| 41 | # Some distributions do not add /sbin to the default PATH, where mkfs lives |
| 42 | if '/sbin' not in os.environ["PATH"].split(os.pathsep): |
| 43 | os.environ["PATH"] += os.pathsep + '/sbin' |
| 44 | |
| 45 | try: |
Simon Glass | fbd9550 | 2022-10-29 19:47:06 -0600 | [diff] [blame] | 46 | check_call(f'rm -f {fs_img}', shell=True) |
| 47 | check_call(f'dd if=/dev/zero of={fs_img} bs=1M count={count}', |
| 48 | shell=True) |
| 49 | check_call(f'mkfs.{fs_lnxtype} {mkfs_opt} {fs_img}', shell=True) |
Simon Glass | 1d5006c | 2022-10-29 19:47:05 -0600 | [diff] [blame] | 50 | if fs_type == 'ext4': |
Simon Glass | fbd9550 | 2022-10-29 19:47:06 -0600 | [diff] [blame] | 51 | sb_content = check_output(f'tune2fs -l {fs_img}', |
| 52 | shell=True).decode() |
Simon Glass | 1d5006c | 2022-10-29 19:47:05 -0600 | [diff] [blame] | 53 | if 'metadata_csum' in sb_content: |
Simon Glass | fbd9550 | 2022-10-29 19:47:06 -0600 | [diff] [blame] | 54 | check_call(f'tune2fs -O ^metadata_csum {fs_img}', shell=True) |
Simon Glass | 1d5006c | 2022-10-29 19:47:05 -0600 | [diff] [blame] | 55 | return fs_img |
| 56 | except CalledProcessError: |
Simon Glass | fbd9550 | 2022-10-29 19:47:06 -0600 | [diff] [blame] | 57 | call(f'rm -f {fs_img}', shell=True) |
Simon Glass | 1d5006c | 2022-10-29 19:47:05 -0600 | [diff] [blame] | 58 | raise |
Simon Glass | fbd9550 | 2022-10-29 19:47:06 -0600 | [diff] [blame] | 59 | |
| 60 | # Just for trying out |
| 61 | if __name__ == "__main__": |
| 62 | import collections |
| 63 | |
| 64 | CNF= collections.namedtuple('config', 'persistent_data_dir') |
| 65 | |
| 66 | mk_fs(CNF('.'), 'ext4', 0x1000000, 'pref') |