Last active
August 19, 2024 09:28
-
-
Save jdforsythe/7cb9a10cec8dde2da2181d25645456b6 to your computer and use it in GitHub Desktop.
dd tricks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
See also https://gist.github.com/AndreiCherniaev/12e360601b1bfe15cdb1e79ce3a8e31e