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
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
badp
  • 2,977
  • 2
  • 24
  • 32
  • `dd` is the only traditional tool that exposes the `seek` system call (see [dd vs cat -- is dd still relevant these days?](http://unix.stackexchange.com/q/12532) but there are other ways to skin this cat on typical current unices. – Gilles 'SO- stop being evil' Dec 21 '12 at 01:45

3 Answers3

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
Chris Down
  • 122,090
  • 24
  • 265
  • 262
  • 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
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.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
gertvdijk
  • 13,459
  • 7
  • 45
  • 59
  • 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 00:09
7

See also the GNU truncate command:

truncate -s 2G some-file
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501