17
17
Create a disk for cloud-init to utilize nocloud
20
-h | --help show usage
21
-d | --disk-format D disk format to output. default: raw
22
-H | --hostname H set hostname in metadata to H
23
-f | --filesystem F filesystem format (vfat or iso), default: iso9660
20
-h | --help show usage
21
-d | --disk-format D disk format to output. default: raw
22
can be anything supported by qemu-img or
23
tar, tar-seed-local, tar-seed-net
24
-H | --hostname H set hostname in metadata to H
25
-f | --filesystem F filesystem format (vfat or iso), default: iso9660
25
-i | --interfaces F write network interfaces file into metadata
26
-m | --dsmode M add 'dsmode' ('local' or 'net') to the metadata
27
default in cloud-init is 'net', meaning network is
29
-v | --verbose increase verbosity
27
-i | --interfaces F write network interfaces file into metadata
28
-N | --network-config F write network config file to local datasource
29
-m | --dsmode M add 'dsmode' ('local' or 'net') to the metadata
30
default in cloud-init is 'net', meaning network is
32
-V | --vendor-data F vendor-data file
33
-v | --verbose increase verbosity
31
35
Note, --dsmode, --hostname, and --interfaces are incompatible
59
short_opts="hH:i:d:f:m:o:v"
60
long_opts="disk-format:,dsmode:,filesystem:,help,hostname:,interfaces:,output:,verbose"
63
short_opts="hH:i:d:f:m:N:o:V:v"
64
long_opts="disk-format:,dsmode:,filesystem:,help,hostname:,interfaces:,"
65
long_opts="${long_opts}network-config:,output:,vendor-data:,verbose"
61
66
getopt_out=$(getopt --name "${0##*/}" \
62
67
--options "${short_opts}" --long "${long_opts}" -- "$@") &&
63
68
eval set -- "${getopt_out}" ||
70
filesystem=$DEF_FILESYSTEM
71
77
diskformat=$DEF_DISK_FORMAT
81
ncname="network-config"
77
84
while [ $# -ne 0 ]; do
78
85
cur=${1}; next=${2};
80
87
-h|--help) Usage ; exit 0;;
81
-v|--verbose) VERBOSITY=$((${VERBOSITY}+1));;
82
88
-d|--disk-format) diskformat=$next; shift;;
83
89
-f|--filesystem) filesystem=$next; shift;;
90
-H|--hostname) hostname=$next; shift;;
91
-i|--interfaces) interfaces=$next; shift;;
92
-N|--network-config) netcfg=$next; shift;;
84
93
-m|--dsmode) dsmode=$next; shift;;
85
-i|--interfaces) interfaces=$next; shift;;
86
-H|--hostname) hostname=$next; shift;;
94
-v|--verbose) VERBOSITY=$((${VERBOSITY}+1));;
95
-V|--vendor-data) vendordata="$next";;
104
113
"--interfaces, --hostname, --dsmode"
116
case "$diskformat" in
117
tar|tar-seed-local|tar-seed-net)
118
if [ "${filesystem:-tar}" != "tar" ]; then
119
fail "diskformat=tar is incompatible with filesystem"
121
filesystem="$diskformat"
124
fail "supported 'tar' formats are tar, tar-seed-local, tar-seed-net"
127
if [ -z "$filesystem" ]; then
128
filesystem="$DEF_FILESYSTEM"
107
131
[ "$interfaces" = "_unset" -o -r "$interfaces" ] ||
108
132
fail "$interfaces: not a readable file"
131
156
printf "{\n%s\n}\n" "$mdata" > "${TEMP_D}/meta-data"
159
if [ -n "$netcfg" ]; then
160
cp "$netcfg" "${TEMP_D}/$ncname" ||
161
fail "failed to copy network config"
162
files[${#files[@]}]="$TEMP_D/$ncname"
165
if [ -n "$vendordata" ]; then
166
cp "$vendordata" "${TEMP_D}/vendor-data" ||
167
fail "failed to copy vendor data"
168
files[${#files[@]}]="$TEMP_D/vendor-data"
172
for f in "${files[@]}"; do
173
files_rel[${#files_rel[@]}]="${f#${TEMP_D}/}"
134
176
if [ "$userdata" = "-" ]; then
135
177
cat > "$TEMP_D/user-data" || fail "failed to read from stdin"
140
182
## alternatively, create a vfat filesystem with same files
141
img="$TEMP_D/seed.img"
142
truncate --size 100K "$img" || fail "failed truncate image"
183
img="$TEMP_D/seed-data"
184
tar_opts=( --owner=root --group=root )
188
truncate --size 100K "$img" || fail "failed truncate image";;
144
191
case "$filesystem" in
193
tar "${tar_opts[@]}" -C "${TEMP_D}" -cf "$img" "${files_rel[@]}" ||
194
fail "failed to create tarball of ${files_rel[*]}"
196
tar-seed-local|tar-seed-net)
197
if [ "$filesystem" = "tar-seed-local" ]; then
198
path="var/lib/cloud/seed/nocloud"
200
path="var/lib/cloud/seed/nocloud-net"
202
mkdir -p "${TEMP_D}/${path}" ||
203
fail "failed making path for seed files"
204
mv "${files[@]}" "${TEMP_D}/$path" ||
205
fail "failed moving files"
206
tar "${tar_opts[@]}" -C "${TEMP_D}" -cf "$img" "${path}" ||
207
fail "failed to create tarball with $path"
146
210
genisoimage -output "$img" -volid cidata \
147
-joliet -rock "$TEMP_D/user-data" "$TEMP_D/meta-data" \
148
> "$TEMP_D/err" 2>&1 ||
211
-joliet -rock "${files[@]}" > "$TEMP_D/err" 2>&1 ||
149
212
{ cat "$TEMP_D/err" 1>&2; fail "failed to genisoimage"; }
215
truncate --size 100K "$img" || fail "failed truncate image"
152
216
mkfs.vfat -n cidata "$img" || fail "failed mkfs.vfat"
153
mcopy -oi "$img" "$TEMP_D/user-data" "$TEMP_D/meta-data" :: ||
217
mcopy -oi "$img" "${files[@]}" :: ||
154
218
fail "failed to copy user-data, meta-data to img"
156
220
*) fail "unknown filesystem $filesystem";;
159
223
[ "$output" = "-" ] && output="$TEMP_D/final"
160
if [ "$diskformat" != "raw" ]; then
224
if [ "${diskformat#tar}" != "$diskformat" -o "$diskformat" = "raw" ]; then
225
cp "$img" "$output" ||
226
fail "failed to copy image to $output"
161
228
qemu-img convert -f raw -O "$diskformat" "$img" "$output" ||
162
229
fail "failed to convert to disk format $diskformat"
164
cp "$img" "$output" ||
165
fail "failed to copy image to $output"
168
232
[ "$output" != "$TEMP_D/final" ] || { cat "$output" && output="-"; } ||