2

System info:

root@zaidi:/home/uzair# uname -a
Linux zaidi 3.11.0-26-generic #45~precise1-Ubuntu SMP Tue Jul 15 04:04:35 UTC 2014 i686 i686 i386 GNU/Linux

I am trying to write a simple device driver. For that I need to download the source code of the kernel I am running (right?)

I found following two methods:

apt-get install linux-source-$(uname -r)


apt-get source linux-image-$(uname -r)

What is the difference between these two commands? Which one is actually required for building the driver. I have a make file like this:

# Makefile – makefile of our first driver
# if KERNELRELEASE is defined, we've been invoked from the
# kernel build system and can use its language.
ifneq (${KERNELRELEASE},)
    obj-m := ofd.o
# Otherwise we were called directly from the command line.
# Invoke the kernel build system.
else
    KERNEL_SOURCE := /usr/src/linux
    PWD := $(shell pwd)
default:
    ${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} modules
clean:
    ${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} clean
endif

I would like to have the source code in /usr/src/linux

1

Difference between installation process by apt-get install & by apt-get source:

  1. sudo apt-get install <package>installs packages with their dependencies from repositories and cached files are found at /var/cache/apt/archives in .deb extension.
  2. sudo apt-get source <package> will download source files in current directory.

Now take look at how to build & install package from downloaded source files (from repository):

  • First install build-dependencies by command : sudo apt-get build-dep <package>
  • Then download source package by command : sudo apt-get source <package>
  • Then navigate to directory containing source by cd and use dpkg-buildpackage -uc -us -b to get binaries, dpkg-buildpackage -uc -us -S for a source package without using sudo.
  • It get/build .deb files which can be installed by dpkg -i <file-name>.deb

But if <package> is directly available to install from repositories then it is recommended to install packages by following command

sudo apt-get install <package>

For compiling package from source code visit :community help

0

You should only need the appropriate linux-headers package:

apt-cache show linux-source-3.13.0
Package: linux-source-3.13.0
...
Description-en: Linux kernel source for version 3.13.0 with Ubuntu patches
This package provides the source code for the Linux kernel version
3.13.0.
 .
This package is mainly meant for other packages to use, in order to build
custom flavours.
.
If you wish to use this package to create a custom Linux kernel, then it
is suggested that you investigate the package kernel-package, which has
been designed to ease the task of creating kernel image packages.
.
If you are simply trying to build third-party modules for your kernel,
you do not want this package. Install the appropriate linux-headers
package instead.

Do:

sudo apt-get install linux-headers-$(uname -r)
  • But in the makefile I have to give the path of the source, so? – gpuguy Sep 15 '14 at 9:33
  • @gpuguy The headers package installs files in /usr/src. Once installed, do dpkg -L linux-headers-$(uname -r) to check. – muru Sep 15 '14 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.