I'm trying to create a sparse file (for a QEMU HDD image).
Both qemu-img
and fallocate
are proving confusing.
$ fallocate -l 100M disk.img
$ ls -lsh disk.img
101M -rw-r--r-- 1 i336 users 100M Jul 22 12:03 disk.img
Note the 101M
. strace
shows a successful syscall:
$ strace fallocate -l 100M disk.img
open("disk.img", O_RDWR|O_CREAT|O_LARGEFILE, 0666) = 3
fallocate(3, 0, 0, 104857600) = 0
$ ls -lsh disk.img
101M -rw-r--r-- 1 i336 users 100M Jul 22 12:03 disk.img
I'm not sure if stat
is the right tool, but just in case..
$ stat disk.img
File: 'disk.img'
Size: 104857600 Blocks: 204808 IO Block: 4096 regular file
Device: 802h/2050d Inode: 549166 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1337/ i336) Gid: ( 100/ users)
A possible (very weird) clue: 104857600/204808 = 511.9800
. (File size / block count)
qemu-img
has similar output. (I found the preallocation
option in the manual.)
$ qemu-img create -f raw -o preallocation=falloc disk.img 100M
Formatting 'disk.img', fmt=raw size=104857600 preallocation=falloc
$ ls -lsh disk.img
101M -rw-r--r-- 1 i336 users 100M Jul 22 12:06 disk.img
Here's the annoying bit: the image appears to be using real space on disk.
$ df -h /; fallocate -l 1G disk.img; df -h /
Filesystem Size Used Avail Use% Mounted on
/dev/root 48G 43G 3.5G 93% /
Filesystem Size Used Avail Use% Mounted on
/dev/root 48G 44G 2.5G 95% /
And yet, just like a sparse file, it takes no time to create!
$ time fallocate -l 3.3G disk.img
0.00user 0.57system 0:00.91elapsed 63%CPU (0avgtext+0avgdata 5424maxresident)k
200inputs+0outputs (0major+68minor)pagefaults 0swaps
0.91 seconds, on a 5400RPM HDD. There is no way I'm not creating a sparse file.
And yet no matter what tool I use, it appears to be using 101MB of space right off the bat.
What could I be doing wrong or have misconfigured?
$ cat /etc/fstab
/dev/sda2 / ext4 rw,user_xattr 0 0