dd命令笔记

dd命令

用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换

参数

  • if=FILE 指定输入源文件, 缺省为标准输入, < if=input file >
  • iflag=FLAGS 指定输入IO方式(是否跳过系统的读写缓冲等), 逗号分隔
  • of=FILE 指定输出目的文件, 缺省为标准输出, < of=output file >
  • oflag=FLAGS 指定输出IO方式(是否跳过系统的读写缓冲等), 逗号分隔
  • ibs=BYTES 一次读入bytes个字节
  • obs=BYTES 一次输出bytes个字节
  • bs=BYTES 同时设置ibs和obs的块大小为bytes个字节
  • cbs=BYTES 一次转换bytes个字节, 即指定转换缓冲区大小
  • seek=N 跳过输出开头的 N 个obs大小的块
  • skip=N 跳过输入开头的 N 个ibs大小的块
  • count=N 仅拷贝N个输入的块
  • conv=CONVS 指定转换方式, 用逗号分隔
  • status=LEVEL 信息输出的级别

参数 LEVEL

  • 'none' 除了错误信息以外, 什么都不显示
  • 'noxfer' 不显示最后的传输结果统计
  • 'progress' 间隔显示传输统计

参数CONV

  • ascii from EBCDIC to ASCII
  • ebcdic from ASCII to EBCDIC
  • ibm from ASCII to alternate EBCDIC
  • block pad newline-terminated records with spaces to cbs-size
  • unblock replace trailing spaces in cbs-size records with newline
  • lcase change upper case to lower case
  • ucase change lower case to upper case
  • sparse try to seek rather than write the output for NUL input blocks
  • swab swap every pair of input bytes
  • sync pad every input block with NULs to ibs-size; when used with block or unblock, pad with spaces rather than NULs
  • excl fail if the output file already exists
  • nocreat do not create the output file
  • notrunc do not truncate the output file
  • noerror continue after read errors
  • fdatasync physically write output file data before finishing
  • fsync likewise, but also write metadata

参数 FLAG

  • append append mode (makes sense only for output; conv=notrunc suggested)
  • direct use direct I/O for data
  • directory fail unless a directory
  • dsync use synchronized I/O for data
  • sync likewise, but also for metadata
  • fullblock accumulate full blocks of input (iflag only)
  • nonblock use non-blocking I/O
  • noatime do not update access time
  • nocache Request to drop cache. See also oflag=sync
  • noctty do not assign controlling terminal from file
  • nofollow do not follow symlinks
  • count_bytes treat 'count=N' as a byte count (iflag only)
  • skip_bytes treat 'skip=N' as a byte count (iflag only)
  • seek_bytes treat 'seek=N' as a byte count (oflag only)

/dev/null
空设备, 也称为位桶(bit bucket), 任何写入它的输出都会被抛弃, 如果不想让消息以标准输出显示或写入文件, 可以将消息重定向到位桶.

/dev/zero
0设备, 无穷尽地提供0, 可以使用任何你需要的数目——设备提供的要多的多, 可以用于向设备或文件写入字符串0.

dd命令实例

# 将本地的/dev/hdb整盘备份到/dev/hdd
dd if=/dev/hdb of=/dev/hdd

# 将/dev/hdb全盘数据备份到指定路径的image文件
dd if=/dev/hdb of=/root/image

# 将文件sfile拷贝到文件dfile
$ dd if=sfile of=dfile

# 假设串口节点为/dev/ttyS0, 下面的命令将文件写入串口
dd if=1.txt of=/dev/ttyS0

# 创建一个100M的空文件
dd if=/dev/zero of=hello.txt bs=100M count=1

# 将备份文件恢复到指定盘
dd if=/root/image of=/dev/hdb

# 备份/dev/hdb全盘数据,并利用gzip工具进行压缩,保存到指定路径
dd if=/dev/hdb | gzip > /root/image.gz

# 将压缩的备份文件恢复到指定盘
gzip -dc /root/image.gz | dd of=/dev/hdb

