Creating large files

Forum software says I better start a new thread about it.

The mass storage device docs recommend creating large files via dd:

[root@pikvm ~]# dd if=/dev/zero of=/root/flash.img bs=1M count=1000 status=progress

There are much faster ways to allocate large files, plus they avoid unnecessary writes to the precious SSD: fallocate(1) and truncate(1).

For comparison:

[root@pikvm ~]# time dd if=/dev/zero of=/var/lib/kvmd/msd/flash.img bs=1M count=1000 status=progress
1024458752 bytes (1.0 GB, 977 MiB) copied, 43 s, 23.8 MB/s
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB, 1000 MiB) copied, 43.759 s, 24.0 MB/s

real	0m43.771s
user	0m0.001s
sys	0m3.099s
[root@pikvm ~]# time fallocate -l 1000M /var/lib/kvmd/msd/flash2.img

real	0m0.017s
user	0m0.000s
sys	0m0.004s
[root@pikvm ~]# time truncate -r /var/lib/kvmd/msd/flash.img /var/lib/kvmd/msd/flash1.img

real	0m0.005s
user	0m0.001s
sys	0m0.003s
1 Like

Thank you for your suggestion. This is indeed a better way.

Using fallocate is safer than truncate because there’s no risk of damaging existing files. If a file exists and is bigger, it does nothing. If it is smaller, it will be extended.

@badsanta That’s a really useful suggestion, thanks! I’ve just updated the documentation accordingly.