-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-addon.sh
executable file
·427 lines (369 loc) · 10.7 KB
/
make-addon.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!/bin/bash
CLEAN=true
HARDLINK=true
parse_required_parameters() {
iso="$1"
shift
addon="$1"
shift
}
help() {
echo "
make-addon.sh ISO ADDON [OPTIONS]
ISO
is the iso image to create the addon for.
ADDON
Is the file to which the addon is saved.
It should have the ending '.squashfs'.
-a --add FILE-OR-DIRECTORY TARGET-DIRECTORY
FILE-OR-DIRECTORY will be at the path DESTINATION after this.
Directories may be mounted and files hard-linked to save space.
The copied files and folders are included in the addon.
-A --map DIRECTORY TARGET-DIRECTORY
Mount a DIRECTORY to a specific TARGET-DIRECTORY in the file system.
-c --command COMMAND
The COMMAND is executed as root and the result is included in the addon.
-C --map-command COMMAND
The COMMAND is executed but the result is not included in the addon.
Example: cd /opt && wget https://example.com
-s --startup-command NAME COMMAND
The COMMAND is added to the startup routine with a NAME.
It is executed as root when the system boots.
-n --no-clean
Do not clean the files and directories used for this.
-H --no-hardlink
Do not hardlink files.
Each option is executed in the order it is passed to the script.
If you do --map and then --command, you can use what you mapped.
If you do --command and then --map, what you want to map is not
available for the command.
The /tmp folder is excluded from the addon.
"
}
log() {
echo -n "$0 - "
if [ -n "$in_function" ]; then
echo -n "$in_function - "
fi
echo "$@"
}
error() {
log "ERROR:" "$@"
exit 1
}
option_add() {
add_with_persistence "$1" "$2" "mount_persistent"
}
add_with_persistence() {
file_or_directory="$1"
target="$2"
persistence="$3"
directory="`create_new_mount_directory`"
"$persistence" "$directory" || \
error "Could not do $persistence $directory"
target_directory="`mount_directory_to_data_directory \"$directory\"`"
add_to_directory "$file_or_directory" "$target_directory/$target"
}
add_to_directory() {
source="$1"
target="$2"
[ -e "$target" ] || mkdir -p "$target"
if [ -d "$source" ]; then
if [ "$target" == "/" ] ; then
log "Mounting $source"
log " to $target"
mount --bind "$source" "$target" && \
return
log "Mount failed. Copying files."
fi
log "Copying content from $source"
log " to $target"
log "Attempting hard link to save space"
[ "$HARDLINK" == "true" ] && cp -rlTP "$source" "$target" || {
log "Hard link failed. Copying now"
cp -ruTP "$source" "$target"
} || \
error "Could not copy files"
log "Done copying"
else
log "Attempting hard link to save space"
[ "$HARDLINK" == "true" ] && ln -v -t "$target" "$source" || {
log "Hard link failed. Copying."
cp -v -t "$target" "$source" || \
error "Could not copy file"
}
fi
}
option_map() {
add_with_persistence "$1" "$2" "mount_volatile"
}
option_map_command() {
execute_command_with_persistence "$1" "mount_volatile"
}
option_command() {
execute_command_with_persistence "$1" "mount_persistent"
}
execute_command_with_persistence() {
command="$1"
persistence="$2"
directory="`create_new_mount_directory`"
"$persistence" "$directory" || \
error "Could not do $persistence $directory"
execute_command "$directory" "$command"
}
systemd_script() {
local name="$1"
echo "[Unit]"
echo "After=network.target"
echo ""
echo "[Service]"
echo "ExecStart=$name"
echo ""
echo "[Install]"
echo "WantedBy=default.target"
}
service_script() {
local command="$1"
echo "#!/bin/bash"
echo -n "$command"
}
startup_added_to_mount_order="false"
option_startup_command() {
local name="$1"
local command="$2"
[ -n "$name" ] && [ -n "$command" ] || \
error "Missing parameter for startup option"
echo "$name" | grep -vqE "[[:space:]]|'" || \
error "The name \"$name\" contains spaces or '. Expected no space in name."
local startup_dir="$base/startup"
local root_bin_folder="/usr/local/bin"
local root_service_folder="/etc/systemd/system"
local absolute_bin_folder="$startup_dir$root_bin_folder"
local absolute_service_folder="$startup_dir$root_service_folder"
local bin_name="start-$name-service.sh"
local service_name="$name.service"
local absolute_bin_file="$absolute_bin_folder/$bin_name"
local absolute_service_file="$absolute_service_folder/$service_name"
local root_bin_file="$root_bin_folder/$bin_name"
local root_service_file="$root_service_folder/$service_name"
mkdir -p "$absolute_bin_folder" "$absolute_service_folder"
systemd_script "$root_bin_file" > "$absolute_service_file"
service_script "$command" > "$absolute_bin_file"
chmod +x "$absolute_bin_file"
chmod +x "$absolute_service_file"
if [ "$startup_added_to_mount_order" == "false" ]; then
mount_order="$startup_dir:$mount_order"
mount_order_addon="$startup_dir:$mount_order_addon"
startup_added_to_mount_order="true"
fi
linking="ln -s -t '/etc/systemd/system/default.target.wants/' '$root_service_file'"
log "Executing $linking"
execute_command_with_persistence "$linking" "mount_persistent"
}
verify_parameters() {
if [ -z "$iso" ] || [ -z "$addon" ]; then
help
error "Did not find required parameters."
fi
}
find_filesystem() {
(
cd "$iso_mount"
find -name filesystem.squashfs
)
}
mount_root_filesystem() {
local addon_name="`basename \"$addon\"`"
base="/tmp/${addon_name%.*}-`date +%N`"
iso_mount="$base/iso"
fs_mount="$base/fs"
mkdir "$base" || \
error "Temporary failure: temporary name already taken."
log "Mounting $iso"
log " to $iso_mount"
mkdir "$iso_mount" "$fs_mount" || \
error "Could not create directory."
mount "$iso" "$iso_mount" || \
error "Could not mount iso."
log "Searching for filesystem"
relative_filesystem_squashfs="`find_filesystem`"
[ -n "$relative_filesystem_squashfs" ] || \
error "did not find filesystem.squashfs in $iso_mount"
fs_squash="$iso_mount/$relative_filesystem_squashfs"
log "Mounting $fs_squash"
log " to $fs_mount"
mount "$fs_squash" "$fs_mount" || \
error "Could not mount filesystem."
}
setup_root_filesystem() {
host_copy="$base/host"
mkdir "$host_copy"
log "Copying environment from host."
# see https://github.com/fossasia/meilix/blob/master/build.sh
# and https://help.ubuntu.com/community/LiveCDCustomizationFromScratch
mkdir "$host_copy/etc" || \
error "Could not create sub directory for host."
cp -vr "/etc/resolvconf" "$host_copy/etc/resolvconf" || \
error "Could not copy resolvconf"
cp -v "/etc/resolv.conf" "$host_copy/etc/resolv.conf" || \
error "Could not copy resolv.conf"
cp -v "/etc/hosts" "$host_copy/hosts"
mount_special_file_systems "$host_copy"
}
mount_special_file_systems() {
mkdir "$1/sys" "$1/proc" "$1/dev" || \
error "Could not create sub directories for $1."
sudo mount --rbind "/sys" "$1/sys" || \
error "Could not mount sys."
sudo mount --rbind "/dev" "$1/dev" || \
error "Could not mount dev."
sudo mount -t proc none "$1/proc" || \
error "Could not mount proc."
}
initialize_mount_order() {
empty="$base/empty"
mkdir "$empty" || \
error "Could not create empty directory for addon files"
[ -n "$host_copy" ] || \
error "host_copy not initialized."
[ -n "$fs_mount" ] || \
error "fs_mount not initialized."
mount_order="$host_copy=ro:$fs_mount=rr"
mount_order_addon="$empty=ro"
}
create_new_mount_directory() {
local i=0
while true; do
i=$((i + 1))
local directory="$base/step-${i}-mount"
[ -e "$directory" ] || break
done
1>&2 mkdir "$directory" || \
1>&2 error "Could not create directory for step $i"
echo -n "$directory"
}
mount_directory_to_data_directory() {
directory="`echo -n \"$1\" | sed 's/mount$/data/'`"
mkdir -p "$directory"
echo -n "$directory"
}
mount_into() {
order="$1"
directory="$2"
mkdir -p "$directory"
log "Mounting aufs $order"
log " to $directory"
mount -t aufs -o "br=$order" none "$directory" || \
error "Could not mount \"$order\" to \"$directory\""
}
mount_persistent() {
mount_volatile "$1"
mount_order_addon="$data_directory=ro:$mount_order_addon"
}
mount_volatile() {
mount_directory="$1"
data_directory="`mount_directory_to_data_directory \"$mount_directory\"`"
mount_order="$data_directory:$mount_order"
mount_into "$mount_order" "$mount_directory"
}
execute_command() {
directory="$1"
command="$2"
log "In $directory"
log "Executing $command"
chroot "$directory" bash -c "$command" || \
error "Error executing $command"
}
exclude() {
echo "tmp"
}
write_addon() {
data="$base/addon"
exclude_file="$base/exclude.txt"
exclude > "$exclude_file"
type="${addon##*.}"
mount_into "$mount_order_addon" "$data"
log "Files: "`ls "$data"`
log "Creating $type file $addon"
if [ "$type" == "squashfs" ]; then
if [ -z "`which mksquashfs`" ]; then
log "Installing squashfs tools"
apt-get -y install squashfs-tools
fi
mksquashfs "$data" "$addon" -noappend -no-progress -ef "$exclude_file" || \
error "Could not squash to $addon"
else
error "Unrecognized type \"$type\"."
fi
log "Output: $addon"
}
log_call() {
in_function=""
log "$@"
in_function="$1"
"$@"
}
parse_options() {
while [ -n "$1" ]; do
option="$1"
shift
case "$option" in
-a|--add)
log_call option_add "$1" "$2"
shift; shift ;;
-A|--map)
log_call option_map "$1" "$2"
shift; shift ;;
-C|--map-command)
log_call option_map_command "$1"
shift ;;
-c|--command)
log_call option_command "$1"
shift ;;
-s|--startup-command)
log_call option_startup_command "$1" "$2"
shift; shift ;;
-n|--no-clean)
log "No cleanup"
CLEAN=false ;;
-H|--no-hardlink)
log "No hardlink"
HARDLINK=false ;;
*)
help
error "Invalid option \"$option\"." ;;
esac
done
}
clean_up() {
if [ "$CLEAN" == "false" ]; then
log "Skipping clean up."
return
fi
(
cd "$base"
if [ "`echo step-*-mount`" != "step-*-mount" ]; then
for dir in step-*-mount; do
umount "$dir" 2>>/dev/null
done
fi
)
umount "$fs_mount"
umount "$data"
umount "$iso_mount"
umount "$host_copy/sys" 2>>/dev/null
umount "$host_copy/dev" 2>>/dev/null
umount "$host_copy/proc"
}
main() {
log_call parse_required_parameters "$1" "$2"; shift; shift
log_call verify_parameters
log_call mount_root_filesystem
log_call setup_root_filesystem
log_call initialize_mount_order
log_call parse_options "$@"
log_call write_addon
log_call clean_up
}
main "$@"
exit 0