File: lxc-mac.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-mac [options...] [container name...] [extra packages...]
  27 #
  28 #
  29 # LXC Make (custom) Alpine Container does what it says, installing all the
  30 # package names given (if any).
  31 #
  32 # When not given, this tool asks for the name of the container to make, along
  33 # with which Alpine Linux version/release to base it on: to automate these
  34 # manual steps, you can pass the name as the 1st argument, and pipe the version
  35 # from a simple run of `echo` or `printf`.
  36 #
  37 # While your hardware's architecture should auto-detect successfully, you
  38 # can always set it explicitly via this script's options.
  39 #
  40 # The options are, available both in single and double-dash versions
  41 #
  42 #   -h, -help           show this help message
  43 #   -amd64, -x86_64     use the `amd64` architecture
  44 #   -arm64, -aarch64    use the `arm64` architecture
  45 #   -riscv, -riscv64    use the `riscv64` architecture
  46 
  47 
  48 architecture=''
  49 case "$(arch)" in
  50     amd64|x86_64)
  51         architecture='amd64'
  52     ;;
  53     aarch64|arm64)
  54         architecture='arm64'
  55     ;;
  56     riscv64)
  57         architecture='riscv64'
  58     ;;
  59 esac
  60 
  61 case "$1" in
  62     -h|--h|-help|--help)
  63         awk '/^# +lxc-mac /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  64         exit 0
  65     ;;
  66 
  67     -amd64|--amd64|-x86|--x86|-x86_64|--x86_64)
  68         architecture='amd64'
  69         shift
  70     ;;
  71 
  72     -aarch64|--aarch64|-arm64|--arm64)
  73         architecture='arm64'
  74         shift
  75     ;;
  76 
  77     -riscv|--riscv|-riscv64|--riscv64)
  78         architecture='riscv64'
  79         shift
  80     ;;
  81 esac
  82 
  83 [ "$1" = '--' ] && shift
  84 
  85 if [ -z "${architecture}" ]; then
  86     awk '/^# +lxc-mac /, /^$/ { gsub(/^# ?/, ""); print }' "$0" >&2
  87     printf "\n" >&2
  88     printf "error: can't auto-detect which hardware architecture to use\n" >&2
  89     exit 1
  90 fi
  91 
  92 container="$1"
  93 [ $# -gt 0 ] && shift
  94 
  95 # ask for the container name, when not given
  96 while [ -z "${container}" ]; do
  97     printf "container name: "
  98     read -r container
  99 done
 100 
 101 # ensure line ends in case stdin is being piped-in
 102 printf "\n"
 103 
 104 lxc-create -n "${container}" -t download -- -a "${architecture}" -d alpine \
 105 || exit $?
 106 
 107 # ensure line ends in case stdin is being piped-in
 108 printf "\n"
 109 
 110 lxc-start "${container}"
 111 
 112 {
 113     # ensure the latest available stable versions
 114     # printf "apk update\n"
 115     # printf "apk upgrade\n"
 116 
 117     # add all packages mentioned
 118     for pkg in "$@"; do
 119         # printf "apk add %s\n" "${pkg}"
 120         printf "apk add --no-cache %s\n" "${pkg}"
 121     done
 122 
 123     # save a few megabytes
 124     # printf "rm /var/cache/apk/*\n"
 125 
 126     # quit the install session
 127     printf "exit\n"
 128 } \
 129 | lxc-attach "${container}"
 130 
 131 lxc-stop "${container}"