58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn#!/bin/bash
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn# template script for generating ubuntu container for LXC
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn#
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn# This script consolidates and extends the existing lxc ubuntu scripts
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn#
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
8cd80b50efe2107ac351bfd0285050dd183398e7Stéphane Graber# Copyright © 2013 Canonical Ltd.
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn# Author: Scott Moser <scott.moser@canonical.com>
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn#
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn# This program is free software; you can redistribute it and/or modify
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn# it under the terms of the GNU General Public License version 2, as
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn# published by the Free Software Foundation.
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn# This program is distributed in the hope that it will be useful,
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn# but WITHOUT ANY WARRANTY; without even the implied warranty of
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn# GNU General Public License for more details.
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn# You should have received a copy of the GNU General Public License along
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn# with this program; if not, write to the Free Software Foundation, Inc.,
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
8ec981fc8b0105da5f071e40811e0c2472a6c3c9Stéphane Graber
8ec981fc8b0105da5f071e40811e0c2472a6c3c9Stéphane Graber# Detect use under userns (unsupported)
207bf0e475f1dc6e9a2dac2cee3a209b56427855Stéphane Graber# Make sure the usual locations are in PATH
207bf0e475f1dc6e9a2dac2cee3a209b56427855Stéphane Graberexport PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
207bf0e475f1dc6e9a2dac2cee3a209b56427855Stéphane Graber
58a46e06210a6321c530735f15f66eb648c4657dSerge HallynVERBOSITY=0
58a46e06210a6321c530735f15f66eb648c4657dSerge HallynDOWNLOAD_URL="http://download.cirros-cloud.net/"
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
58a46e06210a6321c530735f15f66eb648c4657dSerge HallynUNAME_M=$(uname -m)
58a46e06210a6321c530735f15f66eb648c4657dSerge HallynARCHES=( i386 x86_64 amd64 arm )
58a46e06210a6321c530735f15f66eb648c4657dSerge HallynSTREAMS=( released devel )
807732062eab6cd44fb033bfbb37fbb38907aa66Serge HallynSOURCES=( nocloud none )
58a46e06210a6321c530735f15f66eb648c4657dSerge HallynBUILD="standard"
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge HallynLXC_TEMPLATE_CONFIG="@LXCTEMPLATECONFIG@"
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallyn
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge HallynLXC_MAPPED_GID=
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge HallynLXC_MAPPED_UID=
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
58a46e06210a6321c530735f15f66eb648c4657dSerge HallynDEF_VERSION="released"
807732062eab6cd44fb033bfbb37fbb38907aa66Serge HallynDEF_SOURCE="nocloud"
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyncase "${UNAME_M}" in
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn i?86) DEF_ARCH="i386";;
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn x86_64) DEF_ARCH="x86_64";;
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn arm*) DEF_ARCH="arm";;
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn *) DEF_ARCH="i386";;
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynesac
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallynam_in_userns() {
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallyn [ -e /proc/self/uid_map ] || { echo no; return; }
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallyn [ "$(wc -l /proc/self/uid_map | awk '{ print $1 }')" -eq 1 ] || { echo yes; return; }
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallyn line=$(awk '{ print $1 " " $2 " " $3 }' /proc/self/uid_map)
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallyn [ "$line" = "0 0 4294967295" ] && { echo no; return; }
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallyn echo yes
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallyn}
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallyn
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallynin_userns=0
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallyn[ $(am_in_userns) = "yes" ] && in_userns=1
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallyn
6dc6f80bfd7cb169948f2ef9a95dcf6d2edee853Kevin Carter# Allow the cache base to be set by environment variable
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallynif [ $(id -u) -eq 0 ]; then
6dc6f80bfd7cb169948f2ef9a95dcf6d2edee853Kevin Carter CACHE_D=${LXC_CACHE_PATH:-"@LOCALSTATEDIR@/cache/lxc/cirros"}
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallynelse
6dc6f80bfd7cb169948f2ef9a95dcf6d2edee853Kevin Carter CACHE_D=${LXC_CACHE_PATH:-"$HOME/.cache/lxc/cirros"}
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallynfi
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallyn
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynerror() { echo "$@" 1>&2; }
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyninargs() {
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn local needle="$1" x=""
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn shift
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn for x in "$@"; do
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn [ "$needle" = "$x" ] && return 0
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn done
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn return 1
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn}
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
58a46e06210a6321c530735f15f66eb648c4657dSerge HallynUsage() {
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn cat <<EOF
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn${0##*/} [options]
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn -a | --arch A architecture to use [${ARCHES[*]}]
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn default: ${DEF_ARCH}
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn -h | --help this usage
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn -v | --verbose increase verbosity
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn -S | --auth-key K insert auth key 'K'
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn -v | --version V version [${STREAMS[*]}]
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn default: ${DEF_VERSION}
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn -u | --userdata U user-data file
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn --tarball T read from tarball 'T' rather than downloading
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn or using cache.
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn --source S insert userdata/metadata via source S
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn [${SOURCES[*]}]
58a46e06210a6321c530735f15f66eb648c4657dSerge HallynEOF
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn}
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallynbad_Usage() { Usage 1>&2; [ $# -eq 0 ] || error "$@"; return 1; }
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyndebug() {
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn local level=${1}; shift;
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn [ "${level}" -gt "${VERBOSITY}" ] && return
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn error "${@}"
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn}
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynjsondict() {
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn local k="" v="" ret="{"
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn for arg in "$@"; do
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn k="${arg%%=*}"
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn v="${arg#*=}"
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn ret="${ret} \"${k}\": \"$v\","
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn done
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn ret="${ret%,} }"
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn echo "$ret"
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn}
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyncopy_configuration()
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn{
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn local path=$1 rootfs=$2 name=$3 arch=$4 release=$5
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyncat >> "$path/config" <<EOF
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn# Template used to create this container: cirros
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynlxc.rootfs = $rootfs
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynlxc.pivotdir = lxc_putold
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynlxc.tty = 4
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynlxc.pts = 1024
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynlxc.utsname = $name
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynlxc.arch = $arch
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynlxc.cap.drop = sys_module mac_admin mac_override sys_time
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn# When using LXC with apparmor, uncomment the next line to run unconfined:
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn#lxc.aa_profile = unconfined
f24a52d5f588ff4e4575046903fb9498c376d833Stéphane Graberlxc.mount.auto = cgroup:mixed proc:mixed sys:mixed
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynlxc.cgroup.devices.deny = a
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn# Allow any mknod (but not using the node)
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynlxc.cgroup.devices.allow = c *:* m
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynlxc.cgroup.devices.allow = b *:* m
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn# /dev/null and zero
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynlxc.cgroup.devices.allow = c 1:3 rwm
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynlxc.cgroup.devices.allow = c 1:5 rwm
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn# consoles
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynlxc.cgroup.devices.allow = c 5:1 rwm
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynlxc.cgroup.devices.allow = c 5:0 rwm
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn# /dev/{,u}random
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynlxc.cgroup.devices.allow = c 1:9 rwm
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynlxc.cgroup.devices.allow = c 1:8 rwm
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynlxc.cgroup.devices.allow = c 136:* rwm
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynlxc.cgroup.devices.allow = c 5:2 rwm
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn# rtc
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynlxc.cgroup.devices.allow = c 254:0 rwm
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn# fuse
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynlxc.cgroup.devices.allow = c 10:229 rwm
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn# tun
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynlxc.cgroup.devices.allow = c 10:200 rwm
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn# full
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynlxc.cgroup.devices.allow = c 1:7 rwm
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn# hpet
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynlxc.cgroup.devices.allow = c 10:228 rwm
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn# kvm
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynlxc.cgroup.devices.allow = c 10:232 rwm
58a46e06210a6321c530735f15f66eb648c4657dSerge HallynEOF
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallyn
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallyn if [ $in_userns -eq 1 ] && [ -e "${LXC_TEMPLATE_CONFIG}/ubuntu-cloud.userns.conf" ]; then
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallyn echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/ubuntu.userns.conf" >> $path/config
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallyn fi
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallyn
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn}
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyninsert_ds_nocloud() {
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn local root_d="$1" authkey="$2" udfile="$3"
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn local sdir="$root_d/var/lib/cloud/seed/nocloud"
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn mkdir -p "$sdir" ||
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn { error "failed to make datasource dir $sdir"; return 1; }
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn rm -f "$sdir/meta-data" "$sdir/user-data" ||
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn { error "failed to clean old data from $sdir"; return 1; }
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn iid="iid-local01"
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn jsondict "instance-id=$iid" \
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn ${authkeys:+"public-keys=${authkeys}"} > "$sdir/meta-data" ||
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn { error "failed to write metadata to $sdir/meta-data"; return 1; }
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn if [ -n "$udfile" ]; then
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn cat "$udfile" > "$sdir/user-data" ||
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn { error "failed to write user-data to $sdir"; return 1; }
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn else
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn rm -f "$sdir/user-data"
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn fi
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn}
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyninsert_ds() {
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn local dstype="$1" root_d="$2" authkey="$3" udfile="$4"
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn case "$dstype" in
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn nocloud) insert_ds_nocloud "$root_d" "$authkey" "$udfile"
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn esac
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn}
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallynextract_rootfs() {
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn local tarball="$1" rootfs_d="$2"
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn mkdir -p "${rootfs_d}" ||
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn { error "failed to make rootfs dir ${rootfs_d}"; return 1; }
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallyn if [ $in_userns -eq 1 ]; then
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallyn tar -C "${rootfs_d}" --anchored --exclude="dev/*" -Sxzf "${tarball}" ||
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallyn { error "failed to populate ${rootfs_d}"; return 1; }
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallyn else
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallyn tar -C "${rootfs_d}" -Sxzf "${tarball}" ||
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallyn { error "failed to populate ${rootfs_d}"; return 1; }
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallyn fi
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn return 0
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn}
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyndownload_tarball() {
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn local arch="$1" ver="$2" cached="$3" baseurl="$4"
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn local out="" outd="" file="" dlpath=""
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn file="cirros-$ver-$arch-lxc.tar.gz"
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn dlpath="$ver/$file"
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn outd="${cached}/${dlpath%/*}"
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn if [ -f "$cached/$dlpath" ]; then
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn _RET="$cached/$dlpath"
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn return 0
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn fi
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn mkdir -p "${outd}" ||
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn { error "failed to create ${outd}"; return 1; }
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn debug 1 "downloading ${baseurl%/}/$dlpath" to "${cached}/$dlpath"
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn wget "${baseurl%/}/$dlpath" -O "$cached/${dlpath}.$$" &&
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn mv "$cached/$dlpath.$$" "$cached/$dlpath" || {
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn rm -f "$cached/$dlpath.$$";
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn error "failed to download $dlpath";
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn return 1;
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn }
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn _RET="$cached/$dlpath"
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn}
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyncreate_main() {
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn local short_opts="a:hn:p:S:uvV"
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallyn local long_opts="arch:,auth-key:,name:,path:,tarball:,userdata:,verbose,version:,rootfs:,mapped-uid:,mapped-gid:"
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn local getopt_out=""
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn getopt_out=$(getopt --name "${0##*/}" \
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn --options "${short_opts}" --long "${long_opts}" -- "$@") &&
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn eval set -- "${getopt_out}" ||
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn { bad_Usage; return; }
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn local arch="${DEF_ARCH}" dsource="${DEF_SOURCE}" version="${DEF_VERSION}"
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn local authkey_f="" authkeys="" userdata_f="" path="" tarball=""
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn local cur="" next=""
4165b2c65648b5df521c6e83b1cbad91d0896a00Serge Hallyn local rootfs_d=""
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn while [ $# -ne 0 ]; do
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn cur=$1; next=$2;
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn case "$cur" in
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn -a|--arch) arch="$next"; shift;;
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn -h|--help) Usage ; return 0;;
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn -n|--name) name="$next"; shift;;
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn -v|--verbose) VERBOSITY=$((${VERBOSITY}+1));;
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn -S|--auth-key) authkey_f="$next"; shift;;
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn -p|--path) path=$next; shift;;
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn -v|--version) version=$next; shift;;
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn -u|--userdata) userdata_f="$next"; shift;;
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn --tarball) tarball="$next"; shift;;
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn --source) dsource="$next"; shift;;
4165b2c65648b5df521c6e83b1cbad91d0896a00Serge Hallyn --rootfs) rootfs_d="$next"; shift;;
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallyn --mapped-uid) LXC_MAPPED_UID=$next; shift;;
6b4105628005f9fdf29aa15a80f85da48960c2d4Serge Hallyn --mapped-gid) LXC_MAPPED_GID=$next; shift;;
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn --) shift; break;;
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn esac
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn shift;
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn done
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
4165b2c65648b5df521c6e83b1cbad91d0896a00Serge Hallyn [ -n "$rootfs_d" ] || rootfs_d="$path/rootfs"
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn [ $# -eq 0 ] || { bad_Usage "unexpected arguments: $*"; return; }
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn [ -n "$path" ] || { error "'path' parameter is required"; return 1; }
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn if [ "$(id -u)" != "0" ]; then
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn { error "must be run as root"; return 1; }
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn fi
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn case "$arch" in
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn i?86) arch="i386";;
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn amd64) arch="x86_64";;
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn esac
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn inargs "$arch" "${ARCHES[@]}" ||
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn { error "bad arch '$arch'. allowed: ${ARCHES[*]}"; return 1; }
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn inargs "$dsource" "${SOURCES[@]}" ||
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn { error "bad source '$dsource'. allowed: ${SOURCES[*]}"; return 1; }
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn if [ "$dsource" = "none" ] && [ -n "$userdata_f" -o -n "$authkey_f" ]; then
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn error "userdata and authkey are incompatible with --source=none";
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn return 1;
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn fi
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn if [ -n "$authkey_f" ]; then
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn if [ ! -f "$authkey_f" ]; then
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn error "--auth-key=${authkey_f} must reference a file"
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn return 1
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn fi
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn authkeys=$(cat "$authkey_f") ||
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn { error "failed to read ${authkey_f}"; return 1; }
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn fi
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn if [ -n "$userdata_f" -a ! -f "${userdata_f}" ]; then
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn error "${userdata_f}: --userdata arg not a file"
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn return 1
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn fi
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn if [ -z "$tarball" ]; then
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn if inargs "$version" "${STREAMS[@]}"; then
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn out=$(wget -O - -q "${DOWNLOAD_URL%/}/version/$version") ||
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn { error "failed to convert 'version=$version'"; return 1; }
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn version="$out"
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn fi
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn download_tarball "$arch" "$version" "${CACHE_D}" "${DOWNLOAD_URL}" ||
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn return
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn tarball="$_RET"
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn fi
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn extract_rootfs "${tarball}" "${rootfs_d}" || return
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn if [ "$version" = "0.3.2~pre1" ]; then
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn debug 1 "fixing console for lxc and '$version'"
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn sed -i 's,^\(#console.* 115200 \)# /dev/console,\1 console,g' \
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn "$rootfs_d/etc/inittab" ||
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn { error "failed to fix console entry for $version"; return 1; }
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn fi
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn if [ "$dsource" != "none" ]; then
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn insert_ds "$dsource" "$path/rootfs" "$authkeys" "$userdata_f" || {
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn error "failed to insert userdata to $path/rootfs"
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn return 1
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn }
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn fi
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn copy_configuration "$path" "$path/rootfs" "$name" "$arch" "$release"
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn return
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyn}
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
807732062eab6cd44fb033bfbb37fbb38907aa66Serge Hallyncreate_main "$@"
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn
58a46e06210a6321c530735f15f66eb648c4657dSerge Hallyn# vi: ts=4 expandtab