在具有不同扇区大小的磁盘上使用dd

我想将分区移到新的驱动器上。 旧驱动器的扇区大小为512字节,而新驱动器的扇区大小为4096字节。 我试图做一个完整的磁盘dd,但失败,因为旧驱动器上的分区没有alignment到4K的边界。 所以相反,我使用fdisk在新驱动器上从头开始创build分区,然后使用dd在磁盘之间复制该分区。 最后,我使用resize2fs扩展了新驱动器上的文件系统。 这似乎工作,因为我可以挂载新的分区(只读迄今)和查看文件,fsck说它是干净的。

但是我仍然怀疑这是否是一件安全的事情。 例如,FS是否以某种方式关心扇区大小,这样做会破坏事物,还是被VFS层或HD本身隔离?

你没有告诉我们最初尝试的dd命令是什么,失败了。 不过,我花了一些时间检查dd命令的源代码(来自coreutils包),看起来像我们在这里有一个问题。

 1852 /* Some devices require alignment on a sector or page boundary 1853 (eg 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 (ie character special) disk 1862 devices on Unixware or other SVR4-derived system. */ 

如果您提供错误消息,我可以进一步search。 但对我来说,这是我们受到打击的地方。 这是alignment512字节的页面边界与4 KiB的页面边界似乎并不正确。

现在来到第二部分,是否将第二个驱动器(带有fdisk)的分区创build为512字节大小。 但是,大多数现代磁盘给OS所宣传的扇区大小是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需要与其alignment。 在我的笔记本电脑中,块的大小和物理扇区的大小是一样的。

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

IO发生在块大小的上下文中,而不是扇区大小。 如果由于物理扇区大小FS遇到任何问题,我会非常惊讶。 但是,VFS并不是那么遥远。 VFS位于应用程序发出IO和实际文件系统之间。 我们正在讨论的是在VFS层下面。