Английская Википедия:Dd (Unix)

Материал из Онлайн справочника
Перейти к навигацииПерейти к поиску

Шаблон:Lowercase title Шаблон:Short description Шаблон:Infobox software dd is a command-line utility for Unix, Plan 9, Inferno, and Unix-like operating systems and beyond, the primary purpose of which is to convert and copy files.[1] On Unix, device drivers for hardware (such as hard disk drives) and special device files (such as /dev/zero and /dev/random) appear in the file system just like normal files; Шаблон:Mono can also read and/or write from/to these files, provided that function is implemented in their respective driver. As a result, Шаблон:Mono can be used for tasks such as backing up the boot sector of a hard drive, and obtaining a fixed amount of random data. The Шаблон:Mono program can also perform conversions on the data as it is copied, including byte order swapping and conversion to and from the ASCII and EBCDIC text encodings.[2]

History

In 1974, the Шаблон:Mono command appeared as part of Version 5 Unix. According to Dennis Ritchie, the name is an allusion to the DD statement found in IBM's Job Control Language (JCL),[3][4] in which it is an abbreviation for "Data Definition".[5][6] According to Douglas McIlroy, Шаблон:Mono was "originally intended for converting files between the ASCII, little-endian, byte-stream world of DEC computers and the EBCDIC, big-endian, blocked world of IBM"; thus, explaining the cultural context of its syntax.[7] Eric S. Raymond believes "the interface design was clearly a prank", due to the command's syntax resembling a JCL statement more than other Unix commands do.[4]

In 1987, the Шаблон:Mono command is specified in the X/Open Portability Guide issue 2 of 1987. This is inherited by IEEE Std 1003.1-2008 (POSIX), which is part of the Single UNIX Specification.[8]

In 1990, David MacKenzie announces GNU fileutils (now part of coreutils) which includes the dd command;[9] it was written by Paul Rubin, David MacKenzie, and Stuart Kemp.[10] Since 1991, Jim Meyering is its maintainer.[11]

In 1995, Plan 9 2nd edition is released; its Шаблон:Mono command interface is redesigned to use a traditional command-line option style instead of a JCL statement style.[12]

Since at least 1999,[13] there's UnxUtils, a native Win32 port for Microsoft Windows using GNU fileutils.[14]

dd is sometimes humorously called "Disk Destroyer", due to its drive-erasing capabilities involving typos.[15]

Usage

The command line syntax of Шаблон:Mono differs from many other Unix programs. It uses the syntax Шаблон:Mono for its command-line options rather than the more standard Шаблон:Mono or Шаблон:Mono formats. By default, Шаблон:Mono reads from stdin and writes to stdout, but these can be changed by using the Шаблон:Mono (input file) and Шаблон:Mono (output file) options.[8]

Certain features of Шаблон:Mono will depend on the computer system capabilities, such as Шаблон:Mono's ability to implement an option for direct memory access. Sending a SIGINFO signal (or a USR1 signal on Linux) to a running Шаблон:Mono process makes it print I/O statistics to standard error once and then continue copying. Шаблон:Mono can read standard input from the keyboard. When end-of-file (EOF) is reached, Шаблон:Mono will exit. Signals and EOF are determined by the software. For example, Unix tools ported to Windows vary as to the EOF: Cygwin uses Шаблон:Key press (the usual Unix EOF) and MKS Toolkit uses Шаблон:Keypress (the usual Windows EOF).

The non-standardized parts of dd invocation vary among implementations.

Output messages

On completion, Шаблон:Mono prints to the stderr stream about statistics of the data transfer. The format is standardized in POSIX.[8]Шаблон:Rp The manual page for GNU dd does not describe this format, but the BSD manuals do.

Each of the "Records in" and "Records out" lines shows the number of complete blocks transferred + the number of partial blocks, e.g. because the physical medium ended before a complete block was read, or a physical error prevented reading the complete block.

Block size

A block is a unit measuring the number of bytes that are read, written, or converted at one time. Command-line options can specify a different block size for input/reading (Шаблон:Mono) compared to output/writing (Шаблон:Mono), though the block size (Шаблон:Mono) option will override both Шаблон:Mono and Шаблон:Mono. The default value for both input and output block sizes is 512 bytes (the traditional block size of disks, and POSIX-mandated size of "a block"). The Шаблон:Mono option for copying is measured in blocks, as are both the Шаблон:Mono count for reading and Шаблон:Mono count for writing. Conversion operations are also affected by the "conversion block size" (Шаблон:Mono).[8]Шаблон:Rp

