# [SOLVED] error: invalid magic number on kernel loading

## alessio_demarchi

Hello everyone

I'm in trouble with new kernel compile.

I'm using kernel 5.4.48-gentoo-x86_64 and all working fine.

Using the same configuration (I have copied .config of the older kernel in new source directory and run "make syncconf") I have compiled today the new source 5.4.60.

Compile phase finish without any error (I used "genkernel --xconfig --clean all"), in boot directory I find the new initamfs and vmlinuz files.

When I restart the system, If in grub I select the new kernel the system halted with message

error: invalid magic number

error: initramfs, load kernel before

I read another post about a similar problem (https://forums.gentoo.org/viewtopic-t-1084672-highlight-invalid+magic+number.html), but I have not understand the solution.

Any suggestion?Last edited by alessio_demarchi on Tue Sep 01, 2020 8:58 am; edited 1 time in total

----------

## eccerr0r

How are you configuring your grub?  

What file did you use for your kernel and initramfs?

----------

## Whissi

Which genkernel version are you using? If you are using genkernel-4.1.1, please upgrade to 4.1.2 and retry.

----------

## alessio_demarchi

My grub is configured to load initram and kernel generated by genkernel.

genkernel is the version 4.1.0-r2

I use to move generated kernel and initramfs files in tree directory from /boot:

old (previous compiled kernel files)

default (actual functioning kernel)

test (new kernel)

in each directory there is:

vmlinuz-genkernel-x86_64-<version>

initramfs-genkernel-x86_64-<version>

Systemmap-genkernel-<version>.map

kernel-config-<version>

in /boot directory I create two group of symbolik link:

initramfs-x86_64-1_old -> linked to initramfs in old directory

initramfs-x86_64-2_tst -> linked to initramfs in test directory

initramfs-x86_64-3_dfl -> linked to initramfs in default directory

kernel-x86_64-1_old, 2_tst, 3_dfl with the same philosophy as the previous tree link

System.map linked to default directory System.map

My system is my working laptop, and is running since 4 years ago with gentoo (dell precision M4800)

Thanks a lot for your replies

----------

## Tony0945

All of that can be automated with a script, eliminating mistakes in typing.

IIRC genkernel will update grub for you, so your script needn't do that.

I always recommend enabling IKCONFIG  IKCONFIG_PROC in the kernel so that the running configuration can be retrieved unambiguously.

----------

## alessio_demarchi

Now I have  tried to recompile my kernel with genkernel-4.1.2, but the result not changed.

----------

## Whissi

Do you build kernel and initramfs with genkernel or do you build kernel manually?  Please also show /var/log/genkernel.log...

Get a debug shell in genkernel and run "gksosreport" and show us that file.

----------

## alessio_demarchi

I build all with genkernel.

How can I show you genkernel.log (it is very huge)

----------

## Whissi

Use services like https://transfer.sh/

----------

## alessio_demarchi

Ok

I used transfer.sh

The link is

https://transfer.sh/%28/Q385Q/genkernel.log%29.tar.gz

thanks

----------

## Whissi

First, genkernel.log is not really the interesting log file in your case for now because generation didn't fail for you.

 *Quote:*   

> Using the same configuration (I have copied .config of the older kernel in new source directory and run "make syncconf") I have compiled today the new source 5.4.60.

 This is wrong. Please read how genkernel determines kernel.config it will use. If you start building a new KV (=when upgrading kernel sources), pay attention to used kernel config. I recommend to always pass kernel config via parameter (--kernel-confog=/path/to/desired/kernel.config). This way you can be sure that nothing unexpected will happen (hint: If new kernel should be based on current running kernel, just use --kernel-config=/proc/config.gz).

If you really did what you wrote in your first posting, i.e. installed new kernel sources, copied any existing kernel.config into that directory and run an arbitrary command in that directory... on next genkernel run, this kernel.config (=/usr/src/linux/.config) will be ignored by default and genkernel will look for kernel.config based on set heuristic (determining KV based on set kernel sources, looking in /etc/kernels/ for an already existing kernel.config for that version... when building a new KV for the first time, a kernel.config for this KV usually doesn't exist yet so genkernel will fall back to its generic kernel.config).

So please notice that shown genkernel.log shows  *Quote:*   

> * Using kernel config file '/etc/kernels/kernel-config-5.4.60-gentoo-x86_64' ...

 which means that something had created a config in there. This doesn't match your story. Please verify...

Rebuild kernel and initramfs with "genkernel all" and ensure that your desired kernel.config was used for that run (--kernel-config=/proc/config.gz)

If genkernel-4.1.2 initramfs will be still unable to boot, get a shell in that environment and run "gksosreport" -- attach this file to this thread like you did with genkernel.log

----------

## alessio_demarchi

Thanks Whissi

The method I used for configuring new kernel from previous kernel version source is explained in Gentoo wiki for kernel update (probably my english is not so good, and my explaination is not correct).

Typically I using genkernel --xconfig --clean all and I always check basic kernel configuration before compilation phase, and it seems to be correct.

I think the problem is not in initramfs file, but in vmlinuz. How can I run gksosreport, it is not and application installed in my system.

Now I try to run genkernel --kernel-config=/proc/config.gz all and then I try to reboot my system.

----------

## Whissi

"gksosreport" is part of genkernel's initramfs. I.e. in case genkernel's initramfs will fail to boot your system you can start a rescue shell and call that command.

----------

## alessio_demarchi

I try new kernel generated by genkernel --kernel-config=/proc/config.gz all, but nothing change

I think the problem is in vmlinuz, initramfs is not loaded, so I can't run gksosreport.

The message reported at boot is:

Loading Linux x86_64-3_dfl ...

error: invalid magic number

Loading initial ramdisk ...

error: you need to load the kernel first.

Press any key to continue...Last edited by alessio_demarchi on Mon Aug 31, 2020 6:51 pm; edited 1 time in total

----------

## Whissi

Which bootloader are you using? For grub, please show us /boot/grub/grub.cfg...

----------

## alessio_demarchi

I use grub2

this is my grub.cfg https://transfer.sh/%28%29.tar.gz

----------

## Whissi

That tar file is empty.

----------

## alessio_demarchi

Sorry

permission problem....

Try this

https://transfer.sh/%28%29.tar.gz

----------

## alessio_demarchi

#

# DO NOT EDIT THIS FILE

#

# It is automatically generated by grub-mkconfig using templates

# from /etc/grub.d and settings from /etc/default/grub

#

### BEGIN /etc/grub.d/00_header ###

if [ -s $prefix/grubenv ]; then

  load_env

fi

if [ "${next_entry}" ] ; then

   set default="${next_entry}"

   set next_entry=

   save_env next_entry

   set boot_once=true

else

   set default="${saved_entry}"

fi

if [ x"${feature_menuentry_id}" = xy ]; then

  menuentry_id_option="--id"

else

  menuentry_id_option=""

fi

export menuentry_id_option

if [ "${prev_saved_entry}" ]; then

  set saved_entry="${prev_saved_entry}"

  save_env saved_entry

  set prev_saved_entry=

  save_env prev_saved_entry

  set boot_once=true

fi

function savedefault {

  if [ -z "${boot_once}" ]; then

    saved_entry="${chosen}"

    save_env saved_entry

  fi

}

function load_video {

  if [ x$feature_all_video_module = xy ]; then

    insmod all_video

  else

    insmod efi_gop

    insmod efi_uga

    insmod ieee1275_fb

    insmod vbe

    insmod vga

    insmod video_bochs

    insmod video_cirrus

  fi

}

if [ x$feature_default_font_path = xy ] ; then

   font=unicode

else

insmod part_msdos

insmod jfs

set root='hd0,msdos7'

if [ x$feature_platform_search_hint = xy ]; then

  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos7 --hint-efi=hd0,msdos7 --hint-baremetal=ahci0,msdos7  3e5f2da8-79d5-4cff-99cc-5fcc68cfa006

else

  search --no-floppy --fs-uuid --set=root 3e5f2da8-79d5-4cff-99cc-5fcc68cfa006

fi

    font="/usr/share/grub/unicode.pf2"

fi

if loadfont $font ; then

  set gfxmode=1920x1080,1440x900,1280x800,1024x600,auto

  load_video

  insmod gfxterm

  set locale_dir=$prefix/locale

  set lang=en_US

  insmod gettext

fi

terminal_output gfxterm

insmod part_msdos

insmod ext2

set root='hd0,msdos6'

if [ x$feature_platform_search_hint = xy ]; then

  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos6 --hint-efi=hd0,msdos6 --hint-baremetal=ahci0,msdos6  48f55fb2-f9e9-404c-9916-8a1c09ab9030

else

  search --no-floppy --fs-uuid --set=root 48f55fb2-f9e9-404c-9916-8a1c09ab9030

fi

insmod png

background_image -m stretch /grub/mybackground.png

if [ x$feature_timeout_style = xy ] ; then

  set timeout_style=menu

  set timeout=3

# Fallback normal timeout code in case the timeout_style feature is

# unavailable.

else

  set timeout=3

fi

### END /etc/grub.d/00_header ###

### BEGIN /etc/grub.d/10_linux ###

menuentry 'Gentoo GNU/Linux' --class gentoo --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-3e5f2da8-79d5-4cff-99cc-5fcc68cfa006' {

        savedefault

        load_video

        set gfxpayload=keep

        insmod gzio

        insmod part_msdos

        insmod ext2

        set root='hd0,msdos6'

        if [ x$feature_platform_search_hint = xy ]; then

          search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos6 --hint-efi=hd0,msdos6 --hint-baremetal=ahci0,msdos6  48f55fb2-f9e9-404c-9916-8a1c09ab9030

        else

          search --no-floppy --fs-uuid --set=root 48f55fb2-f9e9-404c-9916-8a1c09ab9030

        fi

        echo    'Loading Linux x86_64-3_dfl ...'

        linux   /kernel-genkernel-x86_64-3_dfl root=UUID=3e5f2da8-79d5-4cff-99cc-5fcc68cfa006 ro resume=/dev/sda5 quiet splash

        echo    'Loading initial ramdisk ...'

        initrd  /initramfs-genkernel-x86_64-3_dfl

}

submenu 'Advanced options for Gentoo GNU/Linux' $menuentry_id_option 'gnulinux-advanced-3e5f2da8-79d5-4cff-99cc-5fcc68cfa006' {

        menuentry 'Gentoo GNU/Linux, with Linux x86_64-3_dfl' --class gentoo --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-x86_64-3_dfl-advanced-3e5f2da8-79d5-4cff-99cc-5fcc68cfa006' {

        savedefault

                load_video

                set gfxpayload=keep

                insmod gzio

                insmod part_msdos

                insmod ext2

                set root='hd0,msdos6'

                if [ x$feature_platform_search_hint = xy ]; then

                  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos6 --hint-efi=hd0,msdos6 --hint-baremetal=ahci0,msdos6  48f55fb2-f9e9-404c-9916-8a1c09ab9030

                else

                  search --no-floppy --fs-uuid --set=root 48f55fb2-f9e9-404c-9916-8a1c09ab9030

                fi

                echo    'Loading Linux x86_64-3_dfl ...'

                linux   /kernel-genkernel-x86_64-3_dfl root=UUID=3e5f2da8-79d5-4cff-99cc-5fcc68cfa006 ro resume=/dev/sda5 quiet splash

                echo    'Loading initial ramdisk ...'

                initrd  /initramfs-genkernel-x86_64-3_dfl

        }

        menuentry 'Gentoo GNU/Linux, with Linux x86_64-3_dfl (recovery mode)' --class gentoo --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-x86_64-3_dfl-recovery-3e5f2da8-79d5-4cff-99cc-5fcc68cfa006' {

                load_video

                set gfxpayload=keep

                insmod gzio

                insmod part_msdos

                insmod ext2

                set root='hd0,msdos6'

                if [ x$feature_platform_search_hint = xy ]; then

                  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos6 --hint-efi=hd0,msdos6 --hint-baremetal=ahci0,msdos6  48f55fb2-f9e9-404c-9916-8a1c09ab9030

                else

                  search --no-floppy --fs-uuid --set=root 48f55fb2-f9e9-404c-9916-8a1c09ab9030

                fi

                echo    'Loading Linux x86_64-3_dfl ...'

                linux   /kernel-genkernel-x86_64-3_dfl root=UUID=3e5f2da8-79d5-4cff-99cc-5fcc68cfa006 ro single resume=/dev/sda5

                echo    'Loading initial ramdisk ...'

                initrd  /initramfs-genkernel-x86_64-3_dfl

        }

        menuentry 'Gentoo GNU/Linux, with Linux x86_64-2_tst' --class gentoo --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-x86_64-2_tst-advanced-3e5f2da8-79d5-4cff-99cc-5fcc68cfa006' {

        savedefault

                load_video

                set gfxpayload=keep

                insmod gzio

                insmod part_msdos

                insmod ext2

                set root='hd0,msdos6'

                if [ x$feature_platform_search_hint = xy ]; then

                  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos6 --hint-efi=hd0,msdos6 --hint-baremetal=ahci0,msdos6  48f55fb2-f9e9-404c-9916-8a1c09ab9030

                else

                  search --no-floppy --fs-uuid --set=root 48f55fb2-f9e9-404c-9916-8a1c09ab9030

                fi

                echo    'Loading Linux x86_64-2_tst ...'

                linux   /kernel-genkernel-x86_64-2_tst root=UUID=3e5f2da8-79d5-4cff-99cc-5fcc68cfa006 ro resume=/dev/sda5 quiet splash

                echo    'Loading initial ramdisk ...'

                initrd  /initramfs-genkernel-x86_64-2_tst

        }

        menuentry 'Gentoo GNU/Linux, with Linux x86_64-2_tst (recovery mode)' --class gentoo --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-x86_64-2_tst-recovery-3e5f2da8-79d5-4cff-99cc-5fcc68cfa006' {

                load_video

                set gfxpayload=keep

                insmod gzio

                insmod part_msdos

                insmod ext2

                set root='hd0,msdos6'

                if [ x$feature_platform_search_hint = xy ]; then

                  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos6 --hint-efi=hd0,msdos6 --hint-baremetal=ahci0,msdos6  48f55fb2-f9e9-404c-9916-8a1c09ab9030

                else

                  search --no-floppy --fs-uuid --set=root 48f55fb2-f9e9-404c-9916-8a1c09ab9030

                fi

                echo    'Loading Linux x86_64-2_tst ...'

                linux   /kernel-genkernel-x86_64-2_tst root=UUID=3e5f2da8-79d5-4cff-99cc-5fcc68cfa006 ro single resume=/dev/sda5

                echo    'Loading initial ramdisk ...'

                initrd  /initramfs-genkernel-x86_64-2_tst

        }

        menuentry 'Gentoo GNU/Linux, with Linux x86_64-1_old' --class gentoo --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-x86_64-1_old-advanced-3e5f2da8-79d5-4cff-99cc-5fcc68cfa006' {

        savedefault

                load_video

                set gfxpayload=keep

                insmod gzio

                insmod part_msdos

                insmod ext2

                set root='hd0,msdos6'

                if [ x$feature_platform_search_hint = xy ]; then

                  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos6 --hint-efi=hd0,msdos6 --hint-baremetal=ahci0,msdos6  48f55fb2-f9e9-404c-9916-8a1c09ab9030

                else

                  search --no-floppy --fs-uuid --set=root 48f55fb2-f9e9-404c-9916-8a1c09ab9030

                fi

                echo    'Loading Linux x86_64-1_old ...'

                linux   /kernel-genkernel-x86_64-1_old root=UUID=3e5f2da8-79d5-4cff-99cc-5fcc68cfa006 ro resume=/dev/sda5 quiet splash

                echo    'Loading initial ramdisk ...'

                initrd  /initramfs-genkernel-x86_64-1_old

        }

        menuentry 'Gentoo GNU/Linux, with Linux x86_64-1_old (recovery mode)' --class gentoo --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-x86_64-1_old-recovery-3e5f2da8-79d5-4cff-99cc-5fcc68cfa006' {

                load_video

                set gfxpayload=keep

                insmod gzio

                insmod part_msdos

                insmod ext2

                set root='hd0,msdos6'

                if [ x$feature_platform_search_hint = xy ]; then

                  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos6 --hint-efi=hd0,msdos6 --hint-baremetal=ahci0,msdos6  48f55fb2-f9e9-404c-9916-8a1c09ab9030

                else

                  search --no-floppy --fs-uuid --set=root 48f55fb2-f9e9-404c-9916-8a1c09ab9030

                fi

                echo    'Loading Linux x86_64-1_old ...'

                linux   /kernel-genkernel-x86_64-1_old root=UUID=3e5f2da8-79d5-4cff-99cc-5fcc68cfa006 ro single resume=/dev/sda5

                echo    'Loading initial ramdisk ...'

                initrd  /initramfs-genkernel-x86_64-1_old

        }

}

