18

What is the console equivalent of the following Python code:

target = file("disk", "w")    # create a file
target.seek(2*1024*1024*1024) # skip to 2 GB
target.write("\0")
target.close()

Maybe some dd incantation? The idea is making a file with the apparent size of 2 GB for use e.g. in virtualization.

kvm disk -cd whatever.iso #Only allocate space as necessary
8

Generally speaking, just use dd; but as you mention the use of KVM virtualization, you might consider using qemu-img:

qemu-img create -f raw disk 2G

It does the same as the dd command in the answer of Chris Down, effectively.

Regardless of what command you use, for use in virtualization, I would strongly suggest using fallocate to pre-allocate blocks in order to prevent fragmentation and increase performance.

fallocate -l 2G disk

It's not available on all platforms and filesystems, though. This will not write zeroes, but just assigns blocks to the file, rather than doing that on-demand later every time it has to extend the file.

  • Is the quote a typo? – badp Dec 20 '12 at 14:32
  • @badp yes, fixed. – gertvdijk Dec 20 '12 at 14:32
  • 2
    qemu-img and dd both perform one system call to set the file size (ftruncate), but will perform a lot more to load themselves and the libraries they're linked to. And, in that regard, dd is going to be a lot more effective than qemu-img (which is a lot larger and is linked to far more libraries). GNU truncate is going to be even more effective. dd also has the advantage of being ubiquitous. Good point about fallocate though. – Stéphane Chazelas Dec 20 '12 at 17:42
  • @StephaneChazelas I totally second your comment. I've edited my answer to point out that qemu-img is just an obvious alternative in the use of KVM virtualization. – gertvdijk Dec 21 '12 at 0:09
14

You can create a sparse file like this with dd:

dd of=file bs=1 seek=2G count=0
$ du file
0       disk
$ du --apparent-size file
2097152 disk
  • 4
    Also, 2G is a GNU extension to dd. You can use bs=1024 seek=2097152 if you don't have GNU dd. – Chris Down Dec 20 '12 at 14:29
  • Heh, G is an extension, and it's not supported by OpenBSD's version of dd... but, M and K are supported, so maybe seek=2048M is a bit more readable, depending on what platforms you're targetting – Earlz Dec 20 '12 at 20:01
  • You should read from /dev/zero: if=/dev/zero – Daniel Fanjul Dec 25 '12 at 21:03
  • @DanielFanjul Why? There is absolutely no difference, no bytes are written. – Chris Down Dec 25 '12 at 21:39
  • @ChrisDown Because /dev/null contains no data when you read, but /dev/zero contains infinte zeros. Oh, count=1, the number of bytes to write must not be zero. – Daniel Fanjul Dec 26 '12 at 18:39
6

See also the GNU truncate command:

truncate -s 2G some-file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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