diff options
author | Serge Hallyn <serge@hallyn.com> | 2012-02-16 14:14:13 -0600 |
---|---|---|
committer | Daniel Lezcano <daniel.lezcano@free.fr> | 2012-02-26 10:44:41 +0100 |
commit | 4759162d078d86628956cae4846c6efccf548e67 (patch) | |
tree | 481527579251609d380be5e4088b92cec943f4f5 | |
parent | lxc-ubuntu: fix obscure arguments (diff) | |
download | lxc-4759162d078d86628956cae4846c6efccf548e67.tar.gz lxc-4759162d078d86628956cae4846c6efccf548e67.tar.bz2 lxc-4759162d078d86628956cae4846c6efccf548e67.zip |
update ubuntu templates to provide macaddr and more
Add a macaddr if precisely one veth is specified but no hwaddr. Allow
specifying ssh authkeys. In cloud template, copy locales by default and allow
a tarball to be specified.
Signed-off-by: Ben Howard <ben.howard@canonical.com>
Signed-off-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Daniel Lezcano <dlezcano@fr.ibm.com>
-rw-r--r-- | templates/lxc-ubuntu-cloud.in | 98 | ||||
-rw-r--r-- | templates/lxc-ubuntu.in | 24 |
2 files changed, 98 insertions, 24 deletions
diff --git a/templates/lxc-ubuntu-cloud.in b/templates/lxc-ubuntu-cloud.in index 24fa2f6..1a61c7d 100644 --- a/templates/lxc-ubuntu-cloud.in +++ b/templates/lxc-ubuntu-cloud.in @@ -36,6 +36,15 @@ copy_configuration() arch="i686" fi + # if there is exactly one veth network entry, make sure it has an + # associated hwaddr. + nics=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l` + if [ $nics -eq 1 ]; then + grep -q "^lxc.network.hwaddr" $path/config || cat <<EOF >> $path/config +lxc.network.hwaddr= 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//') +EOF + fi + cat <<EOF >> $path/config lxc.utsname = $name @@ -88,14 +97,25 @@ EOF usage() { cat <<EOF -$1 -h|--help [-a|--arch] [-F | --flush-cache] [-r|--release <release>] -release: lucid | maverick | natty | oneiric | precise -arch: amd64 or i386: defaults to host arch +LXC Container configuration for Ubuntu Cloud images. + +Generic Options +[ -r | --release <release> ]: Release name of container, defaults to host +[ -a | --arch ]: Arhcitecture of container, defaults to host arcitecture +[ -C | --cloud ]: Configure container for use with meta-data service, defaults to no +[ -T | --tarball ]: Location of tarball + +Options, mutually exclusive of "-C" and "--cloud": + [ -i | --hostid ]: HostID for cloud-init, defaults to random string + [ -u | --userdata ]: Cloud-init user-data file to configure container on start + [ -S | --auth_key ]: SSH Public key file to inject into container + [ -L | --nolocales ]: Do not copy host's locales into container + EOF return 0 } -options=$(getopt -o a:hp:r:n:Fi:C -l arch:,help,path:,release:,name:,flush-cache,hostid:,cloud -- "$@") +options=$(getopt -o a:hp:r:n:Fi:CLS:T: -l arch:,help,path:,release:,name:,flush-cache,hostid:,auth-key:,cloud,no_locales,tarball: -- "$@") if [ $? -ne 0 ]; then usage $(basename $0) exit 1 @@ -132,20 +152,24 @@ fi hostarch=$arch cloud=0 +locales=1 flushcache=0 while true do case "$1" in - -h|--help) usage $0 && exit 0;; - -p|--path) path=$2; shift 2;; - -n|--name) name=$2; shift 2;; - -F|--flush-cache) flushcache=1; shift 1;; - -r|--release) release=$2; shift 2;; - -a|--arch) arch=$2; shift 2;; - -i|--hostid) host_id=$2; shift 2;; - -u|--userdata) userdata=$2; shift 2;; - -C|--cloud) cloud=1; shift 1;; - --) shift 1; break ;; + -h|--help) usage $0 && exit 0;; + -p|--path) path=$2; shift 2;; + -n|--name) name=$2; shift 2;; + -F|--flush-cache) flushcache=1; shift 1;; + -r|--release) release=$2; shift 2;; + -a|--arch) arch=$2; shift 2;; + -i|--hostid) host_id=$2; shift 2;; + -u|--userdata) userdata=$2; shift 2;; + -C|--cloud) cloud=1; shift 1;; + -S|--auth_key) auth_key=$2; shift 2;; + -L|--no_locales) locales=0; shift 2;; + -T|--tarball) tarball=$2; shift 2;; + --) shift 1; break ;; *) break ;; esac done @@ -185,8 +209,13 @@ cache="/var/cache/lxc/cloud-$release" mkdir -p $cache -url1=`ubuntu-cloudimg-query precise daily $arch --format "%{url}\n"` -url2=`echo $url1 | sed -e 's/.tar.gz/-root\0/'` +if [ -n "$tarball" ]; then + url2="$tarball" +else + url1=`ubuntu-cloudimg-query precise daily $arch --format "%{url}\n"` + url2=`echo $url1 | sed -e 's/.tar.gz/-root\0/'` +fi + filename=`basename $url2` mkdir -p /var/lock/subsys/ @@ -200,7 +229,7 @@ mkdir -p /var/lock/subsys/ fi if [ ! -f $filename ]; then - wget $url2 + wget $url2 fi echo "Extracting rootfs" @@ -215,21 +244,46 @@ mkdir -p /var/lock/subsys/ seed_d=$rootfs/var/lib/cloud/seed/nocloud-net rhostid=$(uuidgen | cut -c -8) - host_id=${hostid:-$rhostid} - mkdir -p $seed_d + host_id=${hostid:-$rhostid} + mkdir -p $seed_d cat > "$seed_d/meta-data" <<EOF instance_id: lxc-$host_id EOF + rm $rootfs/etc/hostname + + if [ $locales -eq 1 ]; then + cp /usr/lib/locale/locale-archive $rootfs/usr/lib/locale/locale-archive + fi + + + if [ -n "$auth_key" -a -f "$auth_key" ]; then + u_path="/home/ubuntu/.ssh" + root_u_path="$rootfs/$u_path" + mkdir -p $root_u_path + cp $auth_key "$root_u_path/authorized_keys" + chroot $rootfs chown -R ubuntu: "$u_path" + + echo "Inserted SSH public key from $auth_key into /home/ubuntu/.ssh/authorized_keys" + fi + if [ ! -f $userdata ]; then - cp $userdata $seed_d/user-data + cp $userdata $data_d/user-data else - echo "#cloud-config" > $seed_d/user-data + if [ -z "$MIRROR" ]; then MIRROR="http://archive.ubuntu.com/ubuntu" fi - echo "apt-mirror: $MIRROR" >> $seed_d/user-data + + cat > "$seed_d/user-data" <<EOF +#cloud-config +output: {all: '| tee -a /var/log/cloud-init-output.log'} +apt-mirror: $MIRROR +manage_etc_hosts: localhost +locale: $(/usr/bin/locale | awk -F= '/LANG=/ {print$NF}') +EOF + fi chroot $rootfs /usr/sbin/usermod -U ubuntu diff --git a/templates/lxc-ubuntu.in b/templates/lxc-ubuntu.in index 26db686..391ee55 100644 --- a/templates/lxc-ubuntu.in +++ b/templates/lxc-ubuntu.in @@ -68,6 +68,15 @@ EOF chroot $rootfs groupadd --system $group >/dev/null 2>&1 || true chroot $rootfs useradd --create-home -s /bin/bash -G $group ubuntu echo "ubuntu:ubuntu" | chroot $rootfs chpasswd + if [ -n "$auth_key" -a -f "$auth_key" ]; then + u_path="/home/ubuntu/.ssh" + root_u_path="$rootfs/$u_path" + mkdir -p $root_u_path + cp $auth_key "$root_u_path/authorized_keys" + chroot $rootfs chown -R ubuntu: "$u_path" + + echo "Inserted SSH public key from $auth_key into /home/ubuntu/.ssh/authorized_keys" + fi return 0 } @@ -284,6 +293,15 @@ copy_configuration() ttydir=" lxc" fi + # if there is exactly one veth network entry, make sure it has an + # associated hwaddr. + nics=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l` + if [ $nics -eq 1 ]; then + grep -q "^lxc.network.hwaddr" $path/config || cat <<EOF >> $path/config +lxc.network.hwaddr= 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//') +EOF + fi + cat <<EOF >> $path/config lxc.utsname = $name @@ -502,16 +520,17 @@ usage() { cat <<EOF $1 -h|--help [-a|--arch] [-b|--bindhome <user>] [--trim] - [-F | --flush-cache] [-r|--release <release>] + [-F | --flush-cache] [-r|--release <release>] [ -S | --auth_key <keyfile>] release: lucid | maverick | natty | oneiric | precise trim: make a minimal (faster, but not upgrade-safe) container bindhome: bind <user>'s home into the container arch: amd64 or i386: defaults to host arch +auth_key: SSH Public key file to inject into container EOF return 0 } -options=$(getopt -o a:b:hp:r:xn:F -l arch:,bindhome:,help,path:,release:,trim,name:,flush-cache -- "$@") +options=$(getopt -o a:b:hp:r:xn:FS: -l arch:,bindhome:,help,path:,release:,trim,name:,flush-cache,auth-key: -- "$@") if [ $? -ne 0 ]; then usage $(basename $0) exit 1 @@ -561,6 +580,7 @@ do -b|--bindhome) bindhome=$2; shift 2;; -a|--arch) arch=$2; shift 2;; -x|--trim) trim_container=1; shift 1;; + -S|--auth_key) auth_key=$2; shift 2;; --) shift 1; break ;; *) break ;; esac |