POSIX rewrite: init.bash

This commit is contained in:
Syahrial Agni Prasetya 2021-05-27 18:02:13 +07:00
parent 5b732eba04
commit 01f0cb9923
No known key found for this signature in database
GPG Key ID: 46F88749C8C79383
5 changed files with 124 additions and 97 deletions

View File

@ -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/exec $PREFIX/share/archbox/bin/exec
install -v -D -m 755 ./src/enter $PREFIX/share/archbox/bin/enter 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/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 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 grep 'PREFIX=' $ETC_DIR/archbox.conf >/dev/null 2>&1 || cat << EOF >> $ETC_DIR/archbox.conf

View File

@ -3,7 +3,7 @@
. /etc/archbox.conf >/dev/null 2>&1 . /etc/archbox.conf >/dev/null 2>&1
. /tmp/archbox_env >/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" ENV="$REQ_ENV $ENV_VAR"
COMMAND="$@" COMMAND="$@"

View File

@ -3,7 +3,7 @@
. /etc/archbox.conf >/dev/null 2>&1 . /etc/archbox.conf >/dev/null 2>&1
. /tmp/archbox_env >/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" ENV="$REQ_ENV $ENV_VAR"
COMMAND="$@" COMMAND="$@"

121
src/init Normal file
View File

@ -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

View File

@ -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