From 4759162d078d86628956cae4846c6efccf548e67 Mon Sep 17 00:00:00 2001 From: Serge Hallyn Date: Thu, 16 Feb 2012 14:14:13 -0600 Subject: [PATCH] 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 Signed-off-by: Serge Hallyn Signed-off-by: Daniel Lezcano --- templates/lxc-ubuntu-cloud.in | 98 +++++++++++++++++++++++++++-------- 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 24fa2f67e..1a61c7d1c 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 <> $path/config +lxc.network.hwaddr= 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//') +EOF + fi + cat <> $path/config lxc.utsname = $name @@ -88,14 +97,25 @@ EOF usage() { cat <] -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 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" < $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" </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 <> $path/config +lxc.network.hwaddr= 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//') +EOF + fi + cat <> $path/config lxc.utsname = $name @@ -502,16 +520,17 @@ usage() { cat <] [--trim] - [-F | --flush-cache] [-r|--release ] + [-F | --flush-cache] [-r|--release ] [ -S | --auth_key ] release: lucid | maverick | natty | oneiric | precise trim: make a minimal (faster, but not upgrade-safe) container bindhome: bind '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 -- 2.39.2