### END /etc/grub.d/10_linux ###

### BEGIN /etc/grub.d/20_linux_xen ###

### END /etc/grub.d/20_linux_xen ###

### BEGIN /etc/grub.d/30_os-prober ###

menuentry 'Windows 10 (on /dev/sda2)' --class windows --class os $menuentry_id_option 'osprober-chain-AE52157E52154C89' {

        savedefault

        insmod part_msdos

        insmod ntfs

        set root='hd0,msdos2'

        if [ x$feature_platform_search_hint = xy ]; then

          search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos2 --hint-efi=hd0,msdos2 --hint-baremetal=ahci0,msdos2  AE52157E52154C89

        else

          search --no-floppy --fs-uuid --set=root AE52157E52154C89

        fi

        parttool ${root} hidden-

        drivemap -s (hd0) ${root}

        chainloader +1

}

### END /etc/grub.d/30_os-prober ###

### BEGIN /etc/grub.d/40_custom ###

# This file provides an easy way to add custom menu entries.  Simply type the

# menu entries you want to add after this comment.  Be careful not to change

# the 'exec tail' line above.

### END /etc/grub.d/40_custom ###

### BEGIN /etc/grub.d/41_custom ###

if [ -f  ${config_directory}/custom.cfg ]; then

  source ${config_directory}/custom.cfg

