Last active
August 9, 2021 11:45
-
-
Save bluecmd/7db1165dc2fbb7d620e19460fd1984e6 to your computer and use it in GitHub Desktop.
Make your Bash shell VRF aware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 --------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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