Skip to content

Instantly share code, notes, and snippets.

@bluecmd
Last active August 9, 2021 11:45
Show Gist options
  • Save bluecmd/7db1165dc2fbb7d620e19460fd1984e6 to your computer and use it in GitHub Desktop.
Save bluecmd/7db1165dc2fbb7d620e19460fd1984e6 to your computer and use it in GitHub Desktop.
Make your Bash shell VRF aware
# System-wide .bashrc file for interactive bash(1) shells.
# To enable the settings / commands in this file for login shells as well,
# this file has to be sourced in /etc/profile.
# --------- CUT HERE ---------
# Default to management VRF if available
[ -z "${VRF}" ] && sudo -n change-vrf "mgmt" "$$" 2>/dev/null && export VRF="mgmt"
# --------- CUT HERE ---------
# If not running interactively, don't do anything
# [.. all the way to the end ..]
# --------- CUT HERE ---------
function _refresh_vrf {
VRF="$(ip vrf identify $$)"
debian_chroot="vrf:${VRF:-default}"
}
function vrf {
sudo change-vrf "${1?:Which VRF?}" "$$"
_refresh_vrf
}
_refresh_vrf
# --------- CUT HERE ---------
#!/usr/bin/env bash
# Usage: switch-vrf <VRF> <PID>
# Requires root (or a bunch of CAPs)
set -euo pipefail
VRF="${1:?Missing VRF name}"
PID="${2:?Missing PID}"
CGROUP="$(ip vrf exec "${VRF}" awk -F : '/^0/ {print $3}' /proc/self/cgroup)"
MNT="$(findmnt -nft cgroup2 -o target)"
echo "${PID}" >> "${MNT}/${CGROUP}/cgroup.procs"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment