Add partial support for NetBSD and OpenBSD
[snf-image] / snf-image-helper / tasks / 60EnforcePersonality.in
1 #! /bin/bash
2
3 # Copyright (C) 2011 GRNET S.A. 
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 # 02110-1301, USA.
19
20 ### BEGIN TASK INFO
21 # Provides:             EnforcePersonality
22 # RunBefore:            UmountImage
23 # RunAfter:             MountImage
24 # Short-Description:    Inject files to the instance
25 ### END TASK INFO
26
27 set -e
28
29 . "@commondir@/common.sh"
30
31 trap task_cleanup EXIT
32 report_task_start
33
34 # Check if the task should be prevented from running.
35 check_if_excluded
36
37 # Default mode for directories
38 DIRMODE="750"
39
40 if [ ! -d "$SNF_IMAGE_TARGET" ]; then
41     log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"
42 fi
43
44 if [ -z "$SNF_IMAGE_PERSONALITY" ]; then
45     warn "This image has no personality (0 files to inject)"
46     exit 0
47 fi
48
49 if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "windows" ]; then
50     echo "$SNF_IMAGE_PERSONALITY" |
51         @scriptsdir@/inject-files.py "$SNF_IMAGE_TARGET"
52     exit 0
53 else
54
55     tmpdir="$(env TMPDIR="$SNF_IMAGE_TARGET/tmp" mktemp -d)"
56     add_cleanup rm -rf "$tmpdir"
57     echo "$SNF_IMAGE_PERSONALITY" |
58         @scriptsdir@/inject-files.py -d "$tmpdir"
59
60     { while read -d $'\0' src; do
61         read -d $'\0' owner
62         read -d $'\0' group
63         read -d $'\0' mode
64         read -d $'\0' dest
65
66         err_msg="Unable to inject file: \`$dest' to the VM. "
67
68         # Default owner (Probably root)
69         if [ -z "$owner" ]; then
70             uid=0
71         else
72             uid="$({ grep "^$owner:" "$SNF_IMAGE_TARGET/etc/passwd" || true; } | cut -d: -f3)"
73             if [ -z "$uid" ]; then
74                 log_error "$err_msg" "File owner: \`$owner' does not exist!"
75             fi
76         fi
77
78         # Default group (Probably root in Linux and wheel in *BSD)
79         if [ -z "$group" ]; then
80             gid=0
81         else
82             gid="$({ grep "^$group:" "$SNF_IMAGE_TARGET/etc/group" || true; } | cut -d: -f3)"
83             if [ -z "$gid" ]; then
84                 log_error "$err_msg" "Group: \`$group' does not exist!"
85             fi
86         fi
87
88         path=( "$(dirname "$dest")" )
89         while true; do
90             parent="$(dirname "${path[0]}")"
91             if [ "$parent" = "${path[0]}" ]; then
92                 break
93             fi
94             path=( "$parent" "${path[@]}" )
95         done
96
97         for dir in "${path[@]}"; do
98             if [ -d "$SNF_IMAGE_TARGET/$dir" ]; then
99                 continue
100             elif [ -e "$SNF_IMAGE_TARGET/$dir" ]; then
101                 log_error "$err_msg" "File: \`$dir' exists and is not a directory."
102             fi
103
104             mkdir -m "$DIRMODE" "$SNF_IMAGE_TARGET/$dir"
105             chown "$uid:$gid" "$SNF_IMAGE_TARGET/$dir"
106         done
107
108         install -o "$uid" -g "$gid" -m "$mode" "$tmpdir/$src" "$SNF_IMAGE_TARGET/$dest"
109     done } < "$tmpdir/manifest"
110 fi
111
112 exit 0
113
114 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
115