| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# |
| 4 |
# template script for generating Void Linux container for LXC |
| 5 |
# |
| 6 |
|
| 7 |
# |
| 8 |
# lxc: linux Container library |
| 9 |
|
| 10 |
# Authors: |
| 11 |
# Gregor Reitzenstein <[email protected]> |
| 12 |
|
| 13 |
# Based on lxc-archlinux template by: |
| 14 |
# Alexander Vladimirov <[email protected]> |
| 15 |
# John Lane <[email protected]> |
| 16 |
|
| 17 |
# This library is free software; you can redistribute it and/or |
| 18 |
# modify it under the terms of the GNU Lesser General Public |
| 19 |
# License as published by the Free Software Foundation; either |
| 20 |
# version 2.1 of the License, or (at your option) any later version. |
| 21 |
|
| 22 |
# This library is distributed in the hope that it will be useful, |
| 23 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 25 |
# Lesser General Public License for more details. |
| 26 |
|
| 27 |
# You should have received a copy of the GNU Lesser General Public |
| 28 |
# License along with this library; if not, write to the Free Software |
| 29 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 30 |
|
| 31 |
# Utility functions |
| 32 |
|
| 33 |
# Check if array $2 contains item $1 |
| 34 |
containsElement() { |
| 35 |
local e |
| 36 |
for e in "${@:2}"; do [[ "$1" == "$e" ]] && return 0; done |
| 37 |
return 1 |
| 38 |
} |
| 39 |
|
| 40 |
# split comma-separated string into an array |
| 41 |
# ${1} - string to split |
| 42 |
# ${2} - separator (default is ",") |
| 43 |
# ${result} - result value on success |
| 44 |
split_string() { |
| 45 |
local ifs=${IFS} |
| 46 |
IFS="${2:-,}" |
| 47 |
read -ra result < <(echo "${1}") |
| 48 |
IFS=${ifs} |
| 49 |
return 0 |
| 50 |
} |
| 51 |
|
| 52 |
# Make sure the usual locations are in PATH |
| 53 |
export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin |
| 54 |
|
| 55 |
# defaults |
| 56 |
default_path="/var/lib/lxc" |
| 57 |
default_path="@LXCPATH@" |
| 58 |
shared_config="@LXCTEMPLATECONFIG@/voidlinux.common.conf" |
| 59 |
userns_config="@LXCTEMPLATECONFIG@/voidlinux.userns.conf" |
| 60 |
|
| 61 |
pkg_blacklist=("linux>=0" "e2fsprogs>=0" "btrfs-progs>=0" "xfsprogs>=0" "f2fs-tools>=0" "dosfstools>=0") |
| 62 |
base_packages=() |
| 63 |
for pkg in $(xbps-query -Mv --repository="https://repo-default.voidlinux.org/current/" -x base-system); do |
| 64 |
containsElement "$pkg" "${pkg_blacklist[@]}" || base_packages+=($pkg) |
| 65 |
done |
| 66 |
declare -a additional_packages |
| 67 |
|
| 68 |
copy_configuration() { |
| 69 |
mkdir -p "${config_path}" |
| 70 |
local config="${config_path}/config" |
| 71 |
echo "lxc.uts.name = ${name}" >> "${config}" |
| 72 |
grep -q "^lxc.rootfs.path" "${config}" 2>/dev/null \ |
| 73 |
|| echo "lxc.rootfs.path = ${rootfs_path}" >> "${config}" |
| 74 |
|
| 75 |
# Detect if were in a UserNS and include the right config |
| 76 |
if [ -z "${LXC_MAPPED_GID+x}" ] || [ -z "${LXC_MAPPED_UID+x}" ]; then |
| 77 |
echo "lxc.include = ${userns_config}" >> "${config}" |
| 78 |
else |
| 79 |
echo "lxc.include = ${shared_config}" >> "${config}" |
| 80 |
fi |
| 81 |
|
| 82 |
if [ $? -ne 0 ]; then |
| 83 |
echo "Failed to configure container" |
| 84 |
return 1 |
| 85 |
fi |
| 86 |
return 0 |
| 87 |
} |
| 88 |
|
| 89 |
install_void() { |
| 90 |
if ! yes | xbps-install -Sy -R https://repo-default.voidlinux.org/current -r "${rootfs_path}" "${base_packages[@]}" |
| 91 |
then |
| 92 |
echo "Failed to install container packages" |
| 93 |
return 1 |
| 94 |
fi |
| 95 |
} |
| 96 |
|
| 97 |
usage() { |
| 98 |
cat <<EOF |
| 99 |
usage: |
| 100 |
${1} -n|--name=<container_name> [-p|--path=<path>] [-a|--arch=<arch of the container>] |
| 101 |
[-r|--root_password=<root password>] [-P|--packages=<pkg1,pkg2,...>] [-h|--help] |
| 102 |
|
| 103 |
Mandatory args: |
| 104 |
-n,--name container name, used to as an identifier for that container from now on |
| 105 |
Optional args: |
| •106 |
-p,--path path to the directory where the container's directory will be created (${default_path}) |
| 107 |
--rootfs path for actual container rootfs, (${default_path}/rootfs) |
| 108 |
-P,--packages preinstall additional packages, comma-separated list |
| 109 |
-c,--config use specified pacman config when installing container packages |
| 110 |
-a,--arch use specified architecture instead of host's architecture |
| 111 |
-r,--root_password set container root password |
| 112 |
-h,--help print this help |
| 113 |
EOF |
| 114 |
return 0 |
| 115 |
} |
| 116 |
|
| 117 |
options=$(getopt -o hp:P:n:c:r: -l help,rootfs:,path:,packages:,name:,config:,root_password:,mapped-uid:,mapped-gid: -- "${@}") |
| 118 |
if [ ${?} -ne 0 ]; then |
| 119 |
usage "$(basename "${0}")" |
| 120 |
exit 1 |
| 121 |
fi |
| 122 |
eval set -- "${options}" |
| 123 |
|
| 124 |
while true |
| 125 |
do |
| 126 |
case "${1}" in |
| 127 |
-h|--help) usage "${0}" && exit 0;; |
| 128 |
-p|--path) path=${2}; shift 2;; |
| 129 |
-n|--name) name=${2}; shift 2;; |
| 130 |
-c|--config) config_path=${2}; shift 2;; |
| 131 |
--rootfs) rootfs_path=${2}; shift 2;; |
| 132 |
-P|--packages) additional_packages=${2}; shift 2;; |
| 133 |
-r|--root_password) root_passwd=${2}; shift 2;; |
| 134 |
--mapped-uid) LXC_MAPPED_UID=$2; shift 2;; |
| 135 |
--mapped-gid) LXC_MAPPED_GID=$2; shift 2;; |
| 136 |
--) shift 1; break ;; |
| 137 |
*) break ;; |
| 138 |
esac |
| 139 |
done |
| 140 |
|
| 141 |
if [ -z "${name}" ]; then |
| 142 |
echo "missing required 'name' parameter" |
| 143 |
exit 1 |
| 144 |
fi |
| 145 |
|
| 146 |
type xbps-install >/dev/null 2>&1 |
| 147 |
if [ ${?} -ne 0 ]; then |
| 148 |
echo "'xbps-install' command is missing." |
| 149 |
fi |
| 150 |
type xbps-query >/dev/null 2>&1 |
| 151 |
if [ ${?} -ne 0 ]; then |
| 152 |
echo "'xbps-query' command is missing." |
| 153 |
fi |
| 154 |
|
| •155 |
if [ -z "${path}" ]; then |
| 156 |
path="${default_path}" |
| 157 |
fi |
| 158 |
|
| 159 |
path="${path}/${name}" |
| 160 |
|
| 161 |
if [ -z "${rootfs_path}" ]; then |
| 162 |
rootfs_path="${path}/rootfs" |
| 163 |
fi |
| 164 |
config_path="${path}" |
| 165 |
|
| 166 |
revert() { |
| 167 |
echo "Interrupted, cleaning up" |
| 168 |
lxc-destroy -n "${name}" |
| 169 |
rm -rf "${path:?}/${name}" |
| 170 |
rm -rf "${default_path:?}/${name}" |
| 171 |
exit 1 |
| 172 |
} |
| 173 |
trap revert SIGHUP SIGINT SIGTERM |
| 174 |
|
| 175 |
copy_configuration |
| 176 |
if [ $? -ne 0 ]; then |
| 177 |
echo "Failed to write configuration file" |
| 178 |
rm -rf "${config_path}" |
| 179 |
exit 1 |
| 180 |
fi |
| 181 |
|
| 182 |
if [ ${#additional_packages[@]} -gt 0 ]; then |
| 183 |
split_string "${additional_packages}" |
| 184 |
base_packages+=(${result[@]}) |
| 185 |
fi |
| 186 |
|
| 187 |
mkdir -p "${rootfs_path}" |
| 188 |
install_void |
| 189 |
if [ ${?} -ne 0 ]; then |
| 190 |
echo "Failed to install Void Linux" |
| 191 |
rm -rf "${config_path}" "${path}" |
| 192 |
exit 1 |
| 193 |
fi |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
if [ -n "${root_passwd}" ]; then |
| 198 |
echo "root:${root_passwd}" | chroot "${rootfs_path}" chpasswd |
| 199 |
fi |
| 200 |
|
| 201 |
cat << EOF |
| 202 |
Void Linux Container ${name} has been successfully created. The configuration is |
| 203 |
stored in ${config_path}/config. Please refer to https://docs.voidlinux.org for |
| 204 |
information regarding Void Linux. |
| 205 |
EOF |