script: make filesystem tools aware of underlying device sector size

This commit is contained in:
2026-01-30 15:57:52 +08:00
parent a89edf9f1a
commit 94312a7025
4 changed files with 52 additions and 3 deletions

View File

@@ -1,5 +1,7 @@
#!/usr/bin/env false
readonly FILESYSTEM_EXT4_SECTOR_SIZE_DEFAULT=4096
filesystem_generate_uuid_ext4() {
uuidgen
}
@@ -19,7 +21,25 @@ filesystem_set_uuid_ext4() {
}
filesystem_make_fs_ext4() {
logcmd mkfs.ext4 "${1}"
target_file="${1}"
device_sector_size="${2}"
# 1. EXT4_SECTOR_SIZE must be aligned to multiple of 1024B
# 2. EXT4_SECTOR_SIZE must be less than 65536
# 2. EXT4_SECTOR_SIZE defaults to 4096 if device sector size less than 4096
fs_sector_size="${FILESYSTEM_EXT4_SECTOR_SIZE_DEFAULT}"
if [ "${device_sector_size}" -gt "${fs_sector_size}" ]; then
fs_sector_size="${device_sector_size}"
fi
if [ "${fs_sector_size}" -gt 65536 ]; then
loge "Target ext4 block size (%d) greater than 65536" "${fs_sector_size}"
exit 1
fi
if [ "$(( fs_sector_size % 1024 ))" -ne 0 ]; then
loge "Target ext4 block size (%d) indivisible by 1024" "${fs_sector_size}"
exit 1
fi
logcmd mkfs.ext4 -F -b "${fs_sector_size}" "${target_file}"
}
filesystem_shrink_ext4() {

View File

@@ -1,5 +1,7 @@
#!/usr/bin/env false
readonly FILESYSTEM_VFAT_SECTOR_SIZE_DEFAULT=512
filesystem_generate_uuid_vfat() {
hexdump -vn4 -e'1/2 "%04X" 1 "-" 1/2 "%04X" 1 "\n"' /dev/urandom
}
@@ -19,5 +21,23 @@ filesystem_set_uuid_vfat() {
}
filesystem_make_fs_vfat() {
logcmd mkfs.vfat "${1}"
target_file="${1}"
device_sector_size="${2}"
# 1. VFAT_SECTOR_SIZE must be aligned to multiple of 512B
# 2. VFAT_SECTOR_SIZE must be less than 4096B
# 3. VFAT_SECTOR_SIZE defaults to DEVICE_SECTOR_SIZE
fs_sector_size="${FILESYSTEM_VFAT_SECTOR_SIZE_DEFAULT}"
if [ "${device_sector_size}" -gt "${fs_sector_size}" ]; then
fs_sector_size="${device_sector_size}"
fi
if [ "${fs_sector_size}" -gt 4096 ]; then
loge "Target vfat sector size (%d) greater than 4096" "${fs_sector_size}"
exit 1
fi
if [ "$(( fs_sector_size % 512 ))" -ne 0 ]; then
loge "Target vfat sector size (%d) indivisible by 512" "${fs_sector_size}"
exit 1
fi
logcmd mkfs.vfat -S "${fs_sector_size}" "${target_file}"
}

View File

@@ -28,8 +28,13 @@ prepare_fs_blocks() {
fs_content="${FS_IMG_DIR}/$(echo "${fs_mountpoint}" | tr '/' ':').${fs_format}"
eval "FILESYSTEM_${fs_idx}_CONTENT=${fs_content}"
if ! is_function "get_device_sector_size_${OUTPUT_FORMAT}"; then
loge "Device sector size hint for \"${OUTPUT_FORMAT}\" not implemented"
exit 1
fi
truncate -s "${fs_size}" "${fs_content}"
"filesystem_make_fs_${fs_format}" "${fs_content}"
"filesystem_make_fs_${fs_format}" "${fs_content}" "$("get_device_sector_size_${OUTPUT_FORMAT}")"
fs_uuid="FILESYSTEM_${fs_idx}_UUID"
fs_uuid="${!fs_uuid}"
"filesystem_set_uuid_${fs_format}" "${fs_content}" "${fs_uuid}"

View File

@@ -19,6 +19,10 @@ get_disk_sector_size() {
fi
}
get_device_sector_size_partitioned_disk() {
get_disk_sector_size
}
match_partition_content_MOUNTPOINT() {
fs_idx="PARTITION_${part_idx}_FILESYSTEM_IDX"
fs_idx="${!fs_idx}"