Grub

From campisano.org
Jump to navigation Jump to search

Grub 2

Install

For BIOS partition table (MBR)

  • note, first you needs i386 grub modules
apt-get install grub-pc-bin
  • then, you can use grub-install to setup grub (and a list of pre-loaded modules) in your bootable drive

WARNING! the pendrive device name on your computer will be different, in this example /dev/sdX is used for the main device and /dev/sdX2 for the grup partition, change them accordingly with your ones!

mkdir /tmp/sdX2
mount /dev/sdX2 /tmp/sdX2
grub-install --target=i386-pc --boot-directory=/tmp/sdX2/boot --modules "part_msdos part_gpt part_apple ntfs ntfscomp hfsplus fat ext2 xfs reiserfs iso9660 udf loopback normal chain boot configfile linux multiboot iorw gfxterm font all_video gettext search" /dev/sdX
# note, you can add a custom (bigger?) font for grub, example
grub-mkfont -s 16 -o /tmp/sdX2/boot/grub/dv.pf2 /usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf
umount /tmp/sdX2
rmdir /tmp/sdX2

For GUID partition table (GPT)

  • note, first you needs efi-amd64 grub modules
apt-get install grub-efi-amd64-bin
  • then, you can use grub-mkimage to setup grub (and a list of pre-loaded modules) in your bootable drive

WARNING! the pendrive device name on your computer will be different, in this example /dev/sdX is used for the main device and /dev/sdX2 for the grup partition, change them accordingly with your ones!

mkdir /tmp/sdX2
mount /dev/sdX2 /tmp/sdX2
mkdir -p /tmp/sdX2/EFI/boot
grub-mkimage --format x86_64-efi --prefix "/EFI/boot" --output /tmp/sdX2/EFI/boot/bootx64.efi part_msdos part_gpt part_apple ntfs ntfscomp hfsplus fat ext2 xfs reiserfs iso9660 udf loopback normal chain boot configfile linux multiboot iorw gfxterm font all_video gettext search
# note, you can add a custom (bigger?) font for grub, example
grub-mkfont -s 32 -o /tmp/sdX2/EFI/boot/dv.pf2 /usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf
umount /tmp/sdX2
rmdir /tmp/sdX2

Configuration

Create a new config file in the bootloader partition, eg

  • for MBR: /tmp/sdX2/boot/grub/grub.cfg
  • for GPT: /tmp/sdX2/EFI/boot/grub.cfg
set timeout=10
set default=0

set menu_color_normal=white/black
set menu_color_highlight=black/light-gray

insmod all_video

if loadfont "$prefix/dv.pf2" ; then
  set gfxmode=640x480x16,auto
  set gfxpayload=keep
  load_video
  insmod gfxterm
  insmod gettext
fi

terminal_output gfxterm

if [ -e "$prefix/menu.cfg" ]; then
  configfile "$prefix/menu.cfg"
fi

Now you can customize this config file, for instance adding a menu, or better, you can add a menu in a menu.cfg file in the same path of the grub.cfg file.


Grub Legacy (v 0.x)

Install

[root@host ~]$ grub
grub> help install
install: install [--stage2=STAGE2_FILE] [--force-lba] STAGE1 [d] DEVICE STAGE2 [ADDR] [p] [CONFIG_FILE] [REAL_CONFIG_FILE]
    Install STAGE1 on DEVICE, and install a blocklist for loading
    STAGE2 as a Stage 2. If the option `d' is present, the Stage 1
    will always look for the disk where STAGE2 was installed, rather
    than using the booting drive. The Stage 2 will be loaded at
    address ADDR, which will be determined automatically if you don't
    specify it. If the option `p' or CONFIG_FILE is present, then the
    first block of Stage 2 is patched with new values of the
    partition and name of the configuration file used by the true
    Stage 2 (for a Stage 1.5, this is the name of the true Stage 2)
    at boot time. If STAGE2 is a Stage 1.5 and REAL_CONFIG_FILE is
    present, then the Stage 2 CONFIG_FILE is patched with the
    configuration filename REAL_CONFIG_FILE. If the option
    `--force-lba' is specified, disable some sanity checks for LBA
    mode. If the option `--stage2' is specified, rewrite the Stage 2
    via your OS's filesystem instead of the raw device.

grub>

install file_stage1 [d] disco_di_avvio file_stage2 [p] [file_di_configurazione]

Questa è la sintassi già mostrata per il comando install. Si tratta di una forma semplificata rispetto a quella reale, che è sufficiente nelle situazioni più comuni. L'opzione d serve a stabilire la collocazione del file stage2 nel disco e nella partizione indicati, senza una relazione con il disco di avvio; l'opzione p è necessaria quasi sempre e serve a richiedere una serie di modifiche al file stage2 e anche al settore di avvio. Si veda eventualmente la documentazione originale per un dettaglio maggiore nell'uso di questo comando.


example:

  1. install (hd0,6)/grub/stage1 d (hd0) (hd0,6)/grub/stage2 p (hd0,6)/grub/menu.lst
  2. write the "install partition" number into the first sector of the stage2 (the "p" at the end)

install= (hd0,2)/boot/grub/stage1 (hd1) (hd1,a)/boot/grub/stage2 0x8000 p /grubdir/configfile

Installare Grub in in pendrive USB

# dopo aver creato una partizione e formattato la stessa,
# monto la partizione, copio componenti di Grub e smonto
[root@host ~]$ mount /dev/sdd1 /mnt/usb
[root@host ~]$ mkdir /mnt/grub
[root@host ~]$ cp -a /usr/lib/grub/i386-pc/* /mnt/usb/grub
[root@host ~]$ umount /dev/sdd1

# avvio grub
[root@host ~]$ grub

# indico come radice la partizione del device dove ho copiato grub
grub> root (hd3,0)
 Filesystem type is ext2fs, partition type 0x83

# installo i componenti di Grub nel device
grub> install /grub/stage1 (hd3) /grub/stage2 p /grub/menu.lst

# fine
grub> quit

Installare Grub in una immagine su file

# CREO L'IMMAGINE
[root@host ~]$ dd if=/dev/zero of=grub-initrd.raw bs=1024 count=2880

# FORMATTO L'IMMAGINE
[root@host ~]$ mkfs.ext3 -cFq -m0 grub-initrd.raw 

# MONTO L'IMMAGINE
[root@host ~]$ mount -o loop grub-initrd.raw tmp/

# CREO NELL'IMMAGINE LA CARTELLA PER GRUB
[root@host ~]$ mkdir -p tmp/boot/grub

# COPIO NELL'IMMAGINE I FILES DI GRUB
[root@host ~]$ cp /usr/share/grub/i386-pc/* tmp/boot/grub/

# SMONTO L'IMMAGINE DA DOVE L'AVEVO MONTATA
[root@host ~]$ umount tmp
NON SO PERCHE' MA FORSE SE IL PERCORSO E' TROPPO LUNGO IL COMANDO device DI GRUB NON FUNZIONA

# DUNQUE SPOSTO L'IMMAGINE APPENA CREATA SULLA DIRECTORY RADICE
[root@host ~]$ mv grub-initrd.raw /

# AVVIO IL BINARIO DI GRUB PER LINUX
[root@host ~]$ grub
Probing devices to guess BIOS drives. This may take a long time.
 
    GNU GRUB  version 0.94  (640K lower / 3072K upper memory)

 [ Minimal BASH-like line editing is supported.  For the first word, TAB
   lists possible command completions.  Anywhere else TAB lists the possible
   completions of a device/filename. ]

# MONTO LA MIA DIRECTORY RADICE
grub> root (hd0,7)
 Filesystem type is ext2fs, partition type 0x83

# DICO A GRUB DI CONSIDERARE LA MIA IMMAGINE COME IL DEVICE (fd1)
grub> device (fd1) /grub-initrd.raw 

# INSTALLO GLI STAGES DI GRUB NELL FINTO DEVICE
grub> install (fd1)/boot/grub/stage1 d (fd1) (fd1)/boot/grub/stage2 p (fd1)/boot/grub/menu.lst

# ESCO
grub> quit

# FINE
[root@host ~]$ mv /grub-initrd.raw ./
[root@host ~]$