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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
#!/bin/bash
#
# Authors: Scott Moser <smoser@canonical.com>
# Marcin Juszkiewicz <marcin.juszkiewicz@linaro.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
source "${0%/*}/common-functions.sh"
TEMP_D=""
UMOUNT=""
DEF_SIZE=${DEF_SIZE:-32M}
DEF_MODULES="acpiphp e1000 ne2k-pci 8139cp pcnet32 ip_tables"
Usage() {
cat <<EOF
Usage: ${0##*/} rootfs.tar kpkg.deb output_dir
[re]Bundle a buildroot rootfs into a mini-cloud image
options:
-s | --size S resize image to size (default: ${DEF_SIZE})
--arch A prepare for arch A
Example:
${0##*/} rootfs.tar linux-image-*-virtuaal*.deb build-output/
EOF
}
cleanup() {
[ -z "${UMOUNT}" ] || umount "${UMOUNT}"
[ -z "${TEMP_D}" -o ! -d "${TEMP_D}" ] || rm -Rf "${TEMP_D}"
}
xrsync() {
rsync --archive --xattrs --hard-links --acls --sparse "$@"
}
short_opts="hs:v"
long_opts="arch:,initrd-busybox:,help,size:,verbose"
getopt_out=$(getopt --name "${0##*/}" \
--options "${short_opts}" --long "${long_opts}" -- "$@") &&
eval set -- "${getopt_out}" ||
bad_Usage
topdir=$(cd "${0%/*}/.." && pwd)
size=${DEF_SIZE}
FS_LABEL="cirros-rootfs"
fs_type="ext3"
arch=""
while [ $# -ne 0 ]; do
cur=${1}; next=${2};
case "$cur" in
-h|--help) Usage; exit 0;;
-s|--size) size=${next}; shift;;
--arch) arch=${next}; shift;;
-v|--verbose) DEBUG=$((${DEBUG}+1));;
--) shift; break;;
esac
shift;
done
[ $# -eq 4 ] || bad_Usage "must give rootfs.tar, kernel pkg, grub-efi.tar, out_dir"
rootfs_in=${1}
kpkg_in=${2}
grub_efi_in=${3}
out_d_in=${4}
PATH="$topdir/bin:$PATH"
src_dir="${topdir}/src"
src_symlinks="${topdir}/symlinks.list"
makedevs_list="${topdir}/makedevs.list"
fixup_fs="${topdir}/fixup-fs"
xgrubd="$topdir/grubd"
[ "$(id -u)" = "0" ] || fail "sorry... must be root"
[ -d "${src_dir}" ] || fail "no source dir ${src_d}"
TEMP_D=$(mktemp -d "${TMPDIR:-/tmp}/.${0##*/}.XXXXXX") ||
fail "failed to make tempd"
trap cleanup EXIT
mkdir -p "${out_d_in}" && out_d=$(readlink -f "${out_d_in}") &&
rootfs=$(readlink -f "${rootfs_in}") &&
kpkg=$(readlink -f "${kpkg_in}") ||
fail "failed to get full path for input"
out_partimg="${out_d}/part.img"
out_diskimg="${out_d}/disk.img"
out_kernel="${out_d}/kernel"
out_initramfs="${out_d}/initramfs"
out_diskimg="${out_d}/disk.img"
out_blankimg="${out_d}/blank.img"
out_filesys_lxc="${out_d}/filesys.tar.gz"
inter_d="$out_d/intermediate"
mp="${TEMP_D}/mnt"
kernel_d="${TEMP_D}/kernel"
kern_list_full="${TEMP_D}/kernel.files.full"
kern_files="${TEMP_D}/kernel.files"
kern_modules="${TEMP_D}/kernel.files.modules"
overlay_d="${TEMP_D}/overlay"
initramfs_d="${TEMP_D}/initramfs"
initramfs="${TEMP_D}/initramfs.img"
stage_d="$TEMP_D/staging"
kernel_tar="$inter_d/kernel.tar"
overlay_tar="$inter_d/overlay.tar"
filesys_tar="$inter_d/filesys.tar"
modfile="$src_dir/etc/modules"
if [ -f "$modfile" ]; then
MODULES=$("$src_dir/etc/init.d/load-modules" parse_modules \
"$modfile" "$arch") ||
fail "failed to read modules"
MODULES=$(echo "$MODULES" | sed 's, .*,,' | tr '\n' ' ')
else
MODULES=${DEF_MODULES}
fi
debug 1 "modules: $MODULES"
prepare-grub "$xgrubd" ||
fail "failed to get grub binary"
mkdir -p "${mp}" "${kernel_d}" "${overlay_d}" \
"${initramfs_d}" "$inter_d" "$stage_d" ||
fail "failed to make temp dirs"
debug 1 "creating filesystem in ${out_partimg}"
rm -f "$out_partimg"
truncate "--size=${size}" "${out_partimg}" ||
fail "failed to create ${out_partimg} of size ${size}"
out=$("mkfs.${fs_type}" -F "${out_partimg}" -L "${FS_LABEL}" 2>&1) ||
fail "failed to make filesystem of type ${fs_type}: ${out}"
cp "$out_partimg" "$out_blankimg" ||
fail "failed to to copy blank partition image"
debug 1 "preparing kernel overlay"
# creating kernel tarball
case "$kpkg_in" in
*.deb)
dpkg -x "${kpkg_in}" "${kernel_d}" ||
fail "failed extracting kernel deb package in ${kpkg_in}"
;;
*.tar*)
tar -C "$kernel_d" -xf "$kpkg_in" ||
fail "failed extracting kernel tarball in ${kpkg_in}"
;;
*) fail "unknown file format for $kpkg_in";;
esac
( cd "${kernel_d}" && find * -type f ) > "${kern_list_full}" ||
fail "failed to extract kernel to ${kernel_d}"
kver=""
for x in "$kernel_d/lib/modules"/*; do
[ -d "$x/kernel" ] || continue
[ -z "$kver" ] ||
fail "2 or more things looked like kernels in lib/modules of $kpkg_in"
kver="${x##*/}"
done
[ -n "$kver" ] ||
fail "failed to find kernel version. no lib/modules/* ?"
depmod -a --basedir "${kernel_d}" "${kver}" ||
fail "failed to run depmod"
mdep="${kernel_d}/lib/modules/${kver}/modules.dep"
for x in ${MODULES}; do
grep -q "/${x}.ko" "${mdep}" ||
{ error "WARNING: no ${x} in kernel package!"; continue; }
awk -F: '$1 ~ mat {
sub(":","",$1)
printf("%s/%s\n",p,$1)
leng=split($0,deps," ")
x=2 # strange, but 0 contains nothing, 1 contains first field (with :)
while ( x<=leng ) {
printf("%s/%s\n", p, deps[x]);
x++
}
}' mat="/${x}.ko$" p="lib/modules/${kver}" "${mdep}"
done > "${kern_modules}"
sort -u "${kern_modules}" > "${kern_files}"
vmlinuz=$( cd "${kernel_d}" && [ -f boot/vmlinu?-* ] &&
echo boot/vmlinu?-* ) && echo "${vmlinuz}" >> "${kern_files}" &&
ln -sf "$vmlinuz" "$kernel_d/vmlinuz" && echo "vmlinuz" >> "$kern_files" ||
fail "no kernel (boot/vmlinuz-*) found in ${kpkg_in}"
echo "boot/config-$kver" >> "$kern_files"
tar -C "${kernel_d}" -cpf - \
--files-from "${kern_files}" > "${kernel_tar}" ||
fail "failed to collect kernel files"
debug 1 "preparing source overlay from ${src_dir}"
xrsync "${src_dir}/" "${overlay_d}" ||
fail "failed to copy source dir"
chown -R 0:0 "${overlay_d}" || fail "failed to chown files in overlay"
if [ -f "${src_symlinks}" ]; then
( cd "${overlay_d}" &&
while read src target; do
{ [ -d "${target%/*}" ] || mkdir -p "${target%/*}"; } ||
{ error "could not create ${target%/*}"; exit 1; }
ln -sf "${src}" "${target}" || exit 1
done < "${src_symlinks}"
) || fail "failed to create symlinks"
fi
if [ -f "${makedevs_list}" ]; then
xmakedevs "$makedevs_list" "$overlay_d" ||
fail "failed to makedevs on overlay"
fi
( cd "$overlay_d" && tar -cpf - * ) > "$overlay_tar" ||
fail "failed to make overlay_tar"
debug 1 "populating staging directory"
tar -C "$stage_d" -xpf - < "$rootfs_in" ||
fail "failed to extract rootfs_tar"
tar -C "$stage_d" -xpf - < "$overlay_tar" ||
fail "failed to extract overlay_tar"
if [ -x "${fixup_fs}" ]; then
"${fixup_fs}" "${stage_d}" ||
fail "failed to fixup filesystem"
fi
( cd "$stage_d" && tar -Scpzf - -- * ) > "$out_filesys_lxc" ||
fail "failed to create filesys tarball"
tar -C "$stage_d" -xpf - < "$kernel_tar" ||
fail "failed to extract kernel_tar"
tar -C "$stage_d" -xpf - < "$xgrubd/bootgrub.tar" ||
fail "failed to extract bootgrub"
depmod -a --basedir "$stage_d" "${kver}" ||
fail "failed to run depmod for kver ${kver} in output"
debug 1 "creating initramfs"
xrsync "$stage_d/" "$initramfs_d" ||
fail "failed to copy to initramfs_d"
rm -Rf "$initramfs_d/vmlinuz" "$initramfs_d/boot" ||
fail "failed to remove files in initramfs staging dir"
( cd "$initramfs_d" && find . | cpio --quiet -o -H newc |
gzip -9 ) > "$initramfs"
# Here we create /boot/initrd.img-${kver}.
# That is the initramfs that is put inside of disk images.
# Since the disk images have all the modules available in the
# filesystem, we put no modules in this initramfs. Having them
# in both places would just waste space.
#
# That means that the kernel must have builtin drivers for
# any block devices. Another path to accomplishing the same
# space savings is what cloud-initramfs-copymods does.
# The solution there is that if there is no /lib/modules/<kver>
# inside the target filesystem, then the initramfs mount binds
# its /lib/modules/<kver> into the target.
rm -Rf "$initramfs_d/lib/modules/$kver" ||
fail "failed to remove lib/modules for mini initramfs"
( cd "$initramfs_d" && find . | cpio --quiet -o -H newc |
gzip -9 ) > "${initramfs}.smaller"
cp "${initramfs}.smaller" "$stage_d/boot/initrd.img-${kver}" &&
ln -s "boot/initrd.img-${kver}" "${stage_d}/initrd.img" ||
fail "failed to copy initramfs to stage dir"
debug 1 "packing clean kernel_tar"
tar -C "$stage_d" -cpf - \
vmlinuz initrd.img boot/ lib/modules/ > "$kernel_tar" ||
fail "failed to create clean kerne_tar"
( cd "$stage_d" && tar -cpf - * ) > "$filesys_tar" ||
fail "failed to create filesys_tar"
debug 1 "populating image"
mount -o loop "${out_partimg}" "${mp}" && UMOUNT=${mp} ||
fail "failed to mount ${out_partimg} loopback"
tar -C "$mp" -xpf - < "$filesys_tar" ||
fail "failed to populate mount point"
umount "${mp}" && UMOUNT="" ||
fail "failed to unmount ${out_partimg}"
cp "${kernel_d}/${vmlinuz}" "${out_kernel}" ||
fail "failed to copy kernel to ${out_kernel}"
{ [ -z "${out_initramfs}" ] || cp "${initramfs}" "${out_initramfs}"; } ||
fail "failed to copy initramfs to ${out_initramfs}"
debug 1 "fixing grub entry in partimg"
tmp_part="$TEMP_D/part.img.disk"
cp "$out_partimg" "$tmp_part" &&
mount -o loop "$tmp_part" "$mp" && UMOUNT="$mp" ||
fail "failed to mount $tmp_part"
sed -i 's/(hd0)/(hd0,0)/' "$mp/boot/grub/menu.lst" ||
fail "failed to edit /boot/grub/menu.lst in image"
umount "$mp" && UMOUNT="" ||
fail "failed to unmount partimg"
case $arch in
i386|x86_64) grub_options="--grub1 --grub-efi";;
arm|aarch64) grub_options="--grub-efi";;
*) grub_options=;;
esac
debug 1 "creating disk image"
out=$(PATH=$xgrubd:$PATH part2disk $grub_options "$tmp_part" \
"$grub_efi_in" "$arch" "$out_diskimg.raw" 2>&1) ||
fail "failed to create disk image: $out"
qemu-img convert -O qcow2 -c "$out_diskimg.raw" "$out_diskimg" ||
fail "failed to convert disk image"
rm -f "$out_diskimg.raw" "$tmp_part"
if [ -n "${SUDO_USER}" ]; then
u=${SUDO_USER}
g=$(id -g "${u}") || g=${u}
chown "${u}:${g}" -R "$out_d" ||
fail "failed to grant ownership of ${u}:${g} to ${u}:${g}"
fi
echo "wrote ${out_partimg}"
echo "wrote ${out_diskimg}"
echo "wrote ${out_kernel}"
echo "wrote ${out_initramfs}"
echo "wrote ${out_blankimg}"
echo "wrote ${out_filesys_lxc}"
exit 0
# vi: tabstop=4 expandtab
|