Web lists-archives.com

Re: grub tries to read outside of a disk.

On 23/11/2018 03.09, Daniel Fishman wrote:

I am trying to install Debian on a 4TB external HDD, and want to be able to
boot it both on UEFI and BIOS systems. Using a simple BIOS/GPT configuration
(GPT partitioning with BIOS boot) didn't work out - after receiving some
important tips from this mailing list about problems that I had with this
configuration I succeeded to boot on BIOS system, but not on UEFI system.
Therefore I tried to combine BIOS+UEFI boots by creating both EFI and
BIOS boot partitions, and then installing grub into both. Here is how the
HDD was partitioned:

[root@arch]# parted /dev/sdb print
Model: WD Elements 25A1 (scsi)
Disk /dev/sdb: 4001GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system     Name  Flags
  1      1049kB  2097kB  1049kB                        bios_grub
  2      2097kB  202MB   200MB   fat32                 boot, esp
  3      202MB   714MB   512MB   ext4
  4      714MB   8714MB  8000MB  linux-swap(v1)
  5      8714MB  4001GB  3992GB  ext4

Partition 1 is intended for BIOS boot partition (for Stage 1.5 loader),
partition 2 is the EFI partition, partition 3 is intended for the /boot
(as was suggested previously on this mailing list) so that grub-related
files will be located inside the 2TB limit and therefore be accessible to
Stage 1.5 loader in BIOS mode, partition 4 is a swap, and 5 is the /.

I performed Debian installation on a UEFI machine, and after an installation
succeeded to boot the HDD. After that I installed grub into MBR:

grub-install --target=i386-pc --recheck /dev/sdb

Then I tried to boot on BIOS machine, and got the following error from grub:

"error: attempt to read or write outside of disk 'hd0'"

and was dropped into grub's rescue shell. I tried the following command:

ls (hd0,gpt3)/

which listed the expected contents of the /boot partition. Then:

set root=(hd0,gpt3)
set prefix=(hd0,gpt3)/grub
insmod normal

and again I get the error message

"error: attempt to read or write outside of disk 'hd0'"

As far as I could find, usually this kind of an error happens when
grub's files are located on a partition which is far from the
beginning of a disk, but in my case this is not so. And since grub
can list contents of a partition from a rescue shell, then it has
access to the file system. Then what is the problem here?

Eventually I succeeded to solve the problem, though I still don't understand
the reason for it - and maybe the solution will be useful for somebody else.

I repeated the whole installation process from the beginning, and this time
used a bit different partitioning scheme, where locations of /boot and EFI
partitions were switched, so that /boot partition (with all the files needed
by the grub) are now located immediately after BIOS boot partition, or in
other words:

[root@arch]# parted /dev/sdb print
Model: WD Elements 25A1 (scsi)
Disk /dev/sdb: 4001GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system     Name                 Flags
 1      1049kB  2097kB  1049kB                  BIOS boot partition  bios_grub
 2      2097kB  539MB   537MB   ext4            Linux filesystem
 3      539MB   749MB   210MB   fat32           EFI System           boot, esp
 4      749MB   9339MB  8590MB  linux-swap(v1)  Linux swap
 5      9339MB  4001GB  3991GB  ext4            Linux filesystem

The idea was that maybe somehow (maybe due to a buggy BIOS) the fact that EFI
partition was located before /boot partition confused BIOS and therefore
prevented it from being able to access files on /boot partition.

There was a number of other small things that I did somewhat differently
(for example, this time a partitioning was done with gdisk instead of with
parted, and I think that I used a bit older Debian's installation media),
but they don't seem significant enough.