]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - contrib/dir2fs
debian: update changelog for 1.45.3-3 release
[thirdparty/e2fsprogs.git] / contrib / dir2fs
CommitLineData
8804a96f
DW
1#!/bin/sh
2
3dir="$1"
4dev="$2"
5
6if [ "$1" = "--help" ] || [ ! -d "${dir}" ]; then
7 echo "Usage: $0 dir [mke2fs args] dev"
8 exit 1
9fi
10
11shift
12
13# Goal: Put all the files at the beginning (which mke2fs does) and minimize
14# the number of free inodes given the minimum number of blocks required.
15# Hence all this math to get the inode ratio just right.
16
17bytes="$(du -ks "${dir}" | awk '{print $1}')"
18bytes="$((bytes * 1024))"
19inodes="$(find "${dir}" -print0 | xargs -0 stat -c '%i' | sort -g | uniq | wc -l)"
20block_sz=4096
21inode_sz=256
22sb_overhead=4096
23blocks_per_group="$((block_sz * 8))"
24bytes_per_group="$((blocks_per_group * block_sz))"
25inode_bytes="$((inodes * inode_sz))"
26
27# Estimate overhead with the minimum number of groups...
28nr_groups="$(( (bytes + inode_bytes + bytes_per_group - 1) / bytes_per_group))"
29inode_bytes_per_group="$((inode_bytes / nr_groups))"
30inode_blocks_per_group="$(( (inode_bytes_per_group + (block_sz - 1)) / block_sz ))"
31per_grp_overhead="$(( ((3 + inode_blocks_per_group) * block_sz) + 64 ))"
32overhead="$(( sb_overhead + (per_grp_overhead * nr_groups) ))"
33used_bytes="$((bytes + overhead))"
34
35# Then do it again with the real number of groups.
36nr_groups="$(( (used_bytes + (bytes_per_group - 1)) / bytes_per_group))"
37tot_blocks="$((nr_groups * blocks_per_group))"
38tot_bytes="$((tot_blocks * block_sz))"
39
40ratio="$((bytes / inodes))"
41mkfs_blocks="$((tot_blocks * 4 / 3))"
42
43mke2fs -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
44
45e2fsck -fyD "${dev}"
46
47blocks="$(dumpe2fs -h "${dev}" 2>&1 | grep 'Block count:' | awk '{print $3}')"
48while resize2fs -f -M "${dev}"; do
49 new_blocks="$(dumpe2fs -h "${dev}" 2>&1 | grep 'Block count:' | awk '{print $3}')"
50 if [ "${new_blocks}" -eq "${blocks}" ]; then
51 break;
52 fi
53 blocks="${new_blocks}"
54done
55
56if [ ! -b "${dev}" ]; then
57 truncate -s "$((blocks * block_sz))" "${dev}" || (e2image -ar "${dev}" "${dev}.min"; mv "${dev}.min" "${dev}")
58fi
59
60e2fsck -fy "${dev}"
61
62dir_blocks="$((bytes / block_sz))"
63overhead="$((blocks - dir_blocks))"
64echo "Minimized image overhead: $((100 * overhead / dir_blocks))%"
65
66exit 0