2013-09-19 21:15:56 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2014-03-09 06:49:06 +00:00
|
|
|
# This is a modified version of /usr/share/lxc/templates/lxc-download
|
|
|
|
# that comes with ubuntu-lxc 1.0.0 stable from ppa changed to suit vagrant-lxc needs
|
2013-09-19 21:15:56 +00:00
|
|
|
#
|
2014-03-09 06:49:06 +00:00
|
|
|
# Copyright © 2014 Stéphane Graber <stgraber@ubuntu.com>
|
|
|
|
# Copyright © 2014 Fábio Rehm <fgrehm@gmail.com>
|
2013-09-19 21:15:56 +00:00
|
|
|
#
|
2014-03-09 06:49:06 +00:00
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
|
|
|
|
# USA
|
|
|
|
|
|
|
|
set -eu
|
|
|
|
|
|
|
|
LXC_HOOK_DIR="/usr/share/lxc/hooks"
|
|
|
|
LXC_TEMPLATE_CONFIG="/usr/share/lxc/config"
|
|
|
|
|
|
|
|
LXC_MAPPED_GID=
|
|
|
|
LXC_MAPPED_UID=
|
|
|
|
LXC_NAME=
|
|
|
|
LXC_PATH=
|
|
|
|
LXC_ROOTFS=
|
|
|
|
LXC_TARBALL=
|
|
|
|
LXC_CONFIG=
|
|
|
|
|
|
|
|
usage() {
|
|
|
|
cat <<EOF
|
|
|
|
vagrant-lxc default template
|
2013-09-19 21:15:56 +00:00
|
|
|
|
2014-03-09 06:49:06 +00:00
|
|
|
Required arguments:
|
|
|
|
[ --tarball <path> ]: The full path of the rootfs tarball
|
2013-09-19 21:15:56 +00:00
|
|
|
|
2014-03-09 06:49:06 +00:00
|
|
|
Optional arguments:
|
|
|
|
[ --config ]: Configuration file to be used when building the container
|
|
|
|
[ -h | --help ]: This help message
|
2013-09-19 21:15:56 +00:00
|
|
|
|
2014-03-09 06:49:06 +00:00
|
|
|
LXC internal arguments (do not pass manually!):
|
|
|
|
[ --name <name> ]: The container name
|
|
|
|
[ --path <path> ]: The path to the container
|
|
|
|
[ --rootfs <rootfs> ]: The path to the container's rootfs
|
|
|
|
[ --mapped-uid <map> ]: A uid map (user namespaces)
|
|
|
|
[ --mapped-gid <map> ]: A gid map (user namespaces)
|
2013-09-19 21:15:56 +00:00
|
|
|
EOF
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2014-03-09 06:49:06 +00:00
|
|
|
options=$(getopt -o h -l tarball:,config:,help:,name:,path:,rootfs:,mapped-uid:,mapped-gid: -- "$@")SS
|
|
|
|
|
2013-09-19 21:15:56 +00:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
usage $(basename $0)
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
eval set -- "$options"
|
|
|
|
|
|
|
|
while true
|
|
|
|
do
|
|
|
|
case "$1" in
|
2014-03-09 06:49:06 +00:00
|
|
|
-h|--help) usage $0 && exit 0;;
|
|
|
|
--config) LXC_CONFIG=$2; shift 2;;
|
|
|
|
--tarball) LXC_TARBALL=$2; shift 2;;
|
|
|
|
--name) LXC_NAME=$2; shift 2;;
|
|
|
|
--path) LXC_PATH=$2; shift 2;;
|
|
|
|
--rootfs) LXC_ROOTFS=$2; shift 2;;
|
|
|
|
--mapped-uid) LXC_MAPPED_UID=$2; shift 2;;
|
|
|
|
--mapped-gid) LXC_MAPPED_GID=$2; shift 2;;
|
|
|
|
*) break;;
|
2013-09-19 21:15:56 +00:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2014-03-09 06:49:06 +00:00
|
|
|
if [ -z "${LXC_NAME}" ]; then
|
|
|
|
echo "'name' parameter is required"
|
|
|
|
exit 1
|
2013-09-19 21:15:56 +00:00
|
|
|
fi
|
|
|
|
|
2014-03-09 06:49:06 +00:00
|
|
|
if [ -z "${LXC_TARBALL}" ]; then
|
|
|
|
echo "'tarball' parameter is required"
|
|
|
|
exit 1
|
2013-09-19 21:15:56 +00:00
|
|
|
fi
|
|
|
|
|
2014-03-09 06:49:06 +00:00
|
|
|
if [ -z "${LXC_ROOTFS}" ]; then
|
|
|
|
echo "'rootfs' parameter is required"
|
2013-09-19 21:15:56 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2014-03-09 06:49:06 +00:00
|
|
|
if [ -z "${LXC_CONFIG}" ]; then
|
|
|
|
echo "'config' parameter is required"
|
2013-09-19 21:15:56 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2014-03-09 06:49:06 +00:00
|
|
|
if [ -z "${LXC_PATH}" ]; then
|
|
|
|
echo "'path' parameter is required"
|
2013-09-19 21:15:56 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2014-03-09 06:49:06 +00:00
|
|
|
# Unpack the rootfs
|
|
|
|
echo "Unpacking the rootfs"
|
2013-09-19 21:15:56 +00:00
|
|
|
|
2014-03-10 01:59:18 +00:00
|
|
|
(
|
|
|
|
flock -x 200
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo "Cache repository is busy."
|
2014-03-11 13:03:12 +00:00
|
|
|
exit 1
|
2014-03-10 01:59:18 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
mkdir -p ${LXC_ROOTFS}
|
|
|
|
(cd ${LXC_ROOTFS} && tar xfz ${LXC_TARBALL} --strip-components=2)
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo "Failed to extract rootfs"
|
2014-03-11 13:03:12 +00:00
|
|
|
exit 1
|
2014-03-10 01:59:18 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
) 200>/var/lock/subsys/lxc
|
2013-09-19 21:15:56 +00:00
|
|
|
|
2014-03-09 06:49:06 +00:00
|
|
|
mkdir -p ${LXC_ROOTFS}/dev/pts/
|
|
|
|
|
|
|
|
## Extract all the network config entries
|
|
|
|
sed -i -e "/lxc.network/{w ${LXC_PATH}/config-network" -e "d}" \
|
|
|
|
${LXC_PATH}/config
|
|
|
|
|
|
|
|
## Extract any other config entry
|
|
|
|
sed -i -e "/lxc./{w ${LXC_PATH}/config-auto" -e "d}" ${LXC_PATH}/config
|
|
|
|
|
|
|
|
## Add the container-specific config
|
|
|
|
echo "" >> ${LXC_PATH}/config
|
|
|
|
echo "##############################################" >> ${LXC_PATH}/config
|
|
|
|
echo "# Container specific configuration (automatically set)" >> ${LXC_PATH}/config
|
|
|
|
if [ -e "${LXC_PATH}/config-auto" ]; then
|
|
|
|
cat ${LXC_PATH}/config-auto >> ${LXC_PATH}/config
|
|
|
|
rm ${LXC_PATH}/config-auto
|
|
|
|
fi
|
|
|
|
echo "lxc.utsname = ${LXC_NAME}" >> ${LXC_PATH}/config
|
|
|
|
|
|
|
|
## Re-add the previously removed network config
|
|
|
|
if [ -e "${LXC_PATH}/config-network" ]; then
|
|
|
|
echo "" >> ${LXC_PATH}/config
|
|
|
|
echo "##############################################" >> ${LXC_PATH}/config
|
|
|
|
echo "# Network configuration (automatically set)" >> ${LXC_PATH}/config
|
|
|
|
cat ${LXC_PATH}/config-network >> ${LXC_PATH}/config
|
|
|
|
rm ${LXC_PATH}/config-network
|
2013-09-19 21:15:56 +00:00
|
|
|
fi
|
|
|
|
|
2014-03-09 06:49:06 +00:00
|
|
|
## Append the defaults
|
|
|
|
echo "" >> ${LXC_PATH}/config
|
|
|
|
echo "##############################################" >> ${LXC_PATH}/config
|
|
|
|
echo "# vagrant-lxc base box specific configuration" >> ${LXC_PATH}/config
|
|
|
|
cat ${LXC_CONFIG} >> ${LXC_PATH}/config
|
|
|
|
|
|
|
|
# Empty section for lxc.customize calls from vagrantfile
|
|
|
|
echo "" >> ${LXC_PATH}/config
|
|
|
|
echo "##############################################" >> ${LXC_PATH}/config
|
|
|
|
echo "# vagrant-lxc container specific configuration" >> ${LXC_PATH}/config
|
2013-09-19 21:15:56 +00:00
|
|
|
|
2014-03-09 06:49:06 +00:00
|
|
|
exit 0
|