-
Notifications
You must be signed in to change notification settings - Fork 6
/
runbuild
executable file
·194 lines (173 loc) · 6.63 KB
/
runbuild
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
#!/usr/bin/bash
BUILDKEY="$BASHPID"
RED='\033[1;91m'
BLUE='\033[1;94m'
GREEN='\033[1;92m'
YELLOW='\033[1;33m'
RESETCOLOR='\033[1;00m'
EXEPT_RUNSRCNAMES=("Run" "runbuild" "rundesktop" "runupdate" "rpidsmon")
nocolor() { sed -r 's|\x1B\[([0-9]{1,3}(;[0-9]{1,2};?)?)?[mGK]||g' ; }
error_msg() {
echo -e "${RED}[ ERROR ][$(date +"%Y.%m.%d %T")]: $@ $RESETCOLOR"
if [ "$NOT_TERM" == 1 ]
then notify-send -a 'RunImage Error' "$(echo -e "$@"|nocolor)" 2>/dev/null &
fi
}
info_msg() {
if [ "$QUIET_MODE" != 1 ]
then echo -e "${GREEN}[ INFO ][$(date +"%Y.%m.%d %T")]: $@ $RESETCOLOR"
if [[ "$NOT_TERM" == 1 && "$DONT_NOTIFY" != 1 ]]
then notify-send -a 'RunImage Info' "$(echo -e "$@"|nocolor)" 2>/dev/null &
fi
fi
}
warn_msg() {
if [[ "$QUIET_MODE" != 1 && "$NO_WARN" != 1 ]]
then echo -e "${YELLOW}[ WARNING ][$(date +"%Y.%m.%d %T")]: $@ $RESETCOLOR"
if [[ "$NOT_TERM" == 1 && "$DONT_NOTIFY" != 1 ]]
then notify-send -a 'RunImage Warning' "$(echo -e "$@"|nocolor)" 2>/dev/null &
fi
fi
}
is_zstd_cmprs_valid() { [[ "$1" =~ ^[0-9]+$ && "$1" -ge 1 && "$1" -le 22 ]] ; }
[ ! -n "$(tty|grep -v 'not a'|grep -Eo 'tty|pts')" ] && \
NOT_TERM=1
info_msg "RunImage build"
if [ ! -x "$RUNRUNTIME" ]
then
error_msg "RunImage runtime not found!"
exit 1
fi
RUNRUNTIMESIZE=($(du -sb "$RUNRUNTIME" 2>/dev/null))
[ ! -x "$MKSQFS" ] && \
MKSQFS="$RUNSTATIC/mksquashfs"
CMPRS_ARGS=("-zstd" "-xz" "-lz4")
if [[ -n "$1" && ! "${CMPRS_ARGS[@]}" =~ "$1" ]]
then
if [ -d "$(dirname "$1" 2>/dev/null)" ]
then
if [ -d "$1" ]
then
if [[ "${EXEPT_RUNSRCNAMES[@]}" =~ "$RUNSRCNAME" ]]
then RUNBASENAME="$1/runimage"
else RUNBASENAME="$1/$RUNSRCNAME"
fi
else RUNBASENAME="$1"
fi
else
error_msg "The build directory does not exist!"
exit 1
fi
else
if [[ "${EXEPT_RUNSRCNAMES[@]}" =~ "$RUNSRCNAME" ]]
then RUNBASENAME="runimage"
else RUNBASENAME="$RUNSRCNAME"
fi
fi
if [[ "$1" == '-zstd' || "$2" == '-zstd' || "$CMPRS_ALGO" == "zstd" || -n "$ZSDT_CMPRS_LVL" ]]
then
if is_zstd_cmprs_valid "$2"
then ZSDT_CMPRS_LVL="$2"
elif is_zstd_cmprs_valid "$3"
then ZSDT_CMPRS_LVL="$3"
elif ! is_zstd_cmprs_valid "$ZSDT_CMPRS_LVL"
then ZSDT_CMPRS_LVL="19"
fi
CMPRS_MTHD="1M -comp zstd -Xcompression-level $ZSDT_CMPRS_LVL"
info_msg "Compression method: zstd"
info_msg "Compression level: $ZSDT_CMPRS_LVL"
elif [[ "$1" == '-xz' || "$2" == '-xz' || "$CMPRS_ALGO" == "xz" ]]
then
CMPRS_MTHD="1M -comp xz -Xdict-size 100%"
info_msg "Compression method: xz"
fi
if [[ ! -n "$CMPRS_MTHD" || "$1" == '-lz4' || "$2" == '-lz4' || "$CMPRS_ALGO" == "lz4" ]]
then
CMPRS_MTHD="256K -comp lz4 -Xhc"
info_msg "Compression method: lz4"
fi
if [ "$BUILD_WITH_EXTENSION" == 1 ]
then
[ -n "$RUNROOTFSTYPE" ] && \
ROOTFSTYPE=".$RUNROOTFSTYPE"
if [[ "$CMPRS_MTHD" =~ "zstd" ]]
then CMPRSMTHD=".zst"
elif [[ "$CMPRS_MTHD" =~ "xz" ]]
then CMPRSMTHD=".xz"
else CMPRSMTHD=".lz4"
fi
else unset CMPRSMTHD RUNROOTFSTYPE
fi
NEWRUNIMAGE="$(realpath "$RUNBASENAME")${CMPRSMTHD}$ROOTFSTYPE.new$BUILDKEY"
OLDRUNIMAGE="${NEWRUNIMAGE%.new$BUILDKEY}"
[[ -f "$NEWRUNIMAGE" || -d "$NEWRUNIMAGE" || -L "$NEWRUNIMAGE" ]] && \
rm -rf "$NEWRUNIMAGE"
[[ "$INSIDE_RUNIMAGE" == 1 && ! -d "$RUNROOTFS" ]] && \
RUNROOTFS='/'
unset IS_PACLOCKFL
PACLOCKFL="$RUNROOTFS/var/lib/pacman/db.lck"
if [ -w "$RUNROOTFS" ]
then
info_msg "Updating build timestamp..."
date '+%y.%m.%d.%H%M%S' > "$RUNROOTFS/.build"
info_msg "Cleanup rootfs..."
find "$RUNROOTFS/var/log/" -type f -name '*.log' -exec rm -f {} \;
rm -rf "$RUNROOTFS/var/tmp"/*
if [ -f "$PACLOCKFL" ]
then
IS_PACLOCKFL=1
rm -rf "$PACLOCKFL"
fi
rm -rf "$RUNROOTFS/var/lib/pacman/sync"/*
rm -rf "$RUNROOTFS/var/cache/pacman/pkg"/*
rm -rf "$RUNROOTFS/var/cache/apt/archives"/*.deb
rm -rf "$RUNROOTFS/var/cache/apt"/*.bin
rm -rf "$RUNROOTFS/var/lib/apt/lists"/deb.*
rm -rf "$RUNROOTFS/var/log/apt"/*
else
warn_msg "Unable to update build timestamp. Read-only!"
warn_msg "Unable to cleanup rootfs. Read-only!"
fi
info_msg "Creating new RunImage..."
info_msg "Path: '$OLDRUNIMAGE'"
[ -d "$OVERFS_MNT" ] && \
RUNDIR="$OVERFS_MNT"
echo -en "$BLUE"
"$MKSQFS" "$RUNDIR" "$NEWRUNIMAGE" -root-owned -offset "$RUNRUNTIMESIZE" \
-no-xattrs -noappend -quiet -mkfs-time 0 -b $CMPRS_MTHD
echo -en "$RESETCOLOR"
[ "$IS_PACLOCKFL" == 1 ] && \
touch "$PACLOCKFL"
if [ -f "$NEWRUNIMAGE" ]
then
info_msg "Embedding RunImage runtime..."
if dd if="$RUNRUNTIME" bs="$RUNRUNTIMESIZE" count=1 of="$NEWRUNIMAGE" conv=notrunc &>/dev/null
then
info_msg "Marking RunImage as executable..."
chmod +x "$NEWRUNIMAGE"
if [ -f "$OLDRUNIMAGE" ]
then
if [ "$KEEP_OLD_BUILD" == 1 ]
then
info_msg "Saving old RunImage: '${OLDRUNIMAGE}.old'"
mv -f "${OLDRUNIMAGE}" "${OLDRUNIMAGE}.old"
else
info_msg "Removing old RunImage..."
rm -rf "$OLDRUNIMAGE"
[ -f "${OLDRUNIMAGE}.old" ] && \
rm -rf "${OLDRUNIMAGE}.old"
fi
fi
mv -f "$NEWRUNIMAGE" "$OLDRUNIMAGE"
RUNIMAGESIZE=($(du -sm "$OLDRUNIMAGE"))
info_msg "The build is complete!"
info_msg "Final size: $RUNIMAGESIZE MB"
else
error_msg "Failed to embed RunImage runtime!"
rm -rf "$NEWRUNIMAGE"
exit 1
fi
else
error_msg "The build failed!"
exit 1
fi