In Linux, how can I create thin-provisioned file so it can be mounted and a filesystem created on it?

I am building a system that gives users certain amounts of disk space. The way I am doing it is:

  • create a file with dd
  • create an ext4 filesystem inside the file
  • mount the file and tell the user program to use the mountpoint as its working directory

I do not want to use the linux quota system as I dont want the users to be local system users.

The above works fine, but it wastes space as the users are not using all of their allowance but the entire allowance has been allocated already.

Is there a command that can do the same thing as dd but not allocate the whole file at once? So I could make a 20gb file, and when its mounted it would report 20gb of space, but it would only physically take up what has been written to it?


You can use the seek parameter of dd to create a sparse file:

dd if=/dev/zero of=filesystem.img  bs=1k seek=1024M count=1

would create a file which can get up to 1G in size but will only occupy the necessary space.


Have a look at sparse files.

dd if=/dev/zero of=file.img bs=1 count=0 seek=1024M

creates a 1024Mb sparse file that can be formatted and mounted as a filesystem. There is more information on sparse files and using them here.