Reputation: 57
I have to prepare an ext4 image that will contain content of a provided directory. The size of image should be just above the required space.
My procedure was to check the directory content's size, than use dd
to create an empty image with some extra space and finally create ext4 filesystem using mkfs.ext4' with the
-d` flag to provide files to write to it.
# check size in kB (in my case it's 27700)
size=$( du -sk ./trg | awk '{print $1 }')
# add extra 1000kB
extra="1000"
# create empty file with dd
dd if=/dev/zero of=out.dd seek=$size bs=1024 count=$extra
# create filesystem
mkfs.ext4 -F out.dd -d ./trg
I expected that the size of image will be 27700+1000
so everything should fit into 28700kB
, but the output image is much smaller than expected:
$ du -sk ./*
26340 ./out.dd
4 ./test.sh
27700 ./trg
What am I missing? where is this space lost?
Upvotes: 2
Views: 2844
Reputation: 19685
du -sk
prints disk usage in kilobytes. Disk usage is not actual file size but the place it takes on disk. It means that sparse bytes (blanks) may take less space on disk than their amount.
If you want du
to print actual data size rather than disk usage:
Use the --apparent-size
option:
- --apparent-size
- print apparent sizes, rather than disk usage; although the apparent size is usually smaller, it may be larger due to holes in ('sparse') files, internal fragmentation, indirect blocks, and the like
Upvotes: 0