-
Notifications
You must be signed in to change notification settings - Fork 8
/
debdroid.sh
executable file
Β·442 lines (415 loc) Β· 16.5 KB
/
debdroid.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
#!/data/data/com.termux/files/usr/bin/bash
#############################################
# DebDroid 3.16 (debdroid-ng) 2020, 2021
# This script will allow you to install Debian on your Device with just a few taps
# This script is also portable, all links, repos will be read on a single file
# So to make it easier to fork and to create debdroid-based projects
#
# Also you will need to comply the GPLv3 license as some components use that
# All Rights Reserved (2020, 2021-2022) made by @marcusz
#############################################
# Default Debian Location
# This will be placed outside the 'usr' directory when 'termux-reset' is invoked, then all the preferences will be saved
DEBIAN_FS="/data/data/com.termux/files/debian"
# URL Link
# Used for portability and can be used for branch testing
URL_REPO="https://raw.githubusercontent.com/WMCB-Tech/debdroid-ng/master"
# Tempdir
# Used to place temporary files and all downloaded cache will be stored and will be updated once it flushed
TEMPDIR="/data/data/com.termux/files/usr/tmp/.debdroid-cachedir"
# Script Version
SCRIPT_VER="3.16"
# Colored Environment Variables
if [ -e "$(command -v tput)" ]; then
RED="$(tput setaf 1)$(tput bold)"
GREEN="$(tput setaf 2)$(tput bold)"
YELLOW="$(tput setaf 3)$(tput bold)"
NOATTR="$(tput sgr0)"
else
RED=""
GREEN=""
YELLOW=""
NOATTR=""
fi
# Don't run as root
if [ "$(whoami)" == "root" ]; then
echo "${RED}E: running this script is discouraged and therefore not being used by root user${NOATTR}"
exit 2
fi
# Create Temporary Directory (We don't use mktemp as to be used for update delay caching)
mkdir -p "${TEMPDIR}"
# Function to check updates
check_update_cache(){
if [ ! "$(curl --silent --fail --location ${URL_REPO}/version.txt)" == "${SCRIPT_VER}" ]; then
echo "${YELLOW}I: A New Version of this script is available, you may install a new version over this script${NOATTR}"
touch "${TEMPDIR}/update-cache-lock"
fi
}
check_update(){
if curl https://google.com --fail --silent --insecure >/dev/null; then
check_update_cache
else
echo "${YELLOW}N: Cannot Perform Update: Network is down. Skipping....."
fi
}
# Check for Updates but check if network connection is present
if [ ! -e "${TEMPDIR}/update-cache-lock" ]; then
check_update
fi
# Function to handle signal trap
sigtrap(){
echo "${RED}W: The Script generated error code 127, Quitting as requested!${NOATTR}"
exit 127
}
trap 'sigtrap' HUP INT KILL QUIT TERM
# Check if dependencies are installed
if ! [ -e "$(command -v proot)" ] && [ -e "$(command -v curl)" ]; then
echo "${GREEN}I: Installing ${YELLOW}proot, curl${GREEN} if necessary${NOATTR}"
pkg update
pkg install proot curl -yy
fi
# Function to show help
show_help(){
echo "${GREEN}DebDroid: Debian Installer for Android OS"
echo ""
echo "This Script will allow you to install Debian on your Device like Chromebooks, Phone, Tablets and TV with Termux in just a few keystrokes"
echo ""
echo "Here are the commands to operate within the debian container:"
echo "${YELLOW} install"
echo " purge"
echo " reconfigure"
echo " launch"
echo " launch-asroot"
echo " backup | export"
echo " restore | import"
echo ""
echo "${GREEN}You can install Debian Stable by typing ${YELLOW}debdroid install${GREEN} or ${YELLOW}debdroid install stable${GREEN}"
echo "You can list the recognized releases with ${YELLOW}debdroid install list${GREEN} command"
echo ""
echo "To perform reconfiguration (Interrupted Install, Updating the Container) you may enter ${YELLOW}debdroid reconfigure${GREEN}"
echo ""
echo "To launch your debian container, you may type ${YELLOW}debdroid launch${GREEN} or ${YELLOW}debdroid launch-asroot${GREEN}"
echo "See ${YELLOW}debdroid launch --help${GREEN} for details"
echo ""
echo "You can customize your Debian Needs with command ${YELLOW}debianize${GREEN}. This will allow you to install your desired workstation packages automatically in just a few keystrokes"
echo ""
echo "To learn more about operating Debian system, see the Debian Wiki ${YELLOW}https://wiki.debian.org${GREEN} and ${YELLOW}https://wiki.debian.org/DontBreakDebian${NOATTR}"
}
# Function to Add Android Groups if necessary
debdroid_setup_groups(){
echo "aid_$(id -un):x:$(id -u):$(id -g):Android Groups:/:/usr/sbin/nologin" >> "${DEBIAN_FS}/etc/passwd"
echo "aid_$(id -un):*:18446:0:99999:7:::" >> "${DEBIAN_FS}/etc/shadow"
local g
for g in $(id -G); do
echo "aid_$(id -gn "$g"):x:${g}:root,aid_$(id -un)" >> "${DEBIAN_FS}/etc/group"
if [ -f "${DEBIAN_FS}/etc/gshadow" ]; then
echo "aid_$(id -gn "$g"):*::root,aid_$(id -un)" >> "${DEBIAN_FS}/etc/gshadow"
fi
done
}
# Function to enter roofs
run-proot-cmd(){
unset LD_PRELOAD
proot --link2symlink --kill-on-exit \
-0 -p -L -H --kernel-release="5.4.0-debdroid" \
--rootfs="${DEBIAN_FS}" \
--bind="/dev" \
--bind="/proc" \
--bind="/sys" \
--bind="${DEBIAN_FS}/run/shm:/dev/shm" \
--cwd=/root \
/usr/bin/env -i \
PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin \
TERM=${TERM} \
HOME=/root \
USER=root \
"$@"
}
# Function to reconfigure debian
perform_configuration(){
if [ ! -e "${DEBIAN_FS}/usr/bin/apt" ]; then
echo "${RED}E: The Debian Container is invalid, Aborting!!!${NOATTR}"
exit 2
fi
curl --silent --fail --location --output "${DEBIAN_FS}/var/debdroid/libreconf.so" "${URL_REPO}/debian_config.sh"
chmod 755 "${DEBIAN_FS}/var/debdroid/libreconf.so"
# Add Proper /run/shm binding
mkdir -p "${DEBIAN_FS}/run/shm"
# Setup Android Groups if necessary
if [ ! -e "${DEBIAN_FS}/var/debdroid/group-setupdone.lock" ]; then
debdroid_setup_groups
touch "${DEBIAN_FS}/var/debdroid/group-setupdone.lock"
fi
# Run Configuration Wizard
run-proot-cmd "/var/debdroid/libreconf.so"
}
# Function to install debian
install_debian(){
local DEBIAN_SUITE
# If Possible, List recognized releases
if [ "$1" == "--list" ] || [ "$1" == "list" ]; then
echo "${GREEN}Recognized Debian Releases:${YELLOW}"
echo "oldstable/stretch, stable/buster, bullseye, testing, unstable/sid"
echo ""
echo "${GREEN}If the releases marked with * then it is EOL'd, yet still supported under DebDroid${NOATTR}"
exit 0
fi
DEBIAN_SUITE="$@"
# Check if the rootfs exists
if [ -e "${DEBIAN_FS}" ]; then
echo "${RED}E: The Debian Container is installed, perhaps you should be using ${YELLOW}debdroid reconfigure${RED}?${NOATTR}"
exit 2
fi
echo "${GREEN}I: Retrieving Download Links needed for installation${NOATTR}"
case "${DEBIAN_SUITE}" in
sid|unstable|debian-sid|debian-unstable)
source <(curl -sSL ${URL_REPO}/suite/dlmirrors/sid)
;;
testing|debian-testing)
source <(curl -sSL ${URL_REPO}/suite/dlmirrors/testing)
;;
bullseye|debian-bullseye)
source <(curl -sSL ${URL_REPO}/suite/dlmirrors/bullseye)
;;
buster|debian-buster|stable|debian-stable)
source <(curl -sSL ${URL_REPO}/suite/dlmirrors/buster)
;;
stretch|debian-stretch|oldstable|debian-oldstable)
source <(curl -sSL ${URL_REPO}/suite/dlmirrors/stretch)
;;
*)
echo "${YELLOW}I: Unknown Distribution was requested, choosing stable${NOATTR}"
source <(curl -sSL ${URL_REPO}/suite/dlmirrors/buster)
;;
esac
echo "${GREEN}I: The following distribution was requested: ${YELLOW}${DEBIAN_NAME}${NOATTR}"
echo "${GREEN}I: Downloading the Image file${NOATTR}"
curl --output "${TEMPDIR}/${DEBIAN_NAME}-rootfs.tar.xz.part" --location --fail "${CURL_DOWNLOAD_LINK}"
if [ -e "${TEMPDIR}/${DEBIAN_NAME}-rootfs.tar.xz.part" ]; then
mv "${TEMPDIR}/${DEBIAN_NAME}-rootfs.tar.xz.part" "${TEMPDIR}/${DEBIAN_NAME}-rootfs.tar.xz"
else
echo "${RED}E: An Error has occured during the installation: no such file or directory, please try again${NOATTR}"
exit 2
fi
echo "${GREEN}I: Extracting the Image file${NOATTR}"
mkdir -p "${DEBIAN_FS}"
proot --link2symlink -0 tar --preserve-permissions --delay-directory-restore --warning=no-unknown-keyword -xf "${TEMPDIR}/${DEBIAN_NAME}-rootfs.tar.xz" --exclude dev -C "${DEBIAN_FS}" ||:
echo "${GREEN}I: Configuring the base system, this may take some time${NOATTR}"
mkdir "${DEBIAN_FS}/var/debdroid/binds" -p
echo "${DEBIAN_NAME}" > "${DEBIAN_FS}/etc/debian_chroot"
if perform_configuration; then
echo "${GREEN}I: The Debian Container Installed Successfully, you can run it by typing ${YELLOW}debdroid launch${NOATTR}"
exit 0
else
echo "${RED}W: The Debian Container isn't successfully installed, should this happen? you can run the command ${YELLOW}debdroid reconfigure${GREEN} if necessary${NOATTR}"
exit 2
fi
}
# Function to Delete Debian
uninstall-debian(){
local userinput
read -p "${RED}N: Do you want to delete the Debian Container? [y/N] ${NOATTR}" userinput
if [ ! -e "${DEBIAN_FS}" ]; then
echo "${YELLOW}I: Debian Container isn't installed, Continuing Anyway...${NOATTR}"
NO_CHMOD=y
fi
case "${userinput}" in
Y*|y*)
echo "${YELLOW}I: Deleting the Container (debian)${NOATTR}"
if [ ! "${NO_CHMOD}" == "y" ]; then
chmod 777 "${DEBIAN_FS}" -R ||:
fi
rm -rf "${DEBIAN_FS}"
if [ ! -e "${DEBIAN_FS}" ]; then
echo "${GREEN}I: The Debian Container Successfully Deleted${NOATTR}"
exit 0
else
echo "${RED}E: The Debian Container isn't deleted successfully${NOATTR}"
exit 2
fi
;;
N*|n*)
echo "${GREEN}N: Aborting....${NOATTR}"
exit 0
;;
*)
echo "${GREEN}N: Aborting....${NOATTR}"
exit 0
;;
esac
}
# Function to run Debian Container (Actually, this is just a wrapper to make it portable)
launch-debian(){
local extcmd
local prootargs
if [ ! -e "${DEBIAN_FS}/var/debdroid/libdebdroid.so" ]; then
echo "${RED}E: The Debian Container isn't Installed, if you already installed it but seeing this message, try running ${YELLOW}debdroid reconfigure${NOATTR}"
exit 2
fi
# Show help subcommand if help was invoked
if [ "$1" == "--help" ] || [ "$1" == "help" ]; then
echo "${GREEN}This command will launch Debian System as regular user"
echo ""
echo "The Basic Syntax follows as:"
echo "${YELLOW} debdroid launch${GREEN}"
echo ""
echo "To run commands other than shell, you can specify external command by doing:"
echo "${YELLOW} debdroid launch [command]${GREEN}"
echo ""
echo "To learn more about operating Debian system, see the Debian Wiki ${YELLOW}https://wiki.debian.org${GREEN} and ${YELLOW}https://wiki.debian.org/DontBreakDebian${NOATTR}"
exit 0
fi
# Check for an ongoing setup
if [ -e "${DEBIAN_FS}/.setup_has_not_done" ]; then
echo "${RED}N: An Ongoing Setup is running, please finish the configuration first before continuing${NOATTR}"
exit 2
fi
# Source the file
source "${DEBIAN_FS}/var/debdroid/libdebdroid.so"
# Define External Command
extcmd="$@"
# Launch PRoot
if [ ! -z "${extcmd}" ]; then
proot -k "${kompat_source}" ${prootargs} su -l "${DEBIAN_USER_INFO}" -c "${extcmd}"
else
proot -k "${kompat_source}" ${prootargs} su -l "${DEBIAN_USER_INFO}"
fi
}
# Function to run Debian container as root
launch-debian-asroot(){
local extcmd
local prootargs
if [ ! -e "${DEBIAN_FS}/var/debdroid/libdebdroid.so" ]; then
echo "${RED}E: The Debian Container isn't Installed, if you already installed it but seeing this message, try running ${YELLOW}debdroid reconfigure${NOATTR}"
exit 2
fi
# Show help if help was invoked
if [ "$1" == "--help" ] || [ "$1" == "help" ]; then
echo "${GREEN}This command will launch Debian System as regular user"
echo ""
echo "The Basic Syntax follows as:"
echo "${YELLOW} debdroid launch-asroot${GREEN}"
echo ""
echo "To run commands other than shell, you can specify external command by doing:"
echo "${YELLOW} debdroid launch-asroot [command]${GREEN}"
echo ""
echo "To learn more about operating Debian system, see the Debian Wiki ${YELLOW}https://wiki.debian.org${GREEN} and ${YELLOW}https://wiki.debian.org/DontBreakDebian${NOATTR}"
exit 0
fi
# Check for an ongoing setup
if [ -e "${DEBIAN_FS}/.setup_has_not_done" ]; then
echo "${RED}N: An Ongoing Setup is running, please finish the configuration first before continuing${NOATTR}"
exit 2
fi
# Source the file
source "${DEBIAN_FS}/var/debdroid/libdebdroid.so"
# Define External Command
extcmd="$@"
# Launch PRoot
if [ ! -z "${extcmd}" ]; then
proot -k "${kompat_source}" ${prootargs} su -l -c "${extcmd}"
else
proot -k "${kompat_source}" ${prootargs} su -l
fi
}
# Function to Backup the container
backup_debian_container(){
local args
if [ ! -e "${DEBIAN_FS}/var/debdroid/libdebdroid.so" ]; then
echo "${RED}E: Cannot Backup the Debian Container: The Debian Container isn't Installed${NOATTR}"
exit 2
fi
args="$@"
if [ -z "${args}" ]; then
echo "${RED}E: Please specify a filename to output the tarball${NOATTR}"
exit 2
fi
echo "${GREEN}I: The Tarball will be saved in $(realpath -m ${args})${NOATTR}"
echo "${YELLOW}I: Backing up the container... this may take some time${NOATTR}"
if tar --preserve-permissions -zcf "${args}" -C "${PREFIX}/.." debian; then
echo "${GREEN}I: The Container successfully exported${NOATTR}"
exit 0
else
echo "${RED}I: The Container isn't successfully exported${NOATTR}"
exit 2
fi
}
# Function to Restore the container
restore_debian_container(){
local args
local userinput
args="$@"
if [ -z "${args}" ]; then
echo "${RED}E: Please specify a tarball for restoring the container${NOATTR}"
exit 2
fi
# Check if the tarball exists
if [ ! -e "${args}" ]; then
echo "${RED}E: The Tarball that you're trying to import dosen't exist${NOATTR}"
exit 2
fi
# User Input
read -p "${GREEN}I: Do you want to restore the container? all of the existing state will be lost [y/N]? ${NOATTR}" userinput
case "${userinput}" in
Y*|y*) ;;
N*|n*)
echo "${RED}I: Aborting...${NOATTR}"
exit 2
;;
*)
echo "${RED}I: Aborting...${NOATTR}"
exit 2
;;
esac
echo "${YELLOW}I: Restoring the Container...${NOATTR}"
if tar --recursive-unlink --delay-directory-restore --preserve-permissions -zxf "$(realpath -m ${args})" -C "${PREFIX}/.."; then
echo "${GREEN}I: The Container successfully imported${NOATTR}"
exit 0
else
echo "${RED}I: The Container isn't successfully imported${NOATTR}"
exit 2
fi
}
argument="$1"
shift 1
if [ -z "${argument}" ]; then
show_help
exit 2
fi
case "${argument}" in
install)
install_debian "$@"
;;
uninstall|purge)
uninstall-debian "$@"
;;
reconfigure|configure)
if perform_configuration; then
echo "${GREEN}I: Done Configuring the Debian Container${NOATTR}"
exit 0
else
echo "${RED}W: An error has occured during the reconfiguration${NOATTR}"
exit 2
fi
;;
launch|login)
launch-debian "$@"
;;
launch-asroot|login-asroot|launch-su)
launch-debian-asroot "$@"
;;
backup|export)
backup_debian_container "$@"
;;
restore|import)
restore_debian_container "$@"
;;
help|show-help|h)
show_help
;;
*)
echo "${RED}Unknown Option: ${argument}${NOATTR}"
show_help
;;
esac
# END OF MESSAGE EOM