archbox/archbox.bash

128 lines
3.5 KiB
Bash
Raw Normal View History

2020-12-28 05:27:02 +00:00
#!/usr/bin/env bash
2020-10-27 06:12:14 +00:00
source /etc/archbox.conf
checkdep(){
2021-01-10 06:01:43 +00:00
hash $1 2>/dev/null || err "Install $1!"
2020-10-27 06:12:14 +00:00
}
asroot(){
2020-10-27 12:54:58 +00:00
[[ $EUID -ne 0 ]] && err "Run this as root!"
2020-10-27 06:12:14 +00:00
}
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
2021-01-20 08:17:45 +00:00
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
}
2020-10-27 07:30:02 +00:00
help_text(){
cat << EOF
USAGE: $0 <arguments>
2020-10-27 08:45:11 +00:00
2020-10-27 07:30:02 +00:00
OPTIONS:
2020-10-29 15:01:19 +00:00
-c, --create URL Creates a chroot enviroment.
-e, --enter Enters chroot enviroment.
-h, --help Displays this help message.
2021-01-23 09:15:13 +00:00
-m, --mount Mount Archbox directories.
-u, --umount Unmount Archbox directories.
2020-12-15 07:11:47 +00:00
--remount-run Remount /run in chroot enviroment.
2020-12-28 12:15:21 +00:00
--mount-runtime-only Mount XDG_RUNTIME_DIR to chroot enviroment.
2020-10-27 08:23:42 +00:00
2020-10-27 07:30:02 +00:00
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
}
2020-10-27 08:23:42 +00:00
err(){
echo "$(tput bold)$(tput setaf 1)==> $@ $(tput sgr0)" 1>&2
2020-10-27 08:23:42 +00:00
exit 1
}
msg(){
2020-10-27 12:54:58 +00:00
echo "$(tput bold)$(tput setaf 2)==> $@ $(tput sgr0)"
2020-10-27 08:23:42 +00:00
}
2020-10-27 06:12:14 +00:00
case $1 in
2020-10-29 15:01:19 +00:00
-c|--create)
2020-10-27 06:12:14 +00:00
asroot
2020-10-27 12:54:58 +00:00
[[ -z $2 ]] && err "Specify the link of Arch Linux bootstrap tarball!"
2020-10-27 08:23:42 +00:00
msg "Creating chroot directory..."
2020-10-27 12:54:58 +00:00
mkdir -p $INSTALL_PATH
2020-10-27 06:12:14 +00:00
cd $INSTALL_PATH
2020-10-27 08:23:42 +00:00
msg "Downloading Arch Linux tarball..."
while true; do fetch_tarball $2 && break; done
2020-10-27 08:23:42 +00:00
msg "Extracting the tarball..."
2020-12-31 16:16:31 +00:00
checkdep tar
2020-10-27 06:12:14 +00:00
tar xzf archlinux.tar.gz
2020-10-27 08:23:42 +00:00
msg "Enabling internet connection in chroot enviroment..."
2020-10-27 06:12:14 +00:00
cp /etc/resolv.conf $CHROOT/etc/resolv.conf
2020-10-27 08:23:42 +00:00
msg "You will need to edit which mirror you want to use, uncomment needed mirrors and save it."
2020-10-27 06:12:14 +00:00
echo "Editor of your choice:"
read MIRROR_EDITOR
$MIRROR_EDITOR $CHROOT/etc/pacman.d/mirrorlist || exit 1
2020-10-27 12:54:58 +00:00
msg "Disabling Pacman's CheckSpace..."
2020-10-27 06:12:14 +00:00
checkdep sed
sed -i 's/CheckSpace/#CheckSpace/g' $CHROOT/etc/pacman.conf
2020-10-27 12:54:58 +00:00
msg "Mounting necessary filesystems..."
$PREFIX/share/archbox/bin/init start
2021-01-23 09:06:38 +00:00
cp $PREFIX/share/archbox/chroot_setup.bash $CHROOT/chroot_setup
echo $ARCHBOX_USER > /tmp/archbox_user
2020-12-31 16:16:31 +00:00
chroot $CHROOT /bin/bash -c "/chroot_setup"
exit $?
2020-10-27 12:54:58 +00:00
;;
2020-10-29 15:01:19 +00:00
-e|--enter)
2020-12-31 16:16:31 +00:00
storeenv
2021-03-06 05:28:09 +00:00
$PRIV $PREFIX/share/archbox/bin/uth copyresolv
$PRIV $PREFIX/share/archbox/bin/enter
2020-12-31 16:16:31 +00:00
exit $?
;;
2021-01-23 09:15:13 +00:00
-m|--mount)
$PRIV $PREFIX/share/archbox/bin/init start
2021-01-23 09:15:13 +00:00
;;
-u|--umount)
$PRIV $PREFIX/share/archbox/bin/init stop
2021-01-23 09:15:13 +00:00
;;
--remount-run)
2021-03-06 05:39:59 +00:00
$PRIV $PREFIX/share/archbox/bin/uth remountrun
2020-12-31 16:16:31 +00:00
exit $?
;;
2020-12-28 12:15:21 +00:00
--mount-runtime-only)
2021-03-06 05:39:59 +00:00
$PRIV $PREFIX/share/archbox/bin/uth runtimeonly
2020-12-31 16:16:31 +00:00
exit $?
;;
2020-10-29 15:01:19 +00:00
-h|--help)
2020-10-27 07:30:02 +00:00
help_text
2020-12-31 16:16:31 +00:00
exit 0
2020-10-27 07:30:02 +00:00
;;
2020-10-27 06:12:14 +00:00
"")
2020-10-27 07:30:02 +00:00
help_text
2020-12-31 16:16:31 +00:00
exit 1
2020-10-27 06:12:14 +00:00
;;
2020-10-29 15:01:19 +00:00
-*)
2020-10-27 08:23:42 +00:00
err "Unknown option: $1"
;;
2020-10-27 06:12:14 +00:00
*)
2020-12-31 16:16:31 +00:00
storeenv
2021-03-06 05:28:09 +00:00
$PRIV $PREFIX/share/archbox/bin/uth copyresolv
$PRIV $PREFIX/share/archbox/bin/exec $@
2020-12-31 16:16:31 +00:00
exit $?
2020-10-27 06:12:14 +00:00
;;
esac