]> git.proxmox.com Git - mirror_lxc.git/blobdiff - hooks/ubuntu-cloud-prep
Merge pull request #2013 from 3XX0/oci-dhcp-improvements
[mirror_lxc.git] / hooks / ubuntu-cloud-prep
index 159fd021924e08023a29afe35bc29f2b28277ec9..b89e79fbf7d7d973d3430142edf4f10c506a29ac 100755 (executable)
@@ -1,21 +1,22 @@
 #!/bin/bash
-## If the container being cloned has one or more lxc.hook.clone specified,
-## then the specified hooks will be called for the new  container.  The first
-## 3 arguments passed to the clone hook will be:
+## If the container being cloned has one or more lxc.hook.clone
+## specified, then the specified hooks will be called for the new
+## container.  The arguments passed to the clone hook are:
 ##  1. the container name
 ##  2. a section ('lxc')
 ##  3. hook type ('clone')
 ##  4. .. additional arguments to lxc-clone
 ## Environment variables:
-##  LXC_ROOTFS_MOUNT: path under which the container's root fs is mounted.
-##  LXC_CONFIG_FILE:  The configuration file pathname
-##  LXC_SRC_NAME: old container name
-##  LXC_ROOTFS_PATH: path or device on which the root fs is located
+##  LXC_ROOTFS_MOUNT: path to the root filesystem
+##  LXC_CONFIG_FILE:  path to config file
+##  LXC_SRC_NAME:     old container name
+##  LXC_ROOTFS_PATH:  path or device on which the root fs is located
 
+set -f
 VERBOSITY="0"
 
 error() { echo "$@" 1>&2; }
-debug() { [ "$1" -ge "$VERBOSITY" ] || return; shift; error "$@"; }
+debug() { [ "$VERBOSITY" -ge "$1" ] || return 0; shift; error "$@"; }
 fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
 
 prep_usage() {
@@ -30,13 +31,14 @@ Usage: ${0##*/} [options] root-dir
   [ -L | --nolocales ]:    Do not copy host's locales into container
   [ -S | --auth-key ]:     ssh public key file for datasource [ds]
   [ -u | --userdata ]:     user-data file for cloud-init [ds]
+  [ -V | --vendordata ]:   vendor-data file for cloud-init [ds]
 
 EOF
 }
 
 prep() {
     local short_opts="Chi:L:S:u:v"
-    local long_opts="auth-key:,cloud,help,hostid:,name:,nolocales:,userdata:,verbose"
+    local long_opts="auth-key:,cloud,help,hostid:,name:,nolocales:,create-etc-init,userdata:,vendordata:,verbose"
     local getopt_out getopt_ret
     getopt_out=$(getopt --name "${0##*/}" \
         --options "${short_opts}" --long "${long_opts}" -- "$@" 2>/dev/null) ||
@@ -48,7 +50,9 @@ prep() {
     fi
 
     local cur="" next=""
-    local userdata="" hostid="" authkey="" locales=1 cloud=0 name=""
+    local vendordata="" userdata="" hostid="" authkey="" locales=1 cloud=0
+    local create_etc_init=0 name="ubuntucloud-lxc"
+
     while [ $# -ne 0 ]; do
         cur="$1"; next="$2";
         case "$cur" in
@@ -57,10 +61,15 @@ prep() {
                --name) name="$next";;
             -i|--hostid) hostid="$next";;
             -L|--nolocales) locales=0;;
+               --create-etc-init) create_etc_init=1;;
             -S|--auth-key)
                 [ -f "$next" ] ||
                     { error "--auth-key: '$next' not a file"; return 1; }
                 authkey="$next";;
+            -V|--vendordata)
+                [ -f "$next" ] ||
+                    { error "--vendordata: '$next' not a file"; return 1; }
+                vendordata="$next";;
             -u|--userdata)
                 [ -f "$next" ] ||
                     { error "--userdata: '$next' not a file"; return 1; }
@@ -86,8 +95,17 @@ prep() {
         error "${0##*}: usage failed, continuing with defaults"
     fi
 
+    [ "$create_etc_init" -eq 0 ] ||
+        echo "#upstart needs help for overlayfs (LP: #1213925)." > \
+            "$root_d/etc/init/.overlayfs-upstart-helper" ||
+        { error "failed to create /etc/init in overlay"; return 1; }
+
     local seed_d=""
     seed_d="$root_d/var/lib/cloud/seed/nocloud-net"
+
+    echo "$name" > "$root_d/etc/hostname" ||
+        { error "failed to write /etc/hostname"; return 1; }
+
     if [ $cloud -eq 1 ]; then
         debug 1 "--cloud provided, not modifying seed in '$seed_d'"
     else
@@ -101,6 +119,9 @@ prep() {
         echo "instance-id: lxc-$hostid" > "$seed_d/meta-data" ||
             { error "failed to write to $seed_d/meta-data"; return 1; }
 
+        echo "local-hostname: $name" >> "$seed_d/meta-data" ||
+            { error "failed to write to $seed_d/meta-data"; return 1; }
+
         if [ -n "$authkey" ]; then
             {
                 echo "public-keys:" &&
@@ -122,7 +143,10 @@ prep() {
             MIRROR="http://archive.ubuntu.com/ubuntu"
         fi
 
-        {
+        if [ -n "$userdata" ]; then
+            cp "$userdata" "$seed_d/user-data"
+        else
+            {
             local lc=$(locale | awk -F= '/LANG=/ {print $NF; }')
             echo "#cloud-config"
             echo "output: {all: '| tee -a /var/log/cloud-init-output.log'}"
@@ -131,12 +155,19 @@ prep() {
             [ -z "$LANG" ] || echo "locale: $LANG";
             echo "password: ubuntu"
             echo "chpasswd: { expire: false; }"
-            
-        } > "$seed_d/user-data"
+            } > "$seed_d/user-data"
+        fi
         [ $? -eq 0 ] || {
             error "failed to write user-data write to '$seed_d/user-data'";
             return 1;
         }
+
+        if [ -n "$vendordata" ]; then
+            cp "$vendordata" "$seed_d/vendor-data" || {
+                error "failed copy vendordata to $seed_d/vendor-data";
+                return 1;
+            }
+        fi
     fi
 
 }
@@ -146,10 +177,13 @@ main() {
     local _LXC_HOOK
     if [ -n "$LXC_ROOTFS_MOUNT" -a "$3" = "clone" ]; then
         _LXC_HOOK="clone"
-        local name="$1"
+        local name="$1" create_etc_init=""
         shift 3
-        debug 1 prep "--name=$name" "$LXC_ROOTFS_MOUNT" "$@"
-        prep "--name=$name" "$LXC_ROOTFS_MOUNT" "$@"
+        # if mountpoint is overlayfs then add '--create-etc-init'
+        [ "${LXC_ROOTFS_PATH#overlayfs}" != "${LXC_ROOTFS_PATH}" ] &&
+           create_etc_init="--create-etc-init"
+        debug 1 prep "--name=$name" $create_etc_init "$LXC_ROOTFS_MOUNT" "$@"
+        prep "--name=$name" $create_etc_init "$LXC_ROOTFS_MOUNT" "$@"
     else
         _LXC_HOOK=""
         prep "$@"