The value provided for block size options is interpreted as a decimal (base 10) integer number of bytes. It can also contain suffixes to indicate that the block size is an integer number of larger units than bytes. POSIX only specifies the suffixes Шаблон:Mono (blocks) for 512 and Шаблон:Mono (kibibytes) for 1024.[8]Шаблон:Rp Implementation differ on the additional suffixes they support: (Free) BSD uses lowercase Шаблон:Mono (mebibytes), Шаблон:Mono (gibibytes), and so on for tebibytes, exbibytes, pebibytes, zebibytes, and yobibytes,[16] while GNU uses Шаблон:Mono and Шаблон:Mono for the same units, with Шаблон:Mono, Шаблон:Mono, and Шаблон:Mono used for their SI unit counterparts (kilobytes).[10] For example, for GNU Шаблон:Mono, Шаблон:Mono indicates a blocksize of 16 mebibytes (16777216 bytes) and Шаблон:Mono specifies 3000 bytes.

Additionally, some implementations understand the Шаблон:Mono character as a multiplication operator for both block size and count parameters. For example, Шаблон:Mono is interpreted as 2 × 80 × 18 × 512 = Шаблон:Val, the exact size of a 1440 KiB floppy disk. This is required in POSIX.[8]Шаблон:Rp For implementations that do not support this feature, the POSIX shell arithmetic syntax of bs=$((2*80*18))b may be used.

Block size has an effect on the performance of copying Шаблон:Mono commands. Doing many small reads or writes is often slower than doing fewer large ones. Using large blocks requires more RAM and can complicate error recovery. When Шаблон:Mono is used with variable-block-size devices such as tape drives or networks, the block size may determine the tape record size or packet size, depending on the network protocol used.

Uses

The Шаблон:Mono command can be used for a variety of purposes. For plain-copying commands it tends to be slower than the domain-specific alternatives, but it excels at its unique ability to "overwrite or truncate a file at any point or seek in a file", a fairly low-level interface to the Unix file API.[17]

The examples below assume the use of GNU dd, mainly in the block size argument. To make them portable, replace e.g. Шаблон:Code with the shell arithmetic expression Шаблон:Code or Шаблон:Code (written equivalently with a bit shift).

Data transfer

Шаблон:Mono can duplicate data across files, devices, partitions and volumes. The data may be input or output to and from any of these; but there are important differences concerning the output when going to a partition. Also, during the transfer, the data can be modified using the Шаблон:Mono options to suit the medium. (For this purpose, however, Шаблон:Mono is slower than Шаблон:Mono.)[17]

Data transfer forms of Шаблон:Mono
Шаблон:Pre Creates an ISO disk image from a CD-ROM, DVD or Blu-ray disc.[18]
Шаблон:Pre Restores a hard disk drive (or an SD card, for example) from a previously created image.
Шаблон:Pre Create an image of the partition sdb2, using a 64 MiB block size.
Шаблон:Pre Clones one partition to another.
Шаблон:Pre Clones a hard disk drive "ad0" to "ad1".

The Шаблон:Mono option means to keep going if there is an error, while the Шаблон:Mono option causes output blocks to be padded.

In-place modificationШаблон:Anchor

Шаблон:Mono can modify data in place. For example, this overwrites the first 512 bytes of a file with null bytes:

Шаблон:Pre

The Шаблон:Mono conversion option means do not truncate the output file — that is, if the output file already exists, just replace the specified bytes and leave the rest of the output file alone. Without this option, Шаблон:Mono would create an output file 512 bytes long.

Master boot record backup and restore

The example above can also be used to back up and restore any region of a device to a file, such as a master boot record.

To duplicate the first two sectors of a floppy disk: Шаблон:Pre

Disk wipe

Шаблон:Main

For security reasons, it is sometimes necessary to have a disk wipe of a discarded device. This can be achieved by a "data transfer" from the Unix special files.

When compared to the data modification example above, Шаблон:Mono conversion option is not required as it has no effect when the Шаблон:Mono's output file is a block device.[19]

The Шаблон:Mono option makes dd read and write 16 mebibytes at a time. For modern systems, an even greater block size may be faster. Note that filling the drive with random data may take longer than zeroing the drive, because the random data must be created by the CPU, while creating zeroes is very fast. On modern hard-disk drives, zeroing the drive will render most data it contains permanently irrecoverable.[20] However, with other kinds of drives such as flash memories, much data may still be recoverable by data remanence.

