~ubuntu-on-ec2/vmbuilder/automated-ec2-builds-fginther

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/bash

Usage() {
	cat <<EOF
Usage: ${0##*/} [ options ] root_dir output_dir info_file output_prefix

   copy kernels out of root_dir, put them in output_dir
   prefix names with output_prefix, write information about them to info_file

   --help | -h         output help
   --skip-ramdisks r   do not copy or record ramdisks found that match regex r
   --chown c           change perm of files copied out. pass 'c' to chown
EOF
}

my_name=$(readlink -f ${0})
my_path=$(dirname ${my_name})
export PATH="${my_path}:${PATH}"

error() { echo "$@" 1>&2; }
fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
bad_Usage() { Usage 1>&2; fail "$@"; }

short_opts="h"
long_opts="chown:,help,skip-ramdisks:"
getopt_out=$(getopt --name "${0##*/}" --shell sh \
	--options "${short_opts}" --long "${long_opts}" -- "$@") &&
	eval set -- "${getopt_out}" ||
	bad_Usage

skip_ramdisk=""
chown=""
while [ $# -ne 0 ]; do
   case $1 in
         --skip-ramdisks) skip_ramdisks=$2; shift;;
         --chown) chown=$2; shift;;
      -h|--help) Usage; exit 0;;
      --) shift; break;;
   esac
   shift;
done

root_dir=${1}
output_dir=${2:-${KRD_OUTPUT_DIR:-.}}
outfile=${3:-${KRD_OUTPUT_FILE}}
output_prefix=${4:-${KRD_PREFIX}}

[ -n "${root_dir}" -a -n "${output_dir}" ] && 
[ -n "${outfile}" -a -n "${output_prefix}" ] || bad_Usage

bootd="boot"

fail() { echo "$@" 1>&2; exit 1; }

# get a list of kernels in /boot
: > "${outfile}"
for kfp in ${root_dir}/${bootd}/vmlinu?-*; do
   [ -f "${kfp}" ] || continue
   kname=${kfp##*/}
   iname=initrd.img-${kname#vmlinu?-}
   if [ -n "${skip_ramdisks}" ]; then
      echo "${iname}" | egrep -q "${skip_ramdisks}" && iname=""
   fi

   pkg=$(dpkg --root=${root_dir} -S ${kname}); pkg=${pkg%%:*};
   [ -z "${pkg}" ] && fail "kernel ${kvp} is not packaged!"

   flav="${pkg##*-}"
   dquery_cmd=(sudo env PATH=${PATH} chroot_helper ${root_dir} dpkg-query --show)
   vers=$(${dquery_cmd[@]} --showformat="\${Version}\n" "${pkg}");
   arch=$(${dquery_cmd[@]} --showformat="\${Architecture}\n" "${pkg}");
   [ -z "${arch}" ] && fail "failed to get kernel arch info for ${kvp}"
   [ -z "${flav}" ] && fail "failed to get kernel flavour info for ${kvp}"
   [ -z "${vers}" ] && fail "failed to get kernel version info for ${kvp}"

   printf "%s\t%s\t%s\t%s\t%s\n"  "${output_prefix}vmlinuz-${flav}" "${kname}" \
                 "${arch}" "${pkg}" "${vers}" >> "${outfile}"

   [ -z "${iname}" -o ! -e "${bootd}/${ird}" ] ||
           printf "%s\t%s\t%s\t%s\t%s\n" "${output_prefix}initrd-${flav}" "${iname}" \
                  "${arch}" "${pkg}" "${vers}" >> "${outfile}"
done

[ -s "${outfile}" ] || exit 0

[ -z "$(awk '-F\t' '{print $1}' ${outfile} | sort | uniq -d)" ] || {
   cat "${outfile}" 1>&2;
   fail "multiple kernels of the same flavor were found in ${root_dir}";
}

while read tname file arch pkg vers; do
   cp "${root_dir}/${bootd}/${file}" "${output_dir}/${tname}" ||
      fail "failed to copy ${file} -> ${output_dir}/${tname}"
   { [ -z "${chown}" ] || chown "${chown}" "${output_dir}/${tname}"; } ||
      fail "failed to change ownership of ${tname} to '$chown'"
   chmod ugo+r "${output_dir}/${tname}"
done < "${outfile}"