Commit f4891287edf4283fcd8b4aa8e1cbd895ab34895b
1 parent
1dd62c4d
Exists in
master
and in
90 other branches
replace lxc-template with one copied from debian-wheezy box
Showing
1 changed file
with
182 additions
and
127 deletions
Show diff stats
utils/centos6-vagrant-lxc/lxc-template
| 1 | #!/bin/bash | 1 | #!/bin/bash |
| 2 | 2 | ||
| 3 | -# This is a modified version of /usr/share/lxc/templates/lxc-download | ||
| 4 | -# that comes with ubuntu-lxc 1.0.0 stable from ppa changed to suit vagrant-lxc needs | 3 | +# This is a modified version of /usr/share/lxc/templates/lxc-ubuntu |
| 4 | +# that comes with Ubuntu 13.04 changed to suit vagrant-lxc needs | ||
| 5 | + | ||
| 5 | # | 6 | # |
| 6 | -# Copyright © 2014 Stéphane Graber <stgraber@ubuntu.com> | ||
| 7 | -# Copyright © 2014 Fábio Rehm <fgrehm@gmail.com> | 7 | +# template script for generating ubuntu container for LXC |
| 8 | +# | ||
| 9 | +# This script consolidates and extends the existing lxc ubuntu scripts | ||
| 10 | +# | ||
| 11 | + | ||
| 12 | +# Copyright © 2011 Serge Hallyn <serge.hallyn@canonical.com> | ||
| 13 | +# Copyright © 2010 Wilhelm Meier | ||
| 14 | +# Author: Wilhelm Meier <wilhelm.meier@fh-kl.de> | ||
| 15 | +# | ||
| 16 | +# This program is free software; you can redistribute it and/or modify | ||
| 17 | +# it under the terms of the GNU General Public License version 2, as | ||
| 18 | +# published by the Free Software Foundation. | ||
| 19 | + | ||
| 20 | +# This program is distributed in the hope that it will be useful, | ||
| 21 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 22 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 23 | +# GNU General Public License for more details. | ||
| 24 | + | ||
| 25 | +# You should have received a copy of the GNU General Public License along | ||
| 26 | +# with this program; if not, write to the Free Software Foundation, Inc., | ||
| 27 | +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 8 | # | 28 | # |
| 9 | -# This library is free software; you can redistribute it and/or | ||
| 10 | -# modify it under the terms of the GNU Lesser General Public | ||
| 11 | -# License as published by the Free Software Foundation; either | ||
| 12 | -# version 2.1 of the License, or (at your option) any later version. | ||
| 13 | - | ||
| 14 | -# This library is distributed in the hope that it will be useful, | ||
| 15 | -# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 17 | -# Lesser General Public License for more details. | ||
| 18 | - | ||
| 19 | -# You should have received a copy of the GNU Lesser General Public | ||
| 20 | -# License along with this library; if not, write to the Free Software | ||
| 21 | -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 | ||
| 22 | -# USA | ||
| 23 | - | ||
| 24 | -set -eu | ||
| 25 | - | ||
| 26 | -LXC_HOOK_DIR="/usr/share/lxc/hooks" | ||
| 27 | -LXC_TEMPLATE_CONFIG="/usr/share/lxc/config" | ||
| 28 | - | ||
| 29 | -LXC_MAPPED_GID= | ||
| 30 | -LXC_MAPPED_UID= | ||
| 31 | -LXC_NAME= | ||
| 32 | -LXC_PATH= | ||
| 33 | -LXC_ROOTFS= | ||
| 34 | -LXC_TARBALL= | ||
| 35 | -LXC_CONFIG= | ||
| 36 | - | ||
| 37 | -usage() { | ||
| 38 | - cat <<EOF | ||
| 39 | -vagrant-lxc default template | ||
| 40 | 29 | ||
| 41 | -Required arguments: | ||
| 42 | -[ --tarball <path> ]: The full path of the rootfs tarball | 30 | +set -e |
| 43 | 31 | ||
| 44 | -Optional arguments: | ||
| 45 | -[ --config ]: Configuration file to be used when building the container | ||
| 46 | -[ -h | --help ]: This help message | 32 | +if [ -r /etc/default/lxc ]; then |
| 33 | + . /etc/default/lxc | ||
| 34 | +fi | ||
| 35 | + | ||
| 36 | +extract_rootfs() | ||
| 37 | +{ | ||
| 38 | + tarball=$1 | ||
| 39 | + arch=$2 | ||
| 40 | + rootfs=$3 | ||
| 41 | + | ||
| 42 | + echo "Extracting $tarball ..." | ||
| 43 | + mkdir -p $rootfs | ||
| 44 | + (cd $rootfs && tar xfz $tarball --strip-components=2) | ||
| 45 | + return 0 | ||
| 46 | +} | ||
| 47 | + | ||
| 48 | +install_ubuntu() | ||
| 49 | +{ | ||
| 50 | + rootfs=$1 | ||
| 51 | + release=$2 | ||
| 52 | + tarball=$3 | ||
| 53 | + mkdir -p /var/lock/subsys/ | ||
| 54 | + | ||
| 55 | + ( | ||
| 56 | + flock -x 200 | ||
| 57 | + if [ $? -ne 0 ]; then | ||
| 58 | + echo "Cache repository is busy." | ||
| 59 | + return 1 | ||
| 60 | + fi | ||
| 61 | + | ||
| 62 | + extract_rootfs $tarball $arch $rootfs | ||
| 63 | + if [ $? -ne 0 ]; then | ||
| 64 | + echo "Failed to copy rootfs" | ||
| 65 | + return 1 | ||
| 66 | + fi | ||
| 67 | + | ||
| 68 | + return 0 | ||
| 69 | + | ||
| 70 | + ) 200>/var/lock/subsys/lxc | ||
| 71 | + | ||
| 72 | + return $? | ||
| 73 | +} | ||
| 74 | + | ||
| 75 | +copy_configuration() | ||
| 76 | +{ | ||
| 77 | + path=$1 | ||
| 78 | + rootfs=$2 | ||
| 79 | + name=$3 | ||
| 80 | + | ||
| 81 | + grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config | ||
| 82 | + | ||
| 83 | + # if there is exactly one veth network entry, make sure it has an | ||
| 84 | + # associated hwaddr. | ||
| 85 | + nics=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l` | ||
| 86 | + if [ $nics -eq 1 ]; then | ||
| 87 | + grep -q "^lxc.network.hwaddr" $path/config || sed -i -e "/^lxc\.network\.type[ \t]*=[ \t]*veth/a lxc.network.hwaddr = 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')" $path/config | ||
| 88 | + fi | ||
| 89 | + | ||
| 90 | + if [ $? -ne 0 ]; then | ||
| 91 | + echo "Failed to add configuration" | ||
| 92 | + return 1 | ||
| 93 | + fi | ||
| 47 | 94 | ||
| 48 | -LXC internal arguments (do not pass manually!): | ||
| 49 | -[ --name <name> ]: The container name | ||
| 50 | -[ --path <path> ]: The path to the container | ||
| 51 | -[ --rootfs <rootfs> ]: The path to the container's rootfs | ||
| 52 | -[ --mapped-uid <map> ]: A uid map (user namespaces) | ||
| 53 | -[ --mapped-gid <map> ]: A gid map (user namespaces) | ||
| 54 | -EOF | ||
| 55 | return 0 | 95 | return 0 |
| 56 | } | 96 | } |
| 57 | 97 | ||
| 58 | -options=$(getopt -o h -l auth-key:,tarball:,config:,help:,name:,path:,rootfs:,mapped-uid:,mapped-gid: -- "$@")SS | 98 | +post_process() |
| 99 | +{ | ||
| 100 | + rootfs=$1 | ||
| 101 | + | ||
| 102 | + # rmdir /dev/shm for containers that have /run/shm | ||
| 103 | + # I'm afraid of doing rm -rf $rootfs/dev/shm, in case it did | ||
| 104 | + # get bind mounted to the host's /run/shm. So try to rmdir | ||
| 105 | + # it, and in case that fails move it out of the way. | ||
| 106 | + if [ ! -L $rootfs/dev/shm ] && [ -d $rootfs/run/shm ] && [ -e $rootfs/dev/shm ]; then | ||
| 107 | + mv $rootfs/dev/shm $rootfs/dev/shm.bak | ||
| 108 | + ln -s /run/shm $rootfs/dev/shm | ||
| 109 | + fi | ||
| 110 | +} | ||
| 59 | 111 | ||
| 112 | +usage() | ||
| 113 | +{ | ||
| 114 | + cat <<EOF | ||
| 115 | +$1 -h|--help [-a|--arch] [--trim] [-d|--debug] [--rootfs <rootfs>] [-T|--tarball <rootfs-tarball> | ||
| 116 | +arch: the container architecture (e.g. amd64): defaults to host arch | ||
| 117 | +EOF | ||
| 118 | + return 0 | ||
| 119 | +} | ||
| 120 | + | ||
| 121 | +options=$(getopt -o a:b:hp:r:xn:FS:d:C -l arch:,help,path:,release:,trim,name:,flush-cache,auth-key:,debug:,tarball:,rootfs: -- "$@") | ||
| 60 | if [ $? -ne 0 ]; then | 122 | if [ $? -ne 0 ]; then |
| 61 | usage $(basename $0) | 123 | usage $(basename $0) |
| 62 | exit 1 | 124 | exit 1 |
| 63 | fi | 125 | fi |
| 64 | eval set -- "$options" | 126 | eval set -- "$options" |
| 65 | 127 | ||
| 128 | +release=precise # Default to the last Ubuntu LTS release for non-Ubuntu systems | ||
| 129 | +if [ -f /etc/lsb-release ]; then | ||
| 130 | + . /etc/lsb-release | ||
| 131 | + if [ "$DISTRIB_ID" = "Ubuntu" ]; then | ||
| 132 | + release=$DISTRIB_CODENAME | ||
| 133 | + fi | ||
| 134 | +fi | ||
| 135 | + | ||
| 136 | +arch=$(uname -m) | ||
| 137 | + | ||
| 138 | +# Code taken from debootstrap | ||
| 139 | +if [ -x /usr/bin/dpkg ] && /usr/bin/dpkg --print-architecture >/dev/null 2>&1; then | ||
| 140 | + arch=`/usr/bin/dpkg --print-architecture` | ||
| 141 | +elif type udpkg >/dev/null 2>&1 && udpkg --print-architecture >/dev/null 2>&1; then | ||
| 142 | + arch=`/usr/bin/udpkg --print-architecture` | ||
| 143 | +else | ||
| 144 | + arch=$(uname -m) | ||
| 145 | + if [ "$arch" = "i686" ]; then | ||
| 146 | + arch="i386" | ||
| 147 | + elif [ "$arch" = "x86_64" ]; then | ||
| 148 | + arch="amd64" | ||
| 149 | + elif [ "$arch" = "armv7l" ]; then | ||
| 150 | + arch="armel" | ||
| 151 | + fi | ||
| 152 | +fi | ||
| 153 | + | ||
| 154 | +debug=0 | ||
| 155 | +trim_container=0 | ||
| 156 | +hostarch=$arch | ||
| 66 | while true | 157 | while true |
| 67 | do | 158 | do |
| 68 | case "$1" in | 159 | case "$1" in |
| 69 | - -h|--help) usage $0 && exit 0;; | ||
| 70 | - --config) LXC_CONFIG=$2; shift 2;; | ||
| 71 | - --tarball) LXC_TARBALL=$2; shift 2;; | ||
| 72 | - --name) LXC_NAME=$2; shift 2;; | ||
| 73 | - --path) LXC_PATH=$2; shift 2;; | ||
| 74 | - --rootfs) LXC_ROOTFS=$2; shift 2;; | ||
| 75 | - --mapped-uid) LXC_MAPPED_UID=$2; shift 2;; | ||
| 76 | - --mapped-gid) LXC_MAPPED_GID=$2; shift 2;; | ||
| 77 | - *) break;; | 160 | + -h|--help) usage $0 && exit 0;; |
| 161 | + --rootfs) rootfs=$2; shift 2;; | ||
| 162 | + -p|--path) path=$2; shift 2;; | ||
| 163 | + -n|--name) name=$2; shift 2;; | ||
| 164 | + -T|--tarball) tarball=$2; shift 2;; | ||
| 165 | + -a|--arch) arch=$2; shift 2;; | ||
| 166 | + -S|--auth-key) auth_key=$2; shift 2;; | ||
| 167 | + -d|--debug) debug=1; shift 1;; | ||
| 168 | + --) shift 1; break ;; | ||
| 169 | + *) break ;; | ||
| 78 | esac | 170 | esac |
| 79 | done | 171 | done |
| 80 | 172 | ||
| 81 | -if [ -z "${LXC_NAME}" ]; then | ||
| 82 | - echo "'name' parameter is required" | ||
| 83 | - exit 1 | 173 | +if [ $debug -eq 1 ]; then |
| 174 | + set -x | ||
| 175 | +fi | ||
| 176 | + | ||
| 177 | +if [ "$arch" == "i686" ]; then | ||
| 178 | + arch=i386 | ||
| 84 | fi | 179 | fi |
| 85 | 180 | ||
| 86 | -if [ -z "${LXC_TARBALL}" ]; then | ||
| 87 | - echo "'tarball' parameter is required" | 181 | +if [ $hostarch = "i386" -a $arch = "amd64" ]; then |
| 182 | + echo "can't create amd64 container on i386" | ||
| 88 | exit 1 | 183 | exit 1 |
| 89 | fi | 184 | fi |
| 90 | 185 | ||
| 91 | -if [ -z "${LXC_PATH}" ]; then | 186 | +if [ -z "$path" ]; then |
| 92 | echo "'path' parameter is required" | 187 | echo "'path' parameter is required" |
| 93 | exit 1 | 188 | exit 1 |
| 94 | fi | 189 | fi |
| 95 | 190 | ||
| 96 | -if [ -z "${LXC_CONFIG}" ]; then | ||
| 97 | - echo "'config' parameter is required" | 191 | +if [ "$(id -u)" != "0" ]; then |
| 192 | + echo "This script should be run as 'root'" | ||
| 98 | exit 1 | 193 | exit 1 |
| 99 | fi | 194 | fi |
| 100 | 195 | ||
| 101 | -# if $LXC_ROOTFS exists here, it was passed in with --rootfs | ||
| 102 | -if [ -z "${LXC_ROOTFS}" ]; then | ||
| 103 | - config=${LXC_PATH}/config | 196 | +# detect rootfs |
| 197 | +config="$path/config" | ||
| 198 | +# if $rootfs exists here, it was passed in with --rootfs | ||
| 199 | +if [ -z "$rootfs" ]; then | ||
| 104 | if grep -q '^lxc.rootfs' $config 2>/dev/null ; then | 200 | if grep -q '^lxc.rootfs' $config 2>/dev/null ; then |
| 105 | - LXC_ROOTFS=`grep 'lxc.rootfs =' $config | awk -F= '{ print $2 }'` | 201 | + rootfs=`grep 'lxc.rootfs =' $config | awk -F= '{ print $2 }'` |
| 106 | else | 202 | else |
| 107 | - LXC_ROOTFS=$LXC_PATH/rootfs | ||
| 108 | - echo "lxc.rootfs = ${LXC_ROOTFS}" >> $config | 203 | + rootfs=$path/rootfs |
| 109 | fi | 204 | fi |
| 110 | fi | 205 | fi |
| 111 | 206 | ||
| 112 | -# Unpack the rootfs | ||
| 113 | -echo "Unpacking the rootfs" | ||
| 114 | - | ||
| 115 | -mkdir -p /var/lock/subsys | ||
| 116 | -( | ||
| 117 | - flock -x 200 | ||
| 118 | - if [ $? -ne 0 ]; then | ||
| 119 | - echo "Cache repository is busy." | ||
| 120 | - exit 1 | ||
| 121 | - fi | ||
| 122 | - | ||
| 123 | - mkdir -p ${LXC_ROOTFS} | ||
| 124 | - (cd ${LXC_ROOTFS} && tar xfz ${LXC_TARBALL} --strip-components=2) | ||
| 125 | - if [ $? -ne 0 ]; then | ||
| 126 | - echo "Failed to extract rootfs" | ||
| 127 | - exit 1 | ||
| 128 | - fi | ||
| 129 | - | ||
| 130 | -) 200>/var/lock/subsys/lxc | ||
| 131 | - | ||
| 132 | -mkdir -p ${LXC_ROOTFS}/dev/pts/ | ||
| 133 | - | ||
| 134 | -## Extract all the network config entries | ||
| 135 | -sed -i -e "/lxc.network/{w ${LXC_PATH}/config-network" -e "d}" \ | ||
| 136 | - ${LXC_PATH}/config | ||
| 137 | - | ||
| 138 | -## Extract any other config entry | ||
| 139 | -sed -i -e "/lxc./{w ${LXC_PATH}/config-auto" -e "d}" ${LXC_PATH}/config | ||
| 140 | - | ||
| 141 | -## Add the container-specific config | ||
| 142 | -echo "" >> ${LXC_PATH}/config | ||
| 143 | -echo "##############################################" >> ${LXC_PATH}/config | ||
| 144 | -echo "# Container specific configuration (automatically set)" >> ${LXC_PATH}/config | ||
| 145 | -if [ -e "${LXC_PATH}/config-auto" ]; then | ||
| 146 | - cat ${LXC_PATH}/config-auto >> ${LXC_PATH}/config | ||
| 147 | - rm ${LXC_PATH}/config-auto | ||
| 148 | -fi | ||
| 149 | -echo "lxc.utsname = ${LXC_NAME}" >> ${LXC_PATH}/config | ||
| 150 | - | ||
| 151 | -## Re-add the previously removed network config | ||
| 152 | -if [ -e "${LXC_PATH}/config-network" ]; then | ||
| 153 | - echo "" >> ${LXC_PATH}/config | ||
| 154 | - echo "##############################################" >> ${LXC_PATH}/config | ||
| 155 | - echo "# Network configuration (automatically set)" >> ${LXC_PATH}/config | ||
| 156 | - cat ${LXC_PATH}/config-network >> ${LXC_PATH}/config | ||
| 157 | - rm ${LXC_PATH}/config-network | 207 | +install_ubuntu $rootfs $release $tarball |
| 208 | +if [ $? -ne 0 ]; then | ||
| 209 | + echo "failed to install ubuntu $release" | ||
| 210 | + exit 1 | ||
| 158 | fi | 211 | fi |
| 159 | 212 | ||
| 160 | -## Append the defaults | ||
| 161 | -echo "" >> ${LXC_PATH}/config | ||
| 162 | -echo "##############################################" >> ${LXC_PATH}/config | ||
| 163 | -echo "# vagrant-lxc base box specific configuration" >> ${LXC_PATH}/config | ||
| 164 | -cat ${LXC_CONFIG} >> ${LXC_PATH}/config | 213 | +copy_configuration $path $rootfs $name $arch |
| 214 | +if [ $? -ne 0 ]; then | ||
| 215 | + echo "failed write configuration file" | ||
| 216 | + exit 1 | ||
| 217 | +fi | ||
| 165 | 218 | ||
| 166 | -# Empty section for lxc.customize calls from vagrantfile | ||
| 167 | -echo "" >> ${LXC_PATH}/config | ||
| 168 | -echo "##############################################" >> ${LXC_PATH}/config | ||
| 169 | -echo "# vagrant-lxc container specific configuration" >> ${LXC_PATH}/config | 219 | +post_process $rootfs $release $trim_container |
| 170 | 220 | ||
| 171 | -exit 0 | 221 | +echo "" |
| 222 | +echo "##" | ||
| 223 | +echo "# The default user is 'vagrant' with password 'vagrant'!" | ||
| 224 | +echo "# Use the 'sudo' command to run tasks as root in the container." | ||
| 225 | +echo "##" | ||
| 226 | +echo "" |