DD

Základy

DD je ..

.. unixový program pro kopírování dat ze zdroje do cíle po skupině bajtů s možností volby počátečního bajtu, počtu bajtů atp.

DD se hodí na..

  • nekompromisní zálohování
  • vytváření prázdných souborů
  • získávání obrazů CD/DVD/HDD
  • práci s blokovými zařízeními

Syntaxe dd

dd if=<zdroj> of=<cil> [parametry]

Příklady

Vytvoření souboru o určité velikosti

Vytvoření 100MB souboru plného nulových bajtů.

dd if=/dev/zero of=soubor bs=1024K count=100

Pro náhodné bajty stačí použít 'if=/dev/urandom'

Překopírování diskového oddílu na jiný diskový oddíl

dd if=/dev/sda1 of=/dev/sdb1 bs=512

Klonování celého disku

dd if=/dev/sda of=/dev/sdb bs=512

Vytvoření obrazu CD/DVD/HDD

dd if=/dev/xxx of=/tmp/obrazDisku.img

Přijímání standartního vstupu

Do 'dd' můžeme poslat libovolný vstup a ten se bude zpracovávat stejně jako by byl předán pomocí 'if='

Toto vypíše na standartní výstup to co obdrží standartním vstupem skrz rouru.

echo "DD is da best" | dd 2> /dev/null

Tady standartní vstup rozdělíme na bloky o velikosti 1 bajt a na výstupu oddělíme jednotlivé bloky novou řádkou. Zajímavý je rozdíl mezi 'cbs=' a 'bs='.

echo "DD is da best" | dd cbs=1 conv=unblock 2> /dev/null

Zálohování MBR

dd if=/dev/sda of=mbr.img count=1 bs=512

Obnovení MBR

dd if=mbr.img of=/dev/sda

Náhrada shredu

Desetkrát přepíše soubor 512×10 bajtů náhodných dat a pak ještě 512×10 bajtů nul.

{ for ((i=0; i<10; i++)); do dd if=/dev/urandom of=$FILE bs=512 count=10; done; dd if=/dev/zero of=$FILE bs=512 count=10; } 2>/dev/null

Zobrazení virtuální paměti

dd if=/proc/kcore | hexdump -C | less

Změna velikosti písmen v souboru

Všechna písmena velká

dd if=soubor conv=ucase

Všechna písmena malá

dd if=soubor conv=lcase

Vytvoření obrazu paměti

Překopíruje obsah celé operační paměti do souboru

dd if=/dev/mem of=/tmp/mem.img bs=1024

Prohlížení blokového zařízení

dd if=/dev/sda1 | hexdump -C | less

Nebo místo hexdump můžeme použít třeba xxd

dd if=/dev/sda1 | xxd | less

Nelze však výstup použít v editorech jako hexedit, hexcurse, ht a dalších. Výstup je (zpravidla) příliš veliký. Takovýto postup nemá příliš praktické využití, ale v případě, že jej zkombinujeme s prohledáváním blokových zařízení a vyřezáváním oblasti, můžeme provádět (nepříliš efektivní, ale nenahraditelnou) editaci.

Vyhledávání v blokovém zařízení

Určení, zda se hledané slovo nachází na blokovém zařízení nebo v souboru.

dd if=/dev/sda | strings | grep 'hledane slovo'

Určení na jaké adrese se hledané slovo nachází. (Nefunguje pokud je slovo rozděleno do dvou řádků)

dd if=/dev/sda | hexdump -C | grep 'hledane slovo'

Výřez dat

Vytiskne 16 bajtů od bajtu číslo 3372

dd if=disk.img bs=1 skip=3372 count=16 | hexdump -C

Posílání dat po síti

Příjemce

netcat -c -l 3000 | dd of=soubor bs=512

Odesílatel

dd if=soubor bs=512 | netcat 127.0.0.1 3000

Parametry

if=soubor

Specifikuje zdroj dat. Výchozím zdrojem je standartní vstup.

of=soubor

Specifies the output path. Standard output is the default. If the seek=expr conversion is not also specified, the output file will be truncated before the copy begins, unless conv=notrunc is specified. If seek=expr is specified, but conv=notrunc is not, the effect of the copy will be to preserve the blocks in the output file over which dd seeks, but no other portion of the output file will be preserved. (If the size of the seek plus the size of the input file is less than the previous size of the output file, the output file is shortened by the copy.)

ibs=n

Specifies the input block size in n bytes (default is 512).

obs=n

Specifies the output block size in n bytes (default is 512).

bs=n

Nastaví výstupní velikost bloku (obs) a vstupní velikost bloku (ibs) na 'n' bajtů. If no conversion other than sync, noerror, and notrunc is specified, each input block is copied to the output as a single block without aggregating short blocks.

cbs=n

Specifies the conversion block size for block and unblock in bytes by n (default is 0). If cbs= is omitted or given a value of 0, using block or unblock produces unspecified results. This option is used only if ASCII or EBCDIC conversion is specified. For the ascii and asciib operands, the input is handled as described for the unblock operand except that characters are converted to ASCII before the trailing SPACE characters are deleted. For the ebcdic, ebcdicb, ibm, and ibmb operands, the input is handled as described for the block operand except that the characters are converted to EBCDIC or IBM EBCDIC after the trailing SPACE characters are added.

files=n

Copies and concatenates n input files before terminating (makes sense only where input is a magnetic tape or similar device).

skip=n

Skips n input blocks (using the specified input block size) before starting to copy. On seekable files, the implementation reads the blocks or seeks past them. On non-seekable files, the blocks are read and the data is discarded.

