Skip to content

Commit 93bbca8

Browse files
authored
Merge pull request #64 from icedieler/vfat
rootfs: add class to build a VFAT image
2 parents ddc8eb3 + af42252 commit 93bbca8

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

classes/rootfs/vfat.yaml

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
inherit: [rootfs]
2+
3+
depends:
4+
- tools:
5+
target-toolchain: host-compat-toolchain
6+
use: [tools]
7+
depends:
8+
- utils::mtools
9+
10+
buildToolsWeak: [mtools]
11+
buildSetup: |
12+
# Creates a vfat image from a source directory. The target image
13+
# size can be optionally specified. If not, it is automatically calculated.
14+
#
15+
# $1: image source directory
16+
# $2: output name
17+
# $3: output size (optional)
18+
# This can be:
19+
# - nothing: auto calc size + head room (18%)
20+
# - number: absolute overall size in kbytes
21+
# - number%: percentage in the form of 1.20% to add 20%
22+
# - number+: absolute free space in kbytes in the form of 1024+ to add 1MB
23+
createVfatImage()
24+
{
25+
SIZE=${3:-1.18%}
26+
if [[ $SIZE == *% ]]; then
27+
# automatic: calc size and add head room/free space in % as specified
28+
# by the user
29+
SIZE="\$(dir_size_in_kb $1 ${SIZE%\%})"
30+
elif [[ $SIZE == *+ ]]; then
31+
# automatic: calc size and add head room/free space in absolute bytes
32+
# as specified by the user
33+
SIZE="\$(dir_size_in_kb $1 1 ${SIZE%\+})"
34+
fi # default: manually specified
35+
36+
cat >create_vfat_img.sh <<EOF
37+
38+
# return size of dir in kbytes
39+
#
40+
# \$1: source dir
41+
# \$2: multiply by factor (optional; default 1)
42+
# \$3: add additional size in kb (optional; default 0)
43+
function dir_size_in_kb()
44+
{
45+
printf %0.f \$(echo "\$(du -xsb -- \$1 | cut -f 1)*\${2:-1}+\${3:-0}*1024" | bc) | numfmt --to-unit=1024
46+
}
47+
48+
dd if=/dev/zero of=$2.img bs=1K count=$SIZE
49+
mformat -i $2.img ::
50+
mcopy -i $2.img -s $1/* ::
51+
EOF
52+
53+
FAKEROOTDONTTRYCHOWN=1 fakeroot -- sh create_vfat_img.sh
54+
}

0 commit comments

Comments
 (0)