Skip to content

Instantly share code, notes, and snippets.

@shannonwells
Last active June 30, 2021 21:15
Show Gist options
  • Save shannonwells/14b3e645f38081b3738bc16c40b58959 to your computer and use it in GitHub Desktop.
Save shannonwells/14b3e645f38081b3738bc16c40b58959 to your computer and use it in GitHub Desktop.
How to write a bunch of ones to disk
# Lifted from Stack Overflow. This worked for me, it's fast, yes but it still takes awhile to write to a 500 GB
# disk - about 3-4 hours. Instead of just filling up the disk I routed it to an output file and included a count.
# Also I have replaced seq with
# echo {1..65536}
# as being part of coreutils, seq is unavailable in restore mode on OS X.
# So the command I used to run the script was:
# sh /tmp/writeones.sh | dd of=/Volumes/Machintosh\ HD/output.txt bs=65536 count=500000000
# --sew
# HOWEVER, if you think of it first, use the shred tool.
# Source URL https://unix.stackexchange.com/questions/150988/how-to-use-dd-to-fill-drive-with-1s#151006
# This is a very fast and efficient way of writing 64 kB strings of 0xFF over and over, using awk,
# consuming < 8% of CPU and limited only by the speed of the drive you are writing to.
#
# I have tried using tr as was suggested here and found it was painfully slow and consumed a lot of
# CPU translating each and every Byte. My method works in 64 kB blocks and is at least 3.5x
# faster than the single character-based tr (26 MB/s versus 7 MB/s on old PATA hw - finishing in 52 minutes in silence versus 3+ hours with loud cooling fan revs...). I like when people brush off basic computer science knowledge without even testing their opinion first.
#
# I recommend building the following script using printf from the shell rather than attempting to
# write it in vi since most boot CD-ROMs will not give you sufficient tmp space for the buffer
# log to copy-and-paste 65,536 times...
# 1) Compose the script
printf "echo | awk '{\n\twhile (1) {\n\t\tprintf(\"%%s\", \"" >/tmp/writeones.sh
for i in `echo {1..65536}`; do printf '\377' >>/tmp/writeones.sh; done
printf "\");\n}\n}'\n" >>/tmp/writeones.sh
# the output script seems like: (Omit some characters 0xff in string)
#
# echo | awk '{
# while (1) {
# printf("%s", "ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ...");
# }
#}'
# 2) Test the script:
# sh /tmp/writeones.sh | od -Ax -tx1
# 000000 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
# *
# Ctrl+C
#
# 3) Run the script and pipe to your drive with dd (double-check you have the right drive!!!):
# sh /tmp/writeones.sh | dd of=/dev/ada0 bs=65536 &
# Why did I choose 64 kB? 1. Arg list limitation of AWK, and 2. for my hardware, this
# achieves the best speed for me. I also tried writing this in just shell using printf and
# found it was 40% slower and consumed 80% of the CPU because of all the forks.
# edited Oct 19 '19 at 13:34
# yurenchen
# 19711 silver badge99 bronze badges
# answered Jul 7 '15 at 14:06
# Kenneth Salerno
# 3111 bronze badge
@shannonwells
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment