File: lxc-cirros.in

package info (click to toggle)
lxc 1%3A1.0.6-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 4,800 kB
  • sloc: ansic: 33,735; sh: 11,868; python: 1,223; makefile: 734
file content (335 lines) | stat: -rw-r--r-- 10,161 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/bin/bash

# template script for generating ubuntu container for LXC
#
# This script consolidates and extends the existing lxc ubuntu scripts
#

# Copyright © 2013 Canonical Ltd.
# Author: Scott Moser <scott.moser@canonical.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2, as
# published by the Free Software Foundation.

# This program 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 General Public License for more details.

# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# Detect use under userns (unsupported)
for arg in "$@"; do
    [ "$arg" = "--" ] && break
    if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then
        echo "This template can't be used for unprivileged containers." 1>&2
        echo "You may want to try the \"download\" template instead." 1>&2
        exit 1
    fi
done

# Make sure the usual locations are in PATH
export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin

VERBOSITY=0
DOWNLOAD_URL="http://download.cirros-cloud.net/"
CACHE_D="@LOCALSTATEDIR@/cache/lxc/cirros"

UNAME_M=$(uname -m)
ARCHES=( i386 x86_64 amd64 arm )
STREAMS=( released devel )
SOURCES=( nocloud none )
BUILD="standard"

DEF_VERSION="released"
DEF_SOURCE="nocloud"
case "${UNAME_M}" in
    i?86) DEF_ARCH="i386";;
    x86_64) DEF_ARCH="x86_64";;
    arm*) DEF_ARCH="arm";;
    *) DEF_ARCH="i386";;
esac

error() { echo "$@" 1>&2; }
inargs() {
    local needle="$1" x=""
    shift
    for x in "$@"; do
        [ "$needle" = "$x" ] && return 0
    done
    return 1
}

Usage() {
    cat <<EOF
${0##*/} [options]

   -a | --arch     A  architecture to use [${ARCHES[*]}]
                      default: ${DEF_ARCH}
   -h | --help        this usage
   -v | --verbose     increase verbosity
   -S | --auth-key K  insert auth key 'K'
   -v | --version  V  version [${STREAMS[*]}]
                      default: ${DEF_VERSION}
   -u | --userdata U  user-data file
        --tarball  T  read from tarball 'T' rather than downloading
                      or using cache.
        --source   S  insert userdata/metadata via source S
                      [${SOURCES[*]}]
EOF
}

bad_Usage() { Usage 1>&2; [ $# -eq 0 ] || error "$@"; return 1; }

debug() {
    local level=${1}; shift;
    [ "${level}" -gt "${VERBOSITY}" ] && return
    error "${@}"
}
jsondict() {
    local k="" v="" ret="{"
    for arg in "$@"; do
        k="${arg%%=*}"
        v="${arg#*=}"
        ret="${ret} \"${k}\": \"$v\","
    done
    ret="${ret%,} }"
    echo "$ret"
}

copy_configuration()
{
    local path=$1 rootfs=$2 name=$3 arch=$4 release=$5
cat >> "$path/config" <<EOF
# Template used to create this container: cirros

lxc.rootfs = $rootfs
#lxc.mount = $path/fstab
lxc.pivotdir = lxc_putold

lxc.tty = 4
lxc.pts = 1024

lxc.utsname = $name
lxc.arch = $arch
lxc.cap.drop = sys_module mac_admin mac_override sys_time

# When using LXC with apparmor, uncomment the next line to run unconfined:
#lxc.aa_profile = unconfined
# To support container nesting on an Ubuntu host, uncomment next two lines:
#lxc.aa_profile = lxc-container-default-with-nesting
#lxc.mount.auto = cgroup

lxc.cgroup.devices.deny = a
# Allow any mknod (but not using the node)
lxc.cgroup.devices.allow = c *:* m
lxc.cgroup.devices.allow = b *:* m
# /dev/null and zero
lxc.cgroup.devices.allow = c 1:3 rwm
lxc.cgroup.devices.allow = c 1:5 rwm
# consoles
lxc.cgroup.devices.allow = c 5:1 rwm
lxc.cgroup.devices.allow = c 5:0 rwm
# /dev/{,u}random
lxc.cgroup.devices.allow = c 1:9 rwm
lxc.cgroup.devices.allow = c 1:8 rwm
lxc.cgroup.devices.allow = c 136:* rwm
lxc.cgroup.devices.allow = c 5:2 rwm
# rtc
lxc.cgroup.devices.allow = c 254:0 rwm
# fuse
lxc.cgroup.devices.allow = c 10:229 rwm
# tun
lxc.cgroup.devices.allow = c 10:200 rwm
# full
lxc.cgroup.devices.allow = c 1:7 rwm
# hpet
lxc.cgroup.devices.allow = c 10:228 rwm
# kvm
lxc.cgroup.devices.allow = c 10:232 rwm
EOF
}

insert_ds_nocloud() {
    local root_d="$1" authkey="$2" udfile="$3"
    local sdir="$root_d/var/lib/cloud/seed/nocloud"

    mkdir -p "$sdir" ||
        { error "failed to make datasource dir $sdir"; return 1; }
    rm -f "$sdir/meta-data" "$sdir/user-data" ||
        { error "failed to clean old data from $sdir"; return 1; }

    iid="iid-local01"
    jsondict "instance-id=$iid" \
        ${authkeys:+"public-keys=${authkeys}"} > "$sdir/meta-data" ||
        { error "failed to write metadata to $sdir/meta-data"; return 1; }

    if [ -n "$udfile" ]; then
        cat "$udfile" > "$sdir/user-data" ||
            { error "failed to write user-data to $sdir"; return 1; }
    else
        rm -f "$sdir/user-data"
    fi
}

insert_ds() {
    local dstype="$1" root_d="$2" authkey="$3" udfile="$4"
    case "$dstype" in
        nocloud) insert_ds_nocloud "$root_d" "$authkey" "$udfile"
    esac
}

extract_rootfs() {
    local tarball="$1" rootfs_d="$2"
    mkdir -p "${rootfs_d}" ||
        { error "failed to make rootfs dir ${rootfs_d}"; return 1; }

    tar -C "${rootfs_d}" -Sxzf "${tarball}" ||
        { error "failed to populate ${rootfs_d}"; return 1; }
    return 0
}

download_tarball() {
    local arch="$1" ver="$2" cached="$3" baseurl="$4"
    local out="" outd="" file="" dlpath=""
    file="cirros-$ver-$arch-lxc.tar.gz"
    dlpath="$ver/$file"
    outd="${cached}/${dlpath%/*}"
    if [ -f "$cached/$dlpath" ]; then
        _RET="$cached/$dlpath"
        return 0
    fi

    mkdir -p "${outd}" ||
        { error "failed to create ${outd}"; return 1; }

    debug 1 "downloading ${baseurl%/}/$dlpath" to "${cached}/$dlpath"
    wget "${baseurl%/}/$dlpath" -O "$cached/${dlpath}.$$" &&
        mv "$cached/$dlpath.$$" "$cached/$dlpath" || {
            rm -f "$cached/$dlpath.$$";
            error "failed to download $dlpath";
            return 1;
        }
    _RET="$cached/$dlpath"
}

create_main() {
    local short_opts="a:hn:p:S:uvV"
    local long_opts="arch:,auth-key:,name:,path:,tarball:,userdata:,verbose,version:,rootfs:"
    local getopt_out=""
    getopt_out=$(getopt --name "${0##*/}" \
        --options "${short_opts}" --long "${long_opts}" -- "$@") &&
        eval set -- "${getopt_out}" ||
        { bad_Usage; return; }

    local arch="${DEF_ARCH}" dsource="${DEF_SOURCE}" version="${DEF_VERSION}"
    local authkey_f="" authkeys="" userdata_f="" path="" tarball=""
    local cur="" next=""
    local rootfs_d=""

    while [ $# -ne 0 ]; do
        cur=$1; next=$2;
        case "$cur" in
            -a|--arch) arch="$next"; shift;;
            -h|--help) Usage ; return 0;;
            -n|--name) name="$next"; shift;;
            -v|--verbose) VERBOSITY=$((${VERBOSITY}+1));;
            -S|--auth-key) authkey_f="$next"; shift;;
            -p|--path) path=$next; shift;;
            -v|--version) version=$next; shift;;
            -u|--userdata) userdata_f="$next"; shift;;
               --tarball) tarball="$next"; shift;;
               --source) dsource="$next"; shift;;
               --rootfs) rootfs_d="$next"; shift;;
            --) shift; break;;
        esac
        shift;
    done

    [ -n "$rootfs_d" ] || rootfs_d="$path/rootfs"
    [ $# -eq 0 ] || { bad_Usage "unexpected arguments: $*"; return; }
    [ -n "$path" ] || { error "'path' parameter is required"; return 1; }

    if [ "$(id -u)" != "0" ]; then
        { error "must be run as root"; return 1; }
    fi

    case "$arch" in
        i?86) arch="i386";;
        amd64) arch="x86_64";;
    esac

    inargs "$arch" "${ARCHES[@]}" ||
        { error "bad arch '$arch'. allowed: ${ARCHES[*]}"; return 1; }

    inargs "$dsource" "${SOURCES[@]}" ||
        { error "bad source '$dsource'. allowed: ${SOURCES[*]}"; return 1; }

    if [ "$dsource" = "none" ] && [ -n "$userdata_f" -o -n "$authkey_f" ]; then
        error "userdata and authkey are incompatible with --source=none";
        return 1;
    fi

    if [ -n "$authkey_f" ]; then
        if [ ! -f "$authkey_f" ]; then
            error "--auth-key=${authkey_f} must reference a file"
            return 1
        fi
        authkeys=$(cat "$authkey_f") ||
            { error "failed to read ${authkey_f}"; return 1; }
    fi

    if [ -n "$userdata_f" -a ! -f "${userdata_f}" ]; then
        error "${userdata_f}: --userdata arg not a file"
        return 1
    fi

    if [ -z "$tarball" ]; then
        if inargs "$version" "${STREAMS[@]}"; then
            out=$(wget -O - -q "${DOWNLOAD_URL%/}/version/$version") ||
                { error "failed to convert 'version=$version'"; return 1; }
            version="$out"
        fi
        download_tarball "$arch" "$version" "${CACHE_D}" "${DOWNLOAD_URL}" ||
            return
        tarball="$_RET"
    fi

    extract_rootfs "${tarball}" "${rootfs_d}" || return

    # cirros 0.3.1 was broken for /dev/random and /dev/urandom
    if [ -b "$rootfs_d/dev/random" ]; then
        rm -f "$rootfs_d/dev/random" &&
            mknod --mode=666 "$rootfs_d/dev/random" c 1 8 ||
           { error "failed to fix /dev/random"; return 1; }
    fi
    if [ -b "$rootfs_d/dev/urandom" ]; then
        rm -f "$rootfs_d/dev/urandom" &&
        mknod --mode=666 "$rootfs_d/dev/urandom" c 1 9 ||
           { error "failed to fix /dev/urandom"; return 1; }
    fi

    if [ "$version" = "0.3.2~pre1" ]; then
        debug 1 "fixing console for lxc and '$version'"
        sed -i 's,^\(#console.* 115200 \)# /dev/console,\1 console,g' \
            "$rootfs_d/etc/inittab" ||
            { error "failed to fix console entry for $version"; return 1; }
    fi

    if [ "$dsource" != "none" ]; then
        insert_ds "$dsource" "$path/rootfs" "$authkeys" "$userdata_f" || {
            error "failed to insert userdata to $path/rootfs"
            return 1
        }
    fi

    copy_configuration "$path" "$path/rootfs" "$name" "$arch" "$release"
    return
}

create_main "$@"

# vi: ts=4 expandtab