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 82 --) shift ;; 83 84 -*) 85 printf "unsupported option '%s'\n" "$1" >&2 86 exit 1 87 ;; 88 esac 89 90 if [ -z "${architecture}" ]; then 91 awk '/^# +lxc-mac /, /^$/ { gsub(/^# ?/, ""); print }' "$0" >&2 92 printf "\n" >&2 93 printf "error: can't auto-detect which hardware architecture to use\n" >&2 94 exit 1 95 fi 96 97 container="$1" 98 [ $# -gt 0 ] && shift 99 100 # ask for the container name, when not given 101 while [ -z "${container}" ]; do 102 printf "container name: " 103 read -r container 104 done 105 106 # ensure line ends in case stdin is being piped-in 107 printf "\n" 108 109 lxc-create -n "${container}" -t download -- -a "${architecture}" -d alpine \ 110 || exit $? 111 112 # ensure line ends in case stdin is being piped-in 113 printf "\n" 114 115 lxc-start "${container}" 116 117 { 118 # ensure the latest available stable versions 119 # printf "apk update\n" 120 # printf "apk upgrade\n" 121 122 # add all packages mentioned 123 for pkg in "$@"; do 124 # printf "apk add %s\n" "${pkg}" 125 printf "apk add --no-cache %s\n" "${pkg}" 126 done 127 128 # save a few megabytes 129 # printf "rm /var/cache/apk/*\n" 130 131 # quit the install session 132 printf "exit\n" 133 } \ 134 | lxc-attach "${container}" 135 136 lxc-stop "${container}"