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

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

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

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

答案1

您還沒有告訴我們您最初嘗試失敗的 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層以下。

相關內容