Skip to content

Commit

Permalink
builder: vagrant: introduce Vagrantfile
Browse files Browse the repository at this point in the history
The contributor can make use of a VM box (arm64/amd64) using the
following Makefile rules: vagrant-up, vagrant-ssh, vagrant-halt and
vagrant-destroy.

One can override the ARCH env var:
make vagrant-up ARCH=arm64

Whether the override will work depends on the availability of online
boxes and compatibility of architecture and providers (vbox, parallels).

Tested in Darwin (M1) and Linux (amd64).
  • Loading branch information
geyslan committed Sep 8, 2022
1 parent b5b0a6e commit 7e7e035
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ selftest/*/*-static
selftest/*/*-dynamic
selftest/uprobe/ctest
selftest/uprobe/gotest
testing/tracee
testing/tracee
.vagrant*
24 changes: 23 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ HELPERS = ./helpers
CC = gcc
CLANG = clang
GO = go
VAGRANT = vagrant

ARCH := $(shell uname -m | sed 's/x86_64/amd64/g; s/aarch64/arm64/g')
HOSTOS = $(shell uname)
ARCH ?= $(shell uname -m | sed 's/x86_64/amd64/g; s/aarch64/arm64/g')

BTFFILE = /sys/kernel/btf/vmlinux
BPFTOOL = $(shell which bpftool || /bin/false)
Expand Down Expand Up @@ -180,6 +182,26 @@ helpers-test-static-run: libbpfgo-static
helpers-test-dynamic-run: libbpfgo-dynamic
sudo $(GO) test -v $(HELPERS)/...

# vagrant

VAGRANT_DIR = $(abspath ./builder)

.PHONY: vagrant-up
.PHONY: vagrant-destroy
.PHONY: vagrant-halt
.PHONY: vagrant-ssh

vagrant-up: .vagrant-up
vagrant-destroy: .vagrant-destroy
vagrant-halt: .vagrant-halt
vagrant-ssh: .vagrant-ssh

.vagrant-%:
VAGRANT_VAGRANTFILE=$(VAGRANT_DIR)/Vagrantfile.ubuntu \
ARCH=$(ARCH) \
HOSTOS=$(HOSTOS) \
$(VAGRANT) $*

# output

$(OUTPUT):
Expand Down
41 changes: 41 additions & 0 deletions builder/Vagrantfile.ubuntu
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
ARCH = ENV["ARCH"] || "amd64"
HOSTOS = ENV["HOSTOS"] || "undefined"

if ! %w(amd64 arm64).include?(ARCH) then
puts "ERROR: ARCH #{ARCH} not supported"
abort
end

if ! %w(Linux Darwin).include?(HOSTOS) then
puts "ERROR: HOSTOS #{HOSTOS} not supported"
abort
end

VM_NAME = "libbpfgo_#{ARCH}_vm"
VM_SOURCE = "/vagrant"

Vagrant.configure("2") do |config|
case ARCH
when "amd64"
config.vm.box = "generic/ubuntu2004"
when "arm64"
config.vm.box = "jeffnoxon/ubuntu-20.04-arm64"
end

config.vm.synced_folder "./", "#{VM_SOURCE}"
config.ssh.extra_args = ["-t", "cd #{VM_SOURCE}; bash --login"]

case HOSTOS
when "Linux"
config.vm.provider "virtualbox" do |vb|
vb.name = VM_NAME
end
when "Darwin"
config.vm.provider "parallels" do |prl|
prl.name = VM_NAME
end
end

config.vm.provision :shell, inline: "ARCH=#{ARCH} #{VM_SOURCE}/builder/prepare-ubuntu.sh vagrant"
config.vm.provision :shell, inline: "echo FINISHED provision: now connect via ssh"
end
80 changes: 80 additions & 0 deletions builder/prepare-ubuntu.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/bin/bash

#
# This shell script is meant to prepare a building/exec environment for libbpfgo.
#

die() {
echo "ERROR: ${*}"
exit 1
}

info() {
echo "INFO: ${*}"
}

USER="${1}"
[ -z "${USER}" ] && USER="root"
USERHOME=$(eval echo ~"${USER}")
[ -d "${USERHOME}" ] || die "directory ${USERHOME} doesn't exist"

[ -z "${GOVERSION}" ] && GOVERSION="1.17.13"
[ -z "${ARCH}" ] && ARCH="amd64"

CMDS="wget rm tar chown apt-get"
for cmd in ${CMDS}; do
command -v "${cmd}" >/dev/null 2>&1 || die "missing required tool ${cmd}"
done

install_go() {
info "Installing Go ${GOVERSION}"

local file="go${GOVERSION}.linux-${ARCH}.tar.gz"
local link="https://golang.org/dl/${file}"
sudo wget --progress=bar:force "${link}" -P /tmp || die "could not download ${link}"

local dest="/usr/local"
sudo rm -rf "${dest}/go"
sudo tar -C "${dest}" -xzf "/tmp/${file}" || die "could not untar ${file} in ${dest}"
sudo chown -R root:root "${dest}/go" || die "could not chown"

local home_profile="${USERHOME}/.profile"
echo "export PATH=${PATH}:${dest}/go/bin" | sudo tee -a "${home_profile}"

info "Go ${GOVERSION} installed"
}

install_pkgs() {
# silence 'dpkg-preconfigure: unable to re-open stdin: No such file or directory'
export DEBIAN_FRONTEND=noninteractive

sudo -E apt-get update || die "coud not update package list"
for pkg in "${@}"
do
info "Installing ${pkg}"
sudo -E apt-get install -y "${pkg}" || die "could not install ${pkg}"
info "${pkg} installed"
done
}

# startup

info "Starting preparation"

install_go

install_pkgs \
coreutils bsdutils findutils \
build-essential pkgconf \
llvm-12 clang-12 \
clang-format-12 \
linux-headers-generic linux-tools-"$(uname -r)" \
zlib1g-dev libelf-dev libbpf-dev

for tool in "clang" "llc" "llvm-strip" "clang-format"
do
sudo rm -f /usr/bin/${tool}
sudo ln -s /usr/bin/${tool}-12 /usr/bin/${tool}
done

info "Preparation finished"

0 comments on commit 7e7e035

Please sign in to comment.