From 2191eeea79ec5048aefd64599d8b3ffebe566cff Mon Sep 17 00:00:00 2001 From: Syahrial Agni Prasetya Date: Tue, 18 May 2021 18:54:49 +0700 Subject: [PATCH] POSIX rewrite: archbox.bash --- install.sh | 2 +- src/archbox | 135 +++++++++++++++++++++++++++++++++++++++++++++++ src/archbox.bash | 127 -------------------------------------------- 3 files changed, 136 insertions(+), 128 deletions(-) create mode 100644 src/archbox delete mode 100644 src/archbox.bash diff --git a/install.sh b/install.sh index 2cd1b0a..ffc9383 100755 --- a/install.sh +++ b/install.sh @@ -43,7 +43,7 @@ PREFIX="${PREFIX:-/usr/local}" mkdir -p $PREFIX/share/archbox/bin mkdir -p $ETC_DIR -install -v -D -m 755 ./src/archbox.bash $PREFIX/bin/archbox +install -v -D -m 755 ./src/archbox $PREFIX/bin/archbox install -v -D -m 755 ./src/archbox-desktop.bash $PREFIX/bin/archbox-desktop [[ ! -e /etc/archbox.conf || ! -z $FORCE_INSTALL_CONFIG ]] && genconfig > $ETC_DIR/archbox.conf install -v -D -m 755 ./src/exec.bash $PREFIX/share/archbox/bin/exec diff --git a/src/archbox b/src/archbox new file mode 100644 index 0000000..12bdbd4 --- /dev/null +++ b/src/archbox @@ -0,0 +1,135 @@ +#!/bin/sh + +. /etc/archbox.conf + +# Text colors/formatting +red="\033[38;5;1" +green="\033[38;5;2" +bold="\033[1m" +reset="\033[m" + +err(){ + printf "${red}${bold}%s${reset}\n" "==> $*" 1>&2 + exit 1 +} + +msg(){ + printf "${green}${bold}%s${reset}\n" "==> $*" 1>&2 +} + +checkdep(){ + command -v $1 2>/dev/null || err "Install $1!" +} + +asroot(){ + [ $(id -u) -eq 0 ] || err "Run this as root!" +} + +storeenv() { + printf "%s\n" "# This will be sourced when entering Archbox" > /tmp/archbox_env + $PRIV ${PREFIX}/share/archbox/bin/uth chownvar $USER + [ ! -z $WAYLAND_DISPLAY ] && \ + printf "%s\n" "WAYLAND_DISPLAY=${WAYLAND_DISPLAY}" >> /tmp/archbox_env + if [ ! -z $DISPLAY ]; then + command -v xhost >/dev/null 2>&1 && xhost +local: > /dev/null + printf "%s\n" "DISPLAY=${DISPLAY}" >> /tmp/archbox_env + fi + [ ! -z $DBUS_SESSION_BUS_ADDRESS ] && eval "$(dbus-launch)" + printf "%s\n" "DBUS_SESSION_BUS_ADDRESS=${DBUS_SESSION_BUS_ADDRESS}" >> /tmp/archbox_env + printf "%s\n" "XDG_RUNTIME_DIR=${XDG_RUNTIME_DIR:-/tmp}" >> /tmp/archbox_env +} + +help_text(){ + printf "%s" " +USAGE: $0 + +OPTIONS: + -c, --create URL Creates a chroot enviroment. + -e, --enter Enters chroot enviroment. + -h, --help Displays this help message. + -m, --mount Mount Archbox directories. + -u, --umount Unmount Archbox directories. + --remount-run Remount /run in chroot enviroment. + --mount-runtime-only Mount XDG_RUNTIME_DIR to chroot enviroment. + +" +} + +fetch_tarball(){ + if command -v aria2c 2>/dev/null; then + aria2c -o archlinux.tar.gz $1 + elif command -v wget 2>/dev/null; then + wget -O archlinux.tar.gz $1 + elif command -v curl 2>/dev/null; then + curl -o archlinux.tar.gz $1 + else + err "No supported downloader found." + fi +} + +case $1 in + -c|--create) + asroot + [ -z $2 ] && err "Specify the link of Arch Linux bootstrap tarball!" + msg "Creating chroot directory..." + mkdir -p $INSTALL_PATH + cd $INSTALL_PATH + msg "Downloading Arch Linux tarball..." + while true; do fetch_tarball $2 && break; done + msg "Extracting the tarball..." + checkdep tar + tar xzf archlinux.tar.gz + msg "Enabling internet connection in chroot enviroment..." + cp /etc/resolv.conf "${CHROOT}"/etc/resolv.conf + msg "You will need to edit which mirror you want to use, uncomment needed mirrors and save it." + echo "Editor of your choice:" + read MIRROR_EDITOR + $MIRROR_EDITOR "${CHROOT}"/etc/pacman.d/mirrorlist || exit 1 + msg "Disabling Pacman's CheckSpace..." + checkdep sed + sed -i "s/CheckSpace/#CheckSpace/g" "${CHROOT}"/etc/pacman.conf + msg "Mounting necessary filesystems..." + "${PREFIX}"/share/archbox/bin/init start + cp "${PREFIX}"/share/archbox/chroot_setup.bash "${CHROOT}"/chroot_setup + echo $ARCHBOX_USER > /tmp/archbox_user + chroot $CHROOT /bin/bash -c "/chroot_setup" + exit $? + ;; + -e|--enter) + storeenv + $PRIV "${PREFIX}"/share/archbox/bin/uth copyresolv + $PRIV "${PREFIX}"/share/archbox/bin/enter + exit $? + ;; + -m|--mount) + $PRIV "${PREFIX}"/share/archbox/bin/init start + ;; + -u|--umount) + $PRIV "${PREFIX}"/share/archbox/bin/init stop + ;; + --remount-run) + $PRIV "${PREFIX}"/share/archbox/bin/uth remountrun + exit $? + ;; + --mount-runtime-only) + $PRIV "${PREFIX}"/share/archbox/bin/uth runtimeonly + exit $? + ;; + -h|--help) + help_text + exit 0 + ;; + "") + help_text + exit 1 + ;; + -*) + err "Unknown option: $1" + ;; + *) + storeenv + $PRIV ${PREFIX}/share/archbox/bin/uth copyresolv + $PRIV ${PREFIX}/share/archbox/bin/exec $@ + exit $? + ;; +esac diff --git a/src/archbox.bash b/src/archbox.bash deleted file mode 100644 index 7a7d91e..0000000 --- a/src/archbox.bash +++ /dev/null @@ -1,127 +0,0 @@ -#!/usr/bin/env bash - -source /etc/archbox.conf - -checkdep(){ - hash $1 2>/dev/null || err "Install $1!" -} - -asroot(){ - [[ $EUID -ne 0 ]] && err "Run this as root!" -} - -storeenv() { - echo "# This will be sourced when entering Archbox" > /tmp/archbox_env - $PRIV $PREFIX/share/archbox/bin/uth chownvar $USER - [[ ! -z $WAYLAND_DISPLAY ]] && echo "WAYLAND_DISPLAY=$WAYLAND_DISPLAY" >> /tmp/archbox_env - if [[ ! -z $DISPLAY ]]; then - hash xhost >/dev/null 2>&1 && xhost +local: > /dev/null - echo "DISPLAY=$DISPLAY" >> /tmp/archbox_env - fi - echo "DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS" >> /tmp/archbox_env - echo "XDG_RUNTIME_DIR=$XDG_RUNTIME_DIR" >> /tmp/archbox_env -} - -help_text(){ -cat << EOF -USAGE: $0 - -OPTIONS: - -c, --create URL Creates a chroot enviroment. - -e, --enter Enters chroot enviroment. - -h, --help Displays this help message. - -m, --mount Mount Archbox directories. - -u, --umount Unmount Archbox directories. - --remount-run Remount /run in chroot enviroment. - --mount-runtime-only Mount XDG_RUNTIME_DIR to chroot enviroment. - -EOF -} - -fetch_tarball(){ - if hash aria2c 2>/dev/null; then - aria2c -o archlinux.tar.gz $1 - elif hash wget 2>/dev/null; then - wget -O archlinux.tar.gz $1 - elif hash curl 2>/dev/null; then - curl -o archlinux.tar.gz $1 - else - err "No supported downloader found." - fi -} - -err(){ - echo "$(tput bold)$(tput setaf 1)==> $@ $(tput sgr0)" 1>&2 - exit 1 -} - -msg(){ - echo "$(tput bold)$(tput setaf 2)==> $@ $(tput sgr0)" -} - -case $1 in - -c|--create) - asroot - [[ -z $2 ]] && err "Specify the link of Arch Linux bootstrap tarball!" - msg "Creating chroot directory..." - mkdir -p $INSTALL_PATH - cd $INSTALL_PATH - msg "Downloading Arch Linux tarball..." - while true; do fetch_tarball $2 && break; done - msg "Extracting the tarball..." - checkdep tar - tar xzf archlinux.tar.gz - msg "Enabling internet connection in chroot enviroment..." - cp /etc/resolv.conf $CHROOT/etc/resolv.conf - msg "You will need to edit which mirror you want to use, uncomment needed mirrors and save it." - echo "Editor of your choice:" - read MIRROR_EDITOR - $MIRROR_EDITOR $CHROOT/etc/pacman.d/mirrorlist || exit 1 - msg "Disabling Pacman's CheckSpace..." - checkdep sed - sed -i 's/CheckSpace/#CheckSpace/g' $CHROOT/etc/pacman.conf - msg "Mounting necessary filesystems..." - $PREFIX/share/archbox/bin/init start - cp $PREFIX/share/archbox/chroot_setup.bash $CHROOT/chroot_setup - echo $ARCHBOX_USER > /tmp/archbox_user - chroot $CHROOT /bin/bash -c "/chroot_setup" - exit $? - ;; - -e|--enter) - storeenv - $PRIV $PREFIX/share/archbox/bin/uth copyresolv - $PRIV $PREFIX/share/archbox/bin/enter - exit $? - ;; - -m|--mount) - $PRIV $PREFIX/share/archbox/bin/init start - ;; - -u|--umount) - $PRIV $PREFIX/share/archbox/bin/init stop - ;; - --remount-run) - $PRIV $PREFIX/share/archbox/bin/uth remountrun - exit $? - ;; - --mount-runtime-only) - $PRIV $PREFIX/share/archbox/bin/uth runtimeonly - exit $? - ;; - -h|--help) - help_text - exit 0 - ;; - "") - help_text - exit 1 - ;; - -*) - err "Unknown option: $1" - ;; - *) - storeenv - $PRIV $PREFIX/share/archbox/bin/uth copyresolv - $PRIV $PREFIX/share/archbox/bin/exec $@ - exit $? - ;; -esac