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 9, 2022
1 parent b5b0a6e commit acdad2c
Show file tree
Hide file tree
Showing 4 changed files with 176 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
70 changes: 70 additions & 0 deletions builder/Vagrantfile-ubuntu
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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/ubuntu2204"
when "arm64"
# unique 22.04 arm64 box available to date
# sha256sum c209ab013280d3cd26a344def60b7b19fbb427de904ea285057d94ca6ac82dd5
# - http://old-releases.ubuntu.com/releases/jammy/SHA256SUMS
# - http://old-releases.ubuntu.com/releases/jammy/ubuntu-22.04-live-server-arm64.iso
# - https://gitlab.com/jnh3/m1-baseboxes/-/blob/main/ubuntu/ubuntu-22.04-arm64.pkr.hcl#L11-16
config.vm.box = "jharoian3/ubuntu-22.04-arm64"
end

config.vm.hostname = VM_NAME
config.vm.synced_folder "./", "#{VM_SOURCE}"

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

setup_ssh(config)

config.vm.provision :shell, inline: "echo INFO: Starting Provision"

config.vm.provision :shell, inline: "ARCH=#{ARCH} #{VM_SOURCE}/builder/prepare-ubuntu.sh vagrant"

config.vm.provision :shell, inline: "echo INFO: Provision finished, now connect via ssh"
end

def setup_ssh(config)
config.ssh.extra_args = ["-t", "cd #{VM_SOURCE}; bash --login"]

# workaround for ubuntu 22.04 disabled ssh-rsa support
# - https://it-jog.com/khow/vag/vaguperror-ubuntu2204
if ARGV[0] == "up" then
config.ssh.insert_key = false
config.ssh.private_key_path = nil
config.ssh.username = "vagrant"
config.ssh.password = "vagrant"
end
config.vm.provision "shell", inline: <<-SHELL
sudo echo PubkeyAcceptedAlgorithms +ssh-rsa >> /etc/ssh/sshd_config
sudo sudo systemctl restart sshd
SHELL
end
81 changes: 81 additions & 0 deletions builder/prepare-ubuntu.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/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-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 acdad2c

Please sign in to comment.