Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

用 dd 直接讀寫硬碟指定實體扇區的實測過程

dd 天生就是按「塊」搬資料的工具,只要把區塊大小設成 512 字節,就能精準命中某一個實體扇區。

基本語法

讀取指定實體扇區:

dd if=<來源裝置> of=<輸出裝置或檔案> skip=<目標扇區> bs=512 count=1

寫入指定實體扇區:

dd if=<輸入裝置或檔案> of=<目標裝置> seek=<目標扇區> bs=512 count=1

讀用 skip、寫用 seek,是兩者唯一的差別。

實例演練

以 /root/linux-2.6.32.36-0.5/REPORTING-BUGS 為例,檔案大小 4K。用 debugfs 查得其占用的檔案系統塊號為 584933,/dev/sda1 的檔案系統是 ext4、預設塊大小 4K。

換算扇區(512 字節):584933 × 8 = 4679464。
而 /dev/sda1 的起始實體扇區是 2048,所以該檔在 sda 上的起始扇區為 584933 × 8 + 2048 = 4681512。

root@ubuntu:~/linux-2.6.32.36-0.5# ls -l REPORTING-BUGS
-rw-r--r-- 1 root root 3371 2011-05-30 07:39 REPORTING-BUGS
root@ubuntu:~/linux-2.6.32.36-0.5# du -s REPORTING-BUGS
4 REPORTING-BUGS

root@ubuntu:~/linux-2.6.32.36-0.5# debugfs /dev/sda1
debugfs 1.41.14 (22-Dec-2010)
debugfs: cd /root
debugfs: cd linux-2.6.32.36-0.5
debugfs: bmap REPORTING-BUGS 0
584933

root@ubuntu:~# dd if=/dev/sda of=test.dump skip=4681512 bs=512 count=1

root@ubuntu:~/linux-2.6.32.36-0.5# fdisk -lu
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders, total 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Device Boot Start    End      Blocks  Id System
/dev/sda1 *    2048     40894463 20446208 83 Linux
/dev/sda2      40896510 41940991   522241  5 Extended
/dev/sda5      40896512 41940991   522240 82 Linux swap / Solaris

把讀出來的 test.dump 印出來,內容與原檔開頭完全一致:

root@ubuntu:~# cat test.dump
[Some of this is taken from Frohwalt Egerer's original linux-kernel FAQ]
What follows is a suggested procedure for reporting Linux bugs. ...
root@ubuntu:~# cat REPORTING-BUGS
[Some of this is taken from Frohwalt Egerer's original linux-kernel FAQ]
What follows is a suggested procedure for reporting Linux bugs. ...

由此可見,用 dd 直接存取硬碟實體扇區的方式完全可行。實務上要特別小心:of= 指向整顆磁碟時,一個手誤就無法回頭,操作前務必確認裝置名稱與扇區編號。

轉自 https://ilinuxkernel.com/?p=236

Copyright © Linux教程網 All Rights Reserved