diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 15:49:25 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 15:49:25 +0000 |
commit | 464df1d5e5ab1322e2dd0a7796939fff1aeefa9a (patch) | |
tree | 6a403684e0978f0287d7f0ec0e5aab1fd31a59e1 /contrib/dir2fs | |
parent | Initial commit. (diff) | |
download | e2fsprogs-464df1d5e5ab1322e2dd0a7796939fff1aeefa9a.tar.xz e2fsprogs-464df1d5e5ab1322e2dd0a7796939fff1aeefa9a.zip |
Adding upstream version 1.47.0.upstream/1.47.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rwxr-xr-x | contrib/dir2fs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/contrib/dir2fs b/contrib/dir2fs new file mode 100755 index 0000000..abcecb3 --- /dev/null +++ b/contrib/dir2fs @@ -0,0 +1,66 @@ +#!/bin/sh + +dir="$1" +dev="$2" + +if [ "$1" = "--help" ] || [ ! -d "${dir}" ]; then + echo "Usage: $0 dir [mke2fs args] dev" + exit 1 +fi + +shift + +# Goal: Put all the files at the beginning (which mke2fs does) and minimize +# the number of free inodes given the minimum number of blocks required. +# Hence all this math to get the inode ratio just right. + +bytes="$(du -ks "${dir}" | awk '{print $1}')" +bytes="$((bytes * 1024))" +inodes="$(find "${dir}" -print0 | xargs -0 stat -c '%i' | sort -g | uniq | wc -l)" +block_sz=4096 +inode_sz=256 +sb_overhead=4096 +blocks_per_group="$((block_sz * 8))" +bytes_per_group="$((blocks_per_group * block_sz))" +inode_bytes="$((inodes * inode_sz))" + +# Estimate overhead with the minimum number of groups... +nr_groups="$(( (bytes + inode_bytes + bytes_per_group - 1) / bytes_per_group))" +inode_bytes_per_group="$((inode_bytes / nr_groups))" +inode_blocks_per_group="$(( (inode_bytes_per_group + (block_sz - 1)) / block_sz ))" +per_grp_overhead="$(( ((3 + inode_blocks_per_group) * block_sz) + 64 ))" +overhead="$(( sb_overhead + (per_grp_overhead * nr_groups) ))" +used_bytes="$((bytes + overhead))" + +# Then do it again with the real number of groups. +nr_groups="$(( (used_bytes + (bytes_per_group - 1)) / bytes_per_group))" +tot_blocks="$((nr_groups * blocks_per_group))" +tot_bytes="$((tot_blocks * block_sz))" + +ratio="$((bytes / inodes))" +mkfs_blocks="$((tot_blocks * 4 / 3))" + +mke2fs -i "${ratio}" -T ext4 -d "${dir}" -O ^resize_inode,sparse_super2,metadata_csum,64bit,^has_journal -E packed_meta_blocks=1,num_backup_sb=0 -b "${block_sz}" -I "${inodesz}" -F "${dev}" "${mkfs_blocks}" || exit + +e2fsck -fyD "${dev}" + +blocks="$(dumpe2fs -h "${dev}" 2>&1 | grep 'Block count:' | awk '{print $3}')" +while resize2fs -f -M "${dev}"; do + new_blocks="$(dumpe2fs -h "${dev}" 2>&1 | grep 'Block count:' | awk '{print $3}')" + if [ "${new_blocks}" -eq "${blocks}" ]; then + break; + fi + blocks="${new_blocks}" +done + +if [ ! -b "${dev}" ]; then + truncate -s "$((blocks * block_sz))" "${dev}" || (e2image -ar "${dev}" "${dev}.min"; mv "${dev}.min" "${dev}") +fi + +e2fsck -fy "${dev}" + +dir_blocks="$((bytes / block_sz))" +overhead="$((blocks - dir_blocks))" +echo "Minimized image overhead: $((100 * overhead / dir_blocks))%" + +exit 0 |