dd

最後更新: 2019-11-13

 

目錄

  • dd with gzip
  • Options - bs
  • bs & count Q&A
  • Create urandom file
  • 修改 hex file 的某 location
  • 知到要做幾耐(status=progress)
  • 查看 dd 的 status
  • Window 上的 dd
  • Disk IO Test
  • dcfldd

 


dd with gzip

 

dd if=/dev/sda1 | gzip > sda1.img.gz

 

 


Options - bs

 

read and write up to BYTES bytes at a time

# Unit

Default: bs=BYTES

K, M, G         = 1024

kB, MB, GB    = 1000

Speed test with different bs

# Default Setting: 94 MiB/s

dd if=/dev/sda | pv -i 3 > /dev/null

# 8M: 99 MiB/s

dd bs=8M if=/dev/sda | pv -i 3 > /dev/null

# 32M: 98 MiB/s

dd bs=32M if=/dev/sda | pv -i 3 > /dev/null

結論: bs 先速度沒有關係

 


bs & count Q&A

請問大家, 以下兩條指令的結果是不是一樣的呢 ??

# cmd_1

dd if=/dev/zero of=~/myfile1 bs=8 count=8

# cmd_2

dd if=/dev/zero of=~/myfile2 bs=64 count=1

相信大家都會答 "是的"

因為它都是會產生一個大小為 64 byte 的 myfile

那我又問, 以下兩指令的結果是不是一樣的呢 ??

# cmd_3

dd if=/dev/random of=~/myfile1 bs=8 count=8

# cmd_4

dd if=/dev/random of=~/myfile2 bs=32 count=2

答案 :

有機會一樣, 亦可能不一樣 = , =", 原因 ??

因為 /dev/random 的 output 是不保證足 64 byte, 所以就有 urandom 了

 


Create urandom file

 

dd if=/dev/urandom of=sample.txt bs=128M count=1

 


以 hex 修改 Binary file 的某位置

 

# 查看

cat test.txt

0123456789abcdef
0123456789abcdef
0123456789abcdef
0123456789abcdef
----------------
0123456789abcdef
0123456789abcdef
0123456789abcdef
0123456789abcdef

# 修改

# 隨機修改位置 0x8 的 1 byte

# 用 bs 及 count 去限制影響 (bs = read and write) !!

# bs=1 count=1 => 1 byte

# conv=notrunc => Do not truncate the output file (必須!!), 否則 x08 後的內容會被清空

# seek=8 => 跳過頭 8 個, 修改第 9 個, 由於由 0x00 計起, 所以修改緊 x08

dd if=/dev/urandom of=test.txt bs=1 count=1 conv=notrunc seek=8

# 查看

xxd -l 16 test.txt

00000000: 3031 3233 3435 3637 a639 6162 6364 6566  01234567.9abcdef

OR

xxd test.txt

# Write byte at address

# \x2a => "*"

printf '\x2a\x2a\x2a' | dd of=test.txt bs=1 seek=3 count=3 conv=notrunc

xxd -l 16 test.txt

00000000: 3031 322a 2a2a 3637 3839 6162 6364 6566  012***6789abcdef

 


知到要做幾耐(status=progress)

 

# visual size/percentage progress indicator

status=LEVEL

  • 'none' suppresses everything but error messages,
  • 'noxfer' suppresses the final transfer statistics,
  • 'progress' shows periodic transfer statistics

i.e.

dd status=progress if=/dev/mapper/centos-root of=/dev/null

715182592 bytes (715 MB) copied, 3.006233 s, 238 MB/s

 


查看 dd 的 status

 

dd if=/dev/mapper/centos-root of=/dev/null

kill -USR1 $(pgrep ^dd)

2310657+0 records in
2310657+0 records out
1183056384 bytes (1.2 GB) copied, 1.81717 s, 651 MB/s

 


Window 上的 dd

 

Home Page: http://www.chrysocome.net/dd

# Go to un-zip folder

cd C:\dd-0.5>

# 查看本機上的 Disk

dd --list

\\.\Volume{a4942ba8-deae-11e6-b2c7-e0cb4eeccb4b}\
  link to \\?\Device\HarddiskVolume7
  removeable media
  Mounted on \\.\e:

\\.\Volume{2c9987c2-cf93-11e3-b215-806e6f6e6963}\
  link to \\?\Device\CdRom0
  CD-ROM
  Mounted on \\.\o:

Virtual input devices
 /dev/zero   (null data)
 /dev/random (pseudo-random data)
 -           (standard input)

Virtual output devices
 -           (standard output)

 * dd 只對有 "Mounted on \\.\N:" 的地方作操作

# Clone (Partition0 --> DuinOS.img)

dd of=\\?\Device\Harddisk1\Partition0 if=c:\DuinOS.img bs=1M

# IO test

# wirte

ddrelease64.exe if=/dev/zero of=test.bin bs=1M count=10240 --progress

# read

ddrelease64.exe if=test.bin of=/dev/null bs=1M count=10240 --progress

 * 只能對 Partition 作操作

 


dcfldd

 

Enhanced version of dd for forensics and security

HomePage: http://dcfldd.sourceforge.net/

  • Hashing on-the-fly
    dcfldd can hash the input data as it is being transferred, helping to ensure data integrity.
  • Status output
    dcfldd can update the user of its progress in terms of the amount of data transferred and how much longer operation will take.
  • Flexible disk wipes
    dcfldd can be used to wipe disks quickly and with a known pattern if desired.
  • Image/wipe Verify
    dcfldd can verify that a target drive is a bit-for-bit match of the specified input file or pattern.

 


Disk IO Test

 

dd if=/dev/zero of=/data/test.bin bs=1G count=10G oflag=dsync &

oflag=dsync    # direct: use direct I/O for data
                     # dsync: use synchronized I/O for data

#  dstat - versatile tool for generating system resource statistics

dstat -d -D sdb

 


More

 

Allocate file