arnaucube
arnaucube/configsPublic Self-hosted repository
configs/bin/box
bin/boxRaw
  1#!/usr/bin/env bash
  2#
  3# Launch Inucs containers to work in there.
  4#
  5# Steps to do before first run on a new machine:
  6#
  7# sudo incus admin init
  8#
  9# then download the ubuntu image:
 10# sudo incus launch images:ubuntu/24.04 test
 11#
 12# - create a managed network bridge:
 13# incus network create incusbr0 ipv4.address=auto ipv4.nat=true ipv6.address=none
 14# incus profile device set default eth0 network incusbr0
 15#
 16#
 17
 18set -e
 19
 20if [ -z "$1" ]; then
 21    echo "Error: no container name provided."
 22    echo "Usage: box <container_name> <profile_name(optional)>"
 23    exit 1
 24fi
 25
 26# Choose which container manager to use:
 27MNGR="sudo incus"
 28
 29CONTAINER_NAME="$1"
 30HOST_SHARE_DIR="/home/$USER/container_data/$CONTAINER_NAME"
 31CONTAINER_SHARE_DIR="/mnt/shared"
 32IMAGE="images:ubuntu/24.04"
 33
 34PROFILE_NAME=${2:-"default"} # if $2 is empty, use "default"
 35PROFILES_FILE="$HOME/bin/box_profiles.sh"
 36
 37# EXPOSE_PORTS="7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010"
 38EXPOSE_PORTS=""
 39
 40
 41if [ -z "$CONTAINER_NAME" ]; then
 42    echo "error: no container name provided."
 43    echo "Usage: ./box.sh <container_name> [profile_name]"
 44    exit 1
 45fi
 46
 47if [ ! -f "$PROFILES_FILE" ]; then
 48    echo "error: $PROFILES_FILE not found."
 49    exit 1
 50fi
 51
 52source "$PROFILES_FILE"
 53
 54if ! declare -f "profile_$PROFILE_NAME" > /dev/null; then
 55    echo "error: profile '$PROFILE_NAME' not found in $PROFILES_FILE."
 56    echo "Available profiles are:"
 57    grep -oP '(?<=profile_).*(?=\(\))' "$PROFILES_FILE"
 58    exit 1
 59fi
 60
 61echo "profile selected: [$PROFILE_NAME]"
 62
 63mkdir -p "$HOST_SHARE_DIR"
 64
 65echo "launch LXD container: $CONTAINER_NAME..."
 66# security.nesting=true allows Docker to run inside this container
 67$MNGR launch "$IMAGE" "$CONTAINER_NAME" -c security.nesting=true
 68
 69sleep 2 # wait for filesystem
 70
 71echo "set dns"
 72HOST_UID=$(id -u)
 73# map Host User to Container Root
 74# $MNGR config set "$CONTAINER_NAME" raw.idmap "both $HOST_UID 0"
 75# set dns to bypass host dns
 76#$MNGR exec "$CONTAINER_NAME" -- rm -f /etc/resolv.conf
 77#$MNGR exec "$CONTAINER_NAME" -- bash -c "echo 'nameserver 8.8.8.8' | tee /etc/resolv.conf"
 78
 79echo "restart container"
 80$MNGR restart "$CONTAINER_NAME"
 81$MNGR exec "$CONTAINER_NAME" -- true
 82
 83echo "configure shared directory (between container & host)"
 84$MNGR config device add "$CONTAINER_NAME" project_storage disk \
 85    source="$HOST_SHARE_DIR" \
 86    path="$CONTAINER_SHARE_DIR"
 87
 88echo "configure port forwarding"
 89for PORT in $EXPOSE_PORTS; do
 90    $MNGR config device add "$CONTAINER_NAME" "proxy${PORT}" proxy \
 91        listen=tcp:0.0.0.0:${PORT} \
 92        connect=tcp:127.0.0.1:${PORT}
 93done
 94
 95
 96$MNGR config device set $CONTAINER_NAME project_storage shift true
 97
 98echo "check internet"
 99if ! $MNGR exec "$CONTAINER_NAME" -- ping -c 1 8.8.8.8 > /dev/null; then
100    echo "error: no internet access inside the container."
101    echo "note: check out the 'NOTE' at the top of the script for instructions."
102    # remove container, since failed the launch
103    $MNGR stop $CONTAINER_NAME
104    $MNGR delete $CONTAINER_NAME
105    exit 1
106fi
107
108echo "apt update & apt install"
109$MNGR exec "$CONTAINER_NAME" -- bash -c "
110    $(cat "$PROFILES_FILE")
111
112    export DEBIAN_FRONTEND=noninteractive
113    export NEEDRESTART_MODE=a # 'a' for automatic restart
114
115    apt-get update && apt-get upgrade -y
116
117    if declare -f profile_$PROFILE_NAME > /dev/null; then
118        profile_$PROFILE_NAME
119    else
120        echo 'error: profile function $PROFILE_NAME not found.'
121        exit 1
122    fi
123"
124
125echo "setup complete"
126echo "----------------------------------------------------"
127echo "Shared Directory (Host): $HOST_SHARE_DIR"
128echo "Shared Directory (Container): $CONTAINER_SHARE_DIR"
129echo "----------------------------------------------------"
130echo "To enter the container, run: $MNGR shell $CONTAINER_NAME"
131echo "To stop the container, run: $MNGR stop $CONTAINER_NAME"
132echo "To remove the container, run: $MNGR delete $CONTAINER_NAME"