-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-ubuntu-rootfs.sh
executable file
·354 lines (308 loc) · 7.48 KB
/
create-ubuntu-rootfs.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
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
347
348
349
350
351
352
353
354
# Needs support for building arm rootfs on x86_64.
# See qemu-static: https://gnu-linux.org/building-ubuntu-rootfs-for-arm.html.
function help {
echo "Usage: ./create-ubuntu-rootfs [params]"
echo
echo "Mandatory:"
echo "-r release - Specify the release name (e.g '-r xenial')."
echo "-v - The release version which will be downloaded (e.g '-v 16.04' - coresponds to 'xenial' release).'"
echo "-s (digit+)(M(egabytes)|G(igabytes)) - Specify the size of the ext4 rootfs image. Minimal size is 200M."
echo
echo "Optional:"
echo "-o path - Rootfs image path."
echo "--help"
echo "--list-releases - List of the available Ubuntu releases."
echo "--list-release-versions release - List the versions for a specific release (e.g '--list-release-versions xenial')."
echo "--cleanup - Removes the Ubuntu base release from '/tmp'."
echo "--reuse-cache - Reuse the base ubuntu archive, without downloading it."
echo "--base-path path - Path of the base ubuntu archive to be used."
echo ""
exit 1
}
function log_stdout {
echo "$1"
}
function log_stderr {
>&2 echo "E: $1"
}
function list_releases {
if [[ $RELEASE_LIST == '' ]]
then
RELEASE_LIST=`xmllint --html http://cdimage.ubuntu.com/ubuntu-base/releases/ --xpath "/html/body/table" | tail -n +5 | head -n -2 | sed -r 's/.*<a href="(.*)\/">.*/\1/' | sort -r | sed -r 's/[0-9]+\.[0-9]+(\.[0-9]+)?//g' | sed -r '/^\s*$/d'`
fi
log_stdout "$RELEASE_LIST"
}
function list_release_versions {
release=$1
release_registry_url=http://cdimage.ubuntu.com/ubuntu-base/releases/${release}/release/
VERSION_LIST=`xmllint --html ${release_registry_url} --nowarning 2> /dev/null | sed -r 's/.*<a href="(.*\.tar.gz)">(.*)<\/a>.*/\1/' | sed -r 's/^\s*<.*$//' | sed -r '/^\s*$/d' | sed -n '/^ubuntu-base-/p' | sed -r 's/^ubuntu-base-([0-9]+\.[0-9]+(\.[0-9]+)?).*$/\1/' | uniq`
log_stdout "$VERSION_LIST"
}
function download_ubuntu_base {
release=$1
version=$2
UBUNTU_BASE_TAR_GZ="ubuntu-base-$version-core-amd64.tar.gz"
wget "cdimage.ubuntu.com/ubuntu-base/releases/$release/release/${UBUNTU_BASE_TAR_GZ}" -P /tmp
if [ $? == 0 ]
then
UBUNTU_BASE_TAR_GZ_PATH=/tmp/${UBUNTU_BASE_TAR_GZ}
else
log_stderr "Failed downloading the Ubuntu base ${RELEASE} (${VERSION}) release."
exit 1
fi
}
function create_ext4_image {
dd if=/dev/zero bs=1${UNIT} count=${SIZE} of=${ROOTFS_IMAGE_PATH}
if [ $? == 0 ]
then
sudo mkfs.ext4 ${ROOTFS_IMAGE_PATH}
if [ $? != 0 ]
then
log_stderr "EXT4 creation failed: exit code $?."
exit 1
fi
else
log_stderr "EXT4 creation failed: exit code $?."
exit 1
fi
}
# Priviledged function.
function validate_build_args {
if [[ $RELEASE == '' ]] || [[ $SIZE == '' ]] || [[ $UNIT == '' ]] || [[ $VERSION == '' ]]
then
echo "E: Incorrect usage. Missing mandatory arguments."
echo
help
exit 1
fi
if [[ $ROOTFS_IMAGE_PATH == '' ]]
then
ROOTFS_IMAGE_PATH=$PWD/ubuntu-${RELEASE}-${VERSION}.rootfs.ext4
fi
}
function cleanup {
rm -f ${UBUNTU_BASE_TAR_GZ_PATH}
}
# Priviledged function.
function extract_ubuntu_base_into_ext4_image {
log_stdout "Mount location (enter for default - /mnt):"
read MOUNT_DIR
if [[ $MOUNT_DIR == '' ]]
then
MOUNT_DIR=/mnt
fi
sudo mount ${ROOTFS_IMAGE_PATH} ${MOUNT_DIR}
sudo tar -xvf ${UBUNTU_BASE_TAR_GZ_PATH} -C ${MOUNT_DIR}
sudo umount ${MOUNT_DIR}
}
function parse_args {
for arg in $@
do
if [[ $wait_for_base_path == 1 ]]
then
[[ -f ${arg} ]]
if [ $? != 0 ]
then
log_stderr "The archive does not exist: `{$arg}`."
fi
out=`tar -tf ${arg} 2>&1`
if [ $? != 0 ]
then
log_stderr "Can not query the contents of `${arg}`. The archive may be corrupted."
fi
if [[ $out == '' ]]
then
log_stderr "The archive is empty: `${arg}`."
fi
UBUNTU_BASE_TAR_GZ_PATH=$arg
UBUNTU_BASE_TAR_GZ=`basename -- ${UBUNTU_BASE_TAR_GZ_PATH}`
wait_for_base_path=0
continue
fi
if [[ $wait_release == 1 ]]
then
RELEASE_LIST=`list_releases`
echo $RELEASE_LIST | grep $arg &> /dev/null
if [ $? != 0 ]
then
log_stderr "Unkown ubuntu release: '${arg}'."
exit 1
fi
RELEASE=$arg
wait_release=0
if [[ $check_version != '' ]]
then
VERSION_LIST=`list_release_versions $RELEASE`
echo $VERSION_LIST | grep $check_version &> /dev/null
if [ $? != 0 ]
then
log_stderr "Unknown ubuntu release version: '${arg}'."
exit 1
fi
VERSION=$check_version
check_version=''
fi
continue
fi
if [[ $wait_version == 1 ]]
then
if [[ $RELEASE != '' ]]
then
VERSION_LIST=`list_release_versions $RELEASE`
else
check_version=$arg
wait_version=0
continue
fi
echo $VERSION_LIST | grep $arg &> /dev/null
if [ $? != 0 ]
then
log_stderr "Unknown ubuntu release version: '${arg}'."
exit 1
fi
VERSION=$arg
wait_version=0
continue
fi
if [[ $wait_versions_release == 1 ]]
then
RELEASE_LIST=`list_releases`
echo $RELEASE_LIST | grep $arg &> /dev/null
if [ $? != 0 ]
then
log_stderr "Unkown ubuntu release: '${arg}'."
exit 1
fi
list_release_versions "$arg"
wait_versions_release=0
exit 0
fi
if [[ $wait_rootfs_image_path == 1 ]]
then
ROOTFS_IMAGE_PATH=$arg
wait_rootfs_image_path=0
continue
fi
if [[ $wait_size == 1 ]]
then
SIZE=`echo "START${arg}END" | sed -r 's/^START(([2-9][0-9][0-9])|([1-9][0-9][0-9][0-9]+)).*END$/\1/'`
if [ $SIZE == "START${arg}END" ]
then
log_stderr "Rootfs size must be a valid positive number greater than '200'."
exit 1
fi
UNIT=`echo "START${arg}END" | sed -r 's/^START.+([M|G])END$/\1/'`
if [ $UNIT == "START${arg}END" ]
then
log_stderr "Rootfs size valid units are: M(egabytes) and G(igabytes)."
exit 1
fi
wait_size=0
continue
fi
if [ $# == 0 ]
then
help
exit 0
fi
if [ $arg == "--help" ] && [ $# == 1 ]
then
help
exit 0
else
if [ $# != 1 ] && [ $arg == "--help" ]
then
help
exit 1
fi
fi
if [ $arg == "--list-releases" ] && [ $# == 1 ]
then
list_releases
exit 0
else
if [ $# != 1 ] && [ $arg == "--list-releases" ]
then
help
exit 1
fi
fi
if [ $arg == "--list-release-versions" ] && [[ $# == 2 ]]
then
wait_versions_release=1
else
if [ $# != 2 ] && [ $arg == "--list-release-versions" ]
then
help
exit 1
fi
fi
if [ $arg == "-r" ] && [[ $RELEASE == '' ]]
then
wait_release=1
continue
fi
if [ $arg == "-v" ]
then
wait_version=1
fi
if [ $arg == "-s" ]
then
wait_size=1
continue
fi
if [ $arg == "-o" ]
then
wait_rootfs_image_path=1
continue
fi
if [ $arg == "--cleanup" ]
then
CLEANUP=1
continue
fi
if [ $arg == "--reuse-cache" ] && [[ $UBUNTU_BASE_TAR_GZ_PATH == '' ]]
then
REUSE_CACHE=1
continue
fi
if [ $arg == "--base-path" ]
then
wait_for_base_path=1
REUSE_CACHE=0
fi
done
if [[ $check_version != '' ]]
then
help
exit 1
fi
}
# Priviledged function.
function build_rootfs {
validate_build_args
if [[ $REUSE_CACHE == 1 ]]
then
UBUNTU_BASE_TAR_GZ_PATH=/tmp/ubuntu-base-${VERSION}-core-amd64.tar.gz
UBUNTU_BASE_TAR_GZ=`basename -- ${UBUNTU_BASE_TAR_GZ_PATH}`
[[ -f ${UBUNTU_BASE_TAR_GZ_PATH} ]]
if [ $? != 0 ]
then
log_stdout "Base archive from ${UBUNTU_BASE_TAR_GZ_PATH} does not exist."
download_ubuntu_base ${RELEASE} ${VERSION}
fi
else
download_ubuntu_base ${RELEASE} ${VERSION}
fi
create_ext4_image
extract_ubuntu_base_into_ext4_image
if [[ $CLEANUP == 1 ]]
then
cleanup
fi
}
function main {
parse_args $@
build_rootfs
}
main $@