脚本使用sfdisk(C / H / S被忽略?)分割新鲜的原始磁盘映像

我试图自动创build原始磁盘映像的过程。 我不在乎C / H / S,但块大小应该是512字节的粗略标准。 但是,我无法为分区指定正确的维度,以便sfdisk可以导入它们。

我第一次创build了一个32MB的空白文件:

$dd if=/dev/zero of=disk.img bs=1M count=32 

然后我使用cfdisk对其进行分区:

 $cfdisk -h 255 -s 63 -c 4 disk.img 
  • 确认cfdisk看到C / H / S开关(在屏幕的顶部)
  • 创build新的分区32.5MB,磁盘结束 (为后面的引导的东西留出空间)
  • 放弃

然后我输出一个sfdisk转储:

 $sfdisk -H 255 -S 63 -C 4 -d disk.img > disk.parts 

并得到以下(在disk.parts):

 # partition table of disk.img unit: sectors disk.img1 : start= 1276, size= 64260, Id=83, bootable disk.img2 : start= 0, size= 0, Id= 0 disk.img3 : start= 0, size= 0, Id= 0 disk.img4 : start= 0, size= 0, Id= 0 

然而,当我试图重新加载到映像中(作为一个testing)时,sfdisk在读取原始分区表时似乎首先接受C / H / S开关,然后在尝试计算新分区时丢弃它们表:

 $sfdisk -H 255 -S 63 -C 4 disk.img < disk.parts Warning: disk.img is not a block device Disk disk.img: cannot get geometry Disk disk.img: 4 cylinders, 255 heads, 63 sectors/track Old situation: Warning: The partition table looks like it was made for C/H/S=*/21/16 (instead of 4/255/63). For this listing I'll assume that geometry. Units = cylinders of 172032 bytes, blocks of 1024 bytes, counting from 0 Device Boot Start End #cyls #blocks Id System disk.img1 * 3+ 195- 192- 32130 83 Linux start: (c,h,s) expected (3,16,13) found (0,20,17) end: (c,h,s) expected (195,0,16) found (4,20,16) disk.img2 0 - 0 0 0 Empty disk.img3 0 - 0 0 0 Empty disk.img4 0 - 0 0 0 Empty New situation: Warning: The partition table looks like it was made for C/H/S=*/21/16 (instead of 4/255/63). For this listing I'll assume that geometry. Units = sectors of 512 bytes, counting from 0 Device Boot Start End #sectors Id System disk.img1 * 1276 65535 64260 83 Linux start: (c,h,s) expected (3,16,13) found (0,20,17) end: (c,h,s) expected (195,0,16) found (4,20,16) disk.img2 0 - 0 0 Empty disk.img3 0 - 0 0 Empty disk.img4 0 - 0 0 Empty Warning: partition 1 does not end at a cylinder boundary end of partition 1 has impossible value for cylinders: 4 (should be in 0-3) sfdisk: I don't like these partitions - nothing changed. (If you really want this, use the --force option.) 

看起来这两个部分是有冲突的:

  Warning: The partition table looks like it was made for C/H/S=*/21/16 (instead of 4/255/63). For this listing I'll assume that geometry. Units = cylinders of 172032 bytes, blocks of 1024 bytes, counting from 0 

  For this listing I'll assume that geometry. Units = sectors of 512 bytes, counting from 0 

然后它用以下方法加强它:

  end of partition 1 has impossible value for cylinders: 4 (should be in 0-3) 

我试过-f(强制),它给出完全相同的输出。 🙁

为什么sfdisk不能正确处理它自己的转储格式,特别是当我已经提供了所有它需要的信息的时候? 为什么在阅读时处理C / H / S,而不是在写作时? C / H / S不在文件中,那么为什么会说它看起来是 * / 21/16?

更重要的是,我该如何解决这个问题,以便在脚本中生成分区表?

C / H / S已经过时了,不应该使用。 我不是特别熟悉sfdisk,但是最近的任何分区工具都应该允许你用512字节的扇区来指定你的分区边界,而且由于性能的原因,它们总是应该是4kalignment的(可以被8整除)。

你似乎遇到的具体问题是,sfdisk无法检测到映像文件上的C / H / S(因为它不是块设备),最终导致伪造值。