brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: fallocate -l 1G file reserves a file of that size instantly

Linux
~/tips/tip-linux-fallocate-reserve-file-size

fallocate -l 1G test.img

Preallocates disk space for a file of the size you ask, without writing all the data like dd would.

Sometimes I need a big dummy file: to test a disk quota, to fill a partition on purpose or to see how a tool behaves with a 1 GB input. dd works, but it writes every byte. fallocate does not.

fallocate -l 1G test.img
ls -lh test.img
du -h test.img

The command asks the filesystem to reserve the blocks. On filesystems that support it, the allocation is quick because the blocks are marked as uninitialized and nothing is written. The size in ls and the usage in du should both show 1G, which tells you the space is really reserved, unlike a sparse file made with truncate.

Remove the file again when you are done:

rm test.img

Be careful which directory you run it in. A large reservation can fill a disk and make other things on that system fail.

The caveat is the filesystem. Not all of them support preallocation, and then the command fails with an error. A file made this way is also not always suitable as a swap file: reports exist of swapon refusing it with a message about holes, depending on the filesystem. For swap, follow the documentation of your distribution, which may tell you to use dd for the file or a filesystem-specific command instead.

Further reading: fallocate(1) manual page

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link