我想将分区移动到新驱动器。旧驱动器的扇区大小为 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. */
Run Code Online (Sandbox Code Playgroud)
如果您给出错误信息,我可以进一步搜索。但对我来说,这就是我们受到打击的地方。将 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;
Run Code Online (Sandbox Code Playgroud)
*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
Run Code Online (Sandbox Code Playgroud)
IO 发生在块大小而不是扇区大小的上下文中。如果由于物理扇区大小 FS 遇到任何问题,我会感到非常惊讶。然而,VFS 还没有走到这一步。VFS位于发出IO的应用程序和实际文件系统之间。我们讨论的是VFS层以下。