Improve code quality

This commit is contained in:
JeremyStar™ 2023-11-08 19:47:37 +01:00
parent 8e0e3f63a1
commit 60f09947f2
7 changed files with 313 additions and 208 deletions

View file

@ -12,6 +12,6 @@ ensureexec:
run: ensureexec check run: ensureexec check
sudo ./androot.sh --local sudo ./androot.sh --local
test: ensureexec check test: ensureexec check
sudo env DIAG=true ./androot.sh --local sudo env ALLOW_SHM=true DIAG=true ./androot.sh --local
log: log:
@PS4='';if [ -d "/tmp/androot" ]; then set -x; tail -f /tmp/androot/androot.log; elif [ -d "/data/tmp/androot" ]; then set -x; tail -f /data/tmp/androot/androot.log; else echo ":: Error: androot's log directory could not be determined (start androot first)"; exit 1; fi @PS4='';if [ -d "/tmp/androot" ]; then set -x; tail -f /tmp/androot/androot.log; elif [ -d "/data/tmp/androot" ]; then set -x; tail -f /data/tmp/androot/androot.log; else echo ":: Error: androot's log directory could not be determined (start androot first)"; exit 1; fi

View file

@ -75,7 +75,5 @@ use */dev/shm* instead of */tmp* as your install location (*/tmp* uses your disk
# Annotations # Annotations
```plain ```plain
### TOBECHANGED - this should be changed in the future ### TOBECHANGED - this should be changed in the future
### UNIMPLEMENTED - not yet finished
### EXPERIMENTAL - experimental code, probably not safe yet
### MISIMPLEMENTED - implemented, but wrong ### MISIMPLEMENTED - implemented, but wrong
``` ```

View file

@ -1,60 +1,76 @@
#!/bin/bash #!/bin/bash
# shellcheck disable=SC2181 disable=SC1091 disable=SC2236 # disable errors regarding androot.source as
# shellcheck disable=SC2068 # it is created and loaded at runtime
# shellcheck disable=SC1091
### TOBECHANGED ### TOBECHANGED
# disables unreachable code warnings
# shellcheck disable=SC2317 # shellcheck disable=SC2317
### TOBECHANGED_END ### TOBECHANGED_END
# Check if root # check for root user
if [ ! "${UID}" == "0" ]; then if [ ! "${UID}" == "0" ]; then
echo ":: Error: Not running as root user" echo ":: Error: Not running as root user"
exit 1 exit 1
fi fi
# Determine $TMPDIR # determine $TMPDIR
if [ ! -d "${TMPDIR}" ]; then if [ ! -d "${TMPDIR}" ]; then
if [ -d "/tmp" ]; then if [ -d "/tmp" ]; then
export "TMPDIR=/tmp/androot/" export "TMPDIR=/tmp/androot"
elif [ -d "/data/tmp" ]; then elif [ -d "/data/tmp" ]; then
export "TMPDIR=/data/tmp/androot/" export "TMPDIR=/data/tmp/androot"
else else
echo ":: Error: \$TMPDIR could not be determined" echo ":: Error: \$TMPDIR could not be determined"
exit 1 exit 1
fi fi
fi fi
# ... and create it
mkdir -p "${TMPDIR}" mkdir -p "${TMPDIR}"
# Write source file # write source file
cat << EOF > "${TMPDIR}/androot.source" cat << EOF > "${TMPDIR}/androot.source"
# Functions # Functions
## Indicate failure ## Indicate failure
function log_fail() { function indicate_fail() {
echo -e "\r\e[0m[\e[0;31mFAIL\e[0m]" echo -e "\r\e[0m[\e[0;31mFAIL\e[0m]"
} }
## Indicate success ## Indicate success
function log_ok() { function indicate_ok() {
echo -e "\r\e[0m[\e[0;32m OK \e[0m]" echo -e "\r\e[0m[\e[0;32m OK \e[0m]"
} }
## Indicate running execution ## Indicate running execution
function log_exec() { function indicate_exec() {
echo -ne "\e[0m[\e[0;34m....\e[0m] \${*}" echo -ne "\e[0m[\e[0;34m....\e[0m] \${*}"
} }
## Diagnostic logging ## Log diagnostic message to term
function log_diag() { function log_diag() {
if [ "\$DIAG" == "true" ]; then echo -e "\e[0m[\e[0;35mDIAG\e[0m] \${*}"; fi if [ "\$DIAG" == "true" ]; then
echo -e "\e[0m[\e[0;35mDIAG\e[0m] \${*}"
fi
} }
## Informational logging ## Log informational message to term
function log_info() { function log_info() {
echo -e "\r\e[0m[\e[0;32mINFO\e[0m] \${*}" echo -e "\r\e[0m[\e[0;32mINFO\e[0m] \${*}"
} }
## Warning logging ## Log warning message to term
function log_warn() { function log_warn() {
echo -e "\r\e[0m[\e[0;33mWARN\e[0m] \${*}" echo -e "\r\e[0m[\e[0;33mWARN\e[0m] \${*}"
} }
## Error logging ## Log error message to term
function log_error() { function log_error() {
echo -e "\r\e[0m[\e[0;31mERR!\e[0m] \${*}" echo -e "\r\e[0m[\e[0;31mERR!\e[0m] \${*}"
} }
## Log message to log file
function log_write() {
echo -e "\${*}" &>> "\${TMPDIR}/androot.log"
}
function log_execute() {
log_write ">>>> \${*}"
\${*} &>> "\${TMPDIR}/androot.log"
EXITCODE=\${?}
log_write "Exited with code \${EXITCODE}"
return \${EXITCODE}
}
## Ask a question ## Ask a question
function log_ask() { function log_ask() {
echo -ne "\e[0m[\e[0;36mASKQ\e[0m] \${*}" echo -ne "\e[0m[\e[0;36mASKQ\e[0m] \${*}"
@ -85,40 +101,72 @@ function log_askyn() {
fi fi
} }
# Global variables # Language
## $BRANCH ## set language to C, useful for bug reports
export "LANG=C.UTF-8"
export "LANGUAGE=\${LANG}"
export "LC_ALL=\${LANG}"
export "LC_CTYPE=\${LANG}"
export "LC_NUMERIC=\${LANG}"
export "LC_TIME=\${LANG}"
export "LC_COLLATE=\${LANG}"
export "LC_MONETARY=\${LANG}"
export "LC_MESSAGES=\${LANG}"
export "LC_PAPER=\${LANG}"
export "LC_NAME=\${LANG}"
export "LC_ADDRESS=\${LANG}"
export "LC_TELEPHONE=\${LANG}"
export "LC_MEASUREMENT=\${LANG}"
export "LC_IDENTIFICATION=\${LANG}"
# Variables
## diagnostic mode
export "DIAG=${DIAG}"
## allow /dev/shm setting
export "ALLOW_SHM=${ALLOW_SHM}"
## temporary directory
export "TMPDIR=${TMPDIR}"
## androot installer branch
export "BRANCH=develop" export "BRANCH=develop"
## $DOWNLOADSERVER_ROOTFS ## unsafe commits list url
export "UNSAFE_COMMITS_LIST=https://fs.staropensource.de/vinf/unsafe/androot-\${BRANCH}"
# Download URLs
## rootfs archives
export "DOWNLOADSERVER_ROOTFS=https://fs.staropensource.de/rootfs/" export "DOWNLOADSERVER_ROOTFS=https://fs.staropensource.de/rootfs/"
## $DOWNLOADSERVER_QEMUSTATIC ## qemu-static archives
export "DOWNLOADSERVER_QEMUSTATIC=https://fs.staropensource.de/qemu-static/" export "DOWNLOADSERVER_QEMUSTATIC=https://fs.staropensource.de/qemu-static/"
## $DOWNLOADSERVER_BOOTSCRIPTS ## bootscripts repository
export "DOWNLOADSERVER_BOOTSCRIPTS=https://git.staropensource.de/StarOpenSource/androot.git" export "DOWNLOADSERVER_BOOTSCRIPTS=https://git.staropensource.de/StarOpenSource/androot.git"
## $ARCH ## installer scripts url
export "DOWNLOADSERVER_RAW=https://git.staropensource.de/StarOpenSource/androot/raw/branch/\${BRANCH}/"
# Function variables
## get architecture
case "$(uname -m)" in case "$(uname -m)" in
# 64-bit architectures (supported) # 64-bit architectures (supported)
x86_64|amd64) x86_64|amd64)
export "ARCH=x86_64";; export "ARCH=x86_64" ;;
aarch64|aarch64_be|armv8b|armv8l) aarch64|aarch64_be|armv8b|armv8l)
export "ARCH=arm64";; export "ARCH=arm64" ;;
ppc64|ppc64le) ppc64|ppc64le)
export "ARCH=powerpc64";; export "ARCH=powerpc64" ;;
mips64) mips64)
export "ARCH=mips64";; export "ARCH=mips64" ;;
# 32-bit architectures (unsupported) # 32-bit architectures (unsupported)
x86|i386|i686) x86|i386|i686)
export "ARCH=x86";; export "ARCH=x86" ;;
arm) arm)
export "ARCH=arm";; export "ARCH=arm" ;;
powerpc|ppc|ppcle) powerpc|ppc|ppcle)
export "ARCH=powerpc";; export "ARCH=powerpc" ;;
mips) mips)
export "ARCH=mips";; export "ARCH=mips" ;;
*) *)
export "ARCH=unknown";; export "ARCH=unknown" ;;
esac esac
## $IS_ANDROID ## check if androot is running on android
if [ -d "/system" ] && [ -d "/data" ] && [ -d "/data/data" ] && [ ! -d "/home" ] && [ ! -d "/root" ]; then if [ -d "/system" ] && [ -d "/data" ] && [ -d "/data/data" ] && [ ! -d "/home" ] && [ ! -d "/root" ]; then
export "IS_ANDROID=true" export "IS_ANDROID=true"
else else
@ -126,7 +174,7 @@ else
fi fi
EOF EOF
# Display banner # display banner
echo -e "\e[0;33m ______ __ __ ____ ____ _____ _____ ______" echo -e "\e[0;33m ______ __ __ ____ ____ _____ _____ ______"
echo -e "\e[0;33m/\\ _ \\/\\ \\/\\ \\/\\ _\`\\ /\\ _\`\\ /\\ __\`\\/\\ __\`\\/\\__ _\\" echo -e "\e[0;33m/\\ _ \\/\\ \\/\\ \\/\\ _\`\\ /\\ _\`\\ /\\ __\`\\/\\ __\`\\/\\__ _\\"
echo -e "\e[0;33m\\ \\ \\L\\ \\ \\ \`\\\\ \\ \\ \\/\\ \\ \\ \\L\\ \\ \\ \\/\\ \\ \\ \\/\\ \\/_/\\ \\/" echo -e "\e[0;33m\\ \\ \\L\\ \\ \\ \`\\\\ \\ \\ \\/\\ \\ \\ \\L\\ \\ \\ \\/\\ \\ \\ \\/\\ \\/_/\\ \\/"
@ -135,31 +183,29 @@ echo -e "\e[0;33m \\ \\ \\/\\ \\ \\ \\\`\\ \\ \\ \\_\\ \\ \\ \\\\ \\\\ \\ \\_\\
echo -e "\e[0;33m \\ \\_\\ \\_\\ \\_\\ \\_\\ \\____/\\ \\_\\ \\_\\ \\_____\\ \\_____\\ \\ \\_\\" echo -e "\e[0;33m \\ \\_\\ \\_\\ \\_\\ \\_\\ \\____/\\ \\_\\ \\_\\ \\_____\\ \\_____\\ \\ \\_\\"
echo -e "\e[0;33m \\/_/\\/_/\\/_/\\/_/\\/___/ \\/_/\\/ /\\/_____/\\/_____/ \\/_/" echo -e "\e[0;33m \\/_/\\/_/\\/_/\\/_/\\/___/ \\/_/\\/ /\\/_____/\\/_____/ \\/_/"
# Load source file # load source file
source "${TMPDIR}/androot.source" source "${TMPDIR}/androot.source"
# Clear/create log file # clear/create log file
{ ## if in diagnostic mode, print newlines
# if in diagnostic mode, print newlines ## makes reading the log file much
## reading the log output using tail during ## easier while using "make log"
## development with this is so much better, believe me if [ "${DIAG}" == "true" ]; then log_write "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"; fi
if [ "${DIAG}" == "true" ]; then echo -e "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"; fi log_write "#### System information"
echo "#### System information" log_write "architecture ${ARCH}"
echo "architecture ${ARCH}" log_write "is_android ${IS_ANDROID}"
echo "is_android ${IS_ANDROID}" log_write "download_server_rootfs ${DOWNLOADSERVER_ROOTFS}"
echo "download_server_rootfs ${DOWNLOADSERVER_ROOTFS}" log_write "download_server_qemustatic ${DOWNLOADSERVER_QEMUSTATIC}"
echo "download_server_qemustatic ${DOWNLOADSERVER_QEMUSTATIC}" log_write "download_server_bootscripts ${DOWNLOADSERVER_BOOTSCRIPTS}"
echo "download_server_bootscripts ${DOWNLOADSERVER_BOOTSCRIPTS}" log_write "args ${*}"
echo "args ${*}" log_write "diag ${DIAG}"
echo "diag ${DIAG}" log_write "#### System information"
echo "#### System information"
} &> "${TMPDIR}/androot.log"
# Diagnostic mode # print diagnostic messages
log_diag "Diagnostic mode enabled" log_diag "Diagnostic mode enabled"
log_diag "Arguments: ${*}" log_diag "Arguments: ${*}"
# Check architecture # check for supported architecture
case "${ARCH}" in case "${ARCH}" in
"unknown") "unknown")
log_error "androot failed to detect your architecture. If you believe this is a bug, create a issue at \"https://git.staropensource.de/StarOpenSource/androot\"." log_error "androot failed to detect your architecture. If you believe this is a bug, create a issue at \"https://git.staropensource.de/StarOpenSource/androot\"."
@ -171,15 +217,18 @@ case "${ARCH}" in
;; ;;
esac esac
# run a androot script and exit if failed
function run_script() { function run_script() {
env "TMPDIR=${TMPDIR}" $@ # $@ is used intentionally here, so ignore error
if [ ! "${?}" == "0" ]; then # shellcheck disable=SC2068
if ! $@; then
exit 1 exit 1
fi fi
} }
case "${1}" in case "${1}" in
"--local") "--local")
# use scripts from cwd
log_diag "Using local mode" log_diag "Using local mode"
run_script ./prepare.sh run_script ./prepare.sh
run_script ./rootfsinstall.sh --download run_script ./rootfsinstall.sh --download
@ -193,21 +242,25 @@ case "${1}" in
run_script ./installsystem.sh --update run_script ./installsystem.sh --update
run_script ./patch.sh run_script ./patch.sh
run_script ./mount.sh --unmount run_script ./mount.sh --unmount
run_script ./finish.sh;; run_script ./finish.sh
;;
"--download") "--download")
# download scripts from the interwebz and execute them
log_diag "Using download mode" log_diag "Using download mode"
### UNIMPLEMENTED indicate_exec "Downloading files"
log_exec Downloading files indicate_fail
log_fail log_error "Unimplemented."
exit 1;; exit 1
### UNIMPLEMENTED_END ;;
"--help"|"-h") "--help"|"-h")
log_diag "Displaying help" log_diag "Displaying help"
log_info "Available arguments:" log_info "Available arguments:"
log_info "--local [execute androot from cwd] --download [download androot scripts and execute] --help [display args and vars]" log_info "--local [execute androot from cwd] --download [download androot scripts and execute] --help [display args and vars]"
log_info "Available environment variables:" log_info "Available environment variables:"
log_info "DIAG [displays diagnostic messages, true/false] ALLOW_SHM [allows using /dev/shm as a location, true/false]";; log_info "DIAG [displays diagnostic messages, true/false] ALLOW_SHM [allows using /dev/shm as a location, true/false]"
;;
*) *)
log_diag "i thought putting a little \"advertisement parody\" in here would be funny" log_diag "i thought putting a little \"advertisement parody\" in here would be funny"
log_error "StarOpenSource is happy to present the latest and greatest version of --help! --help now uses cutting edge technology to display the most useful help to you, and that for only zero dollars!";; log_error "StarOpenSource is happy to present the latest and greatest version of --help! --help now uses cutting edge technology to display the most useful help to you, and that for only zero dollars!"
;;
esac esac

