Skip to content

Instantly share code, notes, and snippets.

@actionjack
Forked from CMCDragonkai/create_sparse_file.sh
Created March 13, 2023 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save actionjack/e4121827d48306317aa6268654567930 to your computer and use it in GitHub Desktop.
Save actionjack/e4121827d48306317aa6268654567930 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