Dd

在具有不同扇區大小的磁碟上使用 dd

  • December 22, 2013

我想將分區移動到新驅動器。舊驅動器的扇區大小為 512 字節,而新驅動器的扇區大小為 4096 字節。我嘗試對整個磁碟進行 dd 操作,但失敗了,因為舊驅動器上的分區未與 4K 邊界對齊。因此,我使用 fdisk 從頭開始在新驅動器上創建了一個分區,然後使用 dd 在磁碟之間複製了該分區。最後,我使用 resize2fs 擴展了新驅動器上的文件系統。這似乎奏效了,因為我可以掛載新分區(目前是只讀的)並查看文件,並且 fsck 說它是乾淨的。

但我仍然懷疑這是否安全。例如,FS 是否關心扇區大小,這樣做會破壞事情,還是被 VFS 層或 HD 本身屏蔽?

您還沒有告訴我們您最初嘗試失敗的 dd 命令是什麼。但是,我花了一些時間檢查 dd 命令的原始碼(來自 coreutils 包),看起來我們在這裡遇到了問題。

1852   /* Some devices require alignment on a sector or page boundary
1853      (e.g. character disk devices).  Align the input buffer to a
1854      page boundary to cover all bases.  Note that due to the swab
1855      algorithm, we must have at least one byte in the page before
1856      the input buffer;  thus we allocate 2 pages of slop in the
1857      real buffer.  8k above the blocksize shouldn't bother anyone.
1858 
1859      The page alignment is necessary on any Linux kernel that supports
1860      either the SGI raw I/O patch or Steven Tweedies raw I/O patch.
1861      It is necessary when accessing raw (i.e. character special) disk
1862      devices on Unixware or other SVR4-derived system.  */

如果你給出錯誤資訊,我可以做進一步的搜尋。但對我來說,這就是我們受到打擊的地方。將 512 字節的頁面邊界與 4 KiB 的頁面邊界對齊似乎不正確。

現在,進入第二部分,您是否將第二個驅動器(使用 fdisk)的分區創建為 512 字節大小。但是,大多數現代磁碟向作業系統宣傳的扇區大小是 1 MiB,即 4096 KiB。

在 fdisk.c 的 update_sector_offset 函式中,您將看到

/*
            * Align the begin of partitions to:
            *
            * a) topology
            *  a2) alignment offset
            *  a1) or physical sector (minimal_io_size, aka "grain")
            *
            * b) or default to 1MiB (2048 sectrors, Windows Vista default)
            *
            * c) or for very small devices use 1 phy.sector
            */
           sector_t x = 0;

           if (fdisk_dev_has_topology(cxt)) {
                   if (cxt->alignment_offset)
                           x = cxt->alignment_offset;
                   else if (cxt->io_size > 2048 * 512)
                           x = cxt->io_size;
           }
           /* default to 1MiB */
           if (!x)
                   x = 2048 * 512;

           sector_offset = x / cxt->sector_size;

*cxt 是 fdisk 結構的描述符。

所以,這部分我不清楚。也就是說,如果您的新磁碟將扇區大小宣傳為 4096 KiB 或 512 字節。

現在,進入最後一部分。

不,文件系統實際上並不關心扇區大小。只要塊大小是 4 KiB,一切都應該沒問題,因為,由於虛擬頁面大小(在 mm 上下文中)是 4 KiB,因此 mmaped IO 需要與之對齊。在我的筆記型電腦中,塊大小和物理扇區大小是相同的。

$ sudo blockdev --getpbsz /dev/sda
[sudo] password for chakraborty: 
4096

$ sudo blockdev --getbsz /dev/sda
4096

IO 發生在塊大小的上下文中,而不是扇區大小。如果由於物理扇區大小 FS 遇到任何問題,我會非常驚訝。然而,VFS 並沒有走那麼遠。VFS 位於應用程序發出 IO 和實際文件系統之間。我們正在討論的是在 VFS 層之下。

引用自:https://serverfault.com/questions/562971