The cirros image was rebuilt against the 3.13.0-83 kernel, drivers e1000e, igbvf...
[packages/trusty/cirros-testvm.git] / cirros-testvm / src-cirros / src / lib / cirros / ds / nocloud
1 #!/bin/sh
2
3 VERBOSITY=1
4 CONFIG="/etc/cirros-init/ds-nocloud"
5 SEED_PRE_D="/var/lib/cloud/seed/nocloud-pre"
6 SEED_POST_D="/var/lib/cloud/seed/nocloud"
7 NAME="${0##*/}"
8 LABEL="cidata"
9
10 . ${CIRROS_SHLIB:=/lib/cirros/shlib} ||
11         { echo "failed to read ${CIRROS_SHLIB}" 1>&2; exit 1; }
12
13 Usage() {
14         cat <<EOF
15 Usage: ${0##*/} mode output_d
16
17    Datasource for 'nocloud' config disk.
18 EOF
19 }
20
21 search_local() {
22         local out_d="$1"
23         local devlist="" num="" found="" dev="" tmpd="" found="" f=""
24         find_devs_with "LABEL=$LABEL" ||
25                 { error "failed to find devs"; return 1; }
26
27         devlist=${_RET}
28
29         if [ -d "${SEED_PRE_D}" ]; then
30                 devlist="$SEED_PRE_D $devlist"
31         fi
32         if [ -d "${SEED_POST_D}" ]; then
33                 devlist="$devlist $SEED_POST_D"
34         fi
35
36         [ -n "$devlist" ] || { debug 2 "no devices labeled $LABEL"; return 0; }
37
38         num=0
39         for dev in ${devlist}; do num=$(($num+1)); done
40         [ $num -eq 1 ] || debug 1 "multiple devices matching $LABEL: $devlist"
41
42         [ -d "$out_d" ] || mkdir -p "$out_d" ||
43                 { error "failed to create outputdir: ${out_d}"; return 1; }
44
45         found=""
46         tmpd="${out_d}/raw"
47         data_d="${out_d}/data"
48         for dev in ${devlist}; do
49                 rm -Rf "$tmpd"
50                 if [ -b "$dev" ]; then
51                         mount_callback_umount "$dev" -o,ro cp -a "${tmpd}" ||
52                                 { debug 1 "mount callback umount $dev failed"; continue; }
53                 else
54                         cp -a "$dev" "${tmpd}" ||
55                                 { debug 1 "failed to copy from $dev"; continue; }
56                 fi
57
58                 [ -f "${tmpd}/meta-data" ] ||
59                         { debug 2 "$dev had no meta-data"; continue; }
60
61                 json2fstree "${data_d}" "${tmpd}/meta-data" || {
62                         error "json2fstree failed on ${tmpd}/meta-data from $dev"
63                         return 1;
64                 }
65
66                 [ ! -f "${tmpd}/user-data" ] || cp "${tmpd}/user-data" "${data_d}" ||
67                         { error "failed to copy user-data from $dev"; return 1; }
68
69                 [ -d "${tmpd}/files" ] && cp -a "${tmpd}/files" "${data_d}"
70
71                 found="$dev"
72                 rm -Rf "$tmpd"
73                 break
74         done
75
76         [ -z "$found" ] && return 0
77
78         # now we have filesystem rendering at $fstree_d
79         # and raw data (copy of config drive data) at $raw_d
80         mkdir -p "${out_d}/data" ||
81                 fail "failed to make data dir"
82
83         if [ ! -f "${data_d}/instance-id" ]; then
84                 echo "i-nocloud-default" > "${data_d}/instance-id"
85         fi
86         if [ -d "${data_d}/public-keys" ]; then
87                 # turn public-keys list into 
88                 mv "${data_d}/public-keys" "${out_d}/public-keys-d";
89                 set +f
90                 for f in "${out_d}/public-keys-d/"*; do
91                         [ -f "${f}" -a "${f##*/}" != "0" ] && cat "$f"
92                 done  > "${data_d}/public-keys"
93                 set -f
94         fi
95
96         echo 0 > "$out_d/result"
97 }
98
99 apply() {
100         local mode="$1" data_d="$2"
101         # support copying 'files' if it happened to be on that disk
102         if [ -d "$data_d/files" ]; then
103                 local omask="" f="" path="" tpath=""
104                 omask=$(umask)
105                 umask 0226
106                 for f in $(find "$data_d/files/" -type f); do
107                         path="${f#${data_d}/files/}"
108                         tpath="${TARGET_ROOT}/${path}"
109                         mkdir -p "${tpath%/*}" && cp "${f}" "${tpath}" ||
110                                 { error "failed to create ${tpath}"; return 1; }
111                 done
112                 umask "$omask"
113         fi
114 }
115
116 short_opts="hv"
117 long_opts="help,verbose"
118 getopt_out=$(getopt --name "${0##*/}" \
119         --options "${short_opts}" --long "${long_opts}" -- "$@") &&
120         eval set -- "${getopt_out}" ||
121         bad_Usage
122
123 while [ $# -ne 0 ]; do
124         cur=${1}; next=${2};
125         case "$cur" in
126                 -h|--help) Usage ; exit 0;;
127                 -v|--verbose) VERBOSITY=$((${VERBOSITY}+1));;
128                 --) shift; break;;
129         esac
130         shift;
131 done
132
133 [ $# -eq 2 ] || bad_Usage "must provide mode and output dir"
134 mode="$1"
135 out_d="$2"
136
137 [ "$mode" = "local" -o "$mode" = "apply-local" ] ||
138         { debug 2 "only supported in mode 'local'"; exit 0; }
139
140 [ ! -e "$CONFIG" ] || . "$CONFIG" ||
141         fail "failed to read $CONFIG"
142
143 if [ "$mode" = "local" ]; then
144         search_local "$out_d"
145 elif [ "$mode" = "apply-local" ]; then
146         apply "$mode" "$out_d"
147 else
148         fail "error, unexpected input"
149 fi
150
151 exit
152 # vi: ts=4 noexpandtab