Partition

dd if=/dev/zero 保持驅動器內容完好?U盤壞了?

  • July 10, 2019

我以為我可以使用dd if=/dev/zero of=/dev/sdX. 在過去,這一直對我有用,但在這種情況下,它沒有按預期工作。

#check the partitions
➜  ~ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   477G  0 disk 
├─sda1   8:1    0   512M  0 part /boot/efi
└─sda2   8:2    0 476.4G  0 part /
sdb      8:16   1  14.6G  0 disk 
├─sdb1   8:17   1   292M  0 part /media/james/Gentoo amd64 20190703T214502Z
└─sdb2   8:18   1   6.3M  0 part /media/james/GENTOOLIVE

#unmount and confirm the drive is still seen.

➜  ~ sudo umount "/media/james/Gentoo amd64 20190703T214502Z"
➜  ~ sudo umount "/media/james/GENTOOLIVE"
➜  ~ lsblk                                                   
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   477G  0 disk 
├─sda1   8:1    0   512M  0 part /boot/efi
└─sda2   8:2    0 476.4G  0 part /
sdb      8:16   1  14.6G  0 disk 
├─sdb1   8:17   1   292M  0 part 
└─sdb2   8:18   1   6.3M  0 part 

#Run dd

➜  ~ sudo dd if=/dev/zero of=/dev/sdb bs=3M                  
dd: error writing '/dev/sdb': No space left on device
2649+0 records in
2648+0 records out
8330620928 bytes (8.3 GB, 7.8 GiB) copied, 5.50879 s, 1.5 GB/s

#the partitions are still there!

➜  ~ lsblk

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   477G  0 disk 
├─sda1   8:1    0   512M  0 part /boot/efi
└─sda2   8:2    0 476.4G  0 part /
sdb      8:16   1  14.6G  0 disk 
├─sdb1   8:17   1   292M  0 part 
└─sdb2   8:18   1   6.3M  0 part 
➜  ~ lsblk

#after unplugging and replugging the drive, the old partition still mounts and still contains files. I was able to open several and read the contents.

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   477G  0 disk 
├─sda1   8:1    0   512M  0 part /boot/efi
└─sda2   8:2    0 476.4G  0 part /
sdb      8:16   1  14.6G  0 disk 
├─sdb1   8:17   1   292M  0 part 
└─sdb2   8:18   1   6.3M  0 part /media/james/GENTOOLIVE

真正讓我困惑的是,如果我查看 Gparted,該設備顯示為 8GB 未分配,但這是一個 16GB 驅動器。

我跑了badblocks -wsv,它過去了,但速度很快(幾分鐘而不是幾小時)。拔掉再重新插入後,驅動器顯示為/dev/sdc,Gparted 看到 14.56GB 分區,名為“gentoo”

Testing with pattern 0xaa: set_o_direct: Invalid argument/0/0 errors)
done                                                 
Reading and comparing: done                                                 
Testing with pattern 0x55: done                                                 
Reading and comparing: done                                                 
Testing with pattern 0xff: done                                                 
Reading and comparing: done                                                 
Testing with pattern 0x00: done                                                 
Reading and comparing: done                                                 
Pass completed, 0 bad blocks found. (0/0/0 errors)
➜  ~ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   477G  0 disk 
├─sda1   8:1    0   512M  0 part /boot/efi
└─sda2   8:2    0 476.4G  0 part /
sdc      8:32   1  14.6G  0 disk 
├─sdc1   8:33   1   292M  0 part 
└─sdc2   8:34   1   6.3M  0 part 

我猜我應該把這個快閃記憶體驅動器放到牧場上,但在我看來,這是一個奇怪的事件序列,我很好奇可能是哪種故障導致它(不是真的在尋找修復)。

編輯:這是在 Xubuntu 18.04 上

Edit2:重新啟動後,歸零按預期工作。我想這只是作業系統的臨時問題。不過,我仍然很好奇是什麼樣的問題。

Edit3:我說得太早了,重啟是不夠的。我認為dd它正在工作,因為它需要正常的時間,但似乎不是。

➜  ~ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   477G  0 disk 
├─sda1   8:1    0   512M  0 part /boot/efi
└─sda2   8:2    0 476.4G  0 part /
sdb      8:16   1  14.6G  0 disk 
├─sdb1   8:17   1   292M  0 part /media/james/Gentoo amd64 20190703T214502Z
└─sdb2   8:18   1   6.3M  0 part 
➜  ~ sudo dd if=/dev/zero of=/dev/sdb
[sudo] password for james: 
Sorry, try again.
[sudo] password for james: 
dd: writing to '/dev/sdb': No space left on device
30629377+0 records in
30629376+0 records out
15682240512 bytes (16 GB, 15 GiB) copied, 4232.1 s, 3.7 MB/s
➜  ~ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   477G  0 disk 
├─sda1   8:1    0   512M  0 part /boot/efi
└─sda2   8:2    0 476.4G  0 part /
sdb      8:16   1  14.6G  0 disk 
├─sdb1   8:17   1   292M  0 part /media/james/Gentoo amd64 20190703T214502Z
└─sdb2   8:18   1   6.3M  0 part 

