This repository has been archived by the owner on Feb 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
build-image.sh
executable file
·188 lines (155 loc) · 4.53 KB
/
build-image.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
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
#!/bin/bash
rootdir=
find_root() {
cwd=$(pwd)
while true; do
if [[ "${cwd}" == "/" ]]; then
echo "error: unable to find root git repository"
return 1
fi
if [[ -d "${cwd}/.git" ]]; then
rootdir=${cwd}
return 0
fi
cwd=$(realpath ${cwd}/..)
done
}
usage() {
cat << EOF
usage: $0 [options]
options:
-n NAME | --name NAME Specify build name (default: aquarium).
-c | --clean Cleanup an existing build directory before building.
-t | --type IMGTYPE Specify image type (default: vagrant)
-h | --help This message.
allowed image types:
vagrant Builds a vagrant image
vagrant-virtualbox Builds a vagrant virtualbox image
self-install Builds an image to be run on bare metal.
live-iso Builds an live iso with persistent storage on bare metal.
EOF
}
usage_error_exit() {
echo "error: $1" > /dev/stderr
usage > /dev/stderr
exit 1
}
error_exit() {
echo "error: $1" > /dev/stderr
exit 1
}
find_root || exit 1
[[ -z "${rootdir}" ]] && \
error_exit "unable to find repository's root dir"
imgdir=${rootdir}/images
srcdir=${rootdir}/src
[[ ! -d "${imgdir}" ]] && \
error_exit "unable to find 'images' directory at ${rootdir}"
[[ ! -d "${srcdir}" ]] && \
error_exit "unable to find 'src' directory at ${rootdir}"
imgtype="vagrant"
build_name="aquarium"
clean=0
while [[ $# -gt 0 ]]; do
case $1 in
-n|--name)
build_name=$2
shift 1
;;
-c|--clean)
clean=1
;;
-t|--type)
imgtype=$2
shift 1
;;
-h|--help)
usage
exit 0
;;
*)
usage_error_exit "unrecognized option: '$1'"
;;
esac
shift 1
done
[[ -z "${build_name}" ]] && \
usage_error_exit "missing build name"
[[ -z "${imgtype}" ]] && \
usage_error_exit "image type must be provided"
profile=""
case ${imgtype} in
vagrant) profile="Ceph-Vagrant" type="oem";;
vagrant-virtualbox) profile="Ceph-Vagrant-VirtualBox" type="oem";;
self-install) profile="Ceph" type="oem";;
live-iso) profile="Ceph" type="iso";;
*)
usage_error_exit "unknown image type: '${imgtype}'"
;;
esac
[[ -z "${profile}" ]] && \
usage_error_exit "bad image type: '${imgtype}"
if ! kiwi-ng --version &>/dev/null ; then
error_exit "missing kiwi-ng"
fi
if ! [[ -f /sbin/mkfs.btrfs || -f /bin/mkfs.btrfs ]]; then
echo "error: missing btrfsprogs"
exit 1
fi
build=${imgdir}/build/${build_name}
if [[ -e "${build}" && "${clean}" -eq "1" ]]; then
echo "warning: removing existing build '${build_name}'"
# TODO: Figure out if there's a way of having the build directory not
# owned by root in the first place.
sudo rm -rf ${build}
fi
if [[ -e "${build}" ]]; then
error_exit "build with name '${build_name}' already exists (use --clean if you want to remove it)"
fi
set -x
mkdir -p ${build}
rm -f ${rootdir}/aquarium*.tar.gz
make dist
# At this point, we have a dist tarball (aquarium-$version.tar.gz), which
# has paths prefixed with aquarium-$version. When using the tarball inside
# kiwi though, we need it to have bare /usr/... paths, so it's extracted
# to the right place, so *sigh*, let's extract the dist tarball and
# recompress it. Bonus: this removes the version from the name, so we can
# just keep using <archive name="aquarium.tar.gz"/> in config.xml
tmpdir=$(mktemp -d)
pushd ${tmpdir}
tar --strip-components=1 -xzf ${rootdir}/aquarium*.tar.gz
sudo cp -r ${imgdir}/microOS/root/* ./
tar -czf ${build}/aquarium.tar.gz .
popd
rm -rf ${tmpdir}
tmpdir=$(mktemp -d)
pushd ${tmpdir}
sudo cp -r ${imgdir}/microOS/aquarium_root/* ./
tar -czf ${build}/aquarium_user.tar.gz .
popd
rm -rf ${tmpdir}
cp ${imgdir}/microOS/config.{sh,xml} ${build}/
mkdir ${build}/{_out,_logs}
osid=$(grep '^ID=' /etc/os-release | sed -e 's/\(ID=["]*\)\(.\+\)/\2/' | tr -d '"')
case $osid in
opensuse-tumbleweed | opensuse-leap)
(set -o pipefail
sudo kiwi-ng --debug --profile=${profile} --type ${type}\
system build --description ${build} \
--target-dir ${build}/_out |\
tee ${build}/_logs/${build_name}-build.log)
[ $? -eq 0 ] || error_exit "Kiwi build failed"
;;
debian | ubuntu)
(set -o pipefail
sudo kiwi-ng --debug --profile=${profile} --type ${type}\
system boxbuild --box tumbleweed --no-update-check -- --description ${build} \
--target-dir ${build}/_out |\
tee ${build}/_logs/${build_name}-build.log)
[ $? -eq 0 ] || error_exit "Kiwi build failed"
;;
*)
error_exit "unsupported distribution ($osid) kiwi-ng may not work"
;;
esac