# Build a kernel during installation: root vs. user?

## kikinovak

Hi,

I'm more or less new to Gentoo, and I'm currently fiddling with it on a pair of sandbox machines. I'm using Greg Kroah-Hartman's Linux Kernel in a Nutshell to try to configure a working kernel for my machine. Until now, I've only used distributions with "canned" kernels. I already successfully installed a working minimal Gentoo with genkernel, but since my hardware is a bit old, building it took a couple of hours. 

1) Greg Kroah-Hartman states that "none of the kernel developers build their kernels as root" and mentions some problems one could face if doing so.

2) The standard Gentoo install procedure more or less builds a kernel as root.

I'm confused. Am I supposed to add a normal user to my system during the installation, adjust some permissions (how ? chown -R normaluser:normaluser /usr/src/linux or what?) and then build my kernel as that user (only to install it as root)? How do you Gentoo gurus go about that?

Ah, and yes : Gentoo seems quite fun and easy to me, only because it's the most accurately documented distro out there.

Cheers from the sunny South of France,

Niki

----------

## DaggyStyle

```
dagg@NCC-5001-D ~ $ cd /usr/src/linux

dagg@NCC-5001-D /usr/src/linux $ make clean

rm: cannot remove `/usr/src/linux-2.6.35-gentoo-r5/arch/x86_64/boot/bzImage': Permission denied

make: *** [archclean] Error 1

dagg@NCC-5001-D /usr/src/linux $ make -j3

rm: cannot remove `include/config/kernel.release': Permission denied

make: *** [include/config/kernel.release] Error 1

```

in order to achieve that you need it fiddle with the permissions of the directory.

I think the book means that they work on the kernel as normal users, compile it, copy it into the right location and chown them.

----------

## Hu

Compiling a kernel as an unprivileged user works fine.  I have compiled Portage-managed kernels as an unprivileged user without changing permissions since I started using Gentoo.  You must set the environment variable $KBUILD_OUTPUT, and preferably also $INSTALL_PATH and $INSTALL_MOD_PATH, to point to writable directories for the build output, the installation of the kernel+config, and the installation of modules, respectively.

Also, a chown is not required if they perform a cp as root and do not specify to preserve permissions.

----------

## DaggyStyle

 *Hu wrote:*   

> Compiling a kernel as an unprivileged user works fine.  I have compiled Portage-managed kernels as an unprivileged user without changing permissions since I started using Gentoo.  You must set the environment variable $KBUILD_OUTPUT, and preferably also $INSTALL_PATH and $INSTALL_MOD_PATH, to point to writable directories for the build output, the installation of the kernel+config, and the installation of modules, respectively.
> 
> Also, a chown is not required if they perform a cp as root and do not specify to preserve permissions.

 

nice, I didn't know that, can you post your $INSTALL_MOD_PATH, $KBUILD_OUTPUT and $INSTALL_PATH?

I think I'm doing something wrong

----------

## Hu

The exact values vary by kernel version, since the build system dumps objects in there with no further directory structure.

Here is the script I use to set up the build environment:

```
#!/bin/bash

# Get the kernel version from the user.  Optionally, the user can tell

# us where to find the source.  If not specified, assume it was put in

# /usr/src/linux-$VERSION.

KV_FULL="$1"; shift

BDIR="$HOME/build/kernel/"

KERNEL_DIR="${1:-/usr/src/linux-"${KV_FULL}"}"; shift

# Check that the kernel source can be found.

if [ ! -d "$KERNEL_DIR" ]; then

    echo "Directory $KERNEL_DIR does not exist." >&2

    exit 1

fi

# Set the variables for the kernel build system.

export KBUILD_OUTPUT="${BDIR}${KV_FULL}/"

export INSTALL_PATH="${BDIR}install/${KV_FULL}/boot/"

export INSTALL_MOD_PATH="${BDIR}install/${KV_FULL}/"

# Make the directories, as a convenience if the user is doing a fresh

# build.  We do not clean out existing directories, so that the user can

# return to the build environment later.

for a in "$KBUILD_OUTPUT" "$INSTALL_PATH"; do

    if [ ! -d "$a" ]; then

        mkdir -p "$a" || exit $?

        echo "Created ${a}"

    fi

done

# Show them, so the user can check that the output is what it should be.

echo KBUILD_OUTPUT="${KBUILD_OUTPUT}"

echo INSTALL_PATH="${INSTALL_PATH}"

echo INSTALL_MOD_PATH="${INSTALL_MOD_PATH}"

cd "$KERNEL_DIR" || exit $?

# Start an interactive shell so the user can run menuconfig, build the

# kernel, etc.

/bin/bash
```

----------

