Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to create a file of 2GB, but I don't care about the contents of it. I currently use dd with /dev/zero, but this is fairly slow.

Is it possible to create a file and a hardlink to it, without initializing the contents of the file (it may contain garbage data that was on that location on the HDD before)?

Note: I really want to allocate the storage. I don’t want a sparse file.

share|improve this question
    
You're going to have to explain why you want to quickly allocate a file but a sparse file will not do what you are after. You could potentially copy a sparse file byte by byte to create a complete file of zeros and I fail to see why your requirement is specifically for "garbage data". –  Mokubai Aug 8 '12 at 21:41

2 Answers 2

What you are referring to is known as a "sparse" file.

From this page

Sparse files are basically just like any other file except that blocks that only contain zeros (i.e., nothing) are not actually stored on disk. This means you can have an apparently 16G file – with 16G of “data” – only taking up 1G of space on disk.

To create one using dd you can use the seek= parameter:

dd if=/dev/zero of=sparse-file bs=1 count=1 seek=1024k

The seek and bs parameters tells it simply to seek to the end of the file after 1 block which should give you a sparse file which you can then hot link to if you like.

This will give you a completely empty file that takes up no space on disk until it is actually written to. Its creation should also be near instantaneous.

-=EDIT=-

If you want a file allocated and don't care about the contents until you write to it you need a sparse file but generally for filesystem security reasons it is considered "bad form" to allow an unprivileged application to create a file using data that already exists on the disk.

This is because if a userland application could do that it could potentially expose sensitive data from other users by allocating a file the size of all the free space on a disk and trawl through it searching for passwords and other sensitive data that another user may have saved, copied, or deleted.

This is explicit bad design in operating systems that like to think of themselves as secure and no modern multi-user operating system will do this.

This is the other reason why sparse files default to zeroes when being read rather than being allowed to allocate an area of disk.

If you want to create a file of arbitrary data quickly without preallocating blocks you need a sparse file, if you want to actually allocate those blocks you need to explicitly write something into them.

share|improve this answer
    
The point is not creating a sparse file, but actually allocating the data. –  daknøk Aug 8 '12 at 21:17
    
@RadekSlupik I've edited my answer. –  Mokubai Aug 8 '12 at 21:47

It might be what you need, instant raw file:

qemu-img create yourfile.img 2G
Formatting 'yourfile.img', fmt=raw size=2147483648

$ls -all
-rw-r--r--  1 mugen users 2147483648 Aug  8 23:48 yourfile.img

Mokubai: "This is because if a userland application could do that it could potentially expose sensitive data from other users by allocating a file the size of all the free space on a disk and trawl through it searching for passwords and other sensitive data that another user may have saved, copied, or deleted. This is considered explicit bad design in operating systems that like to think of themselves as secure and no modern multi-user operating system will do this."

Indeed, thats why shred was invented for.

share|improve this answer
    
This is equivalent to a sparse file: Qemu documentation "raw (default) the raw format is a plain binary image of the disc image, and is very portable. On filesystems that support sparse files, images in this format only use the space actually used by the data recorded in them." –  Mokubai Aug 8 '12 at 21:55
    
Thank you for pointing, i was guessing but now im sure qemu creates sparse file. +1 –  okobaka Aug 8 '12 at 22:10
    
So who found a way to read that garbage treasure? –  okobaka Aug 8 '12 at 23:04

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.