]> git.proxmox.com Git - mirror_lxc.git/blob - templates/lxc-voidlinux.in
confile: lxc.rootfs --> lxc.rootfs.path
[mirror_lxc.git] / templates / lxc-voidlinux.in
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 <dean4devil@paranoidlabs.org>
12
13 # Based on lxc-archlinux template by:
14 # Alexander Vladimirov <alexander.idkfa.vladimirov@gmail.com>
15 # John Lane <lxc@jelmail.com>
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="http://repo2.voidlinux.eu/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 http://repo2.voidlinux.eu/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 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
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 "${rootfs_path}" ]; then
156 rootfs_path="${path}/rootfs"
157 fi
158 config_path="${path}"
159
160 revert() {
161 echo "Interrupted, cleaning up"
162 lxc-destroy -n "${name}"
163 rm -rf "${path:?}/${name}"
164 rm -rf "${default_path:?}/${name}"
165 exit 1
166 }
167 trap revert SIGHUP SIGINT SIGTERM
168
169 copy_configuration
170 if [ $? -ne 0 ]; then
171 echo "Failed to write configuration file"
172 rm -rf "${config_path}"
173 exit 1
174 fi
175
176 if [ ${#additional_packages[@]} -gt 0 ]; then
177 split_string "${additional_packages}"
178 base_packages+=(${result[@]})
179 fi
180
181 mkdir -p "${rootfs_path}"
182 install_void
183 if [ ${?} -ne 0 ]; then
184 echo "Failed to install Void Linux"
185 rm -rf "${config_path}" "${path}"
186 exit 1
187 fi
188
189
190
191 if [ -n "${root_passwd}" ]; then
192 echo "root:${root_passwd}" | chroot "${rootfs_path}" chpasswd
193 fi
194
195 cat << EOF
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.
199 EOF