Grub2

From www.deloptes.org
Jump to navigation Jump to search

Brief instructions

http://unix.stackexchange.com/questions/16327/how-to-check-on-which-drives-grub2-has-actually-installed-an-mbr

There are several steps in the boot process (I'm describing a traditional PC BIOS):

  1. . The BIOS reads the first sector (512 bytes) of the boot disk.
  2. . The code in this first sector reads further data and code at a fixed location through the BIOS interface. This BIOS interface only exposes two hard disks: disk 0 is wherever the first sector was read from, and disk 1 is another disk which isn't easily predictable if you have more than two. The boot sector contains a byte that indicates which hard disk the further data is on; this is the disk containing /boot/grub.
  3. . The code loaded at the previous stage understands partitions, filesystems and other high-level notions. The data includes a filesystem location (i.e. a string like (hd0)/boot/grub) that determines where to find grub.cfg and further Grub modules.
  4. . grub.cfg is executed, typically to show a menu and boot an OS.

The boot sector is generated by grub-setup, normally invoked through grub-install. The boot sector ends up on whatever disk you specified (in Linux syntax) on the grub-install or grub-setup command line. You can check that you have a boot sector on a disk by running file -s /dev/sda. Since you're adding a new disk and want to boot from it, you need to run grub-install on the new disk. Running grub-install multiple times on the same disk is harmless.

The difficult part is in step 2 above. If at all possible, put Grub (i.e. the /boot/grub directory) on the BIOS boot disk (or, approaching this from the other direction, tell your BIOS to boot from the disk where /boot/grub is). This is where device.map comes into play. Make sure that (hd0) is mapped to the disk that contains /boot/grub, then run grub-install on that disk.

If your two disks are in a software RAID-1 configuration, you'll have identical boot sectors. This is the desirable behavior: if the one disk that is the BIOS boot disk fails, booting from the other one will just work (since they contain the same bytes at the same relevant locations). If you've only mirrored certain partitions, then installing a boot sector only affects one of the disks. You should run grub-install again on the second disk, after changing device.map to associate (hd0) with the disk containing the second mirrored copy of /boot/grub.

Step 3 is pretty complex, but usually works out of the box. At step 4, Grub locates filesystems by UUID or looks for named files, so you no longer need to worry about the various ways to designate disks.


Data Migration

 cd /mnt && mkdir {source,target}
  • Checking disk availability
 fdisk -l /dev/sdb
  • Check if Grub2 installed
 dd if=/dev/sda bs=512 count=1 | xxd 
 dd if=/dev/sdb bs=512 count=1 | xxd 
  • mount source and target
 mount /dev/sdb1 /mnt/target
 mount /dev/sda1 /mnt/source
  • copy data
 /mnt/source
 tar cf - . | (cd /mnt/target/; tar xvf -)
  • umount source
 cd ..
 umount /mnt/source
  • install grub2
 grub-install --force --no-floppy --root-directory=/mnt/target/ /dev/sdb

Disk Migration

After adding or replacing a hard drive do following (as root):

 cd /tmp
 grub-mkdevicemap --device-map=device.map
 cat device.map
 mv device.map /boot/grub/device.map

New kernel

update-grub