elif [ -z "${config_directory}" -a -f  $prefix/custom.cfg ]; then

  source $prefix/custom.cfg;

fi

### END /etc/grub.d/41_custom ###

----------

## Whissi

Note: In your first posting you mentioned

 *Quote:*   

> vmlinuz-genkernel-x86_64-<version>
> 
> initramfs-genkernel-x86_64-<version>
> 
> Systemmap-genkernel-<version>.map
> ...

 

These are custom names.

The shown genkernel.log also shows that you created 

```
/boot/initramfs-5.4.60-gentoo-x86_64.img
```

Your shown grub.cfg doesn't use that name schema.

I would suggest cleaning up /boot and re-run 

```
grub-mkconfig -o /boot/grub/grub.cfg
```

. Please double check before reboot that grub.cfg will contain an entry using initramfs-5.4.60-gentoo-x86_64.img and corresponding kernel.

----------

## alessio_demarchi

Sorry

In my first post I used a simplified sintax. The filenames are correct in grub.cfg.

I tried command that you suggest before opening this post

The filename that compare in grub.cfg are symbolik link to initramfs-genkernel-<version>.img and corresponding kernel file

I don't know if it is usefull, this is my actual /boot directory:

drwxr-xr-x 2 root root 4096 Aug 31 21:15 default

drwxr-xr-x 6 root root 4096 Aug 29 13:05 grub

lrwxrwxrwx 1 root root   38 Aug 28 19:15 initramfs-genkernel-x86_64-1_old -> old/initramfs-5.4.48-gentoo-x86_64.img

lrwxrwxrwx 1 root root   39 Jul 17 11:22 initramfs-genkernel-x86_64-2_tst -> test/initramfs-5.4.48-gentoo-x86_64.img

lrwxrwxrwx 1 root root   42 Aug 28 19:43 initramfs-genkernel-x86_64-3_dfl -> default/initramfs-5.4.60-gentoo-x86_64.img

lrwxrwxrwx 1 root root   32 Aug 28 19:15 kernel-genkernel-x86_64-1_old -> old/vmlinuz-5.4.48-gentoo-x86_64

lrwxrwxrwx 1 root root   33 Jul 17 11:22 kernel-genkernel-x86_64-2_tst -> test/vmlinuz-5.4.48-gentoo-x86_64

lrwxrwxrwx 1 root root   42 Aug 28 19:43 kernel-genkernel-x86_64-3_dfl -> default/kernel-config-5.4.60-gentoo-x86_64

drwxr-xr-x 2 root root 4096 Aug 28 19:14 old

lrwxrwxrwx 1 root root   39 Aug 28 19:43 System.map -> default/System.map-5.4.60-gentoo-x86_64

drwxr-xr-x 2 root root 4096 Jul 17 11:23 test

----------

## Whissi

I am kind of lost here. Don't get me wrong here but you are doing advanced stuff and I am not sure you understand what you are doing. I cannot help you with that.

All I can tell you:

If you manage to get genkernel's initramfs loaded (you will see that on boot when initramfs will print you name and version) and it will fail, genkernel's initramfs will offer you to enter rescue shell. If you enter rescue shell you can use "gksosreport" to get further help. If you don't see such a prompt that is another indicator that you don't use genkernel's initramfs.

But at the moment it sounds like your problem is that you do not boot genkernel's initramfs at all so this is not a genkernel problem.

So I recommend you to go back one step and follow handbook step by step. Build both, kernel and initramfs with genkernel (genkernell --kernel-config=/path/to/your/desired/kernel.config all) and tell bootloader to boot that image and initramfs. I am pretty sure that this will work when you use a working kernel config (for example /proc/config.gz from current running kernel).

----------

## alessio_demarchi

Sorry Whissi. You have to be patience with me. I'm an old linux users (it is since 1994 that I recompile linux kernel source as hobbyist user), and a lot my solution it is not so "classical", but trust me, all are running and functional.

My problem is not in initramfs, initramfs is not loaded because vmlinuz (loaded before initramfs) is not recognized as right kernel file.

I don't know why.

If I run "file /boot/default/vmlinuz..." this is the result:

# file /boot/default/vmlinuz-5.4.60-gentoo-x86_64 

/boot/default/vmlinuz-5.4.60-gentoo-x86_64: Linux kernel x86 boot executable bzImage, version 5.4.60-gentoo-x86_64 (root@ganimede) #1 SMP PREEMPT Mon Aug 31 20:59:27 CEST 2020, RO-rootFS, swap_dev 0x6, Normal VGA

 Now I try to clear my /boot partition (all link and directory old, test and default), launch your suggested command (genkernel --kernel-config=/proc/config.gz all) then grub-mkconfig -o /boot/grub.cfg and then reboot my system.

wait a moment and then I write the resultLast edited by alessio_demarchi on Tue Sep 01, 2020 8:44 am; edited 1 time in total

----------

## alessio_demarchi

Ok.

I don't know why, but now it is working.

I have cleared my /boot directory (copied my old functioning kernels file), copy /boot/default/vmlinuz..., initramfs.. and System.map-genkernel.. in /boot.

Run grub-mkconfig -o /boot/grub/grub.cfg

Reboot.

And now my new kernel is running.

Whissi thank a lot. For your patience and competence.

How can I mark as solved this topic?

----------

## Whissi

Glad you sorted the issue.

Next to your first posting you should find an "EDIT" button which will allow you to change title to add "SOLVED" I think.

----------

## eccerr0r

As a followup I suspect the files were copied over but got the names crossed inadvertently.  Sometimes something as simple as this was overlooked and real tough to debug.  Starting over from scratch as done clears things up.

----------

