forked from fghaas/openstacksummit2014-atlanta
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild-lib.sh
126 lines (107 loc) · 3.37 KB
/
build-lib.sh
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
#!/bin/bash
# ========================================================================
# Shell functions for building Crowbar appliances
#
# Requires Kiwi (http://opensuse.github.io/kiwi/).
#
# Build performance:
# - Automatically builds in RAM (tmpfs) if there's enough free memory. This
# brings the build time down significantly, especially without SSDs.
# - Automatically creates and uses Kiwi's boot image cache, which saves about 1
# min with tmpfs.
# - Had added support for Kiwi's image cache, but this had negligible speed-up
# in my setup so removed the code to reduce complexity.
# - Had added auto-detection of pigz (parallel gzip), but this also had
# negligible speed-up in my setup so excluded it too.
#
# ========================================================================
function ensure_root {
if [ $USER != 'root' ]; then
echo "Please run as root."
exit 1
fi
}
function check_kiwi {
ensure_root
kiwi=`command -v kiwi`
if [ $? -ne 0 ]; then
echo "Kiwi is required but not found on your system."
echo "Run the following command to install kiwi:"
echo
echo " zypper install kiwi kiwi-tools kiwi-desc-vmxboot"
echo
exit 1
fi
}
function warn {
echo >&2 -e "$*"
}
function unclean_exit {
local exit_code=$?
warn "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
warn "\nWARNING: premature termination!"
if df $TMP_DIR | egrep -q "^tmpfs"; then
warn "\nLeaving $TMP_DIR mounted."
warn "You must umount it yourself in order to free RAM."
fi
warn "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
exit $exit_code
}
function clean_up {
echo >&2 -e "$*"
# Only clean up once
[ "$CLEAN" ] && return
CLEAN=true
echo "** Cleaning up"
# Save the image and log files
mkdir -p $OUTPUT_DIR
mv $TMP_DIR/{build/image-root.log,*.qcow2,*.vmdk} $OUTPUT_DIR 2>/dev/null || true
local USER=`stat -c %U .`
local GROUP=`stat -c %G .`
chown -R $USER.$GROUP $OUTPUT_DIR
# Save initrd as boot image cache
rsync -ql $TMP_DIR/initrd-* $BOOT_CACHE_DIR 2>/dev/null || true
if df $TMP_DIR | egrep -q "^tmpfs"; then
umount $TMP_DIR
fi
rm -rf $TMP_DIR
}
function create_tmpfs {
mkdir -p $TMP_DIR
local ram_required=$((TMPFS_SIZE))
local free_ram=`free -m | awk '/^-\/\+ buffers/{print $4}'`
echo "** Free RAM: $free_ram MB; RAM required: $ram_required MB"
if [ "$free_ram" -lt "$ram_required" ]; then
echo "** tmpfs: Skipping, insufficient free RAM"
return
fi
if df $TMP_DIR | egrep -q "^tmpfs"; then
echo "** tmpfs: Reusing existing mount point"
else
echo "** tmpfs: Creating new volume ($TMPFS_SIZE MB)"
mount -t tmpfs -o size=${TMPFS_SIZE}M,noatime tmpfs $TMP_DIR
fi
}
function fill_config_xml_template () {
# crowbar-prep.sh uses HOST_MIRROR for the same purpose,
# so reuse that as a default if it's set.
: ${MIRRORS:=${HOST_MIRROR:-/data/install/mirrors}}
sed "s,@@MIRRORS@@,$MIRRORS," $here/source/config.xml.tmpl \
> $here/source/config.xml
}
function run_kiwi {
if [ -z "$NO_TMPFS" ]; then
create_tmpfs
fi
mkdir -p $BOOT_CACHE_DIR
echo "** Running kiwi (with $BOOT_CACHE_DIR as boot image cache)"
time $kiwi --build source/ -d $TMP_DIR --prebuiltbootimage $BOOT_CACHE_DIR "$@"
echo "** Appliance created successfully!"
}
build_image () {
check_kiwi
trap unclean_exit SIGINT SIGTERM
fill_config_xml_template
run_kiwi "$@"
clean_up
}