Skip to content

Instantly share code, notes, and snippets.

@caballerofelipe
Created August 3, 2024 01:14
Show Gist options
  • Save caballerofelipe/d1e79c7cd84efe81fee4fc0eaf385eba to your computer and use it in GitHub Desktop.
Save caballerofelipe/d1e79c7cd84efe81fee4fc0eaf385eba to your computer and use it in GitHub Desktop.
# The complete example from: https://unix.stackexchange.com/a/780608/256005
# Create a 100M file, filled with zeros
dd if=/dev/zero of=100MB.bin bs=1024 count=102400
# Show the size used by 100MB.bin
ls -sh 100MB.bin
# Add a filesystem to 100MB.bin
mkfs -t ext4 100MB.bin
# Show the size used by 100MB.bin
ls -sh 100MB.bin
# Show the apparent file size of 100MB.bin
du --apparent-size -h 100MB.bin
# Create a directory, used later for mounting
mkdir 100MB.mount
# Mount 100MB.bin inside 100MB.mount
# `sudo` needed
sudo mount 100MB.bin 100MB.mount
# Assigning the current user as owner for 100MB.mount
# recursively, sudo needed
sudo chown -R $USER 100MB.mount
# Show the size used by 100MB.bin
ls -sh 100MB.bin
# Using 1T instead of 1TB
# T=power of 1024, TB=power of 1000
truncate -s 1T 100MB.mount/1T_sparse
# Show the size used by 100MB.bin
ls -sh 100MB.bin
# Show the size used by 100MB.mount/1T_sparse
ls -sh 100MB.mount/1T_sparse
# Show the apparent file size of 100MB.mount/1T_sparse
du --apparent-size -h 100MB.mount/1T_sparse
# Create a non sparse 50M file
dd if=/dev/zero of=100MB.mount/50M_nosparse bs=1024 count=51200
# Show apparent size for 100MB.bin, 100MB.mount/1T_sparse and 100MB.mount/50M_nosparse
du -h --apparent-size 100MB.bin; du -h --apparent-size 100MB.mount/1T_sparse; du -h --apparent-size 100MB.mount/50M_nosparse
# Show used space for 100MB.bin, 100MB.mount/1T_sparse and 100MB.mount/50M_nosparse
ls -sh 100MB.bin; ls -sh 100MB.mount/1T_sparse; ls -sh 100MB.mount/50M_nosparse
# Try adding 100MB of zeros to 100MB.mount/1T_sparse
# An error ocurrs because there is not space left in 100MB.mount
# Which is actually 100MB.bin mounted
dd if=/dev/zero of=100MB.mount/1T_sparse bs=1024 skip=102400 count=102400 conv=notrunc
# Show apparent size for 100MB.bin, 100MB.mount/1T_sparse and 100MB.mount/50M_nosparse
du -h --apparent-size 100MB.bin; du -h --apparent-size 100MB.mount/1T_sparse; du -h --apparent-size 100MB.mount/50M_nosparse
# Show used space for 100MB.bin, 100MB.mount/1T_sparse and 100MB.mount/50M_nosparse
ls -sh 100MB.bin; ls -sh 100MB.mount/1T_sparse; ls -sh 100MB.mount/50M_nosparse
# Using fallocate to "punch holes" where there were a bunch of zeros
fallocate -d 100MB.mount/1T_sparse; fallocate -d 100MB.mount/50M_nosparse
# Show apparent size for 100MB.bin, 100MB.mount/1T_sparse and 100MB.mount/50M_nosparse
du -h --apparent-size 100MB.bin; du -h --apparent-size 100MB.mount/1T_sparse; du -h --apparent-size 100MB.mount/50M_nosparse
# Show used space for 100MB.bin, 100MB.mount/1T_sparse and 100MB.mount/50M_nosparse
ls -sh 100MB.bin; ls -sh 100MB.mount/1T_sparse; ls -sh 100MB.mount/50M_nosparse
# Unmount 100MB.mount
sudo umount 100MB.mount
# Using fallocate to "punch holes" where there were a bunch of zeros
fallocate -d 100MB.bin
# Mount 100MB.bin inside 100MB.mount
# `sudo` needed
sudo mount 100MB.bin 100MB.mount
# Show apparent size for 100MB.bin, 100MB.mount/1T_sparse and 100MB.mount/50M_nosparse
du -h --apparent-size 100MB.bin; du -h --apparent-size 100MB.mount/1T_sparse; du -h --apparent-size 100MB.mount/50M_nosparse
# Show used space for 100MB.bin, 100MB.mount/1T_sparse and 100MB.mount/50M_nosparse
ls -sh 100MB.bin; ls -sh 100MB.mount/1T_sparse; ls -sh 100MB.mount/50M_nosparse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment