-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
conty-start.sh
executable file
·1267 lines (1024 loc) · 38.3 KB
/
conty-start.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
## Dependencies: bash gzip fuse2 (or fuse3) tar coreutils
LD_PRELOAD_ORIG="${LD_PRELOAD}"
LD_LIBRARY_PATH_ORIG="${LD_LIBRARY_PATH}"
unset LD_PRELOAD LD_LIBRARY_PATH
LC_ALL_ORIG="${LC_ALL}"
export LC_ALL=C
msg_root="
Do not run this script as root!
If you really need to run it as root and know what you are doing, set
the ALLOW_ROOT environment variable.
"
# Refuse to run as root unless environment variable is set
if (( EUID == 0 )) && [ -z "$ALLOW_ROOT" ]; then
echo "${msg_root}"
exit 1
fi
# Conty version
script_version="1.26.3"
# Important variables to manually adjust after modification!
# Needed to avoid problems with mounting due to an incorrect offset.
#
# If you build Conty without some of the components, you can set their
# size to 0
init_size=50000
bash_size=1752808
script_size=38502
busybox_size=1181592
utils_size=4392469
# Full path to the script
if [ -n "${BASH_SOURCE[0]}" ]; then
script_literal="${BASH_SOURCE[0]}"
else
script_literal="${0}"
if [ "${script_literal}" = "$(basename "${script_literal}")" ]; then
script_literal="$(command -v "${0}")"
fi
fi
script_name="$(basename "${script_literal}")"
script="$(readlink -f "${script_literal}")"
# Check if head supports -c argument
unset HEAD_C_SUPPORTED
head -c 0 /dev/null &>/dev/null && HEAD_C_SUPPORTED=1
# MD5 of the first 4 MB and the last 1 MB of the script
if [ "${HEAD_C_SUPPORTED}" = 1 ]; then
script_md5="$(head -c 4000000 "${script}" | md5sum | head -c 7)"_"$(tail -c 1000000 "${script}" | md5sum | head -c 7)"
else
script_md5="$(dd if="${script}" bs=4000000 count=1 2>/dev/null | md5sum | cut -c1-7)"_"$(dd if="${script}" bs=1000000 skip=100 count=1 2>/dev/null | md5sum | cut -c1-7)"
fi
script_id="$$"
# Working directory where the utils will be extracted
# And where the image will be mounted
# The default path is /tmp/conty_username_scriptmd5
# And if /tmp is mounted with noexec, the default path
# is ~/.local/share/Conty/conty_username_scriptmd5
conty_dir_name=conty_"${USER}"_"${script_md5}"
if [ -z "${BASE_DIR}" ]; then
export working_dir=/tmp/"${conty_dir_name}"
else
export working_dir="${BASE_DIR}"/"${conty_dir_name}"
fi
if [ "${USE_SYS_UTILS}" != 1 ] && [ "${busybox_size}" -gt 0 ]; then
busybox_bin_dir="${working_dir}"/busybox_bins
busybox_path="${busybox_bin_dir}"/busybox
if [ ! -f "${busybox_bin_dir}"/echo ]; then
mkdir -p "${busybox_bin_dir}"
if [ "${HEAD_C_SUPPORTED}" = 1 ]; then
tail -c +$((init_size+bash_size+script_size+1)) "${script}" | head -c "${busybox_size}" > "${busybox_path}"
else
dd if="${script}" of="${busybox_path}" bs=1 skip=$((init_size+bash_size+script_size)) count="${busybox_size}" 2>/dev/null
fi
chmod +x "${busybox_path}" 2>/dev/null
"${busybox_path}" --install -s "${busybox_bin_dir}" &>/dev/null
fi
if "${busybox_bin_dir}"/echo &>/dev/null; then
export PATH="${busybox_bin_dir}:${PATH}"
fi
fi
# Help output
msg_help="
Usage: ${script_name} [COMMAND] [ARGUMENTS]
Arguments:
-e Extract the image
-h Display this text
-H Display bubblewrap help
-g Run the Conty's graphical interface
-l Show a list of all installed packages
-d Export desktop files from Conty into the application menu of
your desktop environment.
Note that not all applications have desktop files, and also that
desktop files are tied to the current location of Conty, so if
you move or rename it, you will need to re-export them.
To remove the exported files, use this argument again.
-m Mount/unmount the image
The image will be mounted if it's not, unmounted otherwise.
Mount point can be changed with the BASE_DIR env variable
(the default is /tmp).
-o Show the image offset
-u Update all packages inside the container
This requires a rebuild of the image, which may take quite
a lot of time, depending on your hardware and internet speed.
Additional disk space (about 6x the size of the current file)
is needed during the update process.
-v Display version of this script
-V Display version of the image
Arguments that don't match any of the above will be passed directly to
bubblewrap, so all bubblewrap arguments are supported as well.
Environment variables:
BASE_DIR Sets a custom directory where Conty will extract its
builtin utilities and mount the image.
The default is /tmp.
DISABLE_NET Disables network access.
DISABLE_X11 Disables access to X server.
Note: Even with this variable enabled applications
can still access your X server if it doesn't use
XAUTHORITY and listens to the abstract socket. This
can be solved by enabling XAUTHORITY, disabling the
abstract socket or by disabling network access.
HOME_DIR Sets the home directory to a custom location.
For example: HOME_DIR=\"$HOME/custom_home\"
Note: If this variable is set the home directory
inside the container will still appear as $HOME,
even though the custom directory is used.
QUIET_MODE Disables all non-error Conty messages.
Doesn't affect the output of applications.
SANDBOX Enables a sandbox.
To control which files and directories are available
inside the container, you can use the --bind and
--ro-bind launch arguments.
(See bubblewrap help for more info).
SANDBOX_LEVEL Controls the strictness of the sandbox.
Available levels:
1: Isolates all user files.
2: Additionally disables dbus and hides all
running processes.
3: Additionally disables network access and
isolates X11 server with Xephyr.
The default is 1.
USE_OVERLAYFS Mounts a writable unionfs-fuse filesystem on top
of the read-only squashfs/dwarfs image, allowing to
modify files inside it.
Overlays are stored in ~/.local/share/Conty. If you
want to undo any changes, delete the entire
directory from there.
NVIDIA_HANDLER Fixes issues with graphical applications on Nvidia
GPUs with the proprietary driver. Enable this only
if you are using an Nvidia GPU, the proprietary
driver and encountering issues running graphical
applications. At least 2 GB of free disk space is
required. This function is enabled by default.
USE_SYS_UTILS Tells the script to use squashfuse/dwarfs and bwrap
installed on the system instead of the builtin ones.
XEPHYR_SIZE Sets the size of the Xephyr window. The default is
800x600.
CUSTOM_MNT Sets a custom mount point for the Conty. This allows
Conty to be used with already mounted filesystems.
Conty will not mount its image on this mount point,
but it will use files that are already present
there.
Additional notes:
System directories/files will not be available inside the container if
you set the SANDBOX variable but don't bind (mount) any items or set
HOME_DIR. A fake temporary home directory will be used instead.
If the executed script is a symlink with a different name, said name
will be used as the command name.
For instance, if the script is a symlink with the name \"wine\" it will
automatically run wine during launch.
Running Conty without any arguments from a graphical interface (for
example, from a file manager) will automatically launch the Conty's
graphical interface.
Besides updating all packages, you can also install and remove packages
using the same -u argument. To install packages add them as additional
arguments, to remove add a minus sign (-) before their names.
To install: ${script_name} -u pkgname1 pkgname2 pkgname3 ...
To remove: ${script_name} -u -pkgname1 -pkgname2 -pkgname3 ...
In this case Conty will update all packages and additionally install
and/or remove specified packages.
If you are using an Nvidia GPU, please read the following:
https://github.com/Kron4ek/Conty#known-issues
"
if [ -n "${CUSTOM_MNT}" ] && [ -d "${CUSTOM_MNT}" ]; then
mount_point="${CUSTOM_MNT}"
else
mount_point="${working_dir}"/mnt
fi
export overlayfs_dir="${HOME}"/.local/share/Conty/overlayfs_"${script_md5}"
export nvidia_drivers_dir="${overlayfs_dir}"/nvidia
export overlayfs_shared_dir="${HOME}"/.local/share/Conty/overlayfs_shared
export nvidia_drivers_shared_dir="${overlayfs_shared_dir}"/nvidia
# Offset where the image is stored
offset=$((init_size+bash_size+script_size+busybox_size+utils_size))
# Detect if the image is compressed with DwarFS or SquashFS
if [ "$(tail -c +$((offset+1)) "${script}" | head -c 6)" = "DWARFS" ]; then
dwarfs_image=1
fi
# These arguments are used to rebuild the image when using the self-update function
squashfs_comp_arguments=(-b 1M -comp zstd -Xcompression-level 19)
dwarfs_comp_arguments=(-l7 -C zstd:level=19 --metadata-compression null \
-S 21 -B 1 --order nilsimsa \
-W 12 -w 4 --no-create-timestamp)
# Enable NVIDIA_HANDLER by default
NVIDIA_HANDLER="${NVIDIA_HANDLER:-1}"
unset script_is_symlink
if [ -L "${script_literal}" ]; then
script_is_symlink=1
fi
if [ -z "${script_is_symlink}" ]; then
if [ -t 0 ] && ([ "$1" = "-h" ] || [ -z "$1" ]); then
echo "${msg_help}"
exit
elif [ "$1" = "-v" ]; then
echo "${script_version}"
exit
elif [ "$1" = "-o" ]; then
echo "${offset}"
exit
fi
fi
show_msg () {
if [ "${QUIET_MODE}" != 1 ]; then
echo "$@"
fi
}
exec_test () {
mkdir -p "${working_dir}"
exec_test_file="${working_dir}"/exec_test
rm -f "${exec_test_file}"
touch "${exec_test_file}"
chmod +x "${exec_test_file}"
[ -x "${exec_test_file}" ]
}
launch_wrapper () {
if [ "${USE_SYS_UTILS}" = 1 ]; then
"$@"
else
"${working_dir}"/utils/ld-linux-x86-64.so.2 --library-path "${working_dir}"/utils "$@"
fi
}
gui () {
if ! command -v zenity 1>/dev/null; then
exit 1
fi
gui_response=$(zenity --title="Conty" \
--entry \
--text="Enter a command or select a file you want to run" \
--ok-label="Run" \
--cancel-label="Quit" \
--extra-button="Select a file" \
--extra-button="Open a terminal")
gui_exit_code=$?
if [ "${gui_response}" = "Select a file" ]; then
filepath="$(zenity --title="A file to run" --file-selection)"
if [ -f "${filepath}" ]; then
[ -x "${filepath}" ] || chmod +x "${filepath}"
"${filepath}"
else
zenity --error --text="You did not select a file"
fi
elif [ "${gui_response}" = "Open a terminal" ]; then
if command -v lxterminal 1>/dev/null; then
lxterminal -T "Conty terminal" --command="bash -c 'echo Welcome to Conty; echo Enter any commands you want to execute; bash'"
else
zenity --error --text="A terminal emulator is not installed in this instance of Conty"
fi
elif [ "${gui_exit_code}" = 0 ]; then
if [ -z "${gui_response}" ]; then
zenity --error --text="You need to enter a command to execute"
else
for a in ${gui_response}; do
if [ "${a:0:1}" = "\"" ] || [ "${a:0:1}" = "'" ] || [ -n "${combined_args}" ]; then
combined_args="${combined_args} ${a}"
if [ "${a: -1}" = "\"" ] || [ "${a: -1}" = "'" ]; then
combined_args="${combined_args:2}"
combined_args="${combined_args%?}"
launch_command+=("${combined_args}")
unset combined_args
fi
continue
fi
launch_command+=("${a}")
done
"${launch_command[@]}"
fi
fi
}
mount_overlayfs () {
mkdir -p "${overlayfs_dir}"/up
mkdir -p "${overlayfs_dir}"/work
mkdir -p "${overlayfs_dir}"/merged
mkdir -p "${nvidia_drivers_dir}"
if [ ! "$(ls "${overlayfs_dir}"/merged 2>/dev/null)" ]; then
if command -v "${unionfs_fuse}" 1>/dev/null; then
if [ "${1}" = "share_nvidia" ]; then
launch_wrapper "${unionfs_fuse}" -o relaxed_permissions,cow,noatime "${overlayfs_dir}"/up=RW:"${overlayfs_shared_dir}"/up=RO:"${mount_point}"=RO "${overlayfs_dir}"/merged
else
launch_wrapper "${unionfs_fuse}" -o relaxed_permissions,cow,noatime "${overlayfs_dir}"/up=RW:"${mount_point}"=RO "${overlayfs_dir}"/merged
fi
else
echo "unionfs-fuse not found"
return 1
fi
fi
}
nvidia_driver_handler () {
OLD_PWD="${PWD}"
rm -rf "${nvidia_drivers_dir}"/nvidia.run "${nvidia_drivers_dir}"/nvidia-driver
mkdir -p "${nvidia_drivers_dir}"
cd "${nvidia_drivers_dir}"
echo "Found Nvidia driver ${nvidia_driver_version}"
echo "Downloading the Nvidia driver ${nvidia_driver_version}..."
# Try to download the driver from the default Nvidia URL
driver_url="https://us.download.nvidia.com/XFree86/Linux-x86_64/${nvidia_driver_version}/NVIDIA-Linux-x86_64-${nvidia_driver_version}.run"
curl -#Lo nvidia.run "${driver_url}"
# If the previous download failed, get the URL from FlatHub repo
if [ ! -s nvidia.run ] || [ "$(stat -c%s nvidia.run)" -lt 30000000 ]; then
rm -f nvidia.run
driver_url="https:$(curl -#Lo - "https://raw.githubusercontent.com/flathub/org.freedesktop.Platform.GL.nvidia/master/data/nvidia-${nvidia_driver_version}-x86_64.data" | cut -d ':' -f 6)"
curl -#Lo nvidia.run "${driver_url}"
fi
if [ -s nvidia.run ]; then
echo "Installing the Nvidia driver, please wait..."
chmod +x nvidia.run
./nvidia.run --target nvidia-driver -x &>/dev/null
if [ -f nvidia-driver/nvidia-installer ]; then
cd nvidia-driver || exit 1
chmod +x nvidia-installer
fakeroot ./nvidia-installer --silent --no-x-check --no-kernel-module &>/dev/null
rm -rf "${nvidia_drivers_dir}"/nvidia.run "${nvidia_drivers_dir}"/nvidia-driver
if [ -s /usr/lib/libGLX_nvidia.so."${nvidia_driver_version}" ] || \
[ -s /usr/lib/libGL.so."${nvidia_driver_version}" ]; then
cp /usr/lib/tls/libnvidia-tls.so.* /usr/lib &>/dev/null
cp /usr/lib32/tls/libnvidia-tls.so.* /usr/lib32 &>/dev/null
echo "${nvidia_driver_version}" > "${nvidia_drivers_dir}"/current-nvidia-version
echo "The driver installed successfully"
else
echo "Failed to install the driver"
fi
else
echo "Failed to extract the driver"
fi
else
echo "Failed to download the driver"
fi
cd "${OLD_PWD}"
}
update_conty () {
if [ "$(ls /var/cache/pacman/pkg_host 2>/dev/null)" ]; then
mkdir -p /var/cache/pacman/pkg
ln -s /var/cache/pacman/pkg_host/* /var/cache/pacman/pkg 2>/dev/null
fi
reflector --protocol https --score 5 --sort rate --save /etc/pacman.d/mirrorlist
fakeroot -- pacman -Syy 2>/dev/null
date -u +"%d-%m-%Y %H:%M (DMY UTC)" > /version
fakeroot -- pacman --noconfirm -S archlinux-keyring 2>/dev/null
fakeroot -- pacman --noconfirm -S chaotic-keyring 2>/dev/null
rm -rf /etc/pacman.d/gnupg/*
fakeroot -- pacman-key --init
echo "keyserver hkps://keyserver.ubuntu.com" >> /etc/pacman.d/gnupg/gpg.conf
fakeroot -- pacman-key --populate archlinux
fakeroot -- pacman-key --populate chaotic
fakeroot -- pacman --noconfirm --overwrite "*" --ignore fakeroot -Su 2>/dev/null
fakeroot -- pacman --noconfirm -Runs ${pkgsremove} 2>/dev/null
fakeroot -- pacman --noconfirm -S ${pkgsinstall} 2>/dev/null
ldconfig -C /etc/ld.so.cache
rm -f /var/cache/pacman/pkg/*
pacman -Q > /pkglist.x86_64.txt
update-ca-trust
locale-gen
}
# Check if FUSE is installed
if ! command -v fusermount3 1>/dev/null && ! command -v fusermount 1>/dev/null; then
echo "Please install fuse2 or fuse3 and run the script again."
exit 1
fi
if command -v fusermount3 1>/dev/null; then
fuse_version=3
fi
# Set the dwarfs block cache size depending on how much RAM is available
# Also set the number of workers depending on the number of CPU cores
dwarfs_cache_size="128M"
dwarfs_num_workers="2"
if [ "${dwarfs_image}" = 1 ]; then
if getconf _PHYS_PAGES &>/dev/null && getconf PAGE_SIZE &>/dev/null; then
memory_size="$(($(getconf _PHYS_PAGES) * $(getconf PAGE_SIZE) / (1024 * 1024)))"
if [ "${memory_size}" -ge 45000 ]; then
dwarfs_cache_size="4096M"
elif [ "${memory_size}" -ge 23000 ]; then
dwarfs_cache_size="2048M"
elif [ "${memory_size}" -ge 15000 ]; then
dwarfs_cache_size="1024M"
elif [ "${memory_size}" -ge 7000 ]; then
dwarfs_cache_size="512M"
elif [ "${memory_size}" -ge 3000 ]; then
dwarfs_cache_size="256M"
elif [ "${memory_size}" -ge 1500 ]; then
dwarfs_cache_size="128M"
else
dwarfs_cache_size="64M"
fi
fi
if getconf _NPROCESSORS_ONLN &>/dev/null; then
dwarfs_num_workers="$(getconf _NPROCESSORS_ONLN)"
if [ "${dwarfs_num_workers}" -ge 8 ]; then
dwarfs_num_workers=8
fi
fi
fi
# Extract utils.tar.gz
mkdir -p "${working_dir}"
if ([ "${USE_SYS_UTILS}" != 1 ] && [ "${utils_size}" -gt 0 ]) || [ "$1" = "-u" ]; then
# Check if filesystem of the working_dir is mounted without noexec
if ! exec_test; then
if [ -z "${BASE_DIR}" ]; then
export working_dir="${HOME}"/.local/share/Conty/"${conty_dir_name}"
if [ -z "${CUSTOM_MNT}" ]; then
mount_point="${working_dir}"/mnt
fi
fi
if ! exec_test; then
echo "Seems like /tmp is mounted with noexec or you don't have write access!"
echo "Please remount it without noexec or set BASE_DIR to a different location."
exit 1
fi
fi
if ! command -v tar 1>/dev/null || ! command -v gzip 1>/dev/null; then
echo "Please install tar and gzip and run the script again."
exit 1
fi
if [ "${dwarfs_image}" = 1 ]; then
mount_tool="${working_dir}"/utils/dwarfs"${fuse_version}"
extraction_tool="${working_dir}"/utils/dwarfsextract
compression_tool="${working_dir}"/utils/mkdwarfs
else
mount_tool="${working_dir}"/utils/squashfuse"${fuse_version}"
extraction_tool="${working_dir}"/utils/unsquashfs
compression_tool="${working_dir}"/utils/mksquashfs
fi
bwrap="${working_dir}"/utils/bwrap
unionfs_fuse="${working_dir}"/utils/unionfs"${fuse_version}"
if [ ! -f "${mount_tool}" ] || [ ! -f "${bwrap}" ]; then
tail -c +$((init_size+bash_size+script_size+busybox_size+1)) "${script}" | head -c "${utils_size}" | tar -C "${working_dir}" -zxf -
if [ ! -f "${mount_tool}" ] || [ ! -f "${bwrap}" ]; then
clear
echo "The integrated utils were not extracted!"
echo "Perhaps something is wrong with the integrated utils.tar.gz."
exit 1
fi
chmod +x "${mount_tool}" 2>/dev/null
chmod +x "${bwrap}" 2>/dev/null
chmod +x "${extraction_tool}" 2>/dev/null
chmod +x "${unionfs_fuse}" 2>/dev/null
chmod +x "${compression_tool}" 2>/dev/null
fi
else
if ! command -v bwrap 1>/dev/null; then
echo "USE_SYS_UTILS is enabled, but bubblewrap is not installed!"
echo "Please install it and run the script again."
exit 1
fi
bwrap=bwrap
unionfs_fuse=unionfs
if [ "${dwarfs_image}" = 1 ]; then
if ! command -v dwarfs 1>/dev/null && ! command -v dwarfs2 1>/dev/null; then
echo "USE_SYS_UTILS is enabled, but dwarfs is not installed!"
echo "Please install it and run the script again."
exit 1
fi
if command -v dwarfs2 1>/dev/null; then
mount_tool=dwarfs2
else
mount_tool=dwarfs
fi
extraction_tool=dwarfsextract
else
if ! command -v squashfuse 1>/dev/null; then
echo "USE_SYS_UTILS is enabled, but squashfuse is not installed!"
echo "Please install it and run the script again."
exit 1
fi
mount_tool=squashfuse
extraction_tool=unsquashfs
fi
show_msg "Using system-wide ${mount_tool} and bwrap"
fi
if [ "$1" = "-e" ] && [ -z "${script_is_symlink}" ]; then
if command -v "${extraction_tool}" 1>/dev/null; then
if [ "${dwarfs_image}" = 1 ]; then
echo "Extracting the image..."
mkdir "$(basename "${script}")"_files
launch_wrapper "${extraction_tool}" -i "${script}" -o "$(basename "${script}")"_files -O "${offset}"
echo "Done"
else
launch_wrapper "${extraction_tool}" -o "${offset}" -user-xattrs -d "$(basename "${script}")"_files "${script}"
fi
else
echo "Extraction tool not found"
exit 1
fi
exit
fi
if [ "$1" = "-H" ] && [ -z "${script_is_symlink}" ]; then
launch_wrapper "${bwrap}" --help
exit
fi
run_bwrap () {
unset sandbox_params
unset unshare_net
unset custom_home
unset non_standard_home
unset xsockets
unset mount_opt
unset command_line
command_line=("${@}")
if [ -n "${WAYLAND_DISPLAY}" ]; then
wayland_socket="${WAYLAND_DISPLAY}"
else
wayland_socket="wayland-0"
fi
if [ -z "${XDG_RUNTIME_DIR}" ]; then
XDG_RUNTIME_DIR="/run/user/${EUID}"
fi
# Handle non-standard HOME locations that are outside of our default
# visibility scope
if [ -n "${HOME}" ] && [ "$(echo "${HOME}" | head -c 6)" != "/home/" ]; then
HOME_BASE_DIR="$(echo "${HOME}" | cut -d '/' -f2)"
case "${HOME_BASE_DIR}" in
tmp|mnt|media|run|var)
;;
*)
NEW_HOME=/home/"${USER}"
non_standard_home+=(--tmpfs /home \
--bind "${HOME}" "${NEW_HOME}" \
--setenv "HOME" "${NEW_HOME}" \
--setenv "XDG_CONFIG_HOME" "${NEW_HOME}"/.config \
--setenv "XDG_DATA_HOME" "${NEW_HOME}"/.local/share)
unset command_line
for arg in "$@"; do
if [[ "${arg}" == *"${HOME}"* ]]; then
arg="$(echo "${arg/"$HOME"/"$NEW_HOME"}")"
fi
command_line+=("${arg}")
done
;;
esac
fi
if [ "${SANDBOX}" = 1 ]; then
sandbox_params+=(--tmpfs /home \
--tmpfs /mnt \
--tmpfs /media \
--tmpfs /var \
--tmpfs /run \
--symlink /run /var/run \
--tmpfs /tmp \
--new-session)
if [ -n "${non_standard_home[*]}" ]; then
sandbox_params+=(--dir "${NEW_HOME}")
else
sandbox_params+=(--dir "${HOME}")
fi
if [ -n "${SANDBOX_LEVEL}" ] && [ "${SANDBOX_LEVEL}" -ge 2 ]; then
sandbox_level_msg="(level 2)"
sandbox_params+=(--dir "${XDG_RUNTIME_DIR}" \
--ro-bind-try "${XDG_RUNTIME_DIR}"/"${wayland_socket}" "${XDG_RUNTIME_DIR}"/"${wayland_socket}" \
--ro-bind-try "${XDG_RUNTIME_DIR}"/pulse "${XDG_RUNTIME_DIR}"/pulse \
--ro-bind-try "${XDG_RUNTIME_DIR}"/pipewire-0 "${XDG_RUNTIME_DIR}"/pipewire-0 \
--unshare-pid \
--unshare-user-try \
--unsetenv "DBUS_SESSION_BUS_ADDRESS")
else
sandbox_level_msg="(level 1)"
sandbox_params+=(--bind-try "${XDG_RUNTIME_DIR}" "${XDG_RUNTIME_DIR}" \
--bind-try /run/dbus /run/dbus)
fi
if [ -n "${SANDBOX_LEVEL}" ] && [ "${SANDBOX_LEVEL}" -ge 3 ]; then
sandbox_level_msg="(level 3)"
DISABLE_NET=1
fi
show_msg "Sandbox is enabled ${sandbox_level_msg}"
fi
if [ "${DISABLE_NET}" = 1 ]; then
show_msg "Network is disabled"
unshare_net=(--unshare-net)
fi
if [ -n "${HOME_DIR}" ]; then
show_msg "Home directory is set to ${HOME_DIR}"
if [ -n "${non_standard_home[*]}" ]; then
custom_home+=(--bind "${HOME_DIR}" "${NEW_HOME}")
else
custom_home+=(--bind "${HOME_DIR}" "${HOME}")
fi
[ ! -d "${HOME_DIR}" ] && mkdir -p "${HOME_DIR}"
fi
# Set the XAUTHORITY variable if it's missing
if [ -z "${XAUTHORITY}" ]; then
XAUTHORITY="${HOME}"/.Xauthority
fi
# Mount X server sockets and XAUTHORITY
xsockets+=(--tmpfs /tmp/.X11-unix)
if [ -n "${non_standard_home[*]}" ] && [ "${XAUTHORITY}" = "${HOME}"/.Xauthority ]; then
xsockets+=(--ro-bind-try "${XAUTHORITY}" "${NEW_HOME}"/.Xauthority \
--setenv "XAUTHORITY" "${NEW_HOME}"/.Xauthority)
else
xsockets+=(--ro-bind-try "${XAUTHORITY}" "${XAUTHORITY}")
fi
if [ "${DISABLE_X11}" != 1 ]; then
if [ "$(ls /tmp/.X11-unix 2>/dev/null)" ]; then
if [ -n "${SANDBOX_LEVEL}" ] && [ "${SANDBOX_LEVEL}" -ge 3 ]; then
xsockets+=(--ro-bind-try /tmp/.X11-unix/X"${xephyr_display}" /tmp/.X11-unix/X"${xephyr_display}" \
--setenv "DISPLAY" :"${xephyr_display}")
else
for s in /tmp/.X11-unix/*; do
xsockets+=(--bind-try "${s}" "${s}")
done
fi
fi
else
show_msg "Access to X server is disabled"
# Unset the DISPLAY and XAUTHORITY env variables and mount an
# empty file to XAUTHORITY to invalidate it
xsockets+=(--ro-bind-try "${working_dir}"/running_"${script_id}" "${XAUTHORITY}" \
--unsetenv "DISPLAY" \
--unsetenv "XAUTHORITY")
fi
if [ ! "$(ls "${mount_point}"/opt 2>/dev/null)" ] && [ -z "${SANDBOX}" ]; then
mount_opt=(--bind-try /opt /opt)
fi
if ([ "${NVIDIA_HANDLER}" = 1 ] || [ "${USE_OVERLAYFS}" = 1 ]) && \
[ "$(ls "${overlayfs_dir}"/merged 2>/dev/null)" ]; then
newroot_path="${overlayfs_dir}"/merged
else
newroot_path="${mount_point}"
fi
if [ "${RW_ROOT}" = 1 ]; then
bind_root=(--bind "${newroot_path}" /)
else
bind_root=(--ro-bind "${newroot_path}" /)
fi
conty_variables="BASE_DIR DISABLE_NET DISABLE_X11 HOME_DIR QUIET_MODE \
SANDBOX SANDBOX_LEVEL USE_OVERLAYFS NVIDIA_HANDLER \
USE_SYS_UTILS XEPHYR_SIZE CUSTOM_MNT"
for v in ${conty_variables}; do
set_vars+=(--unsetenv "${v}")
done
[ -n "${LD_PRELOAD_ORIG}" ] && set_vars+=(--setenv LD_PRELOAD "${LD_PRELOAD_ORIG}")
[ -n "${LD_LIBRARY_PATH_ORIG}" ] && set_vars+=(--setenv LD_LIBRARY_PATH "${LD_LIBRARY_PATH_ORIG}")
if [ -n "${LC_ALL_ORIG}" ]; then
set_vars+=(--setenv LC_ALL "${LC_ALL_ORIG}")
else
set_vars+=(--unsetenv LC_ALL)
fi
show_msg
launch_wrapper "${bwrap}" \
"${bind_root[@]}" \
--dev-bind /dev /dev \
--ro-bind /sys /sys \
--bind-try /tmp /tmp \
--proc /proc \
--bind-try /home /home \
--bind-try /mnt /mnt \
--bind-try /media /media \
--bind-try /run /run \
--bind-try /var /var \
--ro-bind-try /usr/share/steam/compatibilitytools.d /usr/share/steam/compatibilitytools.d \
--ro-bind-try /etc/resolv.conf /etc/resolv.conf \
--ro-bind-try /etc/hosts /etc/hosts \
--ro-bind-try /etc/nsswitch.conf /etc/nsswitch.conf \
--ro-bind-try /etc/passwd /etc/passwd \
--ro-bind-try /etc/group /etc/group \
--ro-bind-try /etc/machine-id /etc/machine-id \
--ro-bind-try /etc/asound.conf /etc/asound.conf \
--ro-bind-try /etc/localtime /etc/localtime \
"${non_standard_home[@]}" \
"${sandbox_params[@]}" \
"${custom_home[@]}" \
"${mount_opt[@]}" \
"${xsockets[@]}" \
"${unshare_net[@]}" \
"${set_vars[@]}" \
--setenv PATH "${CUSTOM_PATH}" \
--setenv XDG_DATA_DIRS "/usr/local/share:/usr/share:${XDG_DATA_DIRS}" \
"${command_line[@]}"
}
exit_function () {
sleep 3
rm -f "${working_dir}"/running_"${script_id}"
if [ ! "$(ls "${working_dir}"/running_* 2>/dev/null)" ]; then
if [ -d "${overlayfs_dir}"/merged ]; then
fusermount"${fuse_version}" -uz "${overlayfs_dir}"/merged 2>/dev/null || \
umount --lazy "${overlayfs_dir}"/merged 2>/dev/null
fi
if [ -z "${CUSTOM_MNT}" ]; then
fusermount"${fuse_version}" -uz "${mount_point}" 2>/dev/null || \
umount --lazy "${mount_point}" 2>/dev/null
fi
if [ ! "$(ls "${mount_point}" 2>/dev/null)" ] || [ -n "${CUSTOM_MNT}" ]; then
rm -rf "${working_dir}"
fi
fi
exit
}
trap_exit () {
exit_function &
}
trap 'trap_exit' EXIT
if [ "$(ls "${working_dir}"/running_* 2>/dev/null)" ] && [ ! "$(ls "${mount_point}" 2>/dev/null)" ]; then
rm -f "${working_dir}"/running_*
fi
if [ -f "${nvidia_drivers_dir}"/lock ] && [ ! "$(ls "${working_dir}"/running_* 2>/dev/null)" ]; then
rm -f "${nvidia_drivers_dir}"/lock
fi
if [ "${dwarfs_image}" = 1 ]; then
mount_command=("${mount_tool}" \
"${script}" "${mount_point}" \
-o offset="${offset}" \
-o debuglevel=error \
-o workers="${dwarfs_num_workers}" \
-o mlock=try \
-o no_cache_image \
-o cache_files \
-o cachesize="${dwarfs_cache_size}" \
-o decratio=0.6 \
-o tidy_strategy=swap \
-o tidy_interval=5m)
else
mount_command=("${mount_tool}" \
-o offset="${offset}",ro \
"${script}" "${mount_point}")
fi
# Increase file descriptors limit in case soft and hard limits are different
# Useful for unionfs-fuse and for some games
ulimit -n $(ulimit -Hn) &>/dev/null
# Mount the image
mkdir -p "${mount_point}"
if [ "$(ls "${mount_point}" 2>/dev/null)" ] || launch_wrapper "${mount_command[@]}"; then
if [ "$1" = "-m" ] && [ -z "${script_is_symlink}" ]; then
if [ ! -f "${working_dir}"/running_mount ]; then
echo 1 > "${working_dir}"/running_mount
echo "The image has been mounted to ${mount_point}"
else
rm -f "${working_dir}"/running_mount
echo "The image has been unmounted"
fi
exit
fi
if [ "$1" = "-V" ] && [ -z "${script_is_symlink}" ]; then
if [ -f "${mount_point}"/version ]; then
cat "${mount_point}"/version
else
echo "Unknown version"
fi
exit
fi
if [ "$1" = "-d" ] && [ -z "${script_is_symlink}" ]; then
applications_dir="${HOME}"/.local/share/applications/Conty
if [ -d "${applications_dir}" ]; then
rm -rf "${applications_dir}"
echo "Desktop files have been removed"
exit
fi
mkdir -p "${applications_dir}"
cp -r "${mount_point}"/usr/share/applications "${applications_dir}"_temp
cd "${applications_dir}"_temp || exit 1
unset variables
vars="BASE_DIR DISABLE_NET DISABLE_X11 HOME_DIR SANDBOX SANDBOX_LEVEL USE_SYS_UTILS CUSTOM_MNT"
for v in ${vars}; do
if [ -n "${!v}" ]; then
variables="${v}=\"${!v}\" ${variables}"
fi
done
if [ -n "${variables}" ]; then
variables="env ${variables} "
fi
echo "Exporting..."
shift
for f in *.desktop */ */*.desktop; do
if [ "${f}" != "*.desktop" ] && [ "${f}" != "*/*.desktop" ] && [ "${f}" != "*/" ]; then
if [ -d "${f}" ]; then
mkdir -p "${applications_dir}"/"${f}"
continue
fi
if [ -L "${f}" ]; then
cp --remove-destination "${mount_point}"/"$(readlink "${f}")" "${f}"
fi
while read -r line; do
line_function="$(echo "${line}" | head -c 4)"
if [ "${line_function}" = "Name" ]; then
line="${line} (Conty)"
elif [ "${line_function}" = "Exec" ]; then
line="Exec=${variables}\"${script}\" $@ $(echo "${line}" | tail -c +6)"
elif [ "${line_function}" = "TryE" ]; then # pragma: codespell-ignore
continue
fi
echo $line >> "${applications_dir}"/"${f%.desktop}"-conty.desktop
done < "${f}"
fi
done
mkdir -p "${HOME}"/.local/share
cp -nr "${mount_point}"/usr/share/icons "${HOME}"/.local/share 2>/dev/null
rm -rf "${applications_dir}"_temp
echo "Desktop files have been exported"
exit
fi
echo 1 > "${working_dir}"/running_"${script_id}"
show_msg "Running Conty"
export CUSTOM_PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/lib/jvm/default/bin:/usr/local/bin:/usr/local/sbin:${PATH}"
if [ "$1" = "-l" ] && [ -z "${script_is_symlink}" ]; then
run_bwrap --ro-bind "${mount_point}"/var /var pacman -Q
exit
fi