-
Notifications
You must be signed in to change notification settings - Fork 14
/
container-session
executable file
·745 lines (513 loc) · 15.7 KB
/
container-session
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
#!/bin/bash
# dependencies /////////////////////////////////////////////////////////////////
if [[ -z $(command -v frobulator) ]]
then
if [[ $(id -u -n) = "root" ]]
then
SUDO_HOME=/root
USER="${SUDO_USER}"
HOME=/home/"${USER}"
fi
if [[ -z $(command -v curl) ]]
then
yes | apt-get install curl
fi
if [ ! -d "${HOME}"/.local/bin ]
then
mkdir -p "${HOME}"/.local/bin
fi
curl -s -L get.frbltr.app > "${HOME}"/.local/bin/frobulator
chmod +x "${HOME}"/.local/bin/frobulator
fi
. "${HOME}"/.local/bin/frobulator
# script ///////////////////////////////////////////////////////////////////////
script=$(basename -- "${BASH_SOURCE[0]}")
# version //////////////////////////////////////////////////////////////////////
version="12-05-2024"
# usage ////////////////////////////////////////////////////////////////////////
while (($#))
do
case "${1}"
in
-o|--start)
option="start"
;;
-x|--stop)
option="stop"
;;
-u|--username)
option="username"
user_name="${2}"
shift
;;
-d|--directory)
option="directory"
container_directory="${2}"
shift
;;
-h|--help)
echo
echo -e "Usage: ${script} -o [Display Server] | -x | -u [User Name] | -d [Directory] | [OPTION]"
echo
echo -e "Options:"
echo
echo -e "-o, --start Start display session."
echo
echo -e "-x, --stop Stop display session."
echo
echo -e "-u, --username Specify user name for session login."
echo
echo -e "-d, --directory Specify container base directory as /path/to/base/directory containing /distribution/version"
echo
echo -e "-h, --help Show help and usage information."
echo
echo -e "'${script}' [ Version // ${version} ]"
echo
exit
;;
"")
# handle empty argument:
# use default values specified in script
:
;;
*)
echo
echo -e "Usage: ${script} -o [Display Server] | -x | -u [User Name] | -d [Directory] | [OPTION]"
echo
echo -e "${script}: Unknown option '${1}'"
echo -e "Type './${script} --help' for help and usage information."
echo
exit 1
;;
esac
shift
done
# prompt ///////////////////////////////////////////////////////////////////////
frobulator.script "Setting up ${script#*-}"
# variables ////////////////////////////////////////////////////////////////////
TERMUX_X11_APPLICATION="com.termux.x11"
TERMUX_X11_SERVER="termux-x11"
TERMUX_X11_DRIVER="virpipe"
TERMUX_X11_ACCELERATOR="virgl_test_server_android"
termux_x11_activity="com.termux.x11/.MainActivity"
termux_x11_activity_stop="com.termux.x11.ACTION_STOP"
termux_x11_process="app_process -Xnoimage-dextooat / com.termux.x11.Loader"
# defaults /////////////////////////////////////////////////////////////////////
if [ -z "${COLORTERM}" ]
then
COLORTERM="truecolor"
fi
if [ -z "${TERM}" ]
then
TERM="xterm-256color"
fi
if [ -z "${TMPDIR}" ]
then
TMPDIR="${TMPDIR}"
fi
if [ -z "${LANG}" ]
then
LANG="${LANG}"
fi
if [ -z "${SHELL}" ]
then
SHELL="/bin/bash"
fi
if [ -z "${DISPLAY}" ]
then
DISPLAY=":0"
fi
if [ -z "${user_name}" ]
then
user_name="termux"
fi
if [ -f /etc/os-release ]
then
. /etc/os-release
name="${ID}"
version="${VERSION_ID}"
fi
# exports //////////////////////////////////////////////////////////////////////
export TERMUX_X11_APPLICATION="${TERMUX_X11_APPLICATION}"
export TERMUX_X11_SERVER="${TERMUX_X11_SERVER}"
export TERMUX_X11_DRIVER="${TERMUX_X11_DRIVER}"
export TERMUX_X11_ACCELERATOR="${TERMUX_X11_ACCELERATOR}"
# functions ////////////////////////////////////////////////////////////////////
container_parse () {
# parse container directories
for distribution in "${CONTAINER_BASE}"/*
do
for version in "${distribution}"/*
do
distribution_name=$(basename ${distribution})
distribution_version=$(basename ${version})
containers+=(${distribution_name} ${distribution_version})
done
done
if [[ ${#CONTAINER_ROOT[@]} -eq 1 ]]
then
distribution_name="${distribution_name}"
distribution_version="${distribution_version}"
frobulator.inf "Single container found" "[ ${distribution_name} // ${distribution_version} ]"
echo
frobulator.fwd "Proceeding${marker_elp}"
echo
fi
if [[ ${#CONTAINER_ROOT[@]} -ge 2 ]]
then
frobulator.ipt "Select one of the following containers:"
echo
# set padding for uniform output
pad_length=10
# normalize output
echo
# continue list using array selection of suppported values
for (( i=0 ; i<${#containers[@]} ; i+=2 ))
do
number=$((($i/2) + 1))
name="${containers[$i]}"
version="${containers[$i+1]}"
indent="${marker_tab}"
if [ ${#version} -le 2 ]
then
indent+="${marker_tab}"
fi
if (( $i/2 >= 0 && $i/2 <= 8 ))
then
# handle number spacing for entries 1-9
number=" ${number}"
else
# handle number spacing for entries 10+
number="${number}"
fi
# list container entries
echo "${marker_nul}" "[ ${number} ]" "${name}" "${version}"
done
echo
# values #
values_list=(
selection
)
# handle user input for values_list
eval ${frobulator_return[@]}
frobulator.ipt "Selection:"
frobulator.read selection
# integer check:
# handle numerical selection
while [[ "${selection}" -lt 1 ]] || \
[[ "${selection}" -gt "${number}" ]] || \
[[ ! "${selection}" =~ ^[0-9]+$ ]]
do
echo
frobulator.err "Invalid selection:"
echo
frobulator.wrn "Select container [ 1 - ${number} ]"
echo
frobulator.ipt "Selection:"
frobulator.read selection
done
# parse selection parameters
i=$(( ${selection} ))
i=$(( $i * 2 ))
name="${containers[i-2]}"
version="${containers[i-1]}"
distribution_name="${name}"
distribution_version="${version}"
fi
# unset array
containers=()
# specify distribution selection variables
distribution_name="${name}"
distribution_version="${version}"
CONTAINER_ROOT="${CONTAINER_BASE}"/"${distribution_name}"/"${distribution_version}"
# specify container_directory for container input handling
container_directory="${CONTAINER_BASE}"
}
container_input () {
# no container root specified
if [ -z "${container_directory}" ]
then
CONTAINER_BASE="${PREFIX}"/cnt
CONTAINER_ROOT=("${CONTAINER_BASE}"/*/*)
container_directory=("${CONTAINER_ROOT}")
frobulator.wrn "Container directory not specified."
echo
container_parse
fi
# user specified invalid container root
if [ ! -e "${container_directory}" ]
then
CONTAINER_BASE="${PREFIX}"/cnt
CONTAINER_ROOT=("${CONTAINER_BASE}"/*/*)
frobulator.err "Container directory not found."
echo
container_parse
fi
# user specified container root exists
if [ -d "${container_directory}" ]
then
CONTAINER_BASE="${container_directory}"
CONTAINER_ROOT=("${CONTAINER_BASE}"/*/*)
frobulator.scs "Container directory found."
echo
fi
}
container_user () {
# authentication
frobulator.fwd "Authenticating${marker_elp}"
echo
# verify user existence:
# loop through container's /etc/passwd file and make sure 'user' exists
# source environment variables when 'user' corresponds to 'username'
# default to 'root' if 'username' is invalid, is not specified or does not exist
last_entry="$(tail -n 1 "${CONTAINER_ROOT}"/etc/passwd)"
while read -r entry
do
IFS=':'
read USER _ USER_ID GROUP_ID GECOS HOME SHELL <<< "${entry}"
IFS=','
read FULL_NAME ROOM_NUMBER WORK_PHONE HOME_PHONE USER_EMAIL <<< "${GECOS}"
# reset IFS
IFS=' '
# verify login options and parameters
# no user name specified
if [[ -z "${user_name}" ]]
then
user_name="root"
USER="root"
USER_ID="0"
GROUP_ID="0"
UID="${USER_ID}"
GID="${GROUP_ID}"
if [ -d "${CONTAINER_ROOT}"/root ]
then
HOME="/root"
else
HOME="/"
fi
frobulator.wrn "No user name - Defaulting" "[ '${user_name}' ]"
echo
break
fi
# valid user name found
if [[ "${user_name}" == "${USER}" ]]
then
USER="${user_name}"
if [[ "${user_name}" == "root" ]]
then
frobulator.wrn "Valid user name - Superuser" "[ '${user_name}' ]"
echo
else
frobulator.scs "Valid user name - Accessing" "[ '${user_name}' ]"
echo
fi
break
fi
# user naem not found
if [[ "${entry}" == "${last_entry}" ]]
then
frobulator.err "Invalid user name - Aborting" "[ '${user_name}' ]"
echo
exit 1
fi
done < "${CONTAINER_ROOT}"/etc/passwd
}
container_environment () {
# generate container environment
# set path:
# in order of retreival
CONTAINER_PATH=""
# container
[ -d "${CONTAINER_ROOT}"/usr/local/sbin ] && CONTAINER_PATH+="/usr/local/sbin:"
[ -d "${CONTAINER_ROOT}"/usr/local/bin ] && CONTAINER_PATH+="/usr/local/bin:"
[ -d "${CONTAINER_ROOT}"/usr/sbin ] && CONTAINER_PATH+="/usr/sbin:"
[ -d "${CONTAINER_ROOT}"/usr/sbin ] && CONTAINER_PATH+="/usr/bin:"
[ -d "${CONTAINER_ROOT}"/sbin ] && CONTAINER_PATH+="/sbin:"
[ -d "${CONTAINER_ROOT}"/bin ] && CONTAINER_PATH+="/bin:"
# termux
[ -d "${HOME}"/.local/bin ] && CONTAINER_PATH+="${HOME}/.local/bin:"
[ -d "${PREFIX}"/sbin ] && CONTAINER_PATH+="${PREFIX}/sbin:"
[ -d "${PREFIX}"/bin ] && CONTAINER_PATH+="${PREFIX}/bin:"
# android
[ -d /system/sbin ] && CONTAINER_PATH+="/system/sbin:"
[ -d /system/bin ] && CONTAINER_PATH+="/system/bin:"
CONTAINER_PATH="${CONTAINER_PATH}"
export CONTAINER_PATH="${CONTAINER_PATH}"
return
}
container_intent () {
# load session settings
# assume 'termux-exec' is installed
unset LD_PRELOAD
# generate container environment
container_environment
# setup 'proot' command arguments
unset session_command
session_command+="proot"
session_command+=" --kill-on-exit"
session_command+=" --link2symlink"
session_command+=" --rootfs=${CONTAINER_ROOT}"
session_command+=" --change-id=${USER_ID}:${GROUP_ID}"
session_command+=" --pwd=${HOME}"
[ -d "/apex" ] && session_command+=" --bind=/apex:/apex"
[ -e "/linkerconfig/ld.config.txt" ] && session_command+=" --bind=/linkerconfig/ld.config.txt:/linkerconfig/ld.config.txt"
# system directories
[ -d "/data" ] && session_command+=" --bind=/data:/data"
[ -d "/dev" ] && session_command+=" --bind=/dev:/dev"
[ -e "/dev/urandom" ] && session_command+=" --bind=/dev/urandom:/dev/random"
[ -d "/proc" ] && session_command+=" --bind=/proc:/proc"
[ -d "/proc/self/fd" ] && session_command+=" --bind=/proc/self/fd:/dev/fd"
[ -e "/proc/self/fd/0" ] && session_command+=" --bind=/proc/self/fd/0:/dev/stdin"
[ -e "/proc/self/fd/1" ] && session_command+=" --bind=/proc/self/fd/1:/dev/stdout"
[ -e "/proc/self/fd/2" ] && session_command+=" --bind=/proc/self/fd/2:/dev/stderr"
[ -d "/storage" ] && session_command+=" --bind=/storage:/storage"
[ -d "/sys" ] && session_command+=" --bind=/sys:/sys"
[ -d "/system" ] && session_command+=" --bind=/system:/system"
[ -d "/vendor" ] && session_command+=" --bind=/vendor:/vendor"
[ -d "${PREFIX%/*}"/home ] && session_command+=" --bind=${PREFIX%/*}/home:${HOME}"
[ -d "${PREFIX}"/media ] && session_command+=" --bind=${PREFIX}/media:/media"
[ -d "${PREFIX}"/tmp ] && session_command+=" --bind=${PREFIX}/tmp:/tmp"
# environment
session_command+=" env"
session_command+=" --ignore-environment"
# android
session_command+=" ANDROID_ART_ROOT=${ANDROID_ART_ROOT}"
session_command+=" ANDROID_DATA=${ANDROID_DATA}"
session_command+=" ANDROID_I18N_ROOT=${ANDROID_I18N_ROOT}"
session_command+=" ANDROID_ROOT=${ANDROID_ROOT}"
session_command+=" ANDROID_RUNTIME_ROOT=${ANDROID_RUNTIME_ROOT}"
session_command+=" ANDROID_TZDATA_ROOT=${ANDROID_TZDATA_ROOT}"
session_command+=" BOOTCLASSPATH=${BOOTCLASSPATH}"
session_command+=" DEX2OATBOOTCLASSPATH=${DEX2OATBOOTCLASSPATH}"
# termux
session_command+=" PREFIX=${PREFIX}"
session_command+=" TERMUX_BINARIES=${PREFIX}/bin"
session_command+=" TERMUX_VERSION=${TERMUX_VERSION}"
# container
session_command+=" PATH=${PATH}:${CONTAINER_PATH}"
session_command+=" COLORTERM=${COLORTERM}"
session_command+=" DISPLAY=${DISPLAY}"
session_command+=" DISPLAY_DRIVER=${DISPLAY_DRIVER}"
session_command+=" HOME=${HOME}"
session_command+=" LANG=${LANG}"
session_command+=" SHELL=${SHELL}"
session_command+=" TERM=${TERM}"
session_command+=" TMPDIR=${TMPDIR}"
session_command+=" USER=${USER}"
session_command+=" XDG_CACHE_HOME=${HOME}/.cache"
session_command+=" XDG_CONFIG_DIRS=/etc/xdg"
session_command+=" XDG_CONFIG_HOME=${HOME}/.config"
session_command+=" XDG_DATA_DIRS=/usr/local/share:/usr/share"
session_command+=" XDG_DATA_HOME=${HOME}/.local/share"
session_command+=" XDG_RUNTIME_DIR=/run/user/${USER_ID}"
session_command+=" XDG_STATE_HOME=${HOME}/.local/state"
session_command+=" XAUTHORITY=${XDG_RUNTIME_DIR}/Xauthority"
# command
session_command+=" ${@}"
# execute 'proot' login
${session_command}
}
container_display () {
display_activity="${termux_x11_activity}"
unset intent_command
intent_command+="am start"
intent_command+=" --user 0"
intent_command+=" -n ${display_activity}"
frobulator.silence "${intent_command}"
}
container_session () {
# load session
frobulator.fwd "Loading desktop interface${marker_elp}"
echo
if [ -z "${environment}" ]
then
environment="$(cat ${HOME}/.dextop/dextop-environment)"
fi
if [ -z "${session}" ]
then
session="$(cat ${HOME}/.dextop/dextop-session)"
fi
# pause:
# required to let session loader through messaging system
# and load graphical interface
sleep 2
# load desktop environment
frobulator.silence "dbus-launch --exit-with-session ${session}" &
}
display_start () {
# start display server and activity
GALLIUM_DRIVER="${TERMUX_X11_DRIVER}"
export GALLIUM_DRIVER="${GALLIUM_DRIVER}"
if [[ $(pgrep -f dbus) ]]
then
pkill -f dbus
fi
if [[ $(pgrep -f "${TERMUX_X11_ACCELERATOR}") ]]
then
pkill -f "${TERMUX_X11_ACCELERATOR}"
fi
frobulator.silence "${TERMUX_X11_ACCELERATOR}" &
if [[ $(pgrep -f "${TERMUX_X11_APPLICATION}") ]]
then
:
else
frobulator.silence "${TERMUX_X11_SERVER} ${DISPLAY}" &
fi
frobulator.fwd "Opening display${marker_elp}"
echo
display_activity="${termux_x11_activity}"
unset intent_command
intent_command+="am start"
intent_command+=" --user 0"
intent_command+=" -n ${display_activity}"
frobulator.silence "${intent_command}" &
}
display_stop () {
# handle and stop display server and activity
if [[ $(pgrep -f "${TERMUX_X11_APPLICATION}") ]]
then
pkill -f "${TERMUX_X11_APPLICATION}"
display_activity="${termux_x11_activity_stop}"
unset intent_command
intent_command+="am broadcast"
intent_command+=" --user 0"
intent_command+=" -a ${display_activity}"
frobulator.silence "${intent_command}" &
frobulator.process "Stopping display viewer"
else
frobulator.wrn "No active display process detected."
echo
frobulator.fwd "Exiting${marker_elp}"
echo
fi
}
if [ "${option}" = "start" ]
then
# start display session
display_start
# load session
container_session
fi
if [ "${option}" = "stop" ]
then
# stop display session
display_stop
fi
if [ "${option}" = "username" ]
then
# handle manual container directory input
container_input
# validate username
container_user
# start container session
container_intent "${SHELL}"
fi
if [ "${option}" = "directory" ]
then
# handle manual container directory input
container_input
# validate username
container_user
# start container session
container_intent "${SHELL}"
fi