Skip to content

Instantly share code, notes, and snippets.

@CMCDragonkai
Last active April 21, 2023 03:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save CMCDragonkai/97c800ba2dad704ef989 to your computer and use it in GitHub Desktop.
Save CMCDragonkai/97c800ba2dad704ef989 to your computer and use it in GitHub Desktop.
CLI: Creating Sparse Files (Can be used for virtual loopback block devices)
#!/usr/bin/env bash
# seems to work on any filesystem
# final size is (bs * seek) in bytes
# this creates a 4MB sparse file
dd if=/dev/zero of=/tmp/file bs=1 count=0 seek=4194304
# or
dd if=/dev/zero of=/tmp/file bs=1 count=0 seek=4M
# linux specific
# this doesn't work on some filesystems like FAT32 and ZFS, but easier to remember
# its unique feature is that it does actually reserve the space
# preventing the space from being used by other things
fallocate -l 4M /tmp/file
# so you could create a sparse file larger than the amount of space you really have
# seems equivalent to the dd command, doesn't actually reserve the space
truncate -s 4M /tmp/file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment