dd
There is a reason dd is sometimes humorously called disk destroyer! Incorrect use of the dd command can wipe any drive connected to the system. Always backup any data you're not willing to lose before using the command.
dd is a utility used to copy raw data from a source into sink, where source and sink can be a block device, file, or piped input/output. Because of its flexibility dd can be used for a variety of purposes ranging from writing installation media to a backup and recovery tool of last resort.
The dd utility is specified by the POSIX standard. On Gentoo, dd is provided by GNU coreutils, sys-apps/coreutils, installed via the @system set.
Installation
Emerge
In the event that coreutils goes missing:
root #
emerge --ask sys-apps/coreutils
Usage
By default dd takes input from stdin, optionally manipulates the data, and writes to stdout.
Invocation
user $
dd --help
Examples
Some common tasks where dd is used:
Boot stick
This should work with any live media as long as the memory stick /dev/sdX is large enough.
Any data on the memory stick will be lost.
root #
dd if=/home/myLiveCD.iso of=/dev/sdX bs=8M status=progress && sync
if
: Defines the source.of
: Defines the sink.bs
: Defines the block size (amount of data read/written at a time). The default is 512 bytes but most modern devices can read/write much faster. It is possible to define different sizes for source and sink usingibs
andobs
.status
: Defines the level of status information printed.- sync: Synchronizes write caches to make the stick removal safe.
Master boot record backup
To backup the master boot record (MBR), copy only the first 512 bytes:
root #
dd if=/dev/sdX of=/root/mbr.bin bs=512 count=1
count
: The number of blocks to copy.
This is the complete MBR with the partition layout.
Hard disk backup
Using dd as a disk backup is generally not recommended except when a perfect image is needed. This will include unused space in the image making it larger than the data contained inside the image. The storage medium must be as large or larger than the source disk.
To backup a complete hard disk or partition, it is necessary to boot the computer with into a Live CD environment (such as the Gentoo Minimal or Gentoo Admin disk).
The following example will backup a computer drive on /dev/sda to an external USB drive. To be able to mount that USB drive read/write, this example will use its label:
root #
ls /dev/disk/by-label
'Gentoo\x20amd64\x20AdminCD\x2020201230T21' MaxiTux
root #
mkdir /mnt/MaxiTux
root #
mount /dev/disk/by-label/MaxiTux /mnt/MaxiTux
Not every file system includes a label. Using UUID values or verified disk paths are alternatives.
To create a backup:
root #
dd if=/dev/sda conv=sync,noerror bs=64k status=progress > /mnt/MaxiTux/sda_backup.img
To restore a backup:
root #
dd if=/mnt/MaxiTux/sda_backup.img bs=8192 conv=sync,noerror of=/dev/sda status=progress
Input manipulation
As an example, convert any upper case character in a file to lowercase and reverse the input per line, then pipe the output to less to display the file:
user $
dd if=/etc/portage/make.conf conv=swab,lcase,noerror | less
conv=swab
: Revert the input per line by swapping any input byte (writing backwards).conv=lcase
: Convert any upper case letter to lower case. To convert lower case to upper case useconv=ucase
.conv=noerror
: Continue if a read error occurs.
See also
- dcfldd — an enhanced dd tool that includes additional features for forensics and security.
- ddrescue — a tool provided by GNU to retrieve data from failing (block) storage devices like disk drives, CDROMs, or memory sticks, etc.
- pv — a command line tool to view verbose information about data streamed/piped through it.