Skip to content
This repository has been archived by the owner on Jan 31, 2022. It is now read-only.

Commit

Permalink
new: environment setup with chapter-2
Browse files Browse the repository at this point in the history
Signed-off-by: Lorenzo Fontana <lo@linux.com>
  • Loading branch information
fntlnz committed Dec 9, 2019
1 parent ab4c9f9 commit fd4b972
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1 +1,3 @@
.vagrant/
*.log
kernel/
26 changes: 20 additions & 6 deletions README.md
Expand Up @@ -8,9 +8,10 @@ This is the companion code repo for the book [Linux Observability with BPF](http
We believe that even if the examples included in the book were all tested and working when we wrote them, human error is possible and technology changes.
For that reason, the purpose of this repo is to keep them as updated as possible and correct mistakes we made while writing the book.


**Nota Bene**: All the examples in this repository are adapted from the book to assume that you use the Vagrant environment we provide.
Examples can be slightly different in this repository because of that. The reason is that we didn't want to couple the book itself to Vagrant as a tool.
If you don't want a Vagrant based environment, make sure you have: [bcc](https://github.com/iovisor/bcc/blob/master/INSTALL.md) and [clang](https://clang.llvm.org/)


## Vagrant Environment setup

Expand All @@ -29,25 +30,38 @@ cd linux-observability-with-bpf
vagrant up
```

This Vagrant command, will start an Ubuntu VM in Virtualbox, you can SSH into the machine using:
This Vagrant command, will start a Fedora 30 VM in Virtualbox, you can SSH into the machine using:

```
vagrant ssh
```

Before going on, make sure you download the kernel source tree in this repository. It is needed as a dependency for some examples.
We will be downloading the code for Kernel 5.0.9 - We are avoiding a git clone here because the Git history of the kernel is very big.

In the machine:

```bash
cd /tmp
wget -c https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.0.9.tar.gz -O - | tar -xz
sudo mv linux-5.0.9 /kernel-src
```

Please note: we could've done this in the Vagrant machine

Now that you are in the machine, you can `cd` into the `/vagrant` folder, you'll find this
repository in that folder since it's shared between the VM and your Computer.
repository in that folder since it's **one-time synced** between the VM and your Computer.

```
cd /vagrant
```

Now, you can

Now, you can follow the following code examples.

## Code examples

Click on each example to follow the setup instructions:
Click on each example to follow the setup instructions.


### Chapter 2

Expand Down
13 changes: 7 additions & 6 deletions Vagrantfile
Expand Up @@ -4,22 +4,23 @@
VAGRANTFILE_API_VERSION = "2"

$bootstrap=<<SCRIPT
apt-get install bpfcc-tools linux-headers-$(uname -r)
dnf install elfutils-libelf-devel wget tar clang bcc -y
SCRIPT

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
num_machines = 2
net_ip = "192.168.33.10"

config.vm.define "bpfbook" do |net|
net.vm.box = "ubuntu/eoan64"
config.vm.box = "fedora/30-cloud-base"
config.vm.box_version = "30.20190425.0"
net_index = 1
net.vm.hostname = "bpfbook"
net.vm.provider "virtualbox" do |vb|
config.vm.hostname = "bpfbook"
config.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--memory", "1024"]
end
net.vm.network :private_network, ip: "#{net_ip}"
net.vm.provision :shell, inline: $bootstrap, :args => "#{net_ip}"
config.vm.network :private_network, ip: "#{net_ip}"
config.vm.provision :shell, inline: $bootstrap, :args => "#{net_ip}"
end
end

2 changes: 1 addition & 1 deletion code/chapter-2/hello_world/compile-bpf.sh 100644 → 100755
@@ -1,4 +1,4 @@
#!/bin/bash
clang -O2 -target bpf \
-c bpf_program.c \
-c bpf_program.c \
-o bpf_program.o
13 changes: 8 additions & 5 deletions code/chapter-2/hello_world/compile-loader.sh 100644 → 100755
@@ -1,10 +1,13 @@
#!/bin/bash
TOOLS=../../../tools
INCLUDE=../../../libbpf/include
HEADERS=../../../libbpf/src
clang -o loader -l elf \

TOOLS=/kernel-src/samples/bpf
INCLUDE=/kernel-src/tools/lib
PERF_INCLUDE=/kernel-src/tools/perf
KERNEL_TOOLS_INCLUDE=/kernel-src/tools/include/
clang -o loader -lelf\
-I${INCLUDE} \
-I${HEADERS} \
-I${PERF_INCLUDE} \
-I${KERNEL_TOOLS_INCLUDE} \
-I${TOOLS} \
${TOOLS}/bpf_load.c \
loader.c
1 change: 0 additions & 1 deletion code/chapter-2/hello_world/loader.c
@@ -1,6 +1,5 @@
#include "bpf_load.h"
#include <stdio.h>
#include <uapi/linux/bpf.h>

int main(int argc, char **argv) {
if (load_bpf_file("hello_world_kern.o") != 0) {
Expand Down

0 comments on commit fd4b972

Please sign in to comment.