Modern hard disk drives contain a Secure Erase command designed to permanently and securely erase every accessible and inaccessible portion of a drive. It may also work for some solid-state drives (flash drives). As of 2017, it does not work on USB flash drives nor on Secure Digital flash memories.Шаблон:Citation needed When available, this is both faster than using dd, and more secure.Шаблон:Citation needed On Linux machines it is accessible via the hdparm command's Шаблон:Mono option.

The shred program offers multiple overwrites, as well as more secure deletion of individual files.

Data recovery

Data recovery involves reading from a drive with some parts potentially inaccessible. Шаблон:Code is a good fit with this job with its flexible skipping (Шаблон:Code) and other low-level settings. The vanilla Шаблон:Code, however, is clumsy to use as the user has to read the error messages and manually calculate the regions that can be read. The single block size also limits the granuarity of the recovery, as a trade-off has to be made: either use a small one for more data recovered or use a large one for speed.

A C program called Шаблон:Code[21] was written in October 1999. It did away with the conversion functionality of Шаблон:Code, and supports two block sizes to deal with the dilemma. If a read using a large size fails, it falls back to the smaller size to gather as much as data possible. It can also run backwards. In 2003, a Шаблон:Code script was written to automate the process of using Шаблон:Code, keeping track of what areas have been read on its own.[22]

In 2004, GNU wrote a separate utility, unrelated to Шаблон:Code, called Шаблон:Mono. It has a more sophisticated dynamic block-size algorithm and keeps track of what has been read internally. The authors of both Шаблон:Code and Шаблон:Code consider it superior to their implementation.[23] To help distinguish the newer GNU program from the older script, alternate names are sometimes used for GNU's Шаблон:Code, including Шаблон:Code (the name on freecode.com and freshmeat.net), Шаблон:Code (Debian package name), and Шаблон:Code (openSUSE package name).

Another open-source program called Шаблон:Code uses a sophisticated algorithm, but it also requires the installation of its own programming-language interpreter.

Benchmarking drive performance

To make drive benchmark test and analyze the sequential (and usually single-threaded) system read and write performance for 1024-byte blocks:

  • Write performance: dd if=/dev/zero bs=1024 count=1000000 of=1GB_file_to_write
  • Read performance: dd if=1GB_file_to_read of=/dev/null bs=1024

Generating a file with random data

To make a file of 100 random bytes using the kernel random driver: Шаблон:Pre

Converting a file to upper case

To convert a file to uppercase: Шаблон:Pre

Progress indicator

Being a program mainly designed as a filter, Шаблон:Mono normally does not provide any progress indication. This can be overcome by sending an Шаблон:Mono signal to the running GNU Шаблон:Mono process (Шаблон:Mono on BSD systems), resulting in Шаблон:Mono printing the current number of transferred blocks.

The following one-liner results in continuous output of progress every 10 seconds until the transfer is finished, when Шаблон:Mono is replaced by the process-id of Шаблон:Mono: Шаблон:Pre

Newer versions of GNU Шаблон:Mono support the Шаблон:Mono option, which enables periodic printing of transfer statistics to stderr.[24]

Forks

dcfldd Шаблон:Anchor

Шаблон:Mono is a fork of GNU Шаблон:Mono that is an enhanced version developed by Nick Harbour, who at the time was working for the United States' Department of Defense Computer Forensics Lab.[25][26][27] Compared to Шаблон:Mono, Шаблон:Mono allows more than one output file, supports simultaneous multiple checksum calculations, provides a verification mode for file matching, and can display the percentage progress of an operation. As of February 2024, the last release was 1.9.1 from April 2023.[28]

dc3dd

Шаблон:Mono is another fork of GNU Шаблон:Mono from the United States Department of Defense Cyber Crime Center (DC3). It can be seen as a continuation of the dcfldd, with a stated aim of updating whenever the GNU upstream is updated. Шаблон:As of, the last release was 7.3.1 from April 2023.[29]

See also

Шаблон:Portal

References

Шаблон:Reflist

External links

Шаблон:Wikibooks

Шаблон:Optical disc image software Шаблон:Unix commands Шаблон:Plan 9 commands Шаблон:Core Utilities commands

Шаблон:Backup software