View file

@ -1,5 +1,7 @@
#!/bin/bash #!/bin/bash
# shellcheck disable=SC2181 disable=SC1091 disable=SC2236 # disable errors regarding androot.source as
# it is created and loaded at runtime
# shellcheck disable=SC1091
source "${TMPDIR}/androot.source" source "${TMPDIR}/androot.source"
source "${TMPDIR}/androot.env" source "${TMPDIR}/androot.env"
@ -8,49 +10,50 @@ log_diag "rootfsinstall is now executing, with arguments \"${*}\""
case "${1}" in case "${1}" in
"--download") "--download")
log_diag "Using download mode" log_diag "Using download mode"
log_exec "Downloading bootscripts" indicate_exec "Downloading bootscripts"
echo "#### download bootscripts repository" &>> "${TMPDIR}/androot.log" log_write "#### download bootscripts repository"
git clone --quiet --verbose -b "${BRANCH}-bootscripts" "${DOWNLOADSERVER_BOOTSCRIPTS}" "${LOCATION}/bootscripts" &>> "${TMPDIR}/androot.log" if ! log_execute git clone --quiet --verbose -b "${BRANCH}-bootscripts" "${DOWNLOADSERVER_BOOTSCRIPTS}" "${LOCATION}/bootscripts"; then
if [ ! "${?}" == "0" ]; then indicate_fail
log_fail
exit 1 exit 1
fi fi
log_ok;; indicate_ok
;;
"--configure") "--configure")
log_diag "Using configure mode" log_diag "Using configure mode"
log_exec "Configuring bootscript scripts" indicate_exec "Configuring bootscript scripts"
cd "${LOCATION}/bootscripts/"||{ log_error "cd failed";exit 1; } log_execute cd "${LOCATION}/bootscripts/" ||{ indicate_fail; exit 1; }
echo "#### writing bootscripts config.env" &>> "${TMPDIR}/androot.log" log_write "#### writing bootscripts config.env"
cat << EOF >> "${LOCATION}/bootscripts/config.env" cat << EOF >> "${LOCATION}/bootscripts/config.env"
COMMIT=$(git rev-parse HEAD) COMMIT=$(git rev-parse HEAD)
LOCATION=${LOCATION} LOCATION=${LOCATION}
ARCHITECTURE=${ARCH_TARGET}
IS_ANDROID=${IS_ANDROID} IS_ANDROID=${IS_ANDROID}
EOF EOF
log_ok;; indicate_ok
;;
"--install-qemu") "--install-qemu")
log_diag "Using install-qemu mode" log_diag "Using install-qemu mode"
for ARCH_DOWNLOAD in ${ARCH_EXECUTORS}; do for ARCH_DOWNLOAD in ${ARCH_EXECUTORS}; do
log_exec "Installing qemu-static for ${ARCH_DOWNLOAD}" indicate_exec "Installing qemu-static for ${ARCH_DOWNLOAD}"
echo "#### download qemu-static for ${ARCH_DOWNLOAD}" &>> "${TMPDIR}/androot.log" log_write "#### download qemu-static for ${ARCH_DOWNLOAD}"
wget "${DOWNLOADSERVER_QEMUSTATIC}${ARCH_DOWNLOAD}.tar.gz" -qqq -O "${TMPDIR}/${ARCH_DOWNLOAD}.tar.gz" &>> "${TMPDIR}/androot.log" if ! log_execute wget "${DOWNLOADSERVER_QEMUSTATIC}${ARCH_DOWNLOAD}.tar.gz" --no-verbose -O "${TMPDIR}/${ARCH_DOWNLOAD}.tar.gz"; then
if [ ! "${?}" == "0" ]; then indicate_fail;
log_fail;
exit 1 exit 1
fi fi
echo "#### extract qemu-static for ${ARCH_DOWNLOAD}" &>> "${TMPDIR}/androot.log" log_write "#### extract qemu-static for ${ARCH_DOWNLOAD}"
mkdir -p "${LOCATION}/bootscripts/qemu-static/${ARCH_DOWNLOAD}" if ! log_execute mkdir -p "${LOCATION}/bootscripts/qemu-static/${ARCH_DOWNLOAD}"; then
if [ ! "${?}" == "0" ]; then indicate_fail;
log_fail;
exit 1 exit 1
fi fi
bsdtar -xf "${TMPDIR}/${ARCH_DOWNLOAD}.tar.gz" -C "${LOCATION}/bootscripts/qemu-static/${ARCH_DOWNLOAD}/" &>> "${TMPDIR}/andronix.log" if ! log_execute bsdtar -xf "${TMPDIR}/${ARCH_DOWNLOAD}.tar.gz" -C "${LOCATION}/bootscripts/qemu-static/${ARCH_DOWNLOAD}/"; then
if [ ! "${?}" == "0" ]; then indicate_fail;
log_fail;
exit 1 exit 1
fi fi
log_ok indicate_ok
done;; done
;;
*) *)
log_error "--download or --decompress required" log_error "--download or --decompress required"
exit 1;; exit 1
;;
esac esac

