Grub2的磁盘内容备份和还原

April 24, 2018 | 1 Minute Read

GRUB 2 除了使用MBR的512个字节外,还在MBR后面到第一个分区开始的空隙保存core.img
文件。所以第一个分区要从2048s扇区开始,保留“ 1 MiB (2048 blocks) ”的空隙给grub
使用才行。  grub2-install 的工作就是 -> grub2-bios-setup  -> 会往磁盘写入
这boot.img和core.img 文件
https://github.com/coreos/grub/blob/2.02-coreos/util/setup.c

https://wiki.archlinux.org/index.php/GRUB
Master Boot Record (MBR) specific instructions
Usually the post-MBR gap (after the 512 byte MBR region and before the start of the first partition) in many MBR (or 'msdos' disklabel) partitioned systems is 31 KiB when DOS compatibility cylinder alignment issues are satisfied in the partition table. However a post-MBR gap of about 1 to 2 MiB is recommended to provide sufficient room for embedding GRUB's core.img (FS#24103). It is advisable to use a partitioning tool that supports 1 MiB partition alignment to obtain this space as well as to satisfy other non-512 byte sector issues (which are unrelated to embedding of core.img).

mount -t ext4 /dev/sdb1 /mnt
mkdir /mnt/boot
grub2-install --boot-directory=/mnt/boot /dev/sdb
dd if=/dev/sdb of=pnpgw_grub.mbr bs=512 count=2048
umount /mnt

分区时,第一个分区从第2048个扇区开始,这样保留前面1兆给GRUB2使用了

parted -s /dev/sdb -a optimal -- mklabel msdos \
unit MB \
mkpart primary ext4 2048s 128MB \
mkpart primary ext4 128MB 640MB \
mkpart primary ext4 640MB -1s \
set 1 boot on \
print


mke2fs -F -L "boot" -t ext4  /dev/sdb1
mke2fs -F -L "disk1" -t ext4   /dev/sdb2
mke2fs -F -L "disk2" -t ext4   /dev/sdb3

这里有grub的磁盘布局的详细示意图
https://en.wikipedia.org/wiki/GNU_GRUB