#!/bin/sh # The MIT License (MIT) # # Copyright (c) 2026 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # lxc-mac [options...] [container name...] [extra packages...] # # # LXC Make (custom) Alpine Container does what it says, installing all the # package names given (if any). # # When not given, this tool asks for the name of the container to make, along # with which Alpine Linux version/release to base it on: to automate these # manual steps, you can pass the name as the 1st argument, and pipe the version # from a simple run of `echo` or `printf`. # # While your hardware's architecture should auto-detect successfully, you # can always set it explicitly via this script's options. # # The options are, available both in single and double-dash versions # # -h, -help show this help message # -amd64, -x86_64 use the `amd64` architecture # -arm64, -aarch64 use the `arm64` architecture # -riscv, -riscv64 use the `riscv64` architecture architecture='' case "$(arch)" in amd64|x86_64) architecture='amd64' ;; aarch64|arm64) architecture='arm64' ;; riscv64) architecture='riscv64' ;; esac case "$1" in -h|--h|-help|--help) awk '/^# +lxc-mac /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; -amd64|--amd64|-x86|--x86|-x86_64|--x86_64) architecture='amd64' shift ;; -aarch64|--aarch64|-arm64|--arm64) architecture='arm64' shift ;; -riscv|--riscv|-riscv64|--riscv64) architecture='riscv64' shift ;; esac [ "$1" = '--' ] && shift if [ -z "${architecture}" ]; then awk '/^# +lxc-mac /, /^$/ { gsub(/^# ?/, ""); print }' "$0" >&2 printf "\n" >&2 printf "error: can't auto-detect which hardware architecture to use\n" >&2 exit 1 fi container="$1" [ $# -gt 0 ] && shift # ask for the container name, when not given while [ -z "${container}" ]; do printf "container name: " read -r container done # ensure line ends in case stdin is being piped-in printf "\n" lxc-create -n "${container}" -t download -- -a "${architecture}" -d alpine \ || exit $? # ensure line ends in case stdin is being piped-in printf "\n" lxc-start "${container}" { # ensure the latest available stable versions # printf "apk update\n" # printf "apk upgrade\n" # add all packages mentioned for pkg in "$@"; do # printf "apk add %s\n" "${pkg}" printf "apk add --no-cache %s\n" "${pkg}" done # save a few megabytes # printf "rm /var/cache/apk/*\n" # quit the install session printf "exit\n" } \ | lxc-attach "${container}" lxc-stop "${container}"