View file

@ -1,26 +1,27 @@
#!/bin/bash #!/bin/bash
# shellcheck disable=SC2181 disable=SC1091 disable=SC2236 # disable errors regarding androot.source as
# shellcheck disable=SC2068 # it is created and loaded at runtime
# shellcheck disable=SC1091
source "${TMPDIR}/androot.source" source "${TMPDIR}/androot.source"
source "${TMPDIR}/androot.env" source "${TMPDIR}/androot.env"
log_diag "mount is now executing, with arguments \"${*}\"" log_diag "mount is now executing, with arguments \"${*}\""
function mount_fail() { # to avoid repetition
echo "#### mount ${1}" &>> "${TMPDIR}/androot.log" function mountf() {
shift # $@ is used intentionally here, so ignore error
mount ${@} &>> "${TMPDIR}/androot.log" # shellcheck disable=SC2068
if [ ! "${?}" == "0" ]; then if ! log_execute mount ${@}; then
log_fail indicate_fail
exit 1 exit 1
fi fi
} }
function unmount_fail() { # to avoid repetition
echo "#### unmount ${1}" &>> "${TMPDIR}/androot.log" function umountf() {
shift # $@ is used intentionally here, so ignore error
umount ${@} &>> "${TMPDIR}/androot.log" # shellcheck disable=SC2068
if [ ! "${?}" == "0" ]; then if ! log_execute umount ${@}; then
log_fail indicate_fail
exit 1 exit 1
fi fi
} }
@ -29,32 +30,39 @@ case "${1}" in
"--mount") "--mount")
log_diag "Using mount mode" log_diag "Using mount mode"
mkdir -p "${LOCATION}/mountdir" mkdir -p "${LOCATION}/mountdir"
# remount /data on android to enable
# suid and exec flags
if [ "${IS_ANDROID}" == "true" ]; then if [ "${IS_ANDROID}" == "true" ]; then
log_exec "Remounting /data" indicate_exec "Remounting /data"
mount_fail "/data (remount)" "/data" -o remount,suid,exec mount_fail "/data (remount)" "/data" -o remount,suid,exec
log_ok indicate_ok
fi fi
log_exec "Mounting rootfs" indicate_exec "Mounting rootfs"
mount_fail "rootfs" --bind "${LOCATION}/rootfs" "${LOCATION}/mountdir" -o rw,suid,dev,exec,auto,nouser,async mountf --bind "${LOCATION}/rootfs" "${LOCATION}/mountdir" -o rw,suid,dev,exec,auto,nouser,async
log_ok indicate_ok
log_exec "Binding directories" indicate_exec "Binding directories"
mount_fail "/dev" --bind "/dev" "${LOCATION}/mountdir/dev" # mount /dev BEFORE /dev/pts
mount_fail "/dev/pts" --bind "/dev/pts" "${LOCATION}/mountdir/dev/pts" mountf --bind "/dev" "${LOCATION}/mountdir/dev"
mount_fail "/sys" --bind "/sys" "${LOCATION}/mountdir/sys" mountf --bind "/dev/pts" "${LOCATION}/mountdir/dev/pts"
mount_fail "/proc" --bind "/proc" "${LOCATION}/mountdir/proc" mountf --bind "/sys" "${LOCATION}/mountdir/sys"
log_ok;; mountf --bind "/proc" "${LOCATION}/mountdir/proc"
indicate_ok
;;
"--unmount") "--unmount")
log_diag "Using unmount mode" log_diag "Using unmount mode"
log_exec "Unmounting binds" indicate_exec "Unmounting binds"
unmount_fail "/dev/pts" "${LOCATION}/mountdir/dev/pts" # unmount /dev AFTER /dev/pts
unmount_fail "/dev" "${LOCATION}/mountdir/dev" umountf "${LOCATION}/mountdir/dev/pts"
unmount_fail "/sys" "${LOCATION}/mountdir/sys" umountf "${LOCATION}/mountdir/dev"
unmount_fail "/proc" "${LOCATION}/mountdir/proc" umountf "${LOCATION}/mountdir/sys"
log_ok umountf "${LOCATION}/mountdir/proc"
log_exec "Unmounting rootfs" indicate_ok
unmount_fail "rootfs" "${LOCATION}/mountdir" indicate_exec "Unmounting rootfs"
log_ok;; umountf "${LOCATION}/mountdir"
indicate_ok
;;
*) *)
log_error "--mount or --unmount required" log_error "--mount or --unmount required"
exit 1;; exit 1
;;
esac esac

