Thursday, August 21, 2014

Using fallocate instead of dd for creating empty files

I've been always using dd for empty disk image creation (for new virtual machine for example). But I've found out a better tool. It called fallocate.

Usually when I want to create disk image I'll use something like this:

[root@kra images]$ dd if=/dev/zero of=disk.img bs=1M count=2048
2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB) copied, 3.82779 s, 561 MB/s

When I need bigger image it takes a lot of time. And my computer isn't happy during image creation because of higher IO load. I can deal with I/O load using ionice:

[root@kra images]$ ionice -c 3 dd if=/dev/zero of=disk.img bs=1M count=2048

This will put dd to idle scheduler class. Processes in idle class gets to IO operation only when no other process in other classes needs IO. Further I can limit IO rate with throttle:

[root@kra images]# dd if=/dev/zero bs=1M count=2048 | throttle -M 300 > disk.img 
2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB) copied, 7.807 s, 275 MB/s

But best solution is to use fallocate utility which comes from util-linux package. It will create the file with instant speed:

[root@kra images]# rm disk.img 
[root@kra images]# time fallocate -l 2048MiB disk.img

real    0m0.001s
user    0m0.000s
sys     0m0.001s
[root@kra images]# du -sm disk.img 
2048    disk.img

One disadvantage is that this feature may not be supported on all filesystems. According to man page following filesystems are supported: btrfs, ext4, ocfs2, and xfs.

Sources

1 comment: