~hacktivista/scripts

6707633dee8283116d7954b12415282f9aef530d — Felix Freeman 1 year, 4 months ago f80c9cc
LXD helpers
1 files changed, 39 insertions(+), 0 deletions(-)

A lxd-h
A lxd-h => lxd-h +39 -0
@@ 0,0 1,39 @@
#!/bin/sh
#
# LXD helpers
#
# Usage: ./lxd-h <function> <param>
#
# Requirements:
# - LXD
#
# Copyright 2021 Felix Freeman <libsys@hacktivista.org>
#
# This software is licensed under the 'MIT No Attribution' license terms. I
# don't want attribution nor exclusive rights over it, but I'd love that you
# free your software too.

iface_ip4 () {
    IFACE=${1:-eth0}
    ip addr show $IFACE | sed -nEe '3{ s/\s+inet (([0-9]{1,3}\.?){4}).*/\1/p }'
}
iface_ip6() {
    IFACE=${1:-eth0}
    ip addr show $IFACE | sed -nEe '5{ s/\s+inet6 (([0-9a-f]{,4}:?){8}).*/\1/p }'
}
lxc_container_ip4 () {
    INSTANCE="$1"
    lxc list "$INSTANCE" -c4 -fcsv | awk '{print $1}'
}
lxc_container_ip6 () {
    INSTANCE="$1"
    lxc list "$INSTANCE" -c6 -fcsv | awk '{print $1}'
}
lxc_container_fix_ip4 () {
    INSTANCE="$1"
    lxc config device override "$INSTANCE" eth0 >/dev/null
    lxc config device set "$INSTANCE" eth0 ipv4.address $(lxc_container_ip4 "$INSTANCE") >/dev/null
}
cmd=$1
shift
$cmd "$@"