Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.