iseek=n

Seeks n blocks from beginning of input file before copying (appropriate for disk files, where skip can be incredibly slow).

oseek=n

Seeks n blocks from beginning of output file before copying.

seek=n

Skips n blocks (using the specified output block size) from beginning of output file before copying. On non-seekable files, existing blocks are read and space from the current end-of-file to the specified offset, if any, is filled with null bytes. On seekable files, the implementation seeks to the specified offset or reads the blocks as described for non-seekable files.

count=n

Specifikuje počet bajtů, který se ze zdroje má číst

conv=value[,value. . . ]

Where values are comma-separated symbols from the following list:

ascii

Převede EBCDIC na ASCII.

asciib

Converts EBCDIC to ASCII using BSD-compatible character translations.

ebcdic

Converts ASCII to EBCDIC. If converting fixed-length ASCII records without NEWLINEs, sets up a pipeline with dd conv=unblock beforehand.

ebcdicb

Converts ASCII to EBCDIC using BSD-compatible character translations. If converting fixed-length ASCII records without NEWLINEs, sets up a pipeline with dd conv=unblock beforehand.

ibm

Slightly different map of ASCII to EBCDIC. If converting fixed-length ASCII records without NEWLINEs, sets up a pipeline with dd conv=unblock beforehand.

ibmb

Slightly different map of ASCII to EBCDIC using BSD-compatible character translations. If converting fixed-length ASCII records without NEWLINEs, sets up a pipeline with dd conv=unblock beforehand.

The ascii (or asciib), ebcdic (or ebcdicb), and ibm (or ibmb) values are mutually exclusive.

block

Treats the input as a sequence of NEWLINE-terminated or EOF-terminated variable-length records independent of the input block boundaries. Each record is converted to a record with a fixed length specified by the conversion block size. Any NEWLINE character is removed from the input line. SPACE characters are appended to lines that are shorter than their conversion block size to fill the block. Lines that are longer than the conversion block size are truncated to the largest number of characters that will fit into that size. The number of truncated lines is reported.

unblock

Converts fixed-length records to variable length. Reads a number of bytes equal to the conversion block size (or the number of bytes remaining in the input, if less than the conversion block size), delete all trailing SPACE characters, and append a NEWLINE character. The block and unblock values are mutually exclusive.

lcase

Maps upper-case characters specified by the LC_CTYPE keyword tolower to the corresponding lower-case character. Characters for which no mapping is specified are not modified by this conversion.

ucase

Maps lower-case characters specified by the LC_CTYPE keyword toupper to the corresponding upper-case character. Characters for which no mapping is specified are not modified by this conversion. The lcase and ucase symbols are mutually exclusive.

swab

Swaps every pair of input bytes. If the current input record is an odd number of bytes, the last byte in the input record is ignored.

noerror

Does not stop processing on an input error. When an input error occurs, a diagnostic message is written on standard error, followed by the current input and output block counts in the same format as used at completion. If the sync conversion is specified, the missing input is replaced with null bytes and processed normally. Otherwise, the input block will be omitted from the output.

notrunc

Does not truncate the output file. Preserves blocks in the output file not explicitly written by this invocation of dd. (See also the preceding of=file operand.)

sync

Pads every input block to the size of the ibs= buffer, appending null bytes. (If either block or unblock is also specified, appends SPACE characters, rather than null bytes.) If operands other than conv= are specified more than once, the last specified operand=value is used. For the bs=, cbs=, ibs=, and obs= operands, the application must supply an expression specifying a size in bytes. The expression, expr, can be:

a positive decimal number a positive decimal number followed by k, specifying multiplication by 1024 a positive decimal number followed by M, specifying multiplication by 1024*1024 a positive decimal number followed by b, specifying multiplication by 512 two or more positive decimal numbers (with or without k or b) separated by x, specifying the product of the indicated values.

Signál SIGUSR1

Pokud nám běží 'dd' delší dobu, může nás zajímat stav jeho činnosti. Ten můžeme zjistit pomocí příkazu 'kill' s použitím signálu USR1.

bash$ dd if=/dev/zero of=/dev/null &
[1] 10775
bash$ jobs
[1]+  Running                 dd if=/dev/zero of=/dev/null &
bash$ kill -10 10775
bash$ 13006004+0 vstoupivších záznamů
13006003+0 vystoupivších záznamů
6 659 073 536 bajtů (6,7 GB) zkopírováno, 12,0784 s, 551 MB/s

bash$ kill -10 10775
bash$ 31481047+0 vstoupivších záznamů
31481046+0 vystoupivších záznamů
16 118 295 552 bajtů (16 GB) zkopírováno, 29,1654 s, 553 MB/s

bash$ kill -10 10775
bash$ 36411541+0 vstoupivších záznamů
36411541+0 vystoupivších záznamů
18 642 708 992 bajtů (19 GB) zkopírováno, 33,7022 s, 553 MB/s

bash$ kill -10 10775
bash$ 38414511+0 vstoupivších záznamů
38414510+0 vystoupivších záznamů
19 668 229 120 bajtů (20 GB) zkopírováno, 35,5485 s, 553 MB/s

bash$ kill 10775
bash$ jobs
[1]+  Terminated              dd if=/dev/zero of=/dev/null
bash$ 
/home/dum8d0g/www/martin.kopta.eu/trash/wiki/data/pages/software/dd.txt · Poslední úprava: 2010/05/01 10:32 autor: martin
CC Attribution 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0