summaryrefslogtreecommitdiff
path: root/md/writeup/compile_linux_kernel.md
blob: 0e07311380c7f8bdf27e747cd8dcbf55ec10a637 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
title: Compile Linux kernel

# Compile Linux kernel

## Getting sources

To get main repo kernel

Default kernel is located here

```sh
git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
```

There is whole list of different kernels maintainer ones and much
more.

```
https://git.kernel.org/cgit/
```

some distros have their own kernels. Here whole list of kernels
based on ubuntu version

```
http://kernel.ubuntu.com/git/
```

And also there is little different native kernel building ways for some distros

## Config

Easyes way startup config is just to get whatever you running now. If you hw works good
with current distro then use current kernel config its is stored in procfs _/proc/config.gz_.
Lets go to our kernel repo and do

```sh
zcat /proc/config.gz > .config
```
now you are ready to compile kernel that will work at begining

### Making yout own config

When you making you own optimised config you need to check your current hardware settup. 
If you whant minimal kernel there is two options. Just remove hardware that will be newer
used. Like if you have have laptop only with intel video card then everything that realted to 
other video cards could be removed. Second approach is to make only bare minimum without
kernel modules. Here is interesting. You get you runnung kernel. And plug all deiveces that 
you whant to work. Drivers will be autoloaded for this devices and you staticly compile them in.
And you will able to run your stuff only with staticly compiled kernel without loadable modules.

List all loaded modules

```sh
lsmod
```

See all conntected USB devices

```sh
lsusb
```

See all PCI devices and modules that they are using

```sh
lspci -k
```

This bare minimum of command that you whant to run to start modifing your kernel.

## Compiling

Compile kernel on local machine for local machine

Run to configure kernel
```sh
make menuconfig
```

config is saved in _.config_ file. And now we are ready to
compile our first kernel

```sh
make
```

### ARM64

Compile kernel for arm64

```sh
ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make defconfig
```

```sh
ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make 
```

#### Raspberry Pi 4

Clone the official kernel 

```sh
git clone --depth=1 https://github.com/raspberrypi/linux
```


```sh
cd linux
KERNEL=kernel8
ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-  make bcm2711_defconfig
```

```sh
ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make -j4 Image.gz modules dtbs
```


## Install 

It depends from distro to distro expected way how to install new/fresh/clean kernel

### ArchLinux

Install modules. They all will go /lib/modules/`your kernel version`

```sh
make modules_install
```

Compy compiled kernel to boot directory

```sh
cp -v arch/x86_64/boot/bzImage /boot/vmlinuz-linux318
```

Create initram image

```sh
mkinitcpio -k <kernelversion> -g /boot/initramfs-<file name>.img
```

All of this should be enought to run kernel with qemu

<!--
## Compile module
-->

### Raspberry Pi 4

```sh
make -j4 Image.gz modules dtbs
sudo make modules_install
sudo cp arch/arm64/boot/dts/broadcom/*.dtb /boot/
sudo cp arch/arm64/boot/dts/overlays/*.dtb* /boot/overlays/
sudo cp arch/arm64/boot/dts/overlays/README /boot/overlays/
sudo cp arch/arm64/boot/Image.gz /boot/kernel8.img
```

## Linux patches


### Grsecurity

Linux security enhancments

https://grsecurity.net/

Latest patches could be downloaded from https://grsecurity.net/download.php#test

Apply patch
```sh
cd linux-4.7.10
patch -p1 < ../grsecurity-3.1-4.7.10-201610222037.patch
```

Should work without troubles

### Tomoyo

MAC based securty mechanism

http://tomoyo.osdn.jp/

## Links

1. https://git.kernel.org/cgit/
2. https://wiki.ubuntu.com/Kernel/SourceCode
3. https://wiki.ubuntu.com/Kernel/BuildYourOwnKernel
4. https://fedoraproject.org/wiki/Building_a_custom_kernel
5. https://wiki.archlinux.org/index.php/Kernels/Traditional_compilation
6. https://www.raspberrypi.com/documentation/computers/linux_kernel.html