Debian – Get kernel source: apt-get install vs apt-get source

aptdebiankernellinuxsource

I'm looking forward to download a Linux Kernel to get to know how to modify it and how to compile it.

I am using Debian distribution and I'm interested in the Debian-modified Linux Kernel rather than in the vanilla Kernel form kernel.org.

Doing some research I found out there are mainly two ways for achiving this purpose:

  • Install source package (i.e. apt-get install linux-source-3.19)
  • Download source from binary package (i.e. apt-get source linux-image-3.19.0-trunk-amd64)

The first option will download the source tarball into /usr/src/linux-source-3.19.tar.xz and the later will download a source tarball (linux_3.19.1.orig.tar.xz), a patch (linux_3.19.1-1~exp1.debian.tar.xz) and a description file (linux_3.19.1-1~exp1.dsc). The latter will also unpack and extract everything into a 'linux-3.19.1' directory.

At first I thought both versions would result with the same code, as they have the same kernel version and patch level (based on the report of the apt-cache command). However, diff command reported differences when comparing the unpacked source from apt-get install with the unpacked source from apt-get source (for both patched and non-patched code).

When comparing apt-get install with apt-get source:

$ diff -rq apt-get-install/ apt-get-source/ | wc -l
253

$ diff -rq apt-get-install/ apt-get-source/ | grep "Only in"
Only in apt-get-install/arch/arm/boot/dts: sun7i-a20-bananapro.dts
Only in apt-get-install/arch/s390/include/asm: cmb.h.1
Only in apt-get-install/drivers/dma-buf: reservation.c.1
Only in apt-get-install/drivers/dma-buf: seqno-fence.c.1
Only in apt-get-install/drivers/gpu/drm/i915: i915_irq.c.1
Only in apt-get-install/drivers/scsi: constants.c.1
Only in apt-get-install/drivers/usb/gadget/function: f_acm.c.1
Only in apt-get-install/drivers/usb/gadget/function: f_ecm.c.1
Only in apt-get-install/drivers/usb/gadget/function: f_obex.c.1
Only in apt-get-install/drivers/usb/gadget/function: f_serial.c.1
Only in apt-get-install/drivers/usb/gadget/function: f_subset.c.1
Only in apt-get-install/include/linux: reservation.h.1
Only in apt-get-install/kernel: sys.c.1
Only in apt-get-install/lib: crc32.c.1
Only in apt-get-install/sound/soc: soc-cache.c.1

And when comparing apt-get install with apt-get source (+ patch):

$ diff -rq apt-get-install/ apt-get-source+patch/
Only in apt-get-install/arch/s390/include/asm: cmb.h.1
Only in apt-get-source+patch/: debian
Only in apt-get-install/drivers/dma-buf: reservation.c.1
Only in apt-get-install/drivers/dma-buf: seqno-fence.c.1
Only in apt-get-install/drivers/gpu/drm/i915: i915_irq.c.1
Only in apt-get-install/drivers/scsi: constants.c.1
Only in apt-get-install/drivers/usb/gadget/function: f_acm.c.1
Only in apt-get-install/drivers/usb/gadget/function: f_ecm.c.1
Only in apt-get-install/drivers/usb/gadget/function: f_obex.c.1
Only in apt-get-install/drivers/usb/gadget/function: f_serial.c.1
Only in apt-get-install/drivers/usb/gadget/function: f_subset.c.1
Only in apt-get-install/include/linux: reservation.h.1
Only in apt-get-install/kernel: sys.c.1
Only in apt-get-install/lib: crc32.c.1
Only in apt-get-source+patch/: .pc
Only in apt-get-install/sound/soc: soc-cache.c.1

I've found some links where both methods are mentioned but I couldn't get anything clear from those:

https://kernel-handbook.alioth.debian.org/ch-common-tasks.html#s-common-official
https://help.ubuntu.com/community/Kernel/Compile (Option B vs Alternate option B)

I would really appreciate if someone could tell me the differences and advise me which is the preferred option. Thank you.

Best Answer

In Debian terminology, when you run

apt-get source linux-image-3.19.0-trunk-amd64

(or the equivalent apt-get source linux), you're actually downloading and extracting the source package. This contains the upstream code (the kernel source code downloaded from kernel.org) and all the Debian packaging, including patches added to the kernel by the Debian kernel team.

When you run

apt-get install linux-source-3.19

you're actualling installing a binary package which happens to contain the source code of the Linux kernel with the Debian patches applied and none of the Debian packaging infrastructure.

The source package's name is just linux; apt-get source will convert any binary package name it is given into the corresponding source package name.

By the way, since experimental packages aren't upgraded automatically, you should make sure you've updated your copy of linux-source-3.19 and re-extracted it before comparing; the .dts file you're seeing in your diff was introduced in the latest update. The packages currently in the archive all contain this file.

The remaining differences are pretty much normal: as has been indicated in the comments, debian contains all the packaging and is only in the source package, .pc is used by quilt to keep track of the original files modified by patches, and is also only in the source package, and the .1 files are generated manpages, probably a side-effect of the kernel build, and therefore only appear in the binary package (but they shouldn't really be there).

The reference package is the source package, as obtained by apt-get source. This builds all the kernel binary packages, including linux-source-3.19 which you install with apt-get install. The latter is provided as a convenience for other packages which may need the kernel source; it's guaranteed to be in the same place all the time, unlike the source package which is just downloaded in the current directory at the time apt-get source is run.

As far as documentation goes, I'd follow the Debian documentation in the kernel handbook (section 4.5). Rebuilding the full Debian kernel as documented in section 4.2 which you linked to takes a very long time because it builds a number of variants.

Related Question