#! /bin/bash # Copyright (C) 2011 GRNET S.A. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # 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. ### BEGIN TASK INFO # Provides: EnforcePersonality # RunBefore: UmountImage # RunAfter: MountImage # Short-Description: Inject files to the instance ### END TASK INFO set -e . "@commondir@/common.sh" trap task_cleanup EXIT report_task_start # Check if the task should be prevented from running. check_if_excluded # Default mode for directories DIRMODE="750" if [ ! -d "$SNF_IMAGE_TARGET" ]; then log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing" fi if [ -z "$SNF_IMAGE_PERSONALITY" ]; then warn "This image has no personality (0 files to inject)" exit 0 fi if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "windows" ]; then echo "$SNF_IMAGE_PERSONALITY" | @scriptsdir@/inject-files.py "$SNF_IMAGE_TARGET" exit 0 else tmpdir="$(env TMPDIR="$SNF_IMAGE_TARGET/tmp" mktemp -d)" add_cleanup rm -rf "$tmpdir" echo "$SNF_IMAGE_PERSONALITY" | @scriptsdir@/inject-files.py -d "$tmpdir" { while read -d $'\0' src; do read -d $'\0' owner read -d $'\0' group read -d $'\0' mode read -d $'\0' dest err_msg="Unable to inject file: \`$dest' to the VM. " # Default owner (Probably root) if [ -z "$owner" ]; then uid=0 else uid="$({ grep "^$owner:" "$SNF_IMAGE_TARGET/etc/passwd" || true; } | cut -d: -f3)" if [ -z "$uid" ]; then log_error "$err_msg" "File owner: \`$owner' does not exist!" fi fi # Default group (Probably root in Linux and wheel in *BSD) if [ -z "$group" ]; then gid=0 else gid="$({ grep "^$group:" "$SNF_IMAGE_TARGET/etc/group" || true; } | cut -d: -f3)" if [ -z "$gid" ]; then log_error "$err_msg" "Group: \`$group' does not exist!" fi fi path=( "$(dirname "$dest")" ) while true; do parent="$(dirname "${path[0]}")" if [ "$parent" = "${path[0]}" ]; then break fi path=( "$parent" "${path[@]}" ) done for dir in "${path[@]}"; do if [ -d "$SNF_IMAGE_TARGET/$dir" ]; then continue elif [ -e "$SNF_IMAGE_TARGET/$dir" ]; then log_error "$err_msg" "File: \`$dir' exists and is not a directory." fi mkdir -m "$DIRMODE" "$SNF_IMAGE_TARGET/$dir" chown "$uid:$gid" "$SNF_IMAGE_TARGET/$dir" done install -o "$uid" -g "$gid" -m "$mode" "$tmpdir/$src" "$SNF_IMAGE_TARGET/$dest" done } < "$tmpdir/manifest" fi exit 0 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :