/
pack-into-archiso.sh
executable file
·390 lines (371 loc) · 13.6 KB
/
pack-into-archiso.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/usr/bin/env bash
# -*- coding: utf-8 -*-
# region header
# Copyright Torben Sickert (info["~at~"]torben.website) 16.12.2012
# License
# -------
# This library written by Torben Sickert stand under a creative commons naming
# 3.0 unported license. See https://creativecommons.org/licenses/by/3.0/deed.de
# endregion
# shellcheck disable=SC2016,SC2034,SC2155
# region import
if [ -f "$(dirname "${BASH_SOURCE[0]}")/node_modules/bashlink/module.sh" ]; then
# shellcheck disable=SC1090
source "$(dirname "${BASH_SOURCE[0]}")/node_modules/bashlink/module.sh"
elif [ -f "/usr/lib/bashlink/module.sh" ]; then
# shellcheck disable=SC1091
source "/usr/lib/bashlink/module.sh"
else
echo Needed bashlink library not found 1>&2
exit 1
fi
bl.module.import bashlink.logging
bl.module.import bashlink.tools
# endregion
# region variables
declare -gr pia__documentation__='
This module modifies a given arch linux iso image.
Packs the current pack-into-archiso.bash script into the archiso image.
Remaster archiso file.
```bash
pack-into-archiso ./archiso.iso ./remasteredArchiso.iso
```
Remaster archiso file verbosely.
```bash
pack-into-archiso ./archiso.iso ./remasteredArchiso.iso --verbose
```
Show help message.
```bash
pack-into-archiso --help
```
'
declare -agr pia__dependencies__=(
bash
cdrkit
grep
mktemp
mount
readlink
rm
squashfs-tools
touch
umount
)
declare -agr pia__optional_dependencies__=(
'sudo: Perform action as another user.'
'arch-install-scripts: Supports to perform an arch-chroot.'
)
## region commandline arguments
declare -g pia_squash_filesystem_compressor=gzip
declare -g pia_keyboard_layout=de-latin1
declare -g pia_key_map_configuration_file_content="KEYMAP=${pia_keyboard_layout}"$'\nFONT=Lat2-Terminus16\nFONT_MAP='
## endregion
declare -g pia_source_path=''
declare -g pia_target_path=''
declare -g pia_mountpoint_path="$(mktemp --directory)"
declare -g pia_temporary_remastering_path="$(mktemp --directory)"
declare -g pia_temporary_filesystem_remastering_path="$(
mktemp --directory
)/mnt"
declare -g pia_temporary_root_filesystem_remastering_path="$(
mktemp --directory)"
declare -ag pia_relative_paths_to_squash_filesystem=(
arch/i686/root-image.fs.sfs
arch/x86_64/root-image.fs.sfs
)
declare -g pia_relative_source_file_path=archInstall.sh
declare -g pia_relative_target_file_path=usr/bin/
declare -g pia_bashrc_code=$'\nalias getInstallScript='"'wget https://goo.gl/bPAqXB --output-document archInstall.sh && chmod +x archInstall.sh'"$'\nalias install='"'([ -f /root/archInstall.sh ] || getInstallScript);/root/archInstall.sh'"
bl_module_scope_rewrites+=(
'^pack[._]into[._]archiso([._][a-zA-Z_-]+)?$/pia\1/'
)
# endregion
# region functions
## region command line interface
alias pia.get_commandline_option_description=pia_get_commandline_option_description
pia_get_commandline_option_description() {
local -r __documentation__='
Prints descriptions about each available command line option.
>>> pia.get_commandline_option_description
+bl.doctest.contains
+bl.doctest.multiline_ellipsis
-h --help Shows this help message.
...
'
# NOTE: "-k" and "--key-map-configuration" isn't needed in the future.
cat << EOF
-h --help Shows this help message.
-v --verbose Tells you what is going on (default: "false").
-d --debug Gives you any output from all tools which are used (default: "false").
-c --squash-filesystem-compressor Defines the squash filesystem compressor. All supported compressors for "mksquashfs" are possible (default: "$pia_squash_filesystem_compressor").
-k --keyboard-layout Defines needed key map (default: "$pia_keyboard_layout").
-m --key-map-configuration FILE_CONTENT Keyboard map configuration (default: "$pia_key_map_configuration_file_content").
EOF
}
alias pia.get_help_message=pia_get_help_message
pia_get_help_message() {
local -r __documentation__='
Provides a help message for this module.
>>> pia.get_help_message
+bl.doctest.contains
+bl.doctest.multiline_ellipsis
...
Usage: pack-into-archiso /path/to/archiso/file.iso /path/to/newly/packe
...
'
echo -e $'\nUsage: pack-into-archiso /path/to/archiso/file.iso /path/to/newly/packed/archiso/file.iso [options]\n'
echo -e "$pia__documentation__"
echo -e $'\nOption descriptions:\n'
pia.get_commandline_option_description "$@"
echo
}
alias pia.commandline_interface=pia_commandline_interface
pia_commandline_interface() {
local -r __documentation__='
Provides the command line interface and interactive questions.
>>> pia.commandline_interface
+bl.doctest.contains
+bl.doctest.multiline_ellipsis
You have to provide source and target file path.
...
'
while true; do
case "$1" in
-h|--help)
shift
bl.logging.plain "$(pia.get_help_message "$0")"
exit 0
;;
-v|--verbose)
shift
if ! bl.logging.is_enabled info; then
bl.logging.set_level info
fi
;;
-d|--debug)
shift
bl.logging.set_level debug
;;
-c|--squash-filesystem-compressor)
shift
pia_squash_filesystem_compressor="$1"
shift
;;
-k|--keyboard-layout)
shift
pia_keyboard_layout="$1"
shift
;;
-m|--key-map-configuation)
shift
pia_key_map_configuration_file_content="$1"
shift
;;
'')
shift
break
;;
*)
if [[ ! "$pia_source_path" ]]; then
pia_source_path="$1"
elif [[ ! "$pia_target_path" ]]; then
pia_target_path="$1"
if [ -d "$pia_target_path" ]; then
pia_target_path="$(
readlink \
--canonicalize \
"$pia_target_path"
)/$(basename "$pia_source_path")"
fi
else
bl.logging.critical \
"Given argument: \"$1\" is not available." '\n'
bl.logging.plain "$(pia.get_help_message "$0")"
fi
shift
esac
done
if \
[[ ! "$pia_source_path" ]] || \
[[ ! "$pia_target_path" ]]
then
bl.logging.critical \
You have to provide source and target file path. $'\n'
bl.logging.plain "$(pia.get_help_message "$0")"
return 1
fi
}
## endregion
## region helper
alias pia.remaster_iso=pia_remaster_iso
pia_remaster_iso() {
local -r __documentation__='
Remasters given iso into new iso. If new systemd programs are used (if
first argument is "true") they could have problems in change root
environment without and exclusive dbus connection.
'
bl.logging.info \
"Mount \"$pia_source_path\" to \"$pia_mountpoint_path\"."
mount \
-t iso9660 \
-o loop \
"$pia_source_path" \
"$pia_target_path"
bl.logging.info \
"Copy content in \"$pia_mountpoint_path\" to \"$pia_temporary_remastering_path\"."
cp --archiv "${pia_mountpoint_path}/"* "$pia_temporary_remastering_path"
local path
local -i return_code=0
for path in "${pia_relative_paths_to_squash_filesystem[@]}"; do
bl.logging.info "Extract squash file system in \"${pia_temporary_remastering_path}/$path\" to \"${pia_temporary_remastering_path}\"."
unsquashfs \
-d "${pia_temporary_filesystem_remastering_path}" \
"${pia_temporary_remastering_path}/${path}"
rm --force "${pia_temporary_remastering_path}/${path}"
bl.logging.info "Mount root file system in \"${pia_temporary_filesystem_remastering_path}\" to \"${pia_temporary_root_filesystem_remastering_path}\"."
mount \
"${pia_temporary_filesystem_remastering_path}/"* \
"$_TEMPORARY_ROOT_FILESYSTEM_REMASTERING_PATH"
bl.logging.info "Copy \"$(
dirname "$(readlink --canonicalize "$0")"
)/$pia_relative_source_file_path\" to \"${pia_temporary_root_filesystem_remastering_path}/${pia_relative_target_file_path}\"."
cp \
"$(dirname "$(readlink --canonicalize "$0")")/$_RELATIVE_SOURCE_FILE_PATH" \
"${pia_temporary_root_filesystem_remastering_path}/${pia_relative_target_file_path}"
bl.logging.info "Set key map to \"$pia_keyboard_layout\"."
if [ "$1" = true ]; then
arch-chroot \
"$pia_temporary_root_filesystem_remastering_path" \
localectl set-keymap "$pia_keyboard_layout"
arch-chroot \
"$pia_temporary_root_filesystem_remastering_path" \
set-locale LANG=en_US.utf8
else
bl.logging.plain \
"$pia_key_map_configuration_file_content" \
1>"${pia_temporary_root_filesystem_remastering_path}/etc/vconsole.conf"
fi
bl.logging.info Set root symbolic link for root user.
local file_name
for file_name in .bashrc .cshrc .kshrc .zshrc; do
if [ -f "${pia_temporary_root_filesystem_remastering_path}/root/$file_name" ]; then
bl.logging.plain \
"$pia_bashrc_code" \
1>>"${pia_temporary_root_filesystem_remastering_path}/root/$file_name"
else
bl.logging.plain \
"$pia_bashrc_code" \
1>"${pia_temporary_root_filesystem_remastering_path}/root/$file_name"
fi
done
bl.logging.info "Unmount \"$pia_temporary_root_filesystem_remastering_path\"."
umount "$pia_temporary_root_filesystem_remastering_path"
bl.logging.info "Make new squash file system from \"${pia_temporary_remastering_path}\" to \"${pia_temporary_remastering_path}/${path}\"."
mksquashfs \
"${pia_temporary_filesystem_remastering_path}" \
"${pia_temporary_remastering_path}/${path}" \
-noappend \
-comp \
"$pia_squash_filesystem_compressor"
rm \
--force \
--recursive \
"${pia_temporary_filesystem_remastering_path}"
return_code=$?
if (( return_code != 0 )); then
bl.logging.info "Unmount \"$pia_mountpoint_path\"."
umount "$pia_mountpoint_path"
return $?
fi
done
local -r volume_id="$(
isoinfo -i "$pia_source_path" -d | \
command grep --extended-regexp 'Volume id:' | \
command grep --extended-regexp --only-matching '[^ ]+$'
)"
bl.logging.info "Create new iso file from \"$pia_temporary_remastering_path\" in \"$pia_target_path\" with old detected volume id \"$volume_id\"."
pushd "${pia_mountpoint_path}" && \
genisoimage \
-boot-info-table \
-boot-load-size 4 \
-eltorito-boot isolinux/isolinux.bin \
-eltorito-catalog isolinux/boot.cat \
-full-iso9660-filenames \
-joliet \
-no-emul-boot \
-output "$pia_target_path" \
-rational-rock \
-verbose \
--volid "$volume_id" \
"$pia_temporary_remastering_path"
popd && \
bl.logging.info "Unmount \"$pia_mountpoint_path\"."
umount "$pia_mountpoint_path"
}
alias pia.tidy_up=pia_tidy_up
pia_tidy_up() {
local -r __documentation__='
Removes temporary created files.
'
bl.logging.info \
"Remove temporary created location \"$pia_mountpoint_path\"."
rm \
--force \
--recursive \
"$pia_mountpoint_path"
bl.logging.info \
"Remove temporary created location \"$pia_temporary_remastering_path\"."
rm \
--force \
--recursive \
"$pia_temporary_remastering_path"
bl.logging.info \
"Remove temporary created location \"$pia_temporary_filesystem_remastering_path\"."
rm \
--force \
--recursive \
"$pia_temporary_filesystem_remastering_path"
bl.logging.info \
"Remove temporary created location \"$pia_temporary_root_filesystem_remastering_path\"."
rm \
--force \
--recursive \
"$pia_temporary_root_filesystem_remastering_path"
}
## endregion
## region controller
alias pia.main=pia_main
pia_main() {
local -r __documentation__='
Main injected point for this module.
>>> pia.main --help
+bl.doctest.contains
+bl.doctest.multiline_ellipsis
...
Usage: pack-into-archiso /path/to/archiso/file.iso /path/to/newly/packe
...
'
pia.commandline_interface "$@" || \
return $?
# Switch user if necessary and possible.
if [[ "$USER" != root ]] && command grep root /etc/passwd &>/dev/null; then
sudo -u root "$0" "$@"
return $?
fi
pia.remaster_iso || \
bl.logging.critical Remastering given iso failed.
pia.tidy_up || \
bl.logging.critical Tidying up failed.
bl.logging.info
Remastering given image \""$pia_source_path"\" to \
\""$pia_target_path"\" has successfully finished.
}
## endregion
# endregion
if bl.tools.is_main; then
pia.main "$@"
fi
# region vim modline
# vim: set tabstop=4 shiftwidth=4 expandtab:
# vim: foldmethod=marker foldmarker=region,endregion:
# endregion