-
Notifications
You must be signed in to change notification settings - Fork 11
/
compress_image
executable file
·166 lines (136 loc) · 4.03 KB
/
compress_image
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
#!/bin/sh
#
# Part of https://github.com/jaclu/AOK-Filesystem-Tools
#
# License: MIT
#
# Copyright (c) 2023: [email protected]
#
# Compresses a FS into a tar file that can be mounted by iSH
#
show_help() {
echo "Usage: $prog_name [-h] [-v] [-z]
This creates a compressed tar file. that iSH can mount as a file system
It autodetects FS type, and will use a matching filename for the tarball.
Available options:
-h --help Print this help and exit.
-l --label Provide a name for tarball (without path or extension)
-j --bzip2 Use bzip2 compression
-v --verbose Display progrss as FS is being compressed."
exit 0
}
clear_build_triggered_logfiles() {
f_fd_log="/var/log/fix_dev.log"
f_sys_log="/var/log/syslog"
br_fd_log="${d_build_root}$f_fd_log"
br_sys_log="${d_build_root}$f_sys_log"
if [ -f "$br_fd_log" ] || [ -f "br_sys_log" ]; then
msg_2 "Removing log files generated during preparations on client FS"
[ -f "$br_fd_log" ] && msg_3 "$f_fd_log" && rm -f "$br_fd_log"
[ -f "$br_sys_log" ] && msg_3 "$f_sys_log" && rm -f "$br_sys_log"
fi
}
#===============================================================
#
# Main
#
#===============================================================
hide_run_as_root=1 . /opt/AOK/tools/run_as_root.sh
. /opt/AOK/tools/utils.sh
prog_name=$(basename "$0")
use_bzip2=false
verbose=false
while [ -n "$1" ]; do
case "$1" in
"-h" | "--help") show_help ;;
"-j" | "--bzip2") use_bzip2=true ;;
"-l" | "--label")
shift
tar_name="$1"
if [ -z "$tar_name" ]; then
echo "ERROR: label needs name as additional param!"
exit 1
fi
;;
"-v" | "--verbose") verbose=true ;;
*)
error_msg "bad param, try -h"
;;
esac
shift
done
if [ ! -d "$d_build_root" ]; then
error_msg "No image built empty dir: [$d_build_root]!"
fi
if [ "$(find "$d_build_root"/dev | wc -l)" -gt 1 ]; then
error_msg "Active chroot session detected!"
fi
if [ -n "$tar_name" ]; then
tarball_fn="${tar_name}"
else
#
# If no tarball name was given, try to identify what is prepared
# and give it a name based on this with versions added.
case "$(destfs_detect)" in
"$destfs_select")
tarball_fn="SelectDistro-AOK-${AOK_VERSION}"
;;
"$distro_alpine")
msg_3 "Getting release details for Alpine"
get_lsb_release chroot || error_msg "get_lsb_release error"
tarball_fn="${lsb_DistributorID}-${lsb_Release}-AOK-${AOK_VERSION}"
;;
"$distro_debian" | "$distro_devuan")
msg_3 "Getting release details for Debian/Devuan"
get_lsb_release chroot || error_msg "get_lsb_release error"
tarball_fn="${lsb_DistributorID}${lsb_Release}-AOK-${AOK_VERSION}"
;;
*) error_msg "Could not detext FS type" ;;
esac
deploy_state_is_it "$deploy_state_pre_build" && tarball_fn="$tarball_fn-pb"
fi
# set location
mkdir -p "$TMPDIR/aok_imgs"
tarball="$TMPDIR/aok_imgs/$tarball_fn"
# echo "removing temp /dev items"
# rm -f "$d_build_root"/dev/*
#
# Tar up and zip the result
#
cd "$d_build_root" || {
error_msg "Failed to cd into: $d_build_root"
}
if $use_bzip2; then
opts="cfj"
tarball="${tarball}.tar.bz2"
tarball_fn="${tarball_fn}.tar.bz2"
else
opts="cfz"
tarball="${tarball}.tgz"
tarball_fn="${tarball_fn}.tgz"
fi
if $verbose; then
opts="v$opts"
fi
clear_build_triggered_logfiles
msg_2 "Writing image file"
msg_3 " $tarball"
if [ -n "$cmd_pigz" ]; then
msg_4 "Using $cmd_pigz"
# time tar -cf - . | pigz -p 2 > foo.tgz
tar -cf - . | $cmd_pigz >"$tarball"
else
msg_4 "No pigz"
tar "$opts" "$tarball" .
fi
ex_code="$?"
#
# copy it to /iCloud if this runs on iSH
#
if this_is_ish && [ "$(find /iCloud/ -maxdepth 1 | wc -l)" -gt 1 ]; then
msg_2 "Creating additional copy: $d_icloud_archive/$tarball_fn"
mkdir -p "$d_icloud_archive"
cp "$tarball" "$d_icloud_archive"
fi
msg_3 "Image is ready: $tarball"
exit "$ex_code" # Propagate any tar error