编程与软件工程vidarholen.net
“dd”的无用之用
Useless Use of "dd" (2015)
2017 — 2022,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这篇文章讨论 Unix 命令 dd 常被滥用:把镜像写入磁盘/设备等许多任务其实用 cat 更简单高效,dd 的逐块读写反而可能更慢。文章也说明 dd 并非一无是处,在按块/偏移读写、创建稀疏文件或绕过页缓存等低级操作中仍有用武之地。
为什么经久不衰:它戳破了“磁盘操作必须用 dd”的长期迷思,评论区又补充了名称来历、O_DIRECT、cat/pv 和 ddrescue 等实践细节,所以多年后仍值得一读。
HN 评论精选
dd 在按块/偏移操作和特殊转换上仍然方便,并非全无用处。
dd 仍有一些有用的场景,虽然别的机制也能做到,但通常更麻烦:读取指定数量的块或字节(例如备份 MBR);从文件指定偏移处读取;向文件指定偏移处写入;创建稀疏文件;做大小写转换;做 ASCII/EBCDIC 转换;处理 IBM 数据磁带时做 blocking/unblocking。
dredmorbius查看英文原文
Useful uses of dd(1). There remain some useful applications of dd. These may of course be achieved by other mechanisms, but typically less conveniently. 1. Read a specific number of blocks or bytes from a source: dd if=/dev/hda of=/root/mbr bs=512 count=1 This will make a copy of, say, your master boot record (first 512 bytes of your first disk drive) and stash it in your /root directory. 2. Read from specific bytes of a file dd if=mydata skip=1k bs=32 count=1 Reads 32 bytes after the first 1024 (1k) bytes of "mydata". 3. Write to specific bytes of a file dd if=source of=target seek=10k bs=512 count=1 conv=notrunc That should write 512 bytes from "source" beginning 10k into "target". (I've not tested this, you should verify.) 4. Create a sparse file. Sparse files appear to have a nonzero size, but take up no space on disk, until data is actually written to them. These are often used as "inflating" dynamic filesystem images for virtual machines. dd if=/dev/zero of=sparsefile bs=1 count=0 seek=20000M # Create 20 GB sparse file 5. Case conversions. Sure, you could use tr(1), but where's the sport? dd if=MixEdCaSE of=lcase conv=lcase # Convert to lower case dd if=MixEdCaSE of=ucase conv=ucase # Convert to upper case 6. ASCII / EBCDIC conversions dd if=ebcdic of=ascii conv=ascii # ebcdic -> ascii dd if=ascii of=ebcdic conv=ebcdic # ascii -> ebcdic When reading to or from IBM data tapes, you might find blocking / unblocking conversions useful. I've done this, but it's so long ago that I don't trust my memory on that any more. Odds are good you'll not have to worry about this. There are other useful applications as well, though these are not typically encountered very often. Do feel free to explore and attempt these on safe media.
大块 direct IO 让 dd 成为测 NVMe 吞吐的有效工具。
虽然文章大体没错,但有些时候可以比 cat 做得好得多。我曾用 dd if=/dev/nvme3n1 bs=4096k iflag=direct of=/dev/null status=progress 让 NVMe 盘跑到约 5.4 GB/s;诀窍是让 Linux 发起超大 IO(4 MiB),盘本身最大只能处理 2 MiB 读,dd 会拆分开。
mgerdts查看英文原文
While there’s a lot of truth here, there are times when you can do much better than cat. A while back I tweeted: Today I found the magical dd command that causes an NVMe drive to run at almost full speed: # dd if=/dev/nvme3n1 bs=4096k iflag=direct of=/dev/null status=progress 959090524160 bytes (959 GB, 893 GiB) copied, 178 s, 5.4 GB/s The trick to getting this throughput is telling Linux to do an insanely large IO (4 MiB). The drive can't do 4 MiB reads - the largest IO it can handle is 2 MiB. # nvme id-ctrl /dev/nvme3 | grep mdts mdts : 9 # echo '4 \* 2^9' | bc -l 2048 More in the tread starting here: https://twitter.com/OMGerdts/status/1514376206082269191?s=20&t=6YcJZRCQIgeV4m1DU5OvRQ
澄清 dd 名称来自 JCL 的 DD,并建议用 O_DIRECT 和进度选项。
“D”并不代表 disk/drive/device,它来自 OS/360 JCL 的 DD(data definition)语句,这也是 dd 的选项语法和其他 Unix 工具不同的原因。另外,用 dd 写 USB 等设备时,尽量绕过 Linux 虚拟内存可避免系统卡顿,尤其对慢设备;可以用 O_DIRECT,dd 现在也支持 status=progress。dd 是更低层的工具,用于高层操作时有坑。
pixelbeat查看英文原文
Yes "D" is not for disk/drive/device/... It comes from the DD (data definition) statement of OS/360 JCL, and hence why dd has the unusual option syntax compared to other unix utils BTW if you are using dd to write usb drives etc. it's useful to bypass the Linux VM as much as possible to avoid systems stalls, especially with slow devices. You can do that with O_DIRECT. Also dd recently got a progress option, so... dd bs=2M if=disk.img of=/dev/sda... status=progress iflag=direct oflag=direct Note dd is a lower level tool, which is why there are some gotchas when using for higher level operations. I've noted a few at: http://www.pixelbeat.org/docs/coreutils-gotchas.html#dd
普通镜像写入应优先 cat/pv,dd 只适合特定块操作。
大多数时候,用 cat(或 pv 显示进度)把文件写到块设备更好也更快,因为它是流式传输,让底层启发式去决定块大小;dd 则是读一块写一块。写之前我通常把 vm.dirty_bytes 和 vm.dirty_background_bytes 降到 16/48 MB,否则进度条显示 300MB/s,完成后还得等很久才真正写完。希望那些教程别再推荐 dd,普通写镜像没有理由用它;我只在写特定块(如恢复 MBR)或当简易十六进制编辑器时用 dd。
voidz查看英文原文
Most of the time it's much better (as in, faster) to just use cat (or pv, to get a nice progress bar) for writing a file to a block device, because it streams, and lets underlying heuristics worry about block sizes and whatnot. So: cat foobar.img > /dev/sdi will stream the file rather than what dd does, i.e. read block, write block, read block, write block and so on. Usually I also lower the vm.dirty_bytes and vm.dirty_background_bytes to 16 resp. 48 MB (in bytes) beforehand, which limits the buffer sizes to those amounts. Else it will seem that the progress bar indicates 300MB/s is written, and when it completes you still have to wait a really long time for things to have been written out. Afterwards I restore back vm.dirty_ratio and vm.dirty_background_ratio to respectively 10 and 5 - the defaults on my system. I wish that all of those projects, tutorials etc. that explain how to write their image to a block device, like an sdcard, would start advise using cat, because there is no reason to use dd, it's just something that people stick with because others do it too. I only use dd for specific blocks, like writing back a backup of the mbr, or as a rudimentary hex editor.
历次讨论
同类还值得读
开源应用架构
The Architecture of Open Source Applications
《开源软件架构》(AOSA)是一本在线免费书,邀请知名开源项目的作者亲自剖析其内部设计,涵盖 SQLAlchemy、LLVM、Audacity、Apache、Bash 等系统。
编程与软件工程上首页 9 次 · 2011–2024aosabook.org编辑精选编程语言简史:简短、不完整且多半错误
A Brief, Incomplete, and Mostly Wrong History of Programming Languages
一篇以戏谑、故意不准确的方式回顾编程语言史的博客文章,用夸张轶事和讽刺定义串起 Lisp、C、C++、Java、JavaScript、PHP 等语言。
编程与软件工程上首页 8 次 · 2009–2021james-iry.blogspot.comSQLite 是如何测试的
How SQLite is tested
SQLite 官方文档页,系统介绍 SQLite 的测试体系:多种测试方法(TCL 测试、模糊测试、回归测试、静态分析等)和海量测试用例,展示了这个小型开源项目如何靠极致测试获得…
编程与软件工程上首页 8 次 · 2009–2025sqlite.org编辑精选高级编程语言(2009)
Advanced programming languages (2009)
Matt Might 的经典文章,按用途推荐“高级编程语言”:如果追求就业就选主流语言,如果追求表达力、学术研究或创业,则推荐 Haskell、OCaml、Scala 等函数式语…
编程与软件工程上首页 8 次 · 2009–2022matt.might.net我的 Forth 与栈机器经历
Yosefk - My history with Forth & stack machines
作者以亲身经历回顾了使用 Forth 语言和栈式机器的历程:既赞叹其简洁优雅、代码量小、可自举的优点,也坦率指出栈操作在实际编程中的痛苦,以及 Forth 社区文化中的问题。
编程与软件工程上首页 8 次 · 2010–2024yosefk.com编辑精选