View file

@ -1,9 +1,12 @@
#!/bin/bash #!/bin/bash
# shellcheck disable=SC2181 disable=SC1091 disable=SC2236 # disable errors regarding androot.source as
# it is created and loaded at runtime
# shellcheck disable=SC1091
source "${TMPDIR}/androot.source" source "${TMPDIR}/androot.source"
log_diag "prepare is now executing, with arguments \"${*}\"" log_diag "prepare is now executing, with arguments \"${*}\""
# ask for target architecture
function arch_target() { function arch_target() {
log_info "" log_info ""
log_info "Please select your target architecture" log_info "Please select your target architecture"
@ -18,10 +21,13 @@ function arch_target() {
*) *)
log_error "Invalid target architecture \"${ANSWER,,}\", please retry." log_error "Invalid target architecture \"${ANSWER,,}\", please retry."
arch_target arch_target
return;; return
;;
esac esac
echo "ARCH_TARGET=${ANSWER,,}" > "${TMPDIR}/androot.env" echo "ARCH_TARGET=${ANSWER,,}" > "${TMPDIR}/androot.env"
} }
# ask for execution architecture(s)
function arch_executors() { function arch_executors() {
log_info "" log_info ""
log_info "Please specify the execution architecture(s)" log_info "Please specify the execution architecture(s)"
@ -43,11 +49,14 @@ function arch_executors() {
*) *)
log_error "Invalid execution architecture \"${ANSWER,,}\", please retry." log_error "Invalid execution architecture \"${ANSWER,,}\", please retry."
arch_executors arch_executors
return;; return
;;
esac esac
### MISIMPLEMENTED ### MISIMPLEMENTED
echo "ARCH_EXECUTORS=${ANSWER}" >> "${TMPDIR}/androot.env" echo "ARCH_EXECUTORS=${ANSWER}" >> "${TMPDIR}/androot.env"
} }
# ask for target distribution
function distribution() { function distribution() {
log_info "" log_info ""
log_info "Please select the target distribution" log_info "Please select the target distribution"
@ -60,83 +69,100 @@ function distribution() {
*) *)
log_error "Invalid distribution \"${ANSWER,,}\", please retry." log_error "Invalid distribution \"${ANSWER,,}\", please retry."
distribution distribution
return;; return
;;
esac esac
echo "DISTRIBUTION=${ANSWER,,}" >> "${TMPDIR}/androot.env" echo "DISTRIBUTION=${ANSWER,,}" >> "${TMPDIR}/androot.env"
} }
# ask for install location
function location() { function location() {
log_info "" log_info ""
log_info "Please enter the rootfs location" log_info "Please enter the install location"
### TOBECHANGED log_info "Note: The installation location is not"
log_info "Note: This location is permanent and cannot be" log_info " permanent and you can freely move"
log_info " changed as of now." log_info " the rootfs directory around as much"
### TOBECHANGED_END log_info " as you want."
log_ask "Location: " log_ask "Location: "
# check against invalid paths
case "${ANSWER}" in case "${ANSWER}" in
"") "") # doing nothing is illegal
log_error "Please enter something >.<" log_error "Please enter something >.<"
location location
return;; return
/tmp*) ;;
/tmp*) # disallow /tmp as disk space issues might haunt us
log_error "Using /tmp is unsupported as you may run into disk space issues very quickly, please retry." log_error "Using /tmp is unsupported as you may run into disk space issues very quickly, please retry."
location location
return;; return
/dev/shm/*) ;;
if [ ! "${DIAG}" == "true" ]; then /dev/shm/*) # disallow /dev/shm too, except if ALLOW_SHM is true
if [ ! "${ALLOW_SHM}" == "true" ]; then
log_error "Using /dev/shm is unsupported as you may run into disk space issues very quickly, please retry." log_error "Using /dev/shm is unsupported as you may run into disk space issues very quickly, please retry."
location location
return return
fi;; fi
/dev|/proc|/run|/sys|/dev/*|/proc/*|/run/*|/sys/*) ;;
/dev|/proc|/run|/sys|/dev/*|/proc/*|/run/*|/sys/*) # disallow THESE directories
log_error "Are you really trying that?" log_error "Are you really trying that?"
location location
return;; return
/) ;;
/) # just do "rm -rf --no-preserve-root /" instead
log_error "you're making a recipe for desaster." log_error "you're making a recipe for desaster."
location location
return;; return
"uwu") ;;
"uwu"|"owo"|"~nya"|"~ nya"|"~ahh"|"~ ahh") # reference
log_error "eww!" log_error "eww!"
location location
return;; return
"I'd like to interject for a moment") ;;
echo "#### $(whoami) wants to interject for a moment" &>> "${TMPDIR}/androot.log" "I'd like to interject for a moment") # reference
log_info "I'd just like to interject for a moment. What you're referring to as Linux," log_write "#### $(whoami) wants to interject for a moment"
log_info "is in fact, GNU/Linux, or as I've recently taken to calling it, GNU plus Linux." log_error "I'd just like to interject for a moment. What you're referring to as Linux,"
log_info "Linux is not an operating system unto itself, but rather another free component" log_error "is in fact, GNU/Linux, or as I've recently taken to calling it, GNU plus Linux."
log_info "of a fully functioning GNU system made useful by the GNU corelibs, shell" log_error "Linux is not an operating system unto itself, but rather another free component"
log_info "utilities and vital system components comprising a full OS as defined by POSIX." log_error "of a fully functioning GNU system made useful by the GNU corelibs, shell"
log_info "Many computer users run a modified version of the GNU system every day," log_error "utilities and vital system components comprising a full OS as defined by POSIX."
log_info "without realizing it. Through a peculiar turn of events, the version of GNU" log_error "Many computer users run a modified version of the GNU system every day,"
log_info "which is widely used today is often called \"Linux\", and many of its users are" log_error "without realizing it. Through a peculiar turn of events, the version of GNU"
log_info "not aware that it is basically the GNU system, developed by the GNU Project." log_error "which is widely used today is often called \"Linux\", and many of its users are"
log_info "There really is a Linux, and these people are using it, but it is just a" log_error "not aware that it is basically the GNU system, developed by the GNU Project."
log_info "part of the system they use. Linux is the kernel: the program in the system" log_error "There really is a Linux, and these people are using it, but it is just a"
log_info "that allocates the machine's resources to the other programs that you run." log_error "part of the system they use. Linux is the kernel: the program in the system"
log_info "The kernel is an essential part of an operating system, but useless by itself;" log_error "that allocates the machine's resources to the other programs that you run."
log_info "it can only function in the context of a complete operating system. Linux is" log_error "The kernel is an essential part of an operating system, but useless by itself;"
log_info "normally used in combination with the GNU operating system: the whole system" log_error "it can only function in the context of a complete operating system. Linux is"
log_info "is basically GNU with Linux added, or GNU/Linux. All the so-called \"Linux\"" log_error "normally used in combination with the GNU operating system: the whole system"
log_info "distributions are really distributions of GNU/Linux." log_error "is basically GNU with Linux added, or GNU/Linux. All the so-called \"Linux\""
log_error "distributions are really distributions of GNU/Linux."
location location
return;; return
;;
"UP UP DOWN DOWN LEFT RIGHT LEFT RIGHT B A START") # reference
log_error "This is a installer script, not a game."
location
return
;;
esac esac
# checks
if [ -a "${ANSWER}" ] && [ ! -d "${ANSWER}" ]; then if [ -a "${ANSWER}" ] && [ ! -d "${ANSWER}" ]; then
log_error "The location exists but is not a directory." log_error "The location exists but is not a directory."
location location
return return
fi fi
if [ -d "${ANSWER}" ]; then if [ -d "${ANSWER}" ]; then
if [ ! -z "$(ls -A "${ANSWER}")" ]; then if [ -n "$(ls -A "${ANSWER}")" ]; then
log_error "The location directory is not empty." log_error "The location directory is not empty."
location location
return return
fi fi
else else
log_info "Creating location directory" # create directory
mkdir -p "${ANSWER}/rootfs" indicate_exec "Creating location directory"
if [ ! "${?}" == "0" ]; then if ! mkdir -p "${ANSWER}/rootfs"; then
log_fail; indicate_fail;
exit 1 exit 1
fi fi
fi fi
@ -148,9 +174,4 @@ arch_executors
distribution distribution
location location
log_diag "androot.env:\n$(cat "${TMPDIR}/androot.env")" log_write "#### rootfs configuration\n$(cat "${TMPDIR}/androot.env")\n#### rootfs configuration"
{
echo "#### rootfs configuration"
cat "${TMPDIR}/androot.env"
echo "#### rootfs configuration"
}&>> "${TMPDIR}/androot.log"

View file

@ -1,5 +1,7 @@
#!/bin/bash #!/bin/bash
# shellcheck disable=SC2181 disable=SC1091 disable=SC2236 # disable errors regarding androot.source as
# it is created and loaded at runtime
# shellcheck disable=SC1091
source "${TMPDIR}/androot.source" source "${TMPDIR}/androot.source"
source "${TMPDIR}/androot.env" source "${TMPDIR}/androot.env"
@ -8,61 +10,81 @@ log_diag "rootfsinstall is now executing, with arguments \"${*}\""
case "${1}" in case "${1}" in
"--download") "--download")
log_diag "Using download mode" log_diag "Using download mode"
if [ -f "${TMPDIR}/rootfs.env" ]; then # check if rootfs has been downloaded already
if [ -f "${TMPDIR}/rootfs.tar.gz" ] && [ -f "${TMPDIR}/rootfs.env" ]; then
# load rootfs info
source "${TMPDIR}/rootfs.env" source "${TMPDIR}/rootfs.env"
# check if downloaded rootfs and rootfs configuration match
if [ "${ARCH_TARGET_WRITTEN}" == "${ARCH_TARGET}" ] && [ "${DISTRIBUTION_WRITTEN}" == "${DISTRIBUTION}" ]; then if [ "${ARCH_TARGET_WRITTEN}" == "${ARCH_TARGET}" ] && [ "${DISTRIBUTION_WRITTEN}" == "${DISTRIBUTION}" ]; then
echo "#### found existing rootfs archive" &>> "${TMPDIR}/androot.log" # ask if downloaded rootfs should be reused
log_write "#### found existing rootfs archive"
log_askyn "true" "androot found a existing rootfs archive matching your configuration, do you want to use it? " log_askyn "true" "androot found a existing rootfs archive matching your configuration, do you want to use it? "
if [ "${ANSWER}" == "true" ]; then if [ "${ANSWER}" == "true" ]; then
echo "#### skipping download" &>> "${TMPDIR}/androot.log" # yes, skip download
log_write "#### skipping download"
exit 0 exit 0
else else
echo "#### removing existing rootfs archive" &>> "${TMPDIR}/androot.log" # no, remove rootfs and continue
log_write "#### removing existing rootfs archive"
rm -rf "${TMPDIR}/rootfs.tar.gz" rm -rf "${TMPDIR}/rootfs.tar.gz"
fi fi
else else
echo "#### removing existing rootfs archive" &>> "${TMPDIR}/androot.log" # downloaded rootfs and rootfs configuration
# do not matchremove and continue
log_write "#### removing existing rootfs archive"
log_diag "Removing existing rootfs archive (not matching)" log_diag "Removing existing rootfs archive (not matching)"
rm -rf "${TMPDIR}/rootfs.tar.gz" rm -rf "${TMPDIR}/rootfs.tar.gz"
fi fi
fi fi
log_exec "Downloading rootfs" indicate_exec "Downloading rootfs"
# check for internal inconsistencies or
# external modifications to androot.env
case "${ARCH_TARGET}" in case "${ARCH_TARGET}" in
"x86_64") ;; "x86_64") ;;
"arm64") ;; "arm64") ;;
*) *)
echo "#### invalid architecture \"${ARCH_TARGET}\"" &>> "${TMPDIR}/androot.log" log_write "#### invalid architecture \"${ARCH_TARGET}\""
log_fail indicate_fail
log_error "Internal inconsistency detected: Invalid target architecture \"${ARCH_TARGET}\"" log_error "Internal inconsistency detected: Invalid target architecture \"${ARCH_TARGET}\""
exit 1;; exit 1
;;
esac esac
case "${DISTRIBUTION}" in case "${DISTRIBUTION}" in
"archlinux") ;; "archlinux") ;;
*) *)
echo "#### invalid distribution \"${DISTRIBUTION}\"" &>> "${TMPDIR}/androot.log" log_write "#### invalid distribution \"${DISTRIBUTION}\""
log_fail indicate_fail
log_error "Internal inconsistency detected: Invalid distribution \"${DISTRIBUTION}\"" log_error "Internal inconsistency detected: Invalid distribution \"${DISTRIBUTION}\""
exit 1;; exit 1
;;
esac esac
echo "#### download rootfs (${DOWNLOADSERVER_ROOTFS}${DISTRIBUTION}-${ARCH_TARGET}.tar.gz)" &>> "${TMPDIR}/androot.log" # download rootfs from download server
wget "${DOWNLOADSERVER_ROOTFS}${DISTRIBUTION}-${ARCH_TARGET}.tar.gz" -qqq -O "${TMPDIR}/rootfs.tar.gz" &>> "${TMPDIR}/androot.log" log_write "#### download rootfs (${DOWNLOADSERVER_ROOTFS}${DISTRIBUTION}-${ARCH_TARGET}.tar.gz)"
if [ ! -f "${TMPDIR}/rootfs.tar.gz" ]; then if ! log_execute wget "${DOWNLOADSERVER_ROOTFS}${DISTRIBUTION}-${ARCH_TARGET}.tar.gz" --no-verbose -O "${TMPDIR}/rootfs.tar.gz"; then
echo "#### rootfs not present" &>> "${TMPDIR}/androot.log" indicate_fail
log_fail
fi
echo -e "ARCH_TARGET_WRITTEN=${ARCH_TARGET}\nDISTRIBUTION_WRITTEN=${DISTRIBUTION}" > "${TMPDIR}/rootfs.env"
log_ok;;
"--decompress")
log_diag "Using decompress mode"
log_exec "Decompressing archive"
echo "#### decompress rootfs" &>> "${TMPDIR}/androot.log"
bsdtar -xpf "${TMPDIR}/rootfs.tar.gz" -C "${LOCATION}/rootfs" &>> "${TMPDIR}/androot.log"
if [ ! "${?}" == "0" ]; then
log_fail
exit 1 exit 1
fi fi
log_ok;; if [ ! -f "${TMPDIR}/rootfs.tar.gz" ]; then
# rootfs is missing (somehow)
log_write "#### rootfs not present"
indicate_fail
fi
# write rootfs configuration into file if the same rootfs config is used again
echo -e "ARCH_TARGET_WRITTEN=${ARCH_TARGET}\nDISTRIBUTION_WRITTEN=${DISTRIBUTION}" > "${TMPDIR}/rootfs.env"
indicate_ok
;;
"--decompress")
log_diag "Using decompress mode"
indicate_exec "Decompressing archive"
log_write "#### decompress rootfs"
if ! log_execute bsdtar -xpf "${TMPDIR}/rootfs.tar.gz" -C "${LOCATION}/rootfs"; then
indicate_fail
exit 1
fi
indicate_ok
;;
*) *)
log_error "--download or --decompress required" log_error "--download or --decompress required"
exit 1;; exit 1
;;
esac esac