Fix a dangerous-use-of-xargs bug in create
[snf-image] / snf-image-helper / common.sh
1 # Copyright 2011 GRNET S.A. All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions
5 # are met:
6 #
7 #   1. Redistributions of source code must retain the above copyright
8 #      notice, this list of conditions and the following disclaimer.
9 #
10 #  2. Redistributions in binary form must reproduce the above copyright
11 #     notice, this list of conditions and the following disclaimer in the
12 #     documentation and/or other materials provided with the distribution.
13 #
14 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 # ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 # SUCH DAMAGE.
25 #
26 # The views and conclusions contained in the software and documentation are
27 # those of the authors and should not be interpreted as representing official
28 # policies, either expressed or implied, of GRNET S.A.
29
30 RESULT=/dev/ttyS1
31 FLOPPY_DEV=/dev/fd0
32
33 PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
34
35 # Programs
36 XMLSTARLET=xmlstarlet
37 RESIZE2FS=resize2fs
38
39 CLEANUP=( )
40
41 add_cleanup() {
42     local cmd=""
43     for arg; do cmd+=$(printf "%q " "$arg"); done
44     CLEANUP+=("$cmd")
45 }
46
47 log_error() {
48     echo "ERROR: $@" | tee $RESULT >&2
49     exit 1
50 }
51
52 get_base_distro() {
53     local root_dir=$1
54
55     if [ -e "$root_dir/etc/debian_version" ]; then
56         echo "debian"
57     elif [ -e "$root_dir/etc/redhat-release" ]; then
58         echo "redhat"
59     elif [ -e "$root_dir/etc/slackware-version" ]; then
60         echo "slackware"
61     elif [ -e "$root_dir/SuSE-release" ]; then
62         echo "suse"
63     elif [ -e "$root_dir/gentoo-release" ]; then
64         echo "gentoo"
65     fi
66 }
67
68 get_distro() {
69     local root_dir=$1
70
71     if [ -e "$root_dir/etc/debian_version" ]; then
72         distro="debian"
73         if [ -e ${root_dir}/etc/lsb-release ]; then
74             ID=$(grep ^DISTRIB_ID= ${root_dir}/etc/lsb-release | cut -d= -f2)
75             if [ "x$ID" = "xUbuntu" ]; then
76                 distro="ubuntu"
77             fi
78         fi
79         echo "$distro"
80     elif [ -e "$root_dir/etc/fedora-release" ]; then
81         echo "fedora"
82     elif [ -e "$root_dir/etc/centos-release" ]; then
83         echo "centos"
84     elif [ -e "$root_dir/etc/redhat-release" ]; then
85         echo "redhat"
86     elif [ -e "$root_dir/etc/slackware-version" ]; then
87         echo "slackware"
88     elif [ -e "$root_dir/SuSE-release" ]; then
89         echo "suse"
90     elif [ -e "$root_dir/gentoo-release" ]; then
91         echo "gentoo"
92     fi
93 }
94
95 cleanup() {
96     # if something fails here, it shouldn't call cleanup again...
97     trap - EXIT
98
99     if [ ${#CLEANUP[*]} -gt 0 ]; then
100         LAST_ELEMENT=$((${#CLEANUP[*]}-1))
101         REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
102         for i in $REVERSE_INDEXES; do
103             # If something fails here, it's better to retry it for a few times
104             # before we give up with an error. This is needed for kpartx when
105             # dealing with ntfs partitions mounted through fuse. umount is not
106             # synchronous and may return while the partition is still busy. A
107             # premature attempt to delete partition mappings through kpartx on a
108             # device that hosts previously mounted ntfs partition may fail with
109             # a `device-mapper: remove ioctl failed: Device or resource busy'
110             # error. A sensible workaround for this is to wait for a while and
111             # then try again.
112             local cmd=${CLEANUP[$i]}
113             $cmd || for interval in 0.25 0.5 1 2 4; do
114             echo "Command $cmd failed!"
115             echo "I'll wait for $interval secs and will retry..."
116             sleep $interval
117             $cmd && break
118         done
119         if [ "$?" != "0" ]; then
120             echo "Giving Up..."
121             exit 1;
122         fi
123     done
124   fi
125 }
126
127 trap cleanup EXIT
128
129 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :