Como o Grub Stage1, exatamente, acessa / carrega o estágio 2?

6

Esta é a minha primeira pergunta, coloquei esta questão na frente dos Red Hat Instructors, mas não encontrei nenhuma resposta satisfatória.

Estou usando o RHEL / CENTOS6, o GRUB Legacy 0,97 e consultei vários documentos explicando o processo de inicialização do Linux.

Quase todos os blogs, documentação, etc. explicam com sucesso as etapas envolvidas e todo o processo, mas falham unanimemente em como o que realmente acontece ao carregar o grub stage2.

Aqui está o meu entendimento do processo e também fiz alguns testes;

  1. O BIOS (que não usa EFI) lê o MBR, localiza a tabela de partições e carrega o GRUB stage1 (primeiros 446 bytes) na memória
  2. Eu tenho / partição de boot em 1024 cilindros, e a idéia que tenho extraído de um monte de documentação é que o estágio 1 do GRUB pode carregar diretamente o estágio 2 se ele estiver localizado em algum lugar abaixo de 1024 cilindros. Alguma documentação que consultei menciona que estágio1.5 está localizado logo após o MBR antes do setor 63, enquanto outros sugerir que pode ser em qualquer lugar no primeiro 1MB de disco e ainda outro grupo alegou que stage1.5 é apenas uma coisa GRUB v2 e não aplicar no legado do GRUB.
  3. O GRUB stage2 tem todos os drivers / módulos necessários para ler o arquivo sistemas e, portanto, carrega kernel e ramdisk e controle de entrega para kernel.
  4. O
  5. Kernel inicia o init no RHEL / CENTOS 6 e o systemd no RHEL / CENTOS 7.

Eu joguei todos os dados do primeiro MB do disco e posso confirmar que não há nada exceto MBR. Eu fico confuso sobre como 446 byte GRUB stage1 pode carregar stage2 de um sistema de arquivos? De acordo com algumas imagens na wikipedia e alguns documentos, quando o GRUB é instalado, o stage1 contém um LBA48 apontado para o stage2.

Tocando no fato, eu tentei testar se o sistema inicializa quando o stage2 é removido ou renomeado do diretório / boot / grub /. Os sistemas ainda eram inicializáveis mesmo quando não havia stage2 no sistema de arquivos.

1º MB de / dev / sda

[root@chief zul.kifal]# dd if=/dev/sda bs=1024k count=1 | hexdump -C
00000000  eb 48 90 10 8e d0 bc 00  b0 b8 00 00 8e d8 8e c0  |.H..............|
00000010  fb be 00 7c bf 00 06 b9  00 02 f3 a4 ea 21 06 00  |...|.........!..|
00000020  00 be be 07 38 04 75 0b  83 c6 10 81 fe fe 07 75  |....8.u........u|
00000030  f3 eb 16 b4 02 b0 01 bb  00 7c b2 80 8a 74 03 02  |.........|...t..|
00000040  80 00 00 80 fc 49 08 00  00 08 fa 90 90 f6 c2 80  |.....I..........|
00000050  75 02 b2 80 ea 59 7c 00  00 31 c0 8e d8 8e d0 bc  |u....Y|..1......|
00000060  00 20 fb a0 40 7c 3c ff  74 02 88 c2 52 f6 c2 80  |. ..@|<.t...R...|
00000070  74 54 b4 41 bb aa 55 cd  13 5a 52 72 49 81 fb 55  |tT.A..U..ZRrI..U|
00000080  aa 75 43 a0 41 7c 84 c0  75 05 83 e1 01 74 37 66  |.uC.A|..u....t7f|
00000090  8b 4c 10 be 05 7c c6 44  ff 01 66 8b 1e 44 7c c7  |.L...|.D..f..D|.|
000000a0  04 10 00 c7 44 02 01 00  66 89 5c 08 c7 44 06 00  |....D...f.\..D..|
000000b0  70 66 31 c0 89 44 04 66  89 44 0c b4 42 cd 13 72  |pf1..D.f.D..B..r|
000000c0  05 bb 00 70 eb 7d b4 08  cd 13 73 0a f6 c2 80 0f  |...p.}....s.....|
000000d0  84 f0 00 e9 8d 00 be 05  7c c6 44 ff 00 66 31 c0  |........|.D..f1.|
000000e0  88 f0 40 66 89 44 04 31  d2 88 ca c1 e2 02 88 e8  |[email protected]........|
000000f0  88 f4 40 89 44 08 31 c0  88 d0 c0 e8 02 66 89 04  |[email protected]..|
00000100  66 a1 44 7c 66 31 d2 66  f7 34 88 54 0a 66 31 d2  |f.D|f1.f.4.T.f1.|
00000110  66 f7 74 04 88 54 0b 89  44 0c 3b 44 08 7d 3c 8a  |f.t..T..D.;D.}<.|
00000120  54 0d c0 e2 06 8a 4c 0a  fe c1 08 d1 8a 6c 0c 5a  |T.....L......l.Z|
00000130  8a 74 0b bb 00 70 8e c3  31 db b8 01 02 cd 13 72  |.t...p..1......r|
00000140  2a 8c c3 8e 06 48 7c 60  1e b9 00 01 8e db 31 f6  |*....H|.......1.|
00000150  31 ff fc f3 a5 1f 61 ff  26 42 7c be 7f 7d e8 40  |1.....a.&B|..}.@|
00000160  00 eb 0e be 84 7d e8 38  00 eb 06 be 8e 7d e8 30  |.....}.8.....}.0|
00000170  00 be 93 7d e8 2a 00 eb  fe 47 52 55 42 20 00 47  |...}.*...GRUB .G|
00000180  65 6f 6d 00 48 61 72 64  20 44 69 73 6b 00 52 65  |eom.Hard Disk.Re|
00000190  61 64 00 20 45 72 72 6f  72 00 bb 01 00 b4 0e cd  |ad. Error.......|
000001a0  10 ac 3c 00 75 f4 c3 00  00 00 00 00 00 00 00 00  |..<.u...........|
000001b0  00 00 00 00 00 00 00 00  19 aa 09 00 00 00 80 20  |............... |
000001c0  21 00 83 dd 1e 3f 00 08  00 00 00 a0 0f 00 00 dd  |!....?..........|
000001d0  1f 3f 8e fe ff ff 00 a8  0f 00 00 58 f0 04 00 00  |.?.........X....|
000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
1+0 records in
1+0 records out
1048576 bytes (1.0 MB) copied, 0.0184614 s, 56.8 MB/s

Palavra mágica 0044-0047h = 0x000849fc

00000040  80 00 00 80 **fc 49 08 00**  00 08 fa 90 90 f6 c2 80  |.....I..........|

[root@chief zul.kifal]# dd if=/dev/sda skip=$((0x849fc)) bs=512 count=1 | hexdump -C
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.00260914 s, 196 kB/s
00000000  52 56 5e bf f8 81 66 8b  2d 83 7d 04 00 0f 84 c4  |RV^...f.-.}.....|
00000010  00 80 7c ff 00 74 3e 66  8b 1d 66 31 c0 b0 7f 39  |..|..t>f..f1...9|
00000020  45 04 7f 03 8b 45 04 29  45 04 66 01 05 c7 04 10  |E....E.)E.f.....|
00000030  00 89 44 02 66 89 5c 08  c7 44 06 00 70 50 66 31  |..D.f.\..D..pPf1|
00000040  c0 89 44 04 66 89 44 0c  b4 42 cd 13 0f 82 93 00  |..D.f.D..B......|
00000050  bb 00 70 eb 56 66 8b 05  66 31 d2 66 f7 34 88 54  |..p.Vf..f1.f.4.T|
00000060  0a 66 31 d2 66 f7 74 04  88 54 0b 89 44 0c 3b 44  |.f1.f.t..T..D.;D|
00000070  08 7d 68 8b 04 2a 44 0a  39 45 04 7f 03 8b 45 04  |.}h..*D.9E....E.|
00000080  29 45 04 66 01 05 8a 54  0d c0 e2 06 8a 4c 0a fe  |)E.f...T.....L..|
00000090  c1 08 d1 8a 6c 0c 5a 52  8a 74 0b 50 bb 00 70 8e  |....l.ZR.t.P..p.|
000000a0  c3 31 db b4 02 cd 13 72  3a 8c c3 8e 45 06 58 c1  |.1.....r:...E.X.|
000000b0  e0 05 01 45 06 60 1e c1  e0 04 89 c1 31 ff 31 f6  |...E........1.1.|
000000c0  8e db fc f3 a4 1f 61 83  7d 04 00 0f 85 42 ff 83  |......a.}....B..|
000000d0  ef 08 e9 34 ff 5a ea 00  82 00 00 be 05 81 e8 3d  |...4.Z.........=|
000000e0  00 eb 06 be 0a 81 e8 35  00 be 0f 81 e8 2f 00 eb  |.......5...../..|
000000f0  fe 4c 6f 61 64 69 6e 67  20 73 74 61 67 65 32 00  |.Loading stage2.|
00000100  2e 00 0d 0a 00 47 65 6f  6d 00 52 65 61 64 00 20  |.....Geom.Read. |
00000110  45 72 72 6f 72 00 bb 01  00 b4 0e cd 10 46 8a 04  |Error........F..|
00000120  3c 00 75 f2 c3 00 00 00  00 00 00 00 00 00 00 00  |<.u.............|
00000130  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001f0  00 00 00 00 00 00 00 00  fd 49 08 00 f6 00 20 08  |.........I.... .|
00000200

(/ boot) começa em 2048.

# fdisk -lu /dev/sda

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders, total 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0009aa19

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux Par...
/dev/sda2         1026048    83886079    41430016   8e  Linux LVM

Será realmente bom se alguém conseguir explicar isso.

    
por Zul K Irshad 31.01.2016 / 14:21

2 respostas

7

De link

US/Red_Hat_Enterprise_Linux/3/html/Reference_Guide/s1-grub-whatis.html

GRUB loads itself into memory in the following stages:

The Stage 1 or primary boot loader is read into memory by the BIOS from the MBR[1]. The primary boot loader exists on less than 512 bytes of disk space within the MBR and is capable of loading either the Stage 1.5 or Stage 2 boot loader.

The Stage 1.5 boot loader is read into memory by the Stage 1 boot loader, if necessary. Some hardware requires an intermediate step to get to the Stage 2 boot loader. This is sometimes true when the /boot/ partition is above the 1024 cylinder head of the hard drive or when using LBA mode. The Stage 1.5 boot loader is found either on the /boot/ partition or on a small part of the MBR and the /boot/ partition.

The Stage 2 or secondary boot loader is read into memory. The secondary boot loader displays the GRUB menu and command environment. This interface allows selection of the kernel or operating system to boot, pass arguments to the kernel, or look at system parameters.

Parece óbvio que o estágio 2 é o binário real do grub. De fato, a documentação declara que o grub 2 é carregado pelo nome.

Eu tentaria fazer:

dd if=/dev/zero of=/boot/stage2

Dados adicionais:

Inspeção / boot / grub:

cópia do bootloader stage1:

stage1

Arquivos para o stage1_5:

e2fs_stage1_5  
fat_stage1_5  
jfs_stage1_5  
minix_stage1_5  
reiserfs_stage1_5  
xfs_stage1_5

Arquivo para o estágio 2:

stage2

Link para o grub image:

    
por 31.01.2016 / 14:45
2

Em computadores que estão em conformidade com a seqüência do BIOS de inicialização do IBM PC:

  • O MBR (setor absoluto 0) do disco é carregado pelo BIOS na memória 0000: 7C00.
  • Esse código é executado.

Forme o IBM ao W7

O código usado pelo IBM PC para inicializar pode ser visto aqui: Primeira versão do MBR da IBM® Personal Computer ™ DOS 2.00

Esse código tem muitas versões que também são apresentadas nas páginas do Starman.
Um ponto de partida sobre essas muitas versões pode ser esta página: do MS-DOS 3.30 até o MS-Windows ™ 95 (A)

Um dos códigos MBR mais comuns é o seguinte: MBR para: MS-Windows ™ 95B, 98 , 98SE e ME

A maioria das versões de código usadas apenas para carregar o próximo VBR (Volume Boot Record). O VBR da partição marcada como inicializável na tabela de partição e, em seguida, transferir a execução para ela.
(Por favor, entenda que o VBR não é o setor de disco absoluto 0 ou W7 MBR )

Pesquise esta nota sobre o montador na página W7 MBR :

; The following code uses INT 13, Function 42h ("Extended Read") to read the
; first sector (VBR) of the bootable partition into Memory at location 0x7c00.

Windows ™ 7 (e Vista) VBR (Volume Boot Record)

É interessante ler isso na página do W7VBR :

; The following code uses INT 13, Function 42h ("Extended Disk Read") to read ; 1 sector at a time of the remaining 15 sectors of the Boot Record Area into ; Memory; starting at location 7E00.

Como você pode confirmar, o código de inicialização iniciado no MBR (setor de disco 0) carregou o VBR (registro de inicialização de volume) e muitos (15 em W7) os seguintes setores.

GRUB

Mas estamos falando sobre o GRUB aqui, então, vá para a página do GRUB :

E pesquise por este código:

[7C44] -> Note: A very important location for anyone using GRUB!
          This (4-byte) Quad-Word contains the location of GRUB's
          stage2 file in sectors! It's called "stage2_sector" in
          the stage1.S code. If GRUB is installed in the MBR by a
          distro that always includes a number of sectors from
          stage2 immediately following the GRUB MBR, you will see
          the bytes 01 00 00 00 in this location; otherwise, it
          will point to stage2 in the "/boot/grub" directory.

Isso é quase sempre 01 00 00 00 (ou apenas: o próximo setor).

O que isso significa é que o BIOS carrega o setor absoluto 0 (MBR), o código do GRUB instalado no MBR continua lendo os seguintes setores. Até o tamanho de core.img (GRUB2) em distros recentes (cerca de 60 setores, ou ~ 30 kB). As unidades atuais deixam um Mega-byte livre após o MBR, portanto, não há problema. Os discos EFI têm uma partição separada para todo esse código e há ainda menos problemas (por tamanho, isto é).

Resposta

O estágio de gravação do GRUB legado2 ou, opcionalmente, o estágio 1.5 em alguns casos de canto, para o MBR e vários / muitos dos 62 setores a seguir.

E isso também é explicado nas imagens em página Grub da Wikipédia .

Do site GNU 10 arquivos de imagem do GRUB :

stage1
   This is an essential image used for booting up GRUB. Usually, this is
   embedded in an MBR or the boot sector of a partition. Because a PC boot
   sector is 512 bytes, the size of this image is exactly 512 bytes.

   All stage1 must do is to load Stage 2 or Stage 1.5 from a local disk.
   Because of the size restriction, stage1 encodes the location of Stage 2
   (or Stage 1.5) in a block list format, so it never understand any
   filesystem structure. 

NOTA : O stage2 pode ser gravado em outro disco físico. A partir da página do GRUB :

[7C40] -> 80 ("Boot Drive") NOTE: For those of you with multi-OS
          booting systems, if your Linux installation with GRUB's
 See:     remaining software (stage2, menu file, etc.) is located
 7C5A     somewhere other than on the Primary Master drive, this
          value will be 81, 82, etc. depending upon which drive
          that Linux OS's /boot/grub directory is located. In the
          stage1.S file, it's called the GRUB_INVALID_DRIVE byte
          and commented as: "the disk to load stage2 from." (The
          word INVALID has something to do with the code logic.)
    
por 01.02.2016 / 23:35

Tags