Skip to content

Instantly share code, notes, and snippets.

@jdforsythe
Last active December 16, 2023 03:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jdforsythe/7cb9a10cec8dde2da2181d25645456b6 to your computer and use it in GitHub Desktop.
Save jdforsythe/7cb9a10cec8dde2da2181d25645456b6 to your computer and use it in GitHub Desktop.
dd tricks
#!/bin/bash
sudo fdisk -lu /dev/sdb
# Disk /dev/sdb: 14.9Gib, 1593153946 bytes, 31116288 sectors
# Units: sectors of 1 * 512 = 512 bytes
# Secor size (logical/physical): 512 bytes / 512 bytes
# I/O size (minimum/optimal): 512 bytes / 512 bytes
# Disklabel type: dos
# Disk identifier: 0x9c4e41cd
#
# Device Boot Start End Sectors Size Id Type
# /dev/sdb1 * 2048 133119 131072 64M c W95 FAT32 (LBA)
## find the END sector number for the last partition, in this case, 133119
## add 1 for the zero-indexed sector, so 133120
## and match the sector size
sudo dd if=/dev/sdb of=./smaller-image.img bs=512 count=133120
## and on restore, use the same
sudo dd if=./smaller-image.img of=/dev/sdb bs=512
#!/bin/bash
## mount the partition so it can be zero-filled
mkdir ~/srcfs && mound /dev/sdXN ~/srcfs
## fill the partition with zeroes
dd if=/dev/zero of=~/srcfs/tempzero.txt
## delete the file
rm ~/srcfs/tempzero.txt
## tell dd to "punch" zeroes from the image when reading
dd conv=sparse if=/dev/sdXN of=./image.img
## punch zeroes when writing back out
dd conv=sparse if=./image.img of=/dev/sdXN
alias ddstatus='sudo kill -USR1 $(pgrep ^dd)'
alias ddwatch='watch -n5 "sudo kill -USR1 $(pgrep ^dd)"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment