-
Notifications
You must be signed in to change notification settings - Fork 2
/
pelfCreator
executable file
·423 lines (383 loc) · 14 KB
/
pelfCreator
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
#!/bin/sh
unset ARGV0 ARGS LD_PRELOAD LD_LIBRARY_PATH
[ "$DEBUGX" = "1" ] && set -x
print_help() {
cat <<EOF
Usage: $0 [|-m|-n|-p|-e|-r|-c|-z|-x|-o|] <|-v|-h|>
Options:
-m, --maintainer Set the maintainer (required)
-n, --name Set the name of the app (required)
-p, --pkg-add Packages to add with APK (required)
-e, --entrypoint Set the entrypoint (required unless using --multicall)
-z, --dontpack Disables .dwfs.AppBundle packaging, thus leaving only the AppDir
-x, --sharun Processes the desired binaries with lib4bin and adds sharun. Then, it ditches the rootfs and only keeps the files specified with '-k'
-s, --sandbox Enable sandbox mode (uses AppRun.rootfs-based)
-k, --keep Moves these directories to the appdir's ./proto, this is for use with -x, to conserve the files a program may need while also removing the rest of the rootfs
-o, --output-to Set the output file name (optional, default: <name>-<date>.dwfs.AppBundle)
--local A directory from which to pick up files such as 'AppRun.sharun', 'rootfs.tgz', 'pelf-dwfs', 'bwrap', etc
-h, --help Display this help and exit
ENV:
\$LOCAL_PATH Same as the '--local' flag. Must point to a valid directory
Examples:
# With .desktop file (enables integration with pelfd)
$0 --maintainer animecowgirl79 --name mousepad --pkg-add mousepad --entrypoint org.xfce.mousepad.desktop
# With binary name (disables integration with pelfd unless you manually add a .DirIcon or .desktop or both)
$0 --maintainer animecowgirl79 --name mousepad --pkg-add mousepad --entrypoint mousepad
# Using a custom rootfs, AppRuns, etc
$0 --maintainer animecowgirl79 --name mousepad --pkg-add mousepad --local ./localArchLinuxResources
Notes:
- [pelfCreator](https://github.com/xplshn/pelf/blob/pelf-ng/pelfCreator) is an experimental tool, part of the pelf project & ecosystem
- Learn more about [pelf](https://github.com/xplshn/pelf)
EOF
}
parse_arguments() {
ARGS=$(getopt -o m:n:p:e:x:k:r:o:sz -l maintainer:,name:,pkg-add:,entrypoint:,sharun:,keep:,getrid:,output:,local:,sandbox,dontpack,help -- "$@") || {
echo "Failed to parse arguments." >&2
exit 1
}
eval set -- "$ARGS"
while true; do
case "$1" in
-m | --maintainer)
MAINTAINER="$2"
shift 2
;;
-n | --name)
NAME="$2"
shift 2
;;
-p | --pkg-add)
PKG_ADD="$2"
shift 2
;;
-e | --entrypoint)
ENTRYPOINT="$2"
shift 2
;;
-z | --dontpack)
DONTPACK=1
shift
;;
-x | --sharun)
SHARUN=1
LIB4BIN_ARGS="$2"
shift 2
;;
-s | --sandbox)
SANDBOX="1"
shift
;;
-k | --keep)
TOBEKEPT_FILES="$2"
shift 2
;;
-r | --getrid)
GETRID_FILES="$2"
shift 2
;;
-o | --output-to)
OUTPUT_TO="$2"
shift 2
;;
--local)
LOCAL_PATH="$2"
shift 2
;;
-h | --help)
print_help
exit 0
;;
--)
shift
break
;;
*)
echo "Unexpected option: $1"
print_help
exit 1
;;
esac
done
}
check_required_arguments() {
if [ -z "$MAINTAINER" ] || [ -z "$NAME" ] || [ -z "$PKG_ADD" ]; then
echo "Error: --maintainer, --name, and --pkg-add are required." >&2
print_help
exit 1
fi
if [ -z "$ENTRYPOINT" ]; then
echo "Warning: The resulting AppBundle won't have a default entrypoint/fallback, because you did not specify -e/--entrypoint. It will only work as a multicall binary" >&2
fi
}
setup_local_path() {
if [ -n "$LOCAL_PATH" ]; then
if [ -d "$LOCAL_PATH" ]; then
export PATH="$LOCAL_PATH:$PATH"
else
echo "Error: --local path must be a directory." >&2
exit 1
fi
fi
}
create_appdir() {
DATE="$(date +%d_%m_%Y)"
APPBUNDLE_ID="$NAME-$DATE-$MAINTAINER"
APPDIR="$NAME-$DATE.AppDir"
OUTPUT_TO="${OUTPUT_TO:-$NAME-$DATE.dwfs.AppBundle}"
COMPRESSION_OPTS="${COMPRESSION_OPTS:=-l7 -C zstd:level=22 --metadata-compression null -S 19 -B 8 --order nilsimsa -W 12 -w 4}"
mkdir -p "$APPDIR/usr/bin" "$APPDIR/proto"
echo "${0##*/} $ARGS" >"$APPDIR/.genSteps" && chmod +x "$APPDIR/.genSteps"
}
download_and_extract_rootfs() {
# Either download & extract rootfs or directly extract it from its local copy
if [ -f "$LOCAL_PATH/rootfs.tgz" ]; then
if ! tar xzf "$LOCAL_PATH/rootfs.tgz" -C "$APPDIR/proto" --no-same-permissions; then
echo "Failed to extract local rootfs"
exit 1
fi
else
: "${ROOTFS_URL:=https://pub.ajam.dev/utils/alpine-mini-$(uname -m)/rootfs.tar.gz}"
if ! wget -qO- "$ROOTFS_URL" | tar xz - -C "$APPDIR/proto" --no-same-permissions; then
echo "Failed to download or extract rootfs"
exit 1
fi
fi
}
add_bwrap() {
# Either download or copy bwrap
if [ -f "$LOCAL_PATH/bwrap" ]; then
cp "$LOCAL_PATH/bwrap" "$APPDIR/usr/bin/bwrap"
else
if ! wget -qO "$APPDIR/usr/bin/bwrap" "https://bin.ajam.dev/$(uname -m)/bwrap-patched"; then
echo "Unable to install bwrap."
exit 1
fi
fi
chmod +x "$APPDIR/usr/bin/bwrap"
}
pkg_add() {
# Either download or copy initial AppRun
if [ -f "$LOCAL_PATH/AppRun.rootfs" ]; then
cp "$LOCAL_PATH/AppRun.rootfs" "$APPDIR/AppRun"
else
if ! wget -qO "$APPDIR/AppRun" https://raw.githubusercontent.com/xplshn/pelf/refs/heads/dev/assets/AppRun.rootfs-based; then
echo "Failed to download AppRun.rootfs-based"
exit 1
fi
fi
chmod +x "$APPDIR/AppRun"
_command="--Xbwrap --uid 0 --gid 0 -- apk -X \"https://dl-cdn.alpinelinux.org/alpine/edge/main\" -X \"https://dl-cdn.alpinelinux.org/alpine/edge/community\" -X \"https://dl-cdn.alpinelinux.org/alpine/edge/testing\" -U --allow-untrusted --no-cache --no-interactive --initdb add \"$PKG_ADD\""
if [ -f "$LOCAL_PATH/pkgadd.sh" ]; then
_command="!disabled"
HOME="$HOME" PWD="$PWD" PATH="$PATH" "$APPDIR/AppRun" --Xbwrap --uid 0 --gid 0 -- sh "$LOCAL_PATH/pkgadd.sh" "$PKG_ADD" || return $?
fi
if [ -f "$LOCAL_PATH/pkgadd" ]; then
_command="$(cat "$LOCAL_PATH/pkgadd")"
fi
echo "sh" >"$APPDIR/proto/entrypoint"
[ "$_command" != "!disabled" ] && eval env -i HOME="$HOME" PWD="$PWD" PATH="$PATH" "$APPDIR/AppRun" "$_command"
[ "$SANDBOX" = "1" ] && {
echo "/usr/local/bin/LAUNCH" >"$APPDIR/proto/usr/local/bin/default"
mkdir -p "$APPDIR/proto/usr/local/bin"
# Either download or copy LAUNCH (multicall helper for rootfs-based)
if [ -f "$LOCAL_PATH/LAUNCH" ]; then
cp "$LOCAL_PATH/LAUNCH" "$APPDIR/proto/usr/local/bin/LAUNCH"
else
if ! wget -qO "$APPDIR/proto/usr/local/bin/LAUNCH" "https://raw.githubusercontent.com/xplshn/pelf/refs/heads/pelf-ng/assets/LAUNCH-multicall.rootfs.entrypoint"; then
echo "Failed to download LAUNCH for multicall"
exit 1
fi
fi
chmod +x "$APPDIR/proto/usr/local/bin/LAUNCH"
chmod +x "$APPDIR/proto/usr/local/bin/default"
}
}
create_entrypoint() {
echo "$ENTRYPOINT" > "$APPDIR/entrypoint" || {
echo "Failed to create entrypoint file at \"$APPDIR/entrypoint\""
exit 1
}
chmod +x "$APPDIR/entrypoint" || {
echo "Failed to make entrypoint executable"
exit 1
}
}
handle_desktop_file() {
if [ "${ENTRYPOINT##*.}" = "desktop" ]; then
if [ -f "$APPDIR/proto/usr/share/applications/$ENTRYPOINT" ]; then
ln -f "$APPDIR/proto/usr/share/applications/$ENTRYPOINT" "$APPDIR/$ENTRYPOINT" || {
echo "Failed to link $APPDIR/proto/usr/share/applications/$ENTRYPOINT"
exit 1
}
ICON_NAME="$(awk -F"=" '/Icon/ {print $2; exit}' "$APPDIR/$ENTRYPOINT" 2>/dev/null)"
# Modified awk command to better handle Exec= entries
ENTRYPOINT="$(awk -F"=" '/^Exec=/ {sub("^Exec=", ""); print $1; exit}' "$APPDIR/$ENTRYPOINT" 2>/dev/null)"
if [ -z "$ENTRYPOINT" ]; then
echo "Failed to extract Exec entry from desktop file"
exit 1
fi
create_entrypoint
if [ -n "$ICON_NAME" ]; then
# Exclude resolutions below 128x128 and select PNG files only
ICON_PATH=$(find "$APPDIR/proto/usr/share/icons" -type f -name "$ICON_NAME*.png" 2>/dev/null \
| grep -v '/\(16x16\|24x24\|32x32\|48x48\|64x64\|96x96\)/' \
| sort -n | head -n 1)
if [ -n "$ICON_PATH" ]; then
ln -f "$ICON_PATH" "$APPDIR/.DirIcon" || {
echo "Failed to copy icon $ICON_PATH"
exit 1
}
else
echo "Icon $ICON_NAME not found in $APPDIR/proto/usr/share/icons/"
fi
# Also copy an SVG version if available
SVG_ICON_PATH=$(find "$APPDIR/proto/usr/share/icons" -type f -name "$ICON_NAME*.svg" 2>/dev/null | head -n 1)
if [ -n "$SVG_ICON_PATH" ]; then
cp "$SVG_ICON_PATH" "$APPDIR/.DirIcon.svg" || {
echo "Failed to copy SVG icon $SVG_ICON_PATH"
exit 1
}
fi
else
echo "No Icon entry found in $APPDIR/proto/usr/share/applications/$ENTRYPOINT"
fi
else
echo "No such desktop file: $APPDIR/proto/usr/share/applications/$ENTRYPOINT"
exit 1
fi
fi
}
select_structure_and_apprun() {
if [ "$SHARUN" = "1" ]; then
# Prepare the list of binaries to be kept
TOBEKEPT_BIN=""
for ITEM in $LIB4BIN_ARGS; do
KEEP=true
for EXCLUDED in $GETRID_FILES; do
if [ "$ITEM" = "$EXCLUDED" ]; then
KEEP=false
break
fi
done
if $KEEP; then
TOBEKEPT_BIN="$TOBEKEPT_BIN $APPDIR/proto/$ITEM"
fi
done
# shellcheck disable=SC2086 # Generate and execute lib4bin script
printf 'LD_LIBRARY_PATH="%s/proto/lib:%s/proto/usr/lib" lib4bin --dst-dir "%s"' "$APPDIR" "$APPDIR" "$APPDIR" >"$APPDIR/.genl4b"
echo " $TOBEKEPT_BIN" >>"$APPDIR/.genl4b"
chmod +x "$APPDIR/.genl4b"
# Either download or copy AppRun.sharun
if "$APPDIR/.genl4b"; then
if [ -f "$LOCAL_PATH/AppRun.sharun" ]; then
cp "$LOCAL_PATH/AppRun.sharun" "$APPDIR/AppRun"
else
if ! wget -qO "$APPDIR/AppRun" \
"https://raw.githubusercontent.com/xplshn/pelf/refs/heads/pelf-ng/assets/AppRun.sharun"; then
echo "Failed to download AppRun.sharun, unable to proceed with the NOROOTFS procedure"
exit 1
fi
fi
chmod +x "$APPDIR/AppRun"
[ -z "$TOBEKEPT_FILES" ] && rm -rf "$APPDIR/proto" # SHARUN mode without Proto mode
fi
fi
if [ -n "$TOBEKEPT_FILES" ]; then
# We're in EXPLICIT "proto" mode/norootfs. Let's only keep the specified files.
for ITEM in $TOBEKEPT_FILES; do # Doesn't do anything if the var is empty
KEEP=true
for EXCLUDED in $GETRID_FILES; do
if [ "$ITEM" = "$EXCLUDED" ]; then
KEEP=false
break
fi
done
if $KEEP; then
mkdir -p "$APPDIR/proto_trimmed/$(dirname "$ITEM")"
cp -r "$APPDIR/proto/$ITEM" "$APPDIR/proto_trimmed/$ITEM"
fi
done
rm -rf "$APPDIR/proto"
fi
# Finalized AppDir/proto or AppDir/proto_trimmed
# Either download or copy fuse-overlayfs
if [ -f "$LOCAL_PATH/fuse-overlayfs" ]; then
cp "$LOCAL_PATH/fuse-overlayfs" "$APPDIR/usr/bin/fuse-overlayfs"
else
if ! wget -qO "$APPDIR/usr/bin/fuse-overlayfs" \
"https://bin.ajam.dev/$(uname -m)/fuse-overlayfs"; then
echo "Unable to install fuse-overlayfs to $APPDIR/usr/bin/fuse-overlayfs"
exit 1
fi
fi
chmod +x "$APPDIR/usr/bin/fuse-overlayfs"
# Either download or copy AppRun.sharun.ovfsProto
if [ -f "$LOCAL_PATH/AppRun.sharun.ovfsProto" ]; then
cp "$LOCAL_PATH/AppRun.sharun.ovfsProto" "$APPDIR/AppRun"
else
if ! wget -qO "$APPDIR/AppRun" "https://raw.githubusercontent.com/xplshn/pelf/refs/heads/pelf-ng/assets/AppRun.sharun.ovfsProto"; then
echo "Failed to download AppRun.sharun.ovfsProto, unable to proceed with the NOROOTFS procedure"
exit 1
fi
fi
chmod +x "$APPDIR/AppRun"
}
tidyup() {
for EXCLUDED in $GETRID_FILES; do
if [ -f "$APPDIR/proto/$EXCLUDED" ] || [ -d "$APPDIR/proto/$EXCLUDED" ]; then
rm -rf "${APPDIR:?}/proto/$EXCLUDED"
fi
done
[ -d "$APPDIR/proto" ] && mkdir -p "$APPDIR/proto/app" "$APPDIR/proto/host" && {
rm -rf "$APPDIR/proto/etc/machine-id" "$APPDIR/proto/etc/machine-id" \
"$APPDIR/proto/etc/resolv.conf" "$APPDIR/proto/etc/passwd" \
"$APPDIR/proto/etc/group" "$APPDIR/proto/etc/hosts" \
"$APPDIR/proto/etc/hostname" "$APPDIR/proto/etc/localtime" \
"$APPDIR/proto/__w" "$APPDIR/proto/github"
[ "$SANDBOX" = "1" ] && {
touch "$APPDIR/proto/etc/machine-id" \
"$APPDIR/proto/etc/hostname" \
"$APPDIR/proto/etc/localtime" \
"$APPDIR/proto/etc/resolv.conf"
}
}
}
upx_static_tools() {
if command -v "upx" >/dev/null 2>&1; then
for file in "$APPDIR/usr/bin"/*; do
upx "$file"
done
fi
}
create_bundle() {
printf 'pelf-dwfs --add-appdir "%s" --appbundle-id "%s" --output-to "%s" --embed-static-tools --compression "%s"\n' "$APPDIR" "$APPBUNDLE_ID" "$OUTPUT_TO" "$COMPRESSION_OPTS" >"$APPDIR/.gen"
chmod +x "$APPDIR/.gen"
if [ "$DONTPACK" != "1" ]; then
# Either download or use the local pelf-dwfs
if [ -f "$LOCAL_PATH/pelf-dwfs" ]; then
"$LOCAL_PATH/pelf-dwfs" --add-appdir "$APPDIR" --appbundle-id "$APPBUNDLE_ID" --output-to "$OUTPUT_TO" --embed-static-tools --compression "$COMPRESSION_OPTS"
else
if ! wget -qO- "https://raw.githubusercontent.com/xplshn/pelf/refs/heads/pelf-ng/pelf-dwfs" | sh -s -- --add-appdir "$APPDIR" --appbundle-id "$APPBUNDLE_ID" --output-to "$OUTPUT_TO" --embed-static-tools --compression "$COMPRESSION_OPTS"; then
echo "Final packaging failed"
exit 1
fi
fi
fi
}
main() {
parse_arguments "$@"
check_required_arguments
setup_local_path
create_appdir
download_and_extract_rootfs
add_bwrap
pkg_add
# Actual logic 0------------------------------------->
create_entrypoint # 1.
handle_desktop_file # 2.
[ "$SANDBOX" != "1" ] && select_structure_and_apprun # 3.
tidyup # 4.
upx_static_tools # 5.
create_bundle # 6.
}
main "$@"