File: lxc-login.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright (c) 2026 pacman64
   6 #
   7 # Permission is hereby granted, free of charge, to any person obtaining a copy
   8 # of this software and associated documentation files (the "Software"), to deal
   9 # in the Software without restriction, including without limitation the rights
  10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11 # copies of the Software, and to permit persons to whom the Software is
  12 # furnished to do so, subject to the following conditions:
  13 #
  14 # The above copyright notice and this permission notice shall be included in
  15 # all copies or substantial portions of the Software.
  16 #
  17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23 # SOFTWARE.
  24 
  25 
  26 # lxc-login [options...] [container]
  27 #
  28 # Start the LXC container given and attach into its running LOGIN shell.
  29 #
  30 # The shell is attached using a `clean` environment, to avoid leaking
  31 # many details via environment variables.
  32 #
  33 # The options are, available both in single and double-dash versions
  34 #
  35 #   -h, -help    show this help message
  36 
  37 
  38 case "$1" in
  39     -h|--h|-help|--help)
  40         awk '/^# +lxc-login /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  41         exit 0
  42     ;;
  43 
  44     -n|--name)
  45         if [ $# -lt 2 ]; then
  46             printf "forgot the container name\n" >&2
  47             exit 1
  48         fi
  49         shift
  50     ;;
  51 
  52     --) shift ;;
  53 
  54     -*)
  55         printf "unsupported option '%s'\n" "$1" >&2
  56         exit 1
  57     ;;
  58 esac
  59 
  60 container="$1"
  61 if [ $# -eq 0 ] && command -v fzf > /dev/null; then
  62     container="$(lxc-ls -1 | fzf --reverse --height 50% --tmux 50%)"
  63     if [ -z "${container}" ]; then
  64         awk '/^# +lxc-login /, /^$/ { gsub(/^# ?/, ""); print }' "$0" >&2
  65         exit 1
  66     fi
  67 fi
  68 
  69 if [ -z "${container}" ]; then
  70     awk '/^# +lxc-login /, /^$/ { gsub(/^# ?/, ""); print }' "$0" >&2
  71     exit 1
  72 fi
  73 
  74 lxc-start "${container}" 2> /dev/null
  75 
  76 printf \
  77     "\n\033[7mentered\033[0m shell from LXC container \033[7m%s\033[0m\n\n" \
  78     "${container}" >&2
  79 
  80 lxc-attach \
  81     --clear-env \
  82     --set-var "LXC_CONTAINER_NAME=${container}" \
  83     --set-var "TERM=${TERM:-xterm}" \
  84     "${container}"
  85 res=$?
  86 
  87 printf \
  88     "\n\033[7mexited\033[0m shell from LXC container \033[7m%s\033[0m\n\n" \
  89     "${container}" >&2
  90 
  91 exit "${res}"