4 # template script for generating Void Linux container for LXC
8 # lxc: linux Container library
11 # Gregor Reitzenstein <dean4devil@paranoidlabs.org>
13 # Based on lxc-archlinux template by:
14 # Alexander Vladimirov <alexander.idkfa.vladimirov@gmail.com>
15 # John Lane <lxc@jelmail.com>
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.
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.
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
33 # Check if array $2 contains item $1
36 for e
in "${@:2}"; do [[ "$1" == "$e" ]] && return 0; done
40 # split comma-separated string into an array
41 # ${1} - string to split
42 # ${2} - separator (default is ",")
43 # ${result} - result value on success
47 read -ra result
< <(echo "${1}")
52 # Make sure the usual locations are in PATH
53 export PATH
=$PATH:/usr
/sbin
:/usr
/bin
:/sbin
:/bin
56 default_path
="/var/lib/lxc"
57 default_path
="@LXCPATH@"
58 shared_config
="@LXCTEMPLATECONFIG@/voidlinux.common.conf"
59 userns_config
="@LXCTEMPLATECONFIG@/voidlinux.userns.conf"
61 pkg_blacklist
=("linux>=0" "e2fsprogs>=0" "btrfs-progs>=0" "xfsprogs>=0" "f2fs-tools>=0" "dosfstools>=0")
63 for pkg
in $
(xbps-query
-Mv --repository="http://repo2.voidlinux.eu/current/" -x base-system
); do
64 containsElement
"$pkg" "${pkg_blacklist[@]}" || base_packages
+=($pkg)
66 declare -a additional_packages
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}"
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}"
79 echo "lxc.include = ${shared_config}" >> "${config}"
83 echo "Failed to configure container"
90 if ! yes | xbps-install
-Sy -R http
://repo2.voidlinux.eu
/current
-r "${rootfs_path}" "${base_packages[@]}"
92 echo "Failed to install container packages"
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]
104 -n,--name container name, used to as an identifier for that container from now on
106 -p,--path path to where the container rootfs 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
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}")"
122 eval set -- "${options}"
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 ;;
141 if [ -z "${name}" ]; then
142 echo "missing required 'name' parameter"
146 type xbps-install
>/dev
/null
2>&1
147 if [ ${?} -ne 0 ]; then
148 echo "'xbps-install' command is missing."
150 type xbps-query
>/dev
/null
2>&1
151 if [ ${?} -ne 0 ]; then
152 echo "'xbps-query' command is missing."
155 if [ -z "${rootfs_path}" ]; then
156 rootfs_path
="${path}/rootfs"
158 config_path
="${path}"
161 echo "Interrupted, cleaning up"
162 lxc-destroy
-n "${name}"
163 rm -rf "${path:?}/${name}"
164 rm -rf "${default_path:?}/${name}"
167 trap revert SIGHUP SIGINT SIGTERM
170 if [ $?
-ne 0 ]; then
171 echo "Failed to write configuration file"
172 rm -rf "${config_path}"
176 if [ ${#additional_packages[@]} -gt 0 ]; then
177 split_string
"${additional_packages}"
178 base_packages
+=(${result[@]})
181 mkdir
-p "${rootfs_path}"
183 if [ ${?} -ne 0 ]; then
184 echo "Failed to install Void Linux"
185 rm -rf "${config_path}" "${path}"
191 if [ -n "${root_passwd}" ]; then
192 echo "root:${root_passwd}" | chroot
"${rootfs_path}" chpasswd
196 Void Linux Container ${name} has been successfully created. The configuration is
197 stored in ${config_path}/config. Please refer to https://wiki.voidlinux.eu for
198 information regarding Void Linux.