編輯 4:好的,dd確實有效,但是 lsblk 直到我彈出並放回時才更新。

➜  ~ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   477G  0 disk 
├─sda1   8:1    0   512M  0 part /boot/efi
└─sda2   8:2    0 476.4G  0 part /
sdb      8:16   1  14.6G  0 disk 

編輯 5:我檢查了 dmesg 並且有關於磁碟未正確安裝的警告。

➜  ~ journalctl --dmesg --since="3 days ago" | grep sdb
Jul 09 19:59:27 james-Latitude-E7470 kernel: sd 3:0:0:0: [sdb] 30595072 512-byte logical blocks: (15.7 GB/14.6 GiB)
Jul 09 19:59:27 james-Latitude-E7470 kernel: sd 3:0:0:0: [sdb] Write Protect is off
Jul 09 19:59:27 james-Latitude-E7470 kernel: sd 3:0:0:0: [sdb] Mode Sense: 43 00 00 00
Jul 09 19:59:27 james-Latitude-E7470 kernel: sd 3:0:0:0: [sdb] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
Jul 09 19:59:27 james-Latitude-E7470 kernel:  sdb: sdb1
Jul 09 19:59:27 james-Latitude-E7470 kernel: sd 3:0:0:0: [sdb] Attached SCSI removable disk
Jul 09 19:59:33 james-Latitude-E7470 kernel: FAT-fs (sdb1): Volume was not properly unmounted. Some data may be corrupt. Please run fsck.
Jul 10 02:38:38 james-Latitude-E7470 kernel: sd 3:0:0:0: [sdb] 30629376 512-byte logical blocks: (15.7 GB/14.6 GiB)
Jul 10 02:38:38 james-Latitude-E7470 kernel: sd 3:0:0:0: [sdb] Write Protect is off
Jul 10 02:38:38 james-Latitude-E7470 kernel: sd 3:0:0:0: [sdb] Mode Sense: 43 00 00 00
Jul 10 02:38:38 james-Latitude-E7470 kernel: sd 3:0:0:0: [sdb] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
Jul 10 02:38:38 james-Latitude-E7470 kernel:  sdb: sdb1 sdb2
Jul 10 02:38:38 james-Latitude-E7470 kernel: sd 3:0:0:0: [sdb] Attached SCSI removable disk
Jul 10 04:12:42 james-Latitude-E7470 kernel: sd 3:0:0:0: [sdb] 30629376 512-byte logical blocks: (15.7 GB/14.6 GiB)
Jul 10 04:12:42 james-Latitude-E7470 kernel: sd 3:0:0:0: [sdb] Write Protect is off
Jul 10 04:12:42 james-Latitude-E7470 kernel: sd 3:0:0:0: [sdb] Mode Sense: 43 00 00 00
Jul 10 04:12:42 james-Latitude-E7470 kernel: sd 3:0:0:0: [sdb] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
Jul 10 04:12:42 james-Latitude-E7470 kernel: sd 3:0:0:0: [sdb] Attached SCSI removable disk

#the partitions are still there!

至少這部分還是正常的。您必須重新讀取分區表才能更新分區資訊。您可以觸發重新閱讀

blockdev --rereadpt /dev/sdx

(或者sfdisk --re-read,該選項已從最新版本中刪除sfdisk)。


對於其他一切,是的,基於快閃記憶體的儲存可能無法進入只讀模式。由於其他原因也可能發生奇怪的事情,例如,如果 USB 連接不穩定,/dev/sdx設備可能會被丟棄,並重新檢測為/dev/sdy所有寫入/dev/sdx進入邊緣,但我想這會顯示在你的lsblk.

有時會出現有趣的錯誤消息dmesg,但總而言之……如果您的 U 盤出現故障,您只需要獲得一個新的,沒有辦法解決。


為了完整起見,這裡也有這種特殊情況(使用者錯誤):

# dd bs=1M if=/dev/zero of=/dev/sdx
dd: error writing '/dev/sdx': No space left on device
7956+0 records in
7955+0 records out
8341966848 bytes (8.3 GB, 7.8 GiB) copied, 2.08568 s, 4.0 GB/s

所以。此命令是否寫入設備?

一點也不。我什至沒有/dev/sdx. 相反,它/dev用 50% RAM 大小的正常零文件填充了我的 tmpfs。(我真的應該調整我的 tmpfs 大小限制。如果你在兩個 tmpfs 實例上這樣做,系統會因為 RAM 已滿而崩潰。)

當設備完全失去或設備名稱輸入錯誤時會發生這種情況,因為dd事先根本不檢查是否存在,並且如果您的機器有很多 RAM,並且/dev不限於像 一樣的合理大小10M,那麼您會得到這個令人困惑的結果。

引用自:https://unix.stackexchange.com/questions/529066