# 备份磁盘开始的512个字节大小的MBR信息到指定文件,count=1指仅拷贝一个块;bs=512指块大小为512个字节。
dd if=/dev/hda of=/root/image count=1 bs=512
# 恢复, 将备份的MBR信息写到磁盘开始部分
dd if=/root/image of=/dev/had

# 备份软盘
dd if=/dev/fd0 of=disk.img count=1 bs=1440k (即块大小为1.44M)

# 拷贝内存内容到硬盘
dd if=/dev/mem of=/root/mem.bin bs=1024 (指定块大小为1k)

# 拷贝光盘内容到指定文件夹,并保存为cd.iso文件
dd if=/dev/cdrom(hdc) of=/root/cd.iso

# 销毁磁盘数据,利用随机的数据填充硬盘
dd if=/dev/urandom of=/dev/hda1

# 测试硬盘的读写速度
dd if=/dev/zero bs=1024 count=1000000 of=/root/1Gb.file
dd if=/root/1Gb.file bs=64k | dd of=/dev/null

# 确定硬盘的最佳块大小, 通过比较以下命令输出中所显示的命令执行时间,即可确定系统最佳的块大小。
dd if=/dev/zero bs=1024 count=1000000 of=/root/1Gb.file
dd if=/dev/zero bs=2048 count=500000 of=/root/1Gb.file
dd if=/dev/zero bs=4096 count=250000 of=/root/1Gb.file
dd if=/dev/zero bs=8192 count=125000 of=/root/1Gb.file

# 修复硬盘. 当硬盘较长时间(一年以上)放置不使用后, 磁盘上会产生magnetic flux point, 当磁头读到这些区域时会遇到困难, 并可能导致I/O错误. 当这种情况影响到硬盘的第一个扇区时, 可能导致硬盘报废. 此命令有可能使这些数据起死回生, 并且这个过程是安全、高效的.
dd if=/dev/sda of=/dev/sda 或dd if=/dev/hda of=/dev/hda

# 利用netcat远程备份, 在源主机上执行此命令备份/dev/hda
dd if=/dev/hda bs=16065b | netcat < targethost-IP > 1234
# 在目的主机上执行此命令来接收数据并写入/dev/hdc
netcat -l -p 1234 | dd of=/dev/hdc bs=16065b
# 目的主机指令的变化分别采用bzip2、gzip对数据进行压缩,并将备份文件保存在当前目录。
netcat -l -p 1234 | bzip2 > partition.img
netcat -l -p 1234 | gzip > partition.img

# 利用dd可以实现截屏,假设屏幕对应的设备节点/dev/fb0,屏幕大小是600×800,屏幕像素的格式是rgb565(每个像素对应两个字节),则命令如下
dd if=/dev/fb0 of=/tmp/1.dat bs=600 count=1600

# 利用dd直接写屏幕,例如下面的命令将随机渲染屏幕
dd if=/dev/urandom of=/dev/fb0 bs=600 count=1600

  

关于 dsync

参数说明: Use synchronized I/O for data. For the output file, this forces a physical write of output data on each write. 使用同步I/O, 写操作时, 强制每一次写入都完成物理写入.

如果每次写入的大小是8kB, 那么dd每次会等待数据被物理写入后才会开始下一次写, 这样就完全绕过了硬盘的硬件缓冲, 对于普通机械硬盘而言, 每秒的写入次数只有20到25次(约40ms一次), 因此写入速度只有大概200kB每秒, 如果是64kB, 就是1.3MB每秒, 在小数据写入时, 主要是受读写次数的限制. 对于数据库应用, 会比较关心这个性能, 因为在事务场景有ACID的要求, 会要求同步写入, 每次写入时需要保证物理写入.

实际测试:

机械硬盘IO次数是每秒20~25
固态硬盘是机械硬盘的十倍, 每秒200以上
腾讯云轻量服务器的硬盘, 和普通固态硬盘性能一样
阿里云的普通ECS, 使用SSD云盘, 速度是普通固态硬盘的3到4倍, 600到800

posted on 2019-11-19 13:13  Milton  阅读(765)  评论(0编辑  收藏  举报

导航