# Kernel compilate as user & out of tree

## big_gie

Hi all,

I'm writing a script to compile the kernel as a normal user (not as root) but I'm facing some issues.

The problem is that I can't find a way to both let the user build without touching /usr/src/linux-[version] AND allow emerge nvidia-drivers. Is it possible?

I'm exporting KBUILD_OUTPUT to the user owned directory, INSTALL_PATH to $KBUILD_OUTPUT/install/.../boot and INSTALL_MOD_PATH similarly.

I then copy a .config to KBUILD_OUTPUT, cd /usr/src/linux, run "make menuconfig", "make prepare", "make modules", "make modules_install" and finally "make install".

This works as expected. The user (or script) can copy the built files to where they should.

The problem comes when an out of tree driver is emerged. For example, x11-drivers/nvidia-drivers complains:

```

 * devdriver_5.0_linux_64_302.06.03.run RMD160 SHA1 SHA256 size ;-) ...                     [ ok ]

 * checking ebuild checksums ;-) ...                                                        [ ok ]

 * checking auxfile checksums ;-) ...                                                       [ ok ]

 * Determining the location of the kernel source code

 * Found kernel source directory:

 *     /usr/src/linux

 * Found sources for kernel version:

 *     3.3.8-gentoo

 * Could not find a usable .config in the kernel source directory.

 * Please ensure that /usr/src/linux points to a configured set of Linux sources.

 * If you are using KBUILD_OUTPUT, please set the environment var so that

 * it points to the necessary object directory so that it might find .config.

 * ERROR: x11-drivers/nvidia-drivers-302.06.03 failed (setup phase):

 *   Kernel not configured; no .config found in /usr/src/linux

```

Ok, copying the config is not too bad, but still fails:

```

 * devdriver_5.0_linux_64_302.06.03.run RMD160 SHA1 SHA256 size ;-) ...                     [ ok ]

 * checking ebuild checksums ;-) ...                                                        [ ok ]

 * checking auxfile checksums ;-) ...                                                       [ ok ]

 * Determining the location of the kernel source code

 * Found kernel source directory:

 *     /usr/src/linux

 * Found sources for kernel version:

 *     3.3.8-gentoo

 * These sources have not yet been prepared.

 * We cannot build against an unprepared tree.

 * To resolve this, please type the following:

 *

 * # cd /usr/src/linux

 * # make oldconfig

 * # make modules_prepare

 *

 * Then please try merging this module again.

 * ERROR: x11-drivers/nvidia-drivers-302.06.03 failed (setup phase):

 *   Kernel sources need compiling first

```

I thus have to run "make oldconfig && make modules_prepare" in /usr/src/linux. I can then emerge x11-drivers/nvidia-drivers fine.

BUT, this breaks the possibility for the user to build. "make menuconfig" runs fine, but "make prepare" fails:

```

$ make prepare V=1

make -C /home/nicolas/build/kernel/3.3.8-gentoo \

KBUILD_SRC=/usr/src/linux-3.3.8-gentoo \

KBUILD_EXTMOD="" -f /usr/src/linux-3.3.8-gentoo/Makefile \

prepare

make -f /usr/src/linux-3.3.8-gentoo/scripts/Makefile.build obj=arch/x86/syscalls all

make[2]: Nothing to be done for `all'.

make -f /usr/src/linux-3.3.8-gentoo/scripts/Makefile.build obj=arch/x86/tools relocs

make[2]: Nothing to be done for `relocs'.

rm -f include/config/kernel.release

echo "3.3.8-gentoo$(/bin/sh /usr/src/linux-3.3.8-gentoo/scripts/setlocalversion /usr/src/linux-3.3.8-gentoo)" > include/config/kernel.release

if [ -f /usr/src/linux-3.3.8-gentoo/.config -o -d /usr/src/linux-3.3.8-gentoo/include/config ]; then \

        echo "  /usr/src/linux-3.3.8-gentoo is not clean, please run 'make mrproper'";\

        echo "  in the '/usr/src/linux-3.3.8-gentoo' directory.";\

        /bin/false; \

fi;

  /usr/src/linux-3.3.8-gentoo is not clean, please run 'make mrproper'

  in the '/usr/src/linux-3.3.8-gentoo' directory.

```

There seems to be a check on /usr/src/linux/.config and /usr/src/linux/include/config in the Makefile ("prepare3" target).

OR, I could use KBUILD_OUTPUT to the user folder when emerging x11-drivers/nvidia-drivers. But I want x11-drivers/nvidia-drivers to be emerged in the user system (emerge will be run as root!), not KBUILD_OUTPUT.

Anyone has an idea how I can reconcile both compiling as users and emerge an out of tree drivers?

Thanks!

----------

## Hu

I think your options are to stop using out-of-tree drivers, stop building out-of-tree, or point $KBUILD_OUTPUT to the build tree.  If the user has not set restrictive directory permissions, this last option should work fine.  The build tree does not need to be owned by the user which builds the out-of-tree driver.

----------

## Apheus

You may also need the variable KERNEL_DIR, set it to the same value as $KBUILD_OUTPUT. See here:

https://forums.gentoo.org/viewtopic-t-859409.html

----------

