From 01f0cb9923c15633bbf3255d18929f3d83fadfac Mon Sep 17 00:00:00 2001 From: Syahrial Agni Prasetya Date: Thu, 27 May 2021 18:02:13 +0700 Subject: [PATCH] POSIX rewrite: init.bash --- install.sh | 2 +- src/enter | 2 +- src/exec | 2 +- src/init | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/init.bash | 94 --------------------------------------- 5 files changed, 124 insertions(+), 97 deletions(-) create mode 100644 src/init delete mode 100644 src/init.bash diff --git a/install.sh b/install.sh index a4b84cf..27b97e0 100755 --- a/install.sh +++ b/install.sh @@ -49,7 +49,7 @@ install -v -D -m 755 ./src/archbox-desktop $PREFIX/bin/archbox-desktop install -v -D -m 755 ./src/exec $PREFIX/share/archbox/bin/exec install -v -D -m 755 ./src/enter $PREFIX/share/archbox/bin/enter install -v -D -m 755 ./src/chroot_setup $PREFIX/share/archbox/chroot_setup -install -v -D -m 755 ./src/init.bash $PREFIX/share/archbox/bin/init +install -v -D -m 755 ./src/init $PREFIX/share/archbox/bin/init install -v -D -m 755 ./src/uth.bash $PREFIX/share/archbox/bin/uth grep 'PREFIX=' $ETC_DIR/archbox.conf >/dev/null 2>&1 || cat << EOF >> $ETC_DIR/archbox.conf diff --git a/src/enter b/src/enter index 852d80c..dd49350 100644 --- a/src/enter +++ b/src/enter @@ -3,7 +3,7 @@ . /etc/archbox.conf >/dev/null 2>&1 . /tmp/archbox_env >/dev/null 2>&1 -REQ_ENV="DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS XDG_RUNTIME_DIR=$XDG_RUNTIME_DIR DISPLAY=$DISPLAY WAYLAND_DISPLAY=$WAYLAND_DISPLAY" +REQ_ENV="DBUS_SESSION_BUS_ADDRESS=${DBUS_SESSION_BUS_ADDRESS} XDG_RUNTIME_DIR=${XDG_RUNTIME_DIR} DISPLAY=${DISPLAY} WAYLAND_DISPLAY=${WAYLAND_DISPLAY}" ENV="$REQ_ENV $ENV_VAR" COMMAND="$@" diff --git a/src/exec b/src/exec index e92e563..c46ee3c 100644 --- a/src/exec +++ b/src/exec @@ -3,7 +3,7 @@ . /etc/archbox.conf >/dev/null 2>&1 . /tmp/archbox_env >/dev/null 2>&1 -REQ_ENV="DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS XDG_RUNTIME_DIR=$XDG_RUNTIME_DIR DISPLAY=$DISPLAY WAYLAND_DISPLAY=$WAYLAND_DISPLAY" +REQ_ENV="DBUS_SESSION_BUS_ADDRESS=${DBUS_SESSION_BUS_ADDRESS} XDG_RUNTIME_DIR=${XDG_RUNTIME_DIR} DISPLAY=${DISPLAY} WAYLAND_DISPLAY=${WAYLAND_DISPLAY}" ENV="$REQ_ENV $ENV_VAR" COMMAND="$@" diff --git a/src/init b/src/init new file mode 100644 index 0000000..81ee364 --- /dev/null +++ b/src/init @@ -0,0 +1,121 @@ +#!/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 +} + +rbind(){ + if mount | grep "${CHROOT}"${1} >/dev/null 2>&1; then + msg "${CHROOT}${1} already mounted." + else + mount -R $1 "${CHROOT}"${1} + msg "${CHROOT}${1} mounted!" + fi + if [ $2 = "make-rslave" ]; then + mount --make-rslave ${CHROOT}${1} + fi +} + +rbind_diff() { + if mount | grep "${CHROOT}"${2}; then + msg "$CHROOT$2 already mounted." + else + mount -R $1 "${CHROOT}"${2} + msg "${CHROOT}${2} mounted!" + fi +} + +bindproc() { + if mount | grep "${CHROOT}"/proc; then + msg "${CHROOT}/proc already mounted." + else + mount -t proc /proc "${CHROOT}"/proc + msg "${CHROOT}/proc mounted!") + fi +} + +rmbind() { + umount_args=-R + [ $LAZY_UMOUNT = "yes" ] && umount_args=-Rl + if mount | grep "${CHROOT}"${1}; then + umount $umount_args "${CHROOT}"${1} + msg "${CHROOT}${1} unmounted!" + fi +} + +case $1 in + start) + bindproc + rbind /tmp + rbind /sys make-rslave + rbind /dev make-rslave + [ $MOUNT_RUN = "yes" ] && rbind /run + if [ $MOUNT_MOD = "yes" ]; then + rbind "$(readlink -f /lib/modules)" + rbind /boot + fi + [ -d /var/lib/dbus ] && rbind /var/lib/dbus + for i in $SHARED_FOLDER; do + if [ $i = *:* ]; then + source=$(printf "%s" "$i" | sed 's/:.*//') + target=$(printf "%s" "$i" | sed 's/.*://') + mkdir -p "${CHROOT}"${target} + rbind_diff $source $target; + else + rbind $i; + fi + done + msg "Starting services" + for j in $SERVICES; do + if [ $j = *:* ]; then + delay=$(printf "%s" "$j" | sed 's/.*://') + service=$(printf "%s" "$j" | sed 's/:.*//') + chroot "$CHROOT" /bin/su -c "/usr/local/bin/archboxctl exec $service" > /dev/null 2>&1 & + sleep $delay + else + chroot "$CHROOT" /bin/su -c "/usr/local/bin/archboxctl exec $j" > /dev/null 2>&1 & + fi + done + if [ -e /etc/archbox.rc ]; then + cp /etc/archbox.rc /tmp/archbox.rc + chmod +x /tmp/archbox.rc + chroot "$CHROOT" /bin/su -c '/tmp/archbox.rc' > /tmp/archbox.rc.log 2>&1 + rm /tmp/archbox.rc + fi + exit 0 + ;; + stop) + rmbind /proc + rmbind /tmp + rmbind /sys + rmbind /dev + [ $MOUNT_RUN = "yes" ] && rmbind /run + if [ $MOUNT_MOD = "yes" ]; then + rmbind $(readlink -f /lib/modules) + rmbind /boot + fi + rmbind /var/lib/dbus + for i in $SHARED_FOLDER; do + if [ $i = *:* ]; then + target=$(printf "%s" "$i" | sed 's/.*://') + rmbind $target; + else + rmbind $i; + fi + done + exit 0 + ;; +esac diff --git a/src/init.bash b/src/init.bash deleted file mode 100644 index bb3e570..0000000 --- a/src/init.bash +++ /dev/null @@ -1,94 +0,0 @@ -#!/usr/bin/env bash - -source /etc/archbox.conf - -msg(){ - echo "$(tput bold)$(tput setaf 2)==> $@ $(tput sgr0)" -} - -rbind() { - [[ $(mount | grep $CHROOT$1) ]] && msg "$CHROOT$1 already mounted." \ - || (mount -R $1 $CHROOT$1 && msg "$CHROOT$1 mounted!") - [[ $2 = "make-rslave" ]] && mount --make-rslave $CHROOT$1 -} - -rbind_diff() { - [[ $(mount | grep $CHROOT$2) ]] && msg "$CHROOT$2 already mounted." \ - || (mount -R $1 $CHROOT$2 && msg "$CHROOT$2 mounted!") -} - -bindproc() { - [[ $(mount | grep $CHROOT/proc) ]] && msg "$CHROOT/proc already mounted." \ - || (mount -t proc /proc $CHROOT/proc && msg "$CHROOT/proc mounted!") -} - -rmbind() { - umount_args=-R - [[ $LAZY_UMOUNT = "yes" ]] && umount_args=-Rl - [[ $(mount | grep $CHROOT$1) ]] && umount $umount_args $CHROOT$1 \ - && msg "$CHROOT$1 unmounted!" -} - -case $1 in - start) - bindproc - rbind /tmp - rbind /sys make-rslave - rbind /dev make-rslave - [[ $MOUNT_RUN = "yes" ]] && rbind /run - if [[ $MOUNT_MOD = "yes" ]]; then - rbind $(readlink -f /lib/modules) - rbind /boot - fi - [[ -d /var/lib/dbus ]] && rbind /var/lib/dbus - for i in ${SHARED_FOLDER[@]}; do - if [[ $i = *:* ]]; then - source=$(echo $i | sed 's/:.*//') - target=$(echo $i | sed 's/.*://') - mkdir -p $CHROOT$target - rbind_diff $source $target; - else - rbind $i; - fi - done - msg "Starting services" - for j in ${SERVICES[@]}; do - if [[ $j = *:* ]]; then - delay=$(echo $j | sed 's/.*://') - service=$(echo $j | sed 's/:.*//') - chroot $CHROOT /bin/su -c "/usr/local/bin/archboxctl exec $service" > /dev/null 2>&1 & - sleep $delay - else - chroot $CHROOT /bin/su -c "/usr/local/bin/archboxctl exec $j" > /dev/null 2>&1 & - fi - done - if [[ -e /etc/archbox.rc ]]; then - cp /etc/archbox.rc /tmp/archbox.rc - chmod +x /tmp/archbox.rc - chroot $CHROOT /bin/su -c '/tmp/archbox.rc' > /tmp/archbox.rc.log 2>&1 - rm /tmp/archbox.rc - fi - exit 0 - ;; - stop) - rmbind /proc - rmbind /tmp - rmbind /sys - rmbind /dev - [[ $MOUNT_RUN = "yes" ]] && rmbind /run - if [[ $MOUNT_MOD = "yes" ]]; then - rmbind $(readlink -f /lib/modules) - rmbind /boot - fi - rmbind /var/lib/dbus - for i in ${SHARED_FOLDER[@]}; do - if [[ $i = *:* ]]; then - target=$(echo $i | sed 's/.*://') - rmbind $target; - else - rmbind $i; - fi - done - exit